diff options
Diffstat (limited to 'advanced_opengl/6.cubemaps')
-rwxr-xr-x | advanced_opengl/6.cubemaps/cubemaps | bin | 1288160 -> 1293704 bytes | |||
-rw-r--r-- | advanced_opengl/6.cubemaps/cubemaps.c | 774 | ||||
-rw-r--r-- | advanced_opengl/6.cubemaps/shaders/cube.frag (renamed from advanced_opengl/6.cubemaps/shaders/cube.fs) | 3 | ||||
-rw-r--r-- | advanced_opengl/6.cubemaps/shaders/cube.vert (renamed from advanced_opengl/6.cubemaps/shaders/cube.vs) | 7 | ||||
-rw-r--r-- | advanced_opengl/6.cubemaps/shaders/reflection.frag (renamed from advanced_opengl/6.cubemaps/shaders/reflection.fs) | 3 | ||||
-rw-r--r-- | advanced_opengl/6.cubemaps/shaders/reflection.vert (renamed from advanced_opengl/6.cubemaps/shaders/refraction.vs) | 7 | ||||
-rw-r--r-- | advanced_opengl/6.cubemaps/shaders/refraction.frag (renamed from advanced_opengl/6.cubemaps/shaders/refraction.fs) | 3 | ||||
-rw-r--r-- | advanced_opengl/6.cubemaps/shaders/refraction.vert (renamed from advanced_opengl/6.cubemaps/shaders/reflection.vs) | 7 | ||||
-rw-r--r-- | advanced_opengl/6.cubemaps/shaders/skybox.frag (renamed from advanced_opengl/6.cubemaps/shaders/skybox.fs) | 3 | ||||
-rw-r--r-- | advanced_opengl/6.cubemaps/shaders/skybox.vert (renamed from advanced_opengl/6.cubemaps/shaders/skybox.vs) | 7 |
10 files changed, 353 insertions, 461 deletions
diff --git a/advanced_opengl/6.cubemaps/cubemaps b/advanced_opengl/6.cubemaps/cubemaps Binary files differindex bdf54f5..bc274ea 100755 --- a/advanced_opengl/6.cubemaps/cubemaps +++ b/advanced_opengl/6.cubemaps/cubemaps diff --git a/advanced_opengl/6.cubemaps/cubemaps.c b/advanced_opengl/6.cubemaps/cubemaps.c index 5ea5300..32a53d1 100644 --- a/advanced_opengl/6.cubemaps/cubemaps.c +++ b/advanced_opengl/6.cubemaps/cubemaps.c @@ -2,450 +2,350 @@ #include "GLFW/glfw3.h" #include "pwyazh.h" - #include "pwyazh_GL.h" #include "common.h" -#include <unistd.h> +int main(void) +{ + GLFWwindow *window; + State state; + S32 width, height; + Input input; -#define WIDTH 1024 -#define HEIGHT 768 + if (glfwInit() == GLFW_FALSE) { + fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); + return(1); + } -static V3F camera_pos; -static F32 dt; -static Input input; -static B32 first_mouse = 1; + width = 1024; + height = 768; -void -key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) -{ - switch (action) - { - case GLFW_PRESS: { - switch (key) - { - case GLFW_KEY_D: { - input.move_right.state = KeyState_PRESS; - } break; - case GLFW_KEY_W: { - input.move_forward.state = KeyState_PRESS; - } break; - case GLFW_KEY_A: { - input.move_left.state = KeyState_PRESS; - } break; - case GLFW_KEY_S: { - input.move_backward.state = KeyState_PRESS; - } break; - case GLFW_KEY_E: { - input.move_up.state = KeyState_PRESS; - } break; - case GLFW_KEY_Q: { - input.move_down.state = KeyState_PRESS; - } break; - } - } break; - - case GLFW_RELEASE: { - switch (key) - { - case GLFW_KEY_D: { - input.move_right.state = KeyState_RELEASE; - } break; - case GLFW_KEY_W: { - input.move_forward.state = KeyState_RELEASE; - } break; - case GLFW_KEY_A: { - input.move_left.state = KeyState_RELEASE; - } break; - case GLFW_KEY_S: { - input.move_backward.state = KeyState_RELEASE; - } break; - case GLFW_KEY_E: { - input.move_up.state = KeyState_RELEASE; - } break; - case GLFW_KEY_Q: { - input.move_down.state = KeyState_RELEASE; - } break; - } - } break; - } -} + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + window = glfwCreateWindow(width, height, "Cubemaps", 0, 0); + if (!window) { + fprintf(stderr, "[ERROR] Failed to create window.\n"); + glfwTerminate(); + return(1); + } -void -cursor_pos_callback(GLFWwindow* window, double xpos, double ypos) -{ - if (first_mouse) - { - input.last_mouse_pos = v2f(xpos, ypos); - first_mouse = 0; - } - input.mouse_offset = v2f(input.mouse_offset.x+((F32)xpos-input.last_mouse_pos.x), - input.mouse_offset.y+((F32)ypos-input.last_mouse_pos.y)); - input.last_mouse_pos = v2f((F32)xpos, (F32)ypos); -} + glfwMakeContextCurrent(window); -int -main(void) -{ - GLFWwindow *window; - - if (glfwInit() == GLFW_FALSE) - { - fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); - return(1); - } - - glfwWindowHint(GLFW_RESIZABLE, 0); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - window = glfwCreateWindow(WIDTH, HEIGHT, "Cubemaps", 0, 0); - if (!window) - { - fprintf(stderr, "[ERROR] Failed to create window.\n"); - glfwTerminate(); - return(1); - } - - glfwMakeContextCurrent(window); - - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); - - glfwSetKeyCallback(window, key_callback); - glfwSetCursorPosCallback(window, cursor_pos_callback); - - if (glewInit() != GLEW_OK) - { - fprintf(stderr, "[ERROR] Failed to initialize glew.\n"); - glfwTerminate(); - return(1); - } - - U32 skybox_shader = create_shader_program("shaders/skybox.vs", "shaders/skybox.fs"); - U32 reflection_shader = create_shader_program("shaders/reflection.vs", "shaders/reflection.fs"); - U32 refraction_shader = create_shader_program("shaders/refraction.vs", "shaders/refraction.fs"); - U32 cube_shader = create_shader_program("shaders/cube.vs", "shaders/cube.fs"); - U32 grid_texture = load_texture("../../data/textures/grid.png"); - - const char *cube_texture_filenames[6] = { - "../../data/textures/skybox/right.jpg", - "../../data/textures/skybox/left.jpg", - "../../data/textures/skybox/top.jpg", - "../../data/textures/skybox/bottom.jpg", - "../../data/textures/skybox/front.jpg", - "../../data/textures/skybox/back.jpg" - }; - U32 skybox_texture = load_cubemap(cube_texture_filenames); - - F32 a = 0.5f; - F32 vertices[] = { - -a, -a, -a, 0.0f, 0.0f, - a, -a, -a, 1.0f, 0.0f, - a, a, -a, 1.0f, 1.0f, - a, a, -a, 1.0f, 1.0f, - -a, a, -a, 0.0f, 1.0f, - -a, -a, -a, 0.0f, 0.0f, - - -a, -a, a, 0.0f, 0.0f, - a, -a, a, 1.0f, 0.0f, - a, a, a, 1.0f, 1.0f, - a, a, a, 1.0f, 1.0f, - -a, a, a, 0.0f, 1.0f, - -a, -a, a, 0.0f, 0.0f, - - -a, a, a, 1.0f, 0.0f, - -a, a, -a, 1.0f, 1.0f, - -a, -a, -a, 0.0f, 1.0f, - -a, -a, -a, 0.0f, 1.0f, - -a, -a, a, 0.0f, 0.0f, - -a, a, a, 1.0f, 0.0f, - - a, a, a, 1.0f, 0.0f, - a, a, -a, 1.0f, 1.0f, - a, -a, -a, 0.0f, 1.0f, - a, -a, -a, 0.0f, 1.0f, - a, -a, a, 0.0f, 0.0f, - a, a, a, 1.0f, 0.0f, - - -a, -a, -a, 0.0f, 1.0f, - a, -a, -a, 1.0f, 1.0f, - a, -a, a, 1.0f, 0.0f, - a, -a, a, 1.0f, 0.0f, - -a, -a, a, 0.0f, 0.0f, - -a, -a, -a, 0.0f, 1.0f, - - -a, a, -a, 0.0f, 1.0f, - a, a, -a, 1.0f, 1.0f, - a, a, a, 1.0f, 0.0f, - a, a, a, 1.0f, 0.0f, - -a, a, a, 0.0f, 0.0f, - -a, a, -a, 0.0f, 1.0f - }; - - Transform cube_positions[] = { - transform_default(), - transform_make_translate(v3f(a, 0.0f, -4.0f*a)) - }; - - Transform platform_position = - transform_make_scale_translate(v3f(10.0f, 0.2f, 10.0f), - v3f(0.0f, -(a+(a*0.2f)+0.01f), 0.0f)); - - U32 cube_vao, vbo; - - glGenVertexArrays(1, &cube_vao); - glBindVertexArray(cube_vao); - - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)0); - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)(3*sizeof(F32))); - glBindVertexArray(0); - - a = 1.0f; - F32 skybox_cube_vertices[] = { - -a, -a, -a, - a, -a, -a, - a, a, -a, - a, a, -a, - -a, a, -a, - -a, -a, -a, - - -a, -a, a, - a, -a, a, - a, a, a, - a, a, a, - -a, a, a, - -a, -a, a, - - -a, a, a, - -a, a, -a, - -a, -a, -a, - -a, -a, -a, - -a, -a, a, - -a, a, a, - - a, a, a, - a, a, -a, - a, -a, -a, - a, -a, -a, - a, -a, a, - a, a, a, - - -a, -a, -a, - a, -a, -a, - a, -a, a, - a, -a, a, - -a, -a, a, - -a, -a, -a, - - -a, a, -a, - a, a, -a, - a, a, a, - a, a, a, - -a, a, a, - -a, a, -a, - }; - - U32 skybox_cube_vao; - glGenVertexArrays(1, &skybox_cube_vao); - glBindVertexArray(skybox_cube_vao); - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(skybox_cube_vertices), skybox_cube_vertices, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(F32), (void *)0); - glBindVertexArray(0); - - F32 normals_cube_vertices[] = { - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, - -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, - - -0.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 1.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f - }; - U32 normals_cube_vao; - glGenVertexArrays(1, &normals_cube_vao); - glBindVertexArray(normals_cube_vao); - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(normals_cube_vertices), normals_cube_vertices, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(F32), (void *)0); - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(F32), (void *)(3*sizeof(F32))); - glBindVertexArray(0); - - F32 target_fps = 60.0f; - F32 target_spf = 1.0f/target_fps; - camera_pos = v3f(0.0f, 1.0f, 3.0f); - F32 yaw = 0.0f, pitch = 0.0f; - - F32 last_time = glfwGetTime(); - - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LEQUAL); - - MAT4 view, projection; - view = mat4_make_translate(v3f_negate(camera_pos)); - view = mat4_rotate_angles(view, v3f(pitch, yaw, 0.0f)); - projection = perspective(90.0f, (F32)WIDTH/(F32)HEIGHT, 0.1f, 100.0f); - - while (!glfwWindowShouldClose(window)) - { - glfwPollEvents(); - - /* INFO(pryazha): update */ - - V3F left, up, forward; - - left = v3f(-view.m0.x, -view.m1.x, -view.m2.x); - up = v3f(view.m0.y, view.m1.y, view.m2.y); - forward = v3f(-view.m0.z, -view.m1.z, -view.m2.z); - - V3F dp = v3f_zero(); - F32 speed = 2.0f; - if (key_is_pressed(input.move_right)) - dp = v3f_add(dp, v3f_scalef(left, -speed*dt)); - if (key_is_pressed(input.move_forward)) - dp = v3f_add(dp, v3f_scalef(forward, speed*dt)); - if (key_is_pressed(input.move_left)) - dp = v3f_add(dp, v3f_scalef(left, speed*dt)); - if (key_is_pressed(input.move_backward)) - dp = v3f_add(dp, v3f_scalef(forward, -speed*dt)); - if (key_is_pressed(input.move_up)) - dp = v3f_add(dp, v3f_scalef(up, speed*dt)); - if (key_is_pressed(input.move_down)) - dp = v3f_add(dp, v3f_scalef(up, -speed*dt)); - - camera_pos = v3f_add(camera_pos, dp); - - /* NOTE(pryazha): Mouse handling */ - F32 sensitivity = 0.1f; - input.mouse_offset = v2f_scalef(input.mouse_offset, sensitivity); - yaw += input.mouse_offset.x; - pitch += input.mouse_offset.y; - if (pitch > 89.0f) - pitch = 89.0f; - if (pitch < -89.0f) - pitch = -89.0f; - - view = mat4_make_translate(v3f_negate(camera_pos)); - view = mat4_rotate_angles(view, v3f(pitch, yaw, 0.0f)); - - input_update_last_state(&input); - - /* NOTE(pryazha): Render */ - glClearColor(0.15f, 0.15f, 0.15f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glUseProgram(cube_shader); - shader_set_mat4fv(cube_shader, "view", view); - shader_set_mat4fv(cube_shader, "projection", projection); - glBindTexture(GL_TEXTURE_2D, grid_texture); - - glBindVertexArray(cube_vao); - for (S32 transform_index = 0; - transform_index < (S32)ArrayCount(cube_positions); - ++transform_index) - { - shader_set_mat4fv(cube_shader, "model", transform_apply(cube_positions[transform_index])); - glDrawArrays(GL_TRIANGLES, 0, 36); - } - - shader_set_mat4fv(cube_shader, "model", transform_apply(platform_position)); - glDrawArrays(GL_TRIANGLES, 0, 36); - glBindVertexArray(0); - - glUseProgram(reflection_shader); - shader_set_mat4fv(reflection_shader, "model", mat4_make_translate(v3f(0.0f, 2.0f, 0.0f))); - shader_set_mat4fv(reflection_shader, "view", view); - shader_set_mat4fv(reflection_shader, "projection", projection); - shader_set_3fv(reflection_shader, "camera_pos", camera_pos); - glBindVertexArray(normals_cube_vao); - glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture); - glDrawArrays(GL_TRIANGLES, 0, 36); - glBindTexture(GL_TEXTURE_CUBE_MAP, 0); - glBindVertexArray(0); - - glUseProgram(refraction_shader); - shader_set_mat4fv(refraction_shader, "model", mat4_make_translate(v3f(2.0f, 2.0f, 0.0f))); - shader_set_mat4fv(refraction_shader, "view", view); - shader_set_mat4fv(refraction_shader, "projection", projection); - shader_set_3fv(refraction_shader, "camera_pos", camera_pos); - glBindVertexArray(normals_cube_vao); - glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture); - glDrawArrays(GL_TRIANGLES, 0, 36); - glBindTexture(GL_TEXTURE_CUBE_MAP, 0); - glBindVertexArray(0); - - glUseProgram(skybox_shader); - MAT4 rotate_view = mat4_identity(); - rotate_view.m0 = v4f(view.m0.x, view.m0.y, view.m0.z, 0.0f); - rotate_view.m1 = v4f(view.m1.x, view.m1.y, view.m1.z, 0.0f); - rotate_view.m2 = v4f(view.m2.x, view.m2.y, view.m2.z, 0.0f); - shader_set_mat4fv(skybox_shader, "view", rotate_view); - shader_set_mat4fv(skybox_shader, "projection", projection); - glBindVertexArray(skybox_cube_vao); - glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture); - glDrawArrays(GL_TRIANGLES, 0, 36); - glBindVertexArray(0); - - glfwSwapBuffers(window); - - F32 elapsed = glfwGetTime()-last_time; - if (elapsed < target_spf) - { - U32 sleep_time = (U32)(target_spf-elapsed); - if (sleep_time > 0) - sleep(sleep_time); - } - F32 current_time = glfwGetTime(); - dt = current_time-last_time; - last_time = current_time; - } - - glfwTerminate(); - return(0); + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + + if (glewInit() != GLEW_OK) { + fprintf(stderr, "[ERROR] Failed to initialize glew.\n"); + glfwTerminate(); + return(1); + } + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + + U32 skybox_shader = create_shader_program("shaders/skybox.vert", "shaders/skybox.frag"); + U32 reflection_shader = create_shader_program("shaders/reflection.vert", "shaders/reflection.frag"); + U32 refraction_shader = create_shader_program("shaders/refraction.vert", "shaders/refraction.frag"); + U32 cube_shader = create_shader_program("shaders/cube.vert", "shaders/cube.frag"); + U32 grid_texture = load_texture("../../data/textures/grid.png"); + + const char *cube_texture_filenames[6] = { + "../../data/textures/skybox/right.jpg", + "../../data/textures/skybox/left.jpg", + "../../data/textures/skybox/top.jpg", + "../../data/textures/skybox/bottom.jpg", + "../../data/textures/skybox/front.jpg", + "../../data/textures/skybox/back.jpg" + }; + U32 skybox_texture = load_cubemap(cube_texture_filenames); + + F32 a = 0.5f; + F32 vertices[] = { + -a, -a, -a, 0.0f, 0.0f, + a, -a, -a, 1.0f, 0.0f, + a, a, -a, 1.0f, 1.0f, + a, a, -a, 1.0f, 1.0f, + -a, a, -a, 0.0f, 1.0f, + -a, -a, -a, 0.0f, 0.0f, + + -a, -a, a, 0.0f, 0.0f, + a, -a, a, 1.0f, 0.0f, + a, a, a, 1.0f, 1.0f, + a, a, a, 1.0f, 1.0f, + -a, a, a, 0.0f, 1.0f, + -a, -a, a, 0.0f, 0.0f, + + -a, a, a, 1.0f, 0.0f, + -a, a, -a, 1.0f, 1.0f, + -a, -a, -a, 0.0f, 1.0f, + -a, -a, -a, 0.0f, 1.0f, + -a, -a, a, 0.0f, 0.0f, + -a, a, a, 1.0f, 0.0f, + + a, a, a, 1.0f, 0.0f, + a, a, -a, 1.0f, 1.0f, + a, -a, -a, 0.0f, 1.0f, + a, -a, -a, 0.0f, 1.0f, + a, -a, a, 0.0f, 0.0f, + a, a, a, 1.0f, 0.0f, + + -a, -a, -a, 0.0f, 1.0f, + a, -a, -a, 1.0f, 1.0f, + a, -a, a, 1.0f, 0.0f, + a, -a, a, 1.0f, 0.0f, + -a, -a, a, 0.0f, 0.0f, + -a, -a, -a, 0.0f, 1.0f, + + -a, a, -a, 0.0f, 1.0f, + a, a, -a, 1.0f, 1.0f, + a, a, a, 1.0f, 0.0f, + a, a, a, 1.0f, 0.0f, + -a, a, a, 0.0f, 0.0f, + -a, a, -a, 0.0f, 1.0f + }; + + Transform cube_positions[] = { + transform_default(), + transform_make_translate(v3f(a, 0.0f, -4.0f*a)) + }; + + V3F platform_pos = v3f(10.0f, 0.2f, 10.0f); + V3F platform_scale = v3f(0.0f, -(a+(a*0.2f)+0.01f), 0.0f); + Transform platform_transform = transform_make_scale_translate(platform_pos, platform_scale); + + U32 cube_vao, vbo; + glGenVertexArrays(1, &cube_vao); + glBindVertexArray(cube_vao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)(3*sizeof(F32))); + glBindVertexArray(0); + + a = 1.0f; + F32 skybox_cube_vertices[] = { + -a, -a, -a, + a, -a, -a, + a, a, -a, + a, a, -a, + -a, a, -a, + -a, -a, -a, + + -a, -a, a, + a, -a, a, + a, a, a, + a, a, a, + -a, a, a, + -a, -a, a, + + -a, a, a, + -a, a, -a, + -a, -a, -a, + -a, -a, -a, + -a, -a, a, + -a, a, a, + + a, a, a, + a, a, -a, + a, -a, -a, + a, -a, -a, + a, -a, a, + a, a, a, + + -a, -a, -a, + a, -a, -a, + a, -a, a, + a, -a, a, + -a, -a, a, + -a, -a, -a, + + -a, a, -a, + a, a, -a, + a, a, a, + a, a, a, + -a, a, a, + -a, a, -a, + }; + + U32 skybox_cube_vao; + glGenVertexArrays(1, &skybox_cube_vao); + glBindVertexArray(skybox_cube_vao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(skybox_cube_vertices), skybox_cube_vertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(F32), (void *)0); + glBindVertexArray(0); + + F32 normals_cube_vertices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f, 0.0f, -1.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, + -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, + + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f + }; + U32 normals_cube_vao; + glGenVertexArrays(1, &normals_cube_vao); + glBindVertexArray(normals_cube_vao); + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(normals_cube_vertices), normals_cube_vertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(F32), (void *)0); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(F32), (void *)(3*sizeof(F32))); + glBindVertexArray(0); + + state.camera = (Camera) { + v3f(0.0f, 1.0f, 3.0f), 100.0f, + 0.1f, 100.0f, 0.0f, 0.0f + }; + input = input_init(); + + F32 target_fps = 60.0f; + F32 target_spf = 1.0f/target_fps; + F32 last_time = glfwGetTime(); + + while (!glfwWindowShouldClose(window)) { + glfwPollEvents(); + + /* INFO(pryazha): update */ + glfwGetFramebufferSize(window, &width, &height); + process_glfw_keyboard(window, &input); + process_glfw_mouse_pos(window, &input); + + if (key_first_press(input.exit)) + glfwSetWindowShouldClose(window, GLFW_TRUE); + + V3F dv = get_dv_camera_first_person(&input, &state.camera, + 4.0f, state.dt); + state.camera.pos = v3f_add(state.camera.pos, dv); + + F32 sensitivity = 0.1f; + input.mouse_offset = v2f_scalef(input.mouse_offset, sensitivity); + state.camera.yaw += input.mouse_offset.x; + state.camera.pitch += input.mouse_offset.y; + if (state.camera.pitch > 89.0f) + state.camera.pitch = 89.0f; + if (state.camera.pitch < -89.0f) + state.camera.pitch = -89.0f; + + input_update_last_state(&input); + + /* NOTE(pryazha): Render */ + MAT4 view, proj, model; + view = get_view_matrix(&state.camera); + proj = perspective(state.camera.fovx, (F32)width/(F32)height, + state.camera.near, state.camera.far); + + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glUseProgram(cube_shader); + shader_set_mat4fv(cube_shader, "view", view); + shader_set_mat4fv(cube_shader, "proj", proj); + glBindTexture(GL_TEXTURE_2D, grid_texture); + glBindVertexArray(cube_vao); + for (S32 i = 0; i < (S32)ArrayCount(cube_positions); ++i) { + model = transform_apply(cube_positions[i]); + shader_set_mat4fv(cube_shader, "model", model); + glDrawArrays(GL_TRIANGLES, 0, 36); + } + + model = transform_apply(platform_transform); + shader_set_mat4fv(cube_shader, "model", model); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindVertexArray(0); + + glUseProgram(reflection_shader); + model = mat4_make_translate(v3f(0.0f, 2.0f, 0.0f)); + shader_set_mat4fv(reflection_shader, "model", model); + shader_set_mat4fv(reflection_shader, "view", view); + shader_set_mat4fv(reflection_shader, "proj", proj); + shader_set_3fv(reflection_shader, "camera_pos", state.camera.pos); + glBindVertexArray(normals_cube_vao); + glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindTexture(GL_TEXTURE_CUBE_MAP, 0); + glBindVertexArray(0); + + glUseProgram(refraction_shader); + model = mat4_make_translate(v3f(2.0f, 2.0f, 0.0f)); + shader_set_mat4fv(refraction_shader, "model", model); + shader_set_mat4fv(refraction_shader, "view", view); + shader_set_mat4fv(refraction_shader, "proj", proj); + shader_set_3fv(refraction_shader, "camera_pos", state.camera.pos); + glBindVertexArray(normals_cube_vao); + glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindTexture(GL_TEXTURE_CUBE_MAP, 0); + glBindVertexArray(0); + + glUseProgram(skybox_shader); + MAT4 rotate_view = mat4_identity(); + rotate_view.m0 = v4f(view.m0.x, view.m0.y, view.m0.z, 0.0f); + rotate_view.m1 = v4f(view.m1.x, view.m1.y, view.m1.z, 0.0f); + rotate_view.m2 = v4f(view.m2.x, view.m2.y, view.m2.z, 0.0f); + shader_set_mat4fv(skybox_shader, "view", rotate_view); + shader_set_mat4fv(skybox_shader, "proj", proj); + glBindVertexArray(skybox_cube_vao); + glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_texture); + glDrawArrays(GL_TRIANGLES, 0, 36); + glBindVertexArray(0); + + glfwSwapBuffers(window); + + F32 elapsed = glfwGetTime()-last_time; + if (elapsed < target_spf) { + U32 sleep_time = (U32)(target_spf-elapsed); + if (sleep_time > 0) + sleep(sleep_time); + } + F32 current_time = glfwGetTime(); + state.dt = current_time-last_time; + last_time = current_time; + } + + glfwTerminate(); + return(0); } diff --git a/advanced_opengl/6.cubemaps/shaders/cube.fs b/advanced_opengl/6.cubemaps/shaders/cube.frag index f58c5d3..71892a7 100644 --- a/advanced_opengl/6.cubemaps/shaders/cube.fs +++ b/advanced_opengl/6.cubemaps/shaders/cube.frag @@ -6,8 +6,7 @@ in vec2 tex_coords; uniform sampler2D texture1; -void -main(void) +void main(void) { frag_color = texture(texture1, tex_coords); } diff --git a/advanced_opengl/6.cubemaps/shaders/cube.vs b/advanced_opengl/6.cubemaps/shaders/cube.vert index aea5c2c..2436f35 100644 --- a/advanced_opengl/6.cubemaps/shaders/cube.vs +++ b/advanced_opengl/6.cubemaps/shaders/cube.vert @@ -5,13 +5,12 @@ layout(location = 1) in vec2 atex_coords; uniform mat4 model; uniform mat4 view; -uniform mat4 projection; +uniform mat4 proj; out vec2 tex_coords; -void -main(void) +void main(void) { - gl_Position = projection*view*model*vec4(apos, 1.0); + gl_Position = proj*view*model*vec4(apos, 1.0); tex_coords = atex_coords; } diff --git a/advanced_opengl/6.cubemaps/shaders/reflection.fs b/advanced_opengl/6.cubemaps/shaders/reflection.frag index 3723ea9..72b1c0d 100644 --- a/advanced_opengl/6.cubemaps/shaders/reflection.fs +++ b/advanced_opengl/6.cubemaps/shaders/reflection.frag @@ -8,8 +8,7 @@ out vec4 frag_color; uniform vec3 camera_pos; uniform samplerCube skybox; -void -main(void) +void main(void) { vec3 i = pos-camera_pos; vec3 r = reflect(i, normalize(normal)); diff --git a/advanced_opengl/6.cubemaps/shaders/refraction.vs b/advanced_opengl/6.cubemaps/shaders/reflection.vert index c9a0698..d7070f2 100644 --- a/advanced_opengl/6.cubemaps/shaders/refraction.vs +++ b/advanced_opengl/6.cubemaps/shaders/reflection.vert @@ -8,12 +8,11 @@ out vec3 normal; uniform mat4 model; uniform mat4 view; -uniform mat4 projection; +uniform mat4 proj; -void -main(void) +void main(void) { pos = vec3(model*vec4(apos, 1.0)); normal = mat3(transpose(inverse(model)))*anormal; - gl_Position = projection*view*model*vec4(apos, 1.0); + gl_Position = proj*view*model*vec4(apos, 1.0); } diff --git a/advanced_opengl/6.cubemaps/shaders/refraction.fs b/advanced_opengl/6.cubemaps/shaders/refraction.frag index ef8c415..fa01b2c 100644 --- a/advanced_opengl/6.cubemaps/shaders/refraction.fs +++ b/advanced_opengl/6.cubemaps/shaders/refraction.frag @@ -8,8 +8,7 @@ out vec4 frag_color; uniform vec3 camera_pos; uniform samplerCube skybox; -void -main(void) +void main(void) { float ratio = 1.0/1.52; vec3 i = pos-camera_pos; diff --git a/advanced_opengl/6.cubemaps/shaders/reflection.vs b/advanced_opengl/6.cubemaps/shaders/refraction.vert index c9a0698..d7070f2 100644 --- a/advanced_opengl/6.cubemaps/shaders/reflection.vs +++ b/advanced_opengl/6.cubemaps/shaders/refraction.vert @@ -8,12 +8,11 @@ out vec3 normal; uniform mat4 model; uniform mat4 view; -uniform mat4 projection; +uniform mat4 proj; -void -main(void) +void main(void) { pos = vec3(model*vec4(apos, 1.0)); normal = mat3(transpose(inverse(model)))*anormal; - gl_Position = projection*view*model*vec4(apos, 1.0); + gl_Position = proj*view*model*vec4(apos, 1.0); } diff --git a/advanced_opengl/6.cubemaps/shaders/skybox.fs b/advanced_opengl/6.cubemaps/shaders/skybox.frag index bf69c8b..22be301 100644 --- a/advanced_opengl/6.cubemaps/shaders/skybox.fs +++ b/advanced_opengl/6.cubemaps/shaders/skybox.frag @@ -6,8 +6,7 @@ out vec4 frag_color; uniform samplerCube skybox; -void -main(void) +void main(void) { frag_color = texture(skybox, tex_coords); } diff --git a/advanced_opengl/6.cubemaps/shaders/skybox.vs b/advanced_opengl/6.cubemaps/shaders/skybox.vert index c790148..c7d91c9 100644 --- a/advanced_opengl/6.cubemaps/shaders/skybox.vs +++ b/advanced_opengl/6.cubemaps/shaders/skybox.vert @@ -5,12 +5,11 @@ layout(location = 0) in vec3 apos; out vec3 tex_coords; uniform mat4 view; -uniform mat4 projection; +uniform mat4 proj; -void -main(void) +void main(void) { tex_coords = apos; - vec4 pos = projection*view*vec4(apos, 1.0); + vec4 pos = proj*view*vec4(apos, 1.0); gl_Position = pos.xyww; } |