a-game

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

commit c833ecd4cdee55419de0b4c42cfee2d0fddffcee
parent cc6a022c5d380463917e0f8ec3c5abce347dc14a
Author: amin <dev@aminmesbah.com>
Date:   Sat,  2 Jun 2018 03:50:06 +0000

Set up projection and view matrices

And make an actual equilateral triangle. That was really annoying me.

FossilOrigin-Name: 0bf4c1f2ec9041f20b1d190e3cc934ac730b48697e072c6298ca14ee211aaf84
Diffstat:
Mshader/triangle_v.glsl | 6++++--
Msrc/game.c | 38+++++++++++++++++++++++++++-----------
Msrc/game.h | 6+++---
Msrc/platform_linux.c | 6+++---
Msrc/platform_linux.h | 2+-
5 files changed, 38 insertions(+), 20 deletions(-)

diff --git a/shader/triangle_v.glsl b/shader/triangle_v.glsl @@ -5,10 +5,12 @@ layout (location = 1) in vec3 color; out vec4 vertex_color; -uniform mat4 transform; +uniform mat4 model; +uniform mat4 projection; +uniform mat4 view; void main() { - gl_Position = transform * vec4(position, 0.0f, 1.0f); + gl_Position = projection * view * model * vec4(position, 0.0f, 1.0f); vertex_color = vec4(color, 1.0f); } diff --git a/src/game.c b/src/game.c @@ -3,13 +3,19 @@ #include <math.h> -void game_init(struct GameState *game_state) +void game_init(struct GameState *game_state, uint32_t screen_width, uint32_t screen_height) { + glViewport(0, 0, screen_width, screen_height); + + float circumradius = 0.5f; + float inradius = circumradius * sinf(glmth_rad(30.0f)); + float half_side_length = circumradius * cosf(glmth_rad(30.0f)); + 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, + // 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, }; GLuint vbo_id; @@ -35,19 +41,29 @@ void game_init(struct GameState *game_state) } -void game_update_and_render(struct GameState *game_state, float dt) +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); - m4 trans = glmth_m4_init_id(); - trans = glmth_translate(trans, glmth_v3_init(0.5f * cosf(dt), 0.5f * sinf(dt), 0.0f)); + m4 model = glmth_m4_init_id(); + model = glmth_translate(model, glmth_v3_init(1.5f * cosf(dt), 1.5f * sinf(dt), 0.0f)); float scale_factor = fabs(1.0f * sinf(dt)); - trans = glmth_scale(trans, glmth_v3_init(scale_factor, scale_factor, scale_factor)); - trans = glmth_rotate_z(trans, -dt); + model = glmth_scale(model, glmth_v3_init(scale_factor, scale_factor, scale_factor)); + model = glmth_rotate_z(model, -dt); shader_use(&game_state->triangle_shader); - shader_setm4(&game_state->triangle_shader, "transform", &trans); + shader_setm4(&game_state->triangle_shader, "model", &model); + + m4 projection = glmth_m4_init_id(); + projection = glmth_projection_perspective_fov(glmth_rad(45.0f), (float)screen_width / (float)screen_height, 0.1f, 100.0f); + shader_setm4(&game_state->triangle_shader, "projection", &projection); + + m4 view = glmth_m4_init_id(); + v3 camera_pos = glmth_v3_init(0.0f, 0.0f, 6.0f); + view = glmth_camera_look_at(camera_pos, glmth_v3_a(camera_pos, glmth_v3_init(0.0f, 0.0f, -1.0f)), glmth_v3_init(0.0f, 1.0f, 0.0f)); + shader_setm4(&game_state->triangle_shader, "view", &view); + glBindVertexArray(game_state->vao_id); glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(0); diff --git a/src/game.h b/src/game.h @@ -11,10 +11,10 @@ struct GameState struct Shader triangle_shader; }; -typedef void (game_update_and_render_func)(struct GameState *game_state, float dt); +typedef void (game_update_and_render_func)(struct GameState *game_state, float dt, uint32_t screen_width, uint32_t screen_height); -void game_init(struct GameState *game_state); -void game_update_and_render(struct GameState *game_state, float dt); +void game_init(struct GameState *game_state, uint32_t screen_width, uint32_t screen_height); +void game_update_and_render(struct GameState *game_state, float dt, uint32_t screen_width, uint32_t screen_height); void game_cleanup(struct GameState *game_state); #endif diff --git a/src/platform_linux.c b/src/platform_linux.c @@ -56,7 +56,7 @@ int main(void) } struct GameState game_state = {0}; - game_init(&game_state); + game_init(&game_state, PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT); #ifdef PLATFORM_HOTLOAD_GAME_CODE struct GameCode game_code = load_game_code(PLATFORM_GAME_LIB_PATH); @@ -85,9 +85,9 @@ int main(void) // TODO: fall back to backup? } } - game_code.game_update_and_render(&game_state, lag/PLATFORM_SECOND); + game_code.game_update_and_render(&game_state, lag/PLATFORM_SECOND, PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT); #else - game_update_and_render(&game_state, lag/PLATFORM_SECOND); + game_update_and_render(&game_state, lag/PLATFORM_SECOND, PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT); #endif // PLATFORM_HOTLOAD_GAME_CODE glfwSwapBuffers(window); diff --git a/src/platform_linux.h b/src/platform_linux.h @@ -7,7 +7,7 @@ #include "game.h" #define PLATFORM_GAME_LIB_PATH "./build/release/game.so" -#define PLATFORM_SCR_WIDTH 800 +#define PLATFORM_SCR_WIDTH 600 #define PLATFORM_SCR_HEIGHT 600 #define PLATFORM_SECOND 1000.0f