a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

commit 4515542024925cd35387f4106fb509b19840a1db
parent 3885c5b602323f1b955680b32b48f7462c6cf1d9
Author: amin <dev@aminmesbah.com>
Date:   Wed, 16 May 2018 07:35:05 +0000

Render a lovely triangle

FossilOrigin-Name: 95bf65f2c6dbd585b375d715ac41c78d3074202284915ee0a858bdeedcf6d21c
Diffstat:
Ashader/triangle_f.glsl | 10++++++++++
Ashader/triangle_v.glsl | 12++++++++++++
Msrc/main.c | 37++++++++++++++++++++++++++++++++++++-
3 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/shader/triangle_f.glsl b/shader/triangle_f.glsl @@ -0,0 +1,10 @@ +#version 330 core + +in vec4 vertex_color; + +out vec4 color; + +void main() +{ + color = vertex_color; +} diff --git a/shader/triangle_v.glsl b/shader/triangle_v.glsl @@ -0,0 +1,12 @@ +#version 330 core + +layout (location = 0) in vec2 position; +layout (location = 1) in vec3 color; + +out vec4 vertex_color; + +void main() +{ + gl_Position = vec4(position, 0.0f, 1.0f); + vertex_color = vec4(color, 1.0f); +} diff --git a/src/main.c b/src/main.c @@ -51,16 +51,51 @@ int main(void) return -1; } - glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT); + struct Shader triangle_shader = shader_compile("shader/triangle_v.glsl", "shader/triangle_f.glsl"); + + GLfloat triangle_vertices[] = { + // positions // colors + -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.5f, 0.0f, 0.0f, 1.0f, + }; + + GLuint VBO; + GLuint VAO; + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + + glBindVertexArray(VAO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW); + + // positions + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0); + glEnableVertexAttribArray(0); + + // colors + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(2 * sizeof(GLfloat))); + glEnableVertexAttribArray(1); + glBindVertexArray(0); while (!glfwWindowShouldClose(window)) { glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); + + glUseProgram(triangle_shader.program); + + glBindVertexArray(VAO); + glDrawArrays(GL_TRIANGLES, 0, 3); + glBindVertexArray(0); + glfwSwapBuffers(window); glfwPollEvents(); } + glDeleteVertexArrays(1, &VAO); + glDeleteBuffers(1, &VBO); + glfwDestroyWindow(window); glfwTerminate(); return 0;