diff options
-rw-r--r-- | demo.c | 49 | ||||
-rw-r--r-- | demo.fs | 8 | ||||
-rw-r--r-- | demo.vs | 3 | ||||
-rw-r--r-- | light.fs | 9 | ||||
-rw-r--r-- | light.vs | 11 | ||||
-rw-r--r-- | src/primitives.c | 135 |
6 files changed, 142 insertions, 73 deletions
@@ -11,11 +11,15 @@ float camera_roll_speed = 1.0; const float cameraMouseSensitivity = 0.1; honey_mesh cube; -honey_shader shader; +mat4 model; +honey_shader cube_shader; honey_texture container; honey_texture happy_face; -mat4 model, view, projection; +honey_mesh light_cube; +vec3 light_color = { 1, 0, 0 }; +mat4 light_model; +honey_shader light_shader; bool wireframe = false; @@ -108,15 +112,16 @@ void draw() { } honey_camera_calculate_view(&camera); - honey_shader_set_mat4(shader, "view", camera.view); - - honey_shader_set_mat4(shader, "model", model); + honey_shader_set_mat4(cube_shader, "view", camera.view); + honey_shader_set_mat4(light_shader, "view", camera.view); + honey_shader_set_mat4(cube_shader, "model", model); + honey_shader_set_mat4(light_shader, "model", light_model); honey_texture_use(container, 0); honey_texture_use(happy_face, 1); - honey_mesh_draw(cube, shader); - honey_mesh_draw(cube, shader); + honey_mesh_draw(cube, cube_shader); + honey_mesh_draw(light_cube, light_shader); glfwSwapBuffers(window); } @@ -141,21 +146,36 @@ int main() { return 1; } - if (honey_shader_load(&shader, "demo.vs", "demo.fs") != SHADER_OK) { + if (honey_shader_load(&cube_shader, "demo.vs", "demo.fs") != SHADER_OK) { return 1; } + if (honey_shader_load(&light_shader, "light.vs", "light.fs") != SHADER_OK) { + return 1; + } + + honey_shader_set_vec3(light_shader, "light_color", light_color); + if (honey_mesh_new_textured_cube(&cube, 1, 1, 1) != MESH_OK) { fprintf(stderr, "Failed to load cube\n"); return 1; } - honey_shader_set_int(shader, "box_texture", 0); - honey_shader_set_int(shader, "happy_texture", 1); + if (honey_mesh_new_cube(&light_cube, 0.5, 0.5, 0.5) != MESH_OK) { + return 1; + } + + glm_mat4_identity(light_model); + glm_translate(light_model, (vec3){4, 5, 0}); + honey_shader_set_mat4(light_shader, "model", light_model); + + honey_shader_set_int(cube_shader, "box_texture", 0); + honey_shader_set_int(cube_shader, "happy_texture", 1); + honey_shader_set_vec3(cube_shader, "light_color", light_color); glm_mat4_identity(model); //glm_rotate_x(model, glm_rad(-55), model); - honey_shader_set_mat4(shader, "model", model); + honey_shader_set_mat4(cube_shader, "model", model); vec3 camera_pos = { -4, 0, 0 }; vec3 camera_angle = { 0, 0, 0 }; @@ -170,8 +190,9 @@ int main() { camera_near, camera_far, camera_fov); - honey_shader_set_mat4(shader, "view", camera.view); - honey_shader_set_mat4(shader, "projection", camera.projection); + honey_shader_set_mat4(cube_shader, "view", camera.view); + honey_shader_set_mat4(cube_shader, "projection", camera.projection); + honey_shader_set_mat4(light_shader, "projection", camera.projection); glEnable(GL_DEPTH_TEST); @@ -183,7 +204,7 @@ int main() { honey_run(window); honey_mesh_delete(cube); - honey_shader_delete(shader); + honey_shader_delete(cube_shader); honey_quit(); @@ -2,16 +2,14 @@ in vec2 texture_coordinate; -uniform vec3 extra_color; +uniform vec3 light_color; uniform sampler2D box_texture; uniform sampler2D happy_texture; -out vec4 FragColor; +out vec4 fragment_color; void main() { - FragColor = mix(texture(box_texture, texture_coordinate), - texture(happy_texture, texture_coordinate), - 0.2); + fragment_color = texture(box_texture, texture_coordinate) * vec4(light_color.xyz, 1.0); } @@ -1,6 +1,7 @@ #version 330 core layout (location = 0) in vec3 position; -layout (location = 1) in vec2 texCoord; +layout (location = 1) in vec3 normal; +layout (location = 2) in vec2 texCoord; out vec2 texture_coordinate; diff --git a/light.fs b/light.fs new file mode 100644 index 0000000..f2a2431 --- /dev/null +++ b/light.fs @@ -0,0 +1,9 @@ +#version 330 core + +out vec4 fragment_color; + +uniform vec3 light_color; + +void main() { + fragment_color = vec4(light_color.xyz, 1.0); +}
\ No newline at end of file diff --git a/light.vs b/light.vs new file mode 100644 index 0000000..e135d84 --- /dev/null +++ b/light.vs @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec3 position; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * view * model * vec4(position.xyz, 1.0); +}
\ No newline at end of file diff --git a/src/primitives.c b/src/primitives.c index 00cfdb9..4e6d93b 100644 --- a/src/primitives.c +++ b/src/primitives.c @@ -13,33 +13,62 @@ enum honey_mesh_result honey_mesh_new_cube(honey_mesh* mesh, float z1 = depth; float vertices[] = { - x0, y0, z0, - x1, y0, z0, - x0, y1, z0, - x1, y1, z0, - x0, y0, z1, - x1, y0, z1, - x0, y1, z1, - x1, y1, z1 }; - - unsigned int indices[] = { 0, 1, 2, - 1, 2, 3, - 4, 5, 6, - 5, 6, 7, - 0, 2, 4, - 2, 4, 6, - 1, 3, 5, - 3, 5, 7, - 2, 3, 6, - 3, 6, 7, - 0, 1, 4, - 1, 4, 5 }; - - unsigned int attrib_sizes[] = { 3 }; + /* position normal tex coord */ + /* back face */ + x0, y0, z0, 0, 0, -1, + x1, y0, z0, 0, 0, -1, + x0, y1, z0, 0, 0, -1, + x1, y1, z0, 0, 0, -1, + + /* front face */ + x0, y0, z1, 0, 0, 1, + x1, y0, z1, 0, 0, 1, + x0, y1, z1, 0, 0, 1, + x1, y1, z1, 0, 0, 1, + + /* left face */ + x0, y0, z0, -1, 0, 0, + x0, y1, z0, -1, 0, 0, + x0, y0, z1, -1, 0, 0, + x0, y1, z1, -1, 0, 0, + + /* right face */ + x1, y0, z0, 1, 0, 0, + x1, y1, z0, 1, 0, 0, + x1, y0, z1, 1, 0, 0, + x1, y1, z1, 1, 0, 0, + + /* bottom face */ + x0, y0, z0, 0, -1, 0, + x1, y0, z0, 0, -1, 0, + x0, y0, z1, 0, -1, 0, + x1, y0, z1, 0, -1, 0, + + /* top face */ + x0, y1, z0, 0, 1, 0, + x1, y1, z0, 0, 1, 0, + x0, y1, z1, 0, 1, 0, + x1, y1, z1, 0, 1, 0 }; + + unsigned int indices[] = { + 0, 1, 2, + 1, 2, 3, + 4, 5, 6, + 5, 6, 7, + 8, 9, 10, + 9, 10, 11, + 12, 13, 14, + 13, 14, 15, + 16, 17, 18, + 17, 18, 19, + 20, 21, 22, + 21, 22, 23 }; + + unsigned int attrib_sizes[] = { 3, 3 }; enum honey_mesh_result result = honey_mesh_new(mesh, - vertices, - 8, 1, attrib_sizes, + vertices, 24, + 2, attrib_sizes, indices, 36); return result; @@ -59,43 +88,43 @@ enum honey_mesh_result honey_mesh_new_textured_cube(honey_mesh* mesh, float y1 = height; float z1 = depth; - float vertices[] = { - /* position tex coord */ + float vertices[] = { + /* position normal tex coord */ /* back face */ - x0, y0, z0, 0, 0, - x1, y0, z0, 1, 0, - x0, y1, z0, 0, 1, - x1, y1, z0, 1, 1, + x0, y0, z0, 0, 0, -1, 0, 0, + x1, y0, z0, 0, 0, -1, 1, 0, + x0, y1, z0, 0, 0, -1, 0, 1, + x1, y1, z0, 0, 0, -1, 1, 1, /* front face */ - x0, y0, z1, 0, 0, - x1, y0, z1, 1, 0, - x0, y1, z1, 0, 1, - x1, y1, z1, 1, 1, + x0, y0, z1, 0, 0, 1, 0, 0, + x1, y0, z1, 0, 0, 1, 1, 0, + x0, y1, z1, 0, 0, 1, 0, 1, + x1, y1, z1, 0, 0, 1, 1, 1, /* left face */ - x0, y0, z0, 0, 0, - x0, y1, z0, 1, 0, - x0, y0, z1, 0, 1, - x0, y1, z1, 1, 1, + x0, y0, z0, -1, 0, 0, 0, 0, + x0, y1, z0, -1, 0, 0, 1, 0, + x0, y0, z1, -1, 0, 0, 0, 1, + x0, y1, z1, -1, 0, 0, 1, 1, /* right face */ - x1, y0, z0, 0, 0, - x1, y1, z0, 1, 0, - x1, y0, z1, 0, 1, - x1, y1, z1, 1, 1, + x1, y0, z0, 1, 0, 0, 0, 0, + x1, y1, z0, 1, 0, 0, 1, 0, + x1, y0, z1, 1, 0, 0, 0, 1, + x1, y1, z1, 1, 0, 0, 1, 1, /* bottom face */ - x0, y0, z0, 0, 0, - x1, y0, z0, 1, 0, - x0, y0, z1, 0, 1, - x1, y0, z1, 1, 1, + x0, y0, z0, 0, -1, 0, 0, 0, + x1, y0, z0, 0, -1, 0, 1, 0, + x0, y0, z1, 0, -1, 0, 0, 1, + x1, y0, z1, 0, -1, 0, 1, 1, /* top face */ - x0, y1, z0, 0, 0, - x1, y1, z0, 1, 0, - x0, y1, z1, 0, 1, - x1, y1, z1, 1, 1 }; + x0, y1, z0, 0, 1, 0, 0, 0, + x1, y1, z0, 0, 1, 0, 1, 0, + x0, y1, z1, 0, 1, 0, 0, 1, + x1, y1, z1, 0, 1, 0, 1, 1 }; unsigned int indices[] = { 0, 1, 2, @@ -111,11 +140,11 @@ enum honey_mesh_result honey_mesh_new_textured_cube(honey_mesh* mesh, 20, 21, 22, 21, 22, 23 }; - unsigned int attrib_sizes[] = { 3, 2 }; + unsigned int attrib_sizes[] = { 3, 3, 2 }; enum honey_mesh_result result; result = honey_mesh_new(mesh, vertices, 24, - 2, attrib_sizes, + 3, attrib_sizes, indices, 36); return result; } |