commit 2a92d937febe092701d1a6aa128961f9c428bf94
parent 73cc5aaf3b9ca897c79ef420b4f21d7c5db9c85b
Author: amin <dev@aminmesbah.com>
Date: Thu, 26 Jul 2018 06:39:11 +0000
Rotate a pyramid
FossilOrigin-Name: c42136578c16024a6886223401f46edac984481326aec5a1065ee73a71771203
Diffstat:
5 files changed, 66 insertions(+), 78 deletions(-)
diff --git a/shader/cube_f.glsl b/shader/cube_f.glsl
@@ -1,10 +0,0 @@
-#version 330 core
-
-uniform vec3 cube_color;
-
-out vec4 frag_color;
-
-void main()
-{
- frag_color = vec4(cube_color, 1.0f);
-}
diff --git a/shader/pyramid_f.glsl b/shader/pyramid_f.glsl
@@ -0,0 +1,10 @@
+#version 330 core
+
+uniform vec3 pyramid_color;
+
+out vec4 frag_color;
+
+void main()
+{
+ frag_color = vec4(pyramid_color, 1.0f);
+}
diff --git a/shader/cube_v.glsl b/shader/pyramid_v.glsl
diff --git a/src/game.c b/src/game.c
@@ -45,73 +45,61 @@ void game_init(struct GameState *game_state, uint32_t screen_width, uint32_t scr
{
glEnable(GL_DEPTH_TEST);
- // load cube vertex data
+ // load pyramid vertex data
{
- GLfloat cube_vertices[] = {
+ float edge_length = 1.0f;
+ float height = 1.0f / sqrtf(2.0f) * edge_length;
+ float half_length = edge_length / 2.0f;
+ float half_height = height / 2.0f;
+
+
+ GLfloat pyramid_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,
+ -half_length, -half_height, -half_length,
+ half_length, -half_height, -half_length,
+ half_length, -half_height, half_length,
+
+ half_length, -half_height, half_length,
+ -half_length, -half_height, half_length,
+ -half_length, -half_height, -half_length,
+
+ -half_length, -half_height, -half_length,
+ 0.0f, half_height, 0.0f,
+ half_length, -half_height, -half_length,
+
+ half_length, -half_height, -half_length,
+ 0.0f, half_height, 0.0f,
+ half_length, -half_height, half_length,
+
+ half_length, -half_height, half_length,
+ 0.0f, half_height, 0.0f,
+ -half_length, -half_height, half_length,
+
+ -half_length, -half_height, half_length,
+ 0.0f, half_height, 0.0f,
+ -half_length, -half_height, -half_length,
};
- GLuint cube_vao_id;
- GLuint cube_vbo_id;
- glGenVertexArrays(1, &cube_vao_id);
- glGenBuffers(1, &cube_vbo_id);
+ GLuint pyramid_vao_id;
+ GLuint pyramid_vbo_id;
+ glGenVertexArrays(1, &pyramid_vao_id);
+ glGenBuffers(1, &pyramid_vbo_id);
- glBindVertexArray(cube_vao_id);
- glBindBuffer(GL_ARRAY_BUFFER, cube_vbo_id);
- glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);
+ glBindVertexArray(pyramid_vao_id);
+ glBindBuffer(GL_ARRAY_BUFFER, pyramid_vbo_id);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(pyramid_vertices), pyramid_vertices, GL_STATIC_DRAW);
// positions
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(*cube_vertices), (GLvoid*)0);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(*pyramid_vertices), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
- game_state->cube_vao_id = cube_vao_id;
- game_state->cube_vbo_id = cube_vbo_id;
+ game_state->pyramid_vao_id = pyramid_vao_id;
+ game_state->pyramid_vbo_id = pyramid_vbo_id;
}
- game_state->cube_shader = shader_compile("shader/cube_v.glsl", "shader/cube_f.glsl");
+ game_state->pyramid_shader = shader_compile("shader/pyramid_v.glsl", "shader/pyramid_f.glsl");
}
@@ -122,32 +110,32 @@ void game_update_and_render(struct GameState *game_state, float dt, uint32_t scr
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- shader_use(&game_state->cube_shader);
+ shader_use(&game_state->pyramid_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);
- shader_setm4(&game_state->cube_shader, "view", &view);
- shader_setm4(&game_state->cube_shader, "projection", &projection);
+ shader_setm4(&game_state->pyramid_shader, "view", &view);
+ shader_setm4(&game_state->pyramid_shader, "projection", &projection);
- // render cube
+ // render pyramid
{
m4 model = glmth_m4_init_id();
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(
+ v3 pyramid_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);
+ shader_setm4(&game_state->pyramid_shader, "model", &model);
+ shader_setv3(&game_state->pyramid_shader, "pyramid_color", &pyramid_color);
- glBindVertexArray(game_state->cube_vao_id);
- glDrawArrays(GL_TRIANGLES, 0, 36);
+ glBindVertexArray(game_state->pyramid_vao_id);
+ glDrawArrays(GL_TRIANGLES, 0, 18);
glBindVertexArray(0);
}
}
@@ -155,6 +143,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->cube_vao_id);
- glDeleteBuffers(1, &game_state->cube_vbo_id);
+ glDeleteVertexArrays(1, &game_state->pyramid_vao_id);
+ glDeleteBuffers(1, &game_state->pyramid_vbo_id);
}
diff --git a/src/game.h b/src/game.h
@@ -8,9 +8,9 @@
struct GameState
{
- GLuint cube_vao_id;
- GLuint cube_vbo_id;
- struct Shader cube_shader;
+ GLuint pyramid_vao_id;
+ GLuint pyramid_vbo_id;
+ struct Shader pyramid_shader;
};
#ifdef PLATFORM_HOTLOAD_GAME_CODE