transparent-cube

Minimal cross-platform native/wasm graphics example.
git clone git://git.amin.space/transparent-cube.git
Log | Files | Refs | README | LICENSE

commit 69f17f27d06f363420c9325e9202ba220e3b8dea
parent af08f1282a6fbee334ed3ff3f7f15ee3041ae8df
Author: amin <dev@aminmesbah.com>
Date:   Sun,  5 Aug 2018 01:47:41 +0000

Render an RGB cube

FossilOrigin-Name: 3aae773eeef32094712198d9ca32a6aebc2e66d47c30824bbf483e319ee1edb5
Diffstat:
Ashader/cube_f.glsl | 11+++++++++++
Ashader/cube_v.glsl | 16++++++++++++++++
Msrc/game.c | 80++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Msrc/game.h | 4++++
4 files changed, 106 insertions(+), 5 deletions(-)

diff --git a/shader/cube_f.glsl b/shader/cube_f.glsl @@ -0,0 +1,11 @@ +#version 300 es + +precision highp float; + +in vec3 color; +out vec4 frag_color; + +void main() +{ + frag_color = vec4(color, 1.0f); +} diff --git a/shader/cube_v.glsl b/shader/cube_v.glsl @@ -0,0 +1,16 @@ +#version 300 es + +layout (location = 0) in vec3 a_position; +layout (location = 1) in vec3 a_color; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +out vec3 color; + +void main() +{ + gl_Position = projection * view * model * vec4(a_position, 1.0f); + color = a_color; +} diff --git a/src/game.c b/src/game.c @@ -99,7 +99,59 @@ void game_init(struct GameState *game_state, uint32_t screen_width, uint32_t scr game_state->pyramid_ebo_id = pyramid_ebo_id; } + // load cube vertex data + { + GLfloat cube_vertices[] = { + // positions // colors + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, + }; + + GLuint elements[] = { + 0, 4, 6, 6, 2, 0, + 1, 5, 7, 7, 3, 1, + 3, 2, 0, 0, 1, 3, + 7, 6, 4, 4, 5, 7, + 0, 4, 5, 5, 1, 0, + 2, 6, 7, 7, 3, 2, + }; + + GLuint cube_vao; + glGenVertexArrays(1, &cube_vao); + glBindVertexArray(cube_vao); + + GLuint cube_vbo; + glGenBuffers(1, &cube_vbo); + glBindBuffer(GL_ARRAY_BUFFER, cube_vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW); + + // positions + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(*cube_vertices), (GLvoid*)0); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(*cube_vertices), (GLvoid*)(3 * sizeof(*cube_vertices))); + glEnableVertexAttribArray(1); + + GLuint cube_ebo; + glGenBuffers(1, &cube_ebo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube_ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); + + glBindVertexArray(0); + + game_state->cube_vao = cube_vao; + game_state->cube_vbo = cube_vbo; + game_state->cube_ebo = cube_ebo; + } + game_state->pyramid_shader = shader_compile("shader/pyramid_v.glsl", "shader/pyramid_f.glsl"); + game_state->cube_shader = shader_compile("shader/cube_v.glsl", "shader/cube_f.glsl"); } @@ -110,18 +162,18 @@ 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->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->pyramid_shader, "view", &view); - shader_setm4(&game_state->pyramid_shader, "projection", &projection); - +#if 0 // render pyramid { + shader_use(&game_state->pyramid_shader); + shader_setm4(&game_state->pyramid_shader, "view", &view); + shader_setm4(&game_state->pyramid_shader, "projection", &projection); + 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)); @@ -138,6 +190,24 @@ void game_update_and_render(struct GameState *game_state, float dt, uint32_t scr glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_INT, 0); glBindVertexArray(0); } +#endif + + // render cube + { + shader_use(&game_state->cube_shader); + shader_setm4(&game_state->cube_shader, "view", &view); + shader_setm4(&game_state->cube_shader, "projection", &projection); + + 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)); + + shader_setm4(&game_state->cube_shader, "model", &model); + + glBindVertexArray(game_state->cube_vao); + glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); + } } diff --git a/src/game.h b/src/game.h @@ -16,7 +16,11 @@ struct GameState GLuint pyramid_vao_id; GLuint pyramid_vbo_id; GLuint pyramid_ebo_id; + GLuint cube_vao; + GLuint cube_vbo; + GLuint cube_ebo; struct Shader pyramid_shader; + struct Shader cube_shader; }; #ifdef PLATFORM_HOTLOAD_GAME_CODE