commit 73cc5aaf3b9ca897c79ef420b4f21d7c5db9c85b
parent 1a6407877d67c17ab2348f086e85b0323727d1ac
Author: amin <dev@aminmesbah.com>
Date: Wed, 25 Jul 2018 06:39:50 +0000
Rotate a cube in perspective projection
FossilOrigin-Name: 9b52456658bf9451e57b3fb7e1800ea48cfaffc3fb49d7b3236a7832ea7107c9
Diffstat:
6 files changed, 110 insertions(+), 73 deletions(-)
diff --git a/shader/cube_f.glsl b/shader/cube_f.glsl
@@ -0,0 +1,10 @@
+#version 330 core
+
+uniform vec3 cube_color;
+
+out vec4 frag_color;
+
+void main()
+{
+ frag_color = vec4(cube_color, 1.0f);
+}
diff --git a/shader/cube_v.glsl b/shader/cube_v.glsl
@@ -0,0 +1,12 @@
+#version 330 core
+
+layout (location = 0) in vec3 a_position;
+
+uniform mat4 model;
+uniform mat4 view;
+uniform mat4 projection;
+
+void main()
+{
+ gl_Position = projection * view * model * vec4(a_position, 1.0f);
+}
diff --git a/shader/triangle_f.glsl b/shader/triangle_f.glsl
@@ -1,10 +0,0 @@
-#version 330 core
-
-in vec4 vertex_color;
-
-out vec4 frag_color;
-
-void main()
-{
- frag_color = vertex_color;
-}
diff --git a/shader/triangle_v.glsl b/shader/triangle_v.glsl
@@ -1,15 +0,0 @@
-#version 330 core
-
-layout (location = 0) in vec2 position;
-layout (location = 1) in vec3 color;
-
-out vec4 vertex_color;
-
-uniform mat4 model;
-uniform mat4 projection;
-
-void main()
-{
- gl_Position = projection * model * vec4(position, 0.0f, 1.0f);
- vertex_color = vec4(color, 1.0f);
-}
diff --git a/src/game.c b/src/game.c
@@ -43,71 +43,111 @@ float randf(float min, float max)
void game_init(struct GameState *game_state, uint32_t screen_width, uint32_t screen_height)
{
- // load triangle vertex data
+ glEnable(GL_DEPTH_TEST);
+
+ // load cube vertex data
{
- float circumradius = 10.0f;
- float inradius = circumradius * sinf(glmth_rad(30.0f));
- float half_side_length = circumradius * cosf(glmth_rad(30.0f));
-
- GLfloat triangle_vertices[] = {
- // positions // colors
- -half_side_length, -inradius, 1.0f, 0.0f, 0.0f,
- half_side_length, -inradius, 0.0f, 1.0f, 0.0f,
- 0.0f, circumradius, 0.0f, 0.0f, 1.0f,
+ GLfloat cube_vertices[] = {
+ // positions
+ -0.5f, -0.5f, -0.5f,
+ 0.5f, -0.5f, -0.5f,
+ 0.5f, 0.5f, -0.5f,
+ 0.5f, 0.5f, -0.5f,
+ -0.5f, 0.5f, -0.5f,
+ -0.5f, -0.5f, -0.5f,
+
+ -0.5f, -0.5f, 0.5f,
+ 0.5f, -0.5f, 0.5f,
+ 0.5f, 0.5f, 0.5f,
+ 0.5f, 0.5f, 0.5f,
+ -0.5f, 0.5f, 0.5f,
+ -0.5f, -0.5f, 0.5f,
+
+ -0.5f, 0.5f, 0.5f,
+ -0.5f, 0.5f, -0.5f,
+ -0.5f, -0.5f, -0.5f,
+ -0.5f, -0.5f, -0.5f,
+ -0.5f, -0.5f, 0.5f,
+ -0.5f, 0.5f, 0.5f,
+
+ 0.5f, 0.5f, 0.5f,
+ 0.5f, 0.5f, -0.5f,
+ 0.5f, -0.5f, -0.5f,
+ 0.5f, -0.5f, -0.5f,
+ 0.5f, -0.5f, 0.5f,
+ 0.5f, 0.5f, 0.5f,
+
+ -0.5f, -0.5f, -0.5f,
+ 0.5f, -0.5f, -0.5f,
+ 0.5f, -0.5f, 0.5f,
+ 0.5f, -0.5f, 0.5f,
+ -0.5f, -0.5f, 0.5f,
+ -0.5f, -0.5f, -0.5f,
+
+ -0.5f, 0.5f, -0.5f,
+ 0.5f, 0.5f, -0.5f,
+ 0.5f, 0.5f, 0.5f,
+ 0.5f, 0.5f, 0.5f,
+ -0.5f, 0.5f, 0.5f,
+ -0.5f, 0.5f, -0.5f,
};
- GLuint triangle_vao_id;
- GLuint triangle_vbo_id;
- glGenVertexArrays(1, &triangle_vao_id);
- glGenBuffers(1, &triangle_vbo_id);
+ GLuint cube_vao_id;
+ GLuint cube_vbo_id;
+ glGenVertexArrays(1, &cube_vao_id);
+ glGenBuffers(1, &cube_vbo_id);
- glBindVertexArray(triangle_vao_id);
- glBindBuffer(GL_ARRAY_BUFFER, triangle_vbo_id);
- glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW);
+ glBindVertexArray(cube_vao_id);
+ glBindBuffer(GL_ARRAY_BUFFER, cube_vbo_id);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);
// positions
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(*triangle_vertices), (GLvoid*)0);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(*cube_vertices), (GLvoid*)0);
glEnableVertexAttribArray(0);
- // colors
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(*triangle_vertices), (GLvoid*)(2 * sizeof(*triangle_vertices)));
- glEnableVertexAttribArray(1);
glBindVertexArray(0);
- game_state->triangle_vao_id = triangle_vao_id;
- game_state->triangle_vbo_id = triangle_vbo_id;
+ game_state->cube_vao_id = cube_vao_id;
+ game_state->cube_vbo_id = cube_vbo_id;
}
- game_state->triangle_shader = shader_compile("shader/triangle_v.glsl", "shader/triangle_f.glsl");
+ game_state->cube_shader = shader_compile("shader/cube_v.glsl", "shader/cube_f.glsl");
}
void game_update_and_render(struct GameState *game_state, float dt, uint32_t screen_width, uint32_t screen_height)
{
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+
+ shader_use(&game_state->cube_shader);
+
+ m4 view = glmth_m4_init_id();
+ m4 projection = glmth_m4_init_id();
+ view = glmth_translate(view, glmth_v3_init(0.0f, 0.0f, -3.0f));
+ projection = glmth_projection_perspective_fov(glmth_rad(45.0f), (float)screen_width / (float)screen_height, 0.1f, 100.0f);
- //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+ shader_setm4(&game_state->cube_shader, "view", &view);
+ shader_setm4(&game_state->cube_shader, "projection", &projection);
- // render triangle
+ // render cube
{
m4 model = glmth_m4_init_id();
- model = glmth_translate(model, glmth_v3_init(screen_width / 2.0f, screen_height / 2.0f, 0.0f));
- model = glmth_translate(model, glmth_v3_init(100.0f * cosf(dt), 100.0f * sinf(dt), 0.0f));
- model = glmth_rotate_z(model, -dt);
- model = glmth_scale(model, glmth_v3_init(10.0f, 10.0f, 10.0f));
- float scale_factor = fabs(1.0f * sinf(dt));
- model = glmth_scale(model, glmth_v3_init(scale_factor, scale_factor, scale_factor));
-
- shader_use(&game_state->triangle_shader);
- shader_setm4(&game_state->triangle_shader, "model", &model);
-
- m4 projection = glmth_m4_init_id();
- projection = glmth_projection_ortho(0.0f, screen_width, screen_height, 0.0f, -1.0f, 1.0f);
- shader_setm4(&game_state->triangle_shader, "projection", &projection);
-
- glBindVertexArray(game_state->triangle_vao_id);
- glDrawArrays(GL_TRIANGLES, 0, 3);
+ f32 angle = 20.0f;
+ model = glmth_rotate(model, dt * glmth_rad(angle), glmth_v3_init(0.5f, 1.0f, 0.0f));
+
+ f32 color_freq = dt * 0.1f;
+ v3 cube_color = glmth_v3_init(
+ sinf(color_freq),
+ sinf(color_freq + (2 * M_PI / 3)),
+ sinf(color_freq + (4 * M_PI / 3)));
+ shader_setm4(&game_state->cube_shader, "model", &model);
+ shader_setv3(&game_state->cube_shader, "cube_color", &cube_color);
+
+ glBindVertexArray(game_state->cube_vao_id);
+ glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
}
}
@@ -115,6 +155,6 @@ void game_update_and_render(struct GameState *game_state, float dt, uint32_t scr
void game_cleanup(struct GameState *game_state)
{
- glDeleteVertexArrays(1, &game_state->triangle_vao_id);
- glDeleteBuffers(1, &game_state->triangle_vbo_id);
+ glDeleteVertexArrays(1, &game_state->cube_vao_id);
+ glDeleteBuffers(1, &game_state->cube_vbo_id);
}
diff --git a/src/game.h b/src/game.h
@@ -8,9 +8,9 @@
struct GameState
{
- GLuint triangle_vao_id;
- GLuint triangle_vbo_id;
- struct Shader triangle_shader;
+ GLuint cube_vao_id;
+ GLuint cube_vbo_id;
+ struct Shader cube_shader;
};
#ifdef PLATFORM_HOTLOAD_GAME_CODE