diff options
Diffstat (limited to 'advanced_opengl/1.depth')
-rwxr-xr-x | advanced_opengl/1.depth/depth | bin | 1282544 -> 1283928 bytes | |||
-rw-r--r-- | advanced_opengl/1.depth/depth.c | 433 | ||||
-rw-r--r-- | advanced_opengl/1.depth/shaders/depth.frag (renamed from advanced_opengl/1.depth/shaders/depth.fs) | 10 | ||||
-rw-r--r-- | advanced_opengl/1.depth/shaders/depth.vert (renamed from advanced_opengl/1.depth/shaders/depth.vs) | 7 |
4 files changed, 178 insertions, 272 deletions
diff --git a/advanced_opengl/1.depth/depth b/advanced_opengl/1.depth/depth Binary files differindex 68f04d6..80da145 100755 --- a/advanced_opengl/1.depth/depth +++ b/advanced_opengl/1.depth/depth diff --git a/advanced_opengl/1.depth/depth.c b/advanced_opengl/1.depth/depth.c index eb79c1c..03092df 100644 --- a/advanced_opengl/1.depth/depth.c +++ b/advanced_opengl/1.depth/depth.c @@ -2,273 +2,184 @@ #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 1280 -#define HEIGHT 720 + if (glfwInit() == GLFW_FALSE) { + fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); + return(1); + } -static V3F camera_pos; -static F32 dt; -static Input input; + 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, "Depth testing.", 0, 0); + if (!window) { + fprintf(stderr, "[ERROR] Failed to create window.\n"); + glfwTerminate(); + return(1); + } -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, "depth testing", 0, 0); - if (!window) - { - fprintf(stderr, "[ERROR] Failed to create window.\n"); - glfwTerminate(); - return(1); - } - - glfwMakeContextCurrent(window); - - glfwSetKeyCallback(window, key_callback); - - if (glewInit() != GLEW_OK) - { - fprintf(stderr, "[ERROR] Failed to initialize glew.\n"); - glfwTerminate(); - return(1); - } - - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LESS); - - U32 shader = create_shader_program("shaders/depth.vs", "shaders/depth.fs"); - U32 marble_texture = load_texture("../../data/textures/marble.jpg"); - U32 metal_texture = load_texture("../../data/textures/metal.png"); - - 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*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 VAO, VBO; - glGenVertexArrays(1, &VAO); - glBindVertexArray(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); - - F32 target_fps = 60.0f; - F32 target_spf = 1.0f/target_fps; - camera_pos = v3f(0.0f, 0.0f, 3.0f); - - /* glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); */ - - F32 last_time = glfwGetTime(); - - while (!glfwWindowShouldClose(window)) - { - glfwPollEvents(); - - /* INFO(pryazha): update */ - V3F left, up, forward; - V3F world_up = v3f(0.0f, 1.0f, 0.0f); - - forward = v3f_norm(v3f_sub(v3f(0.0f, 0.0f, 0.0f), camera_pos)); - left = v3f_norm(v3f_cross(world_up, forward)); - up = v3f_cross(forward, left); - - 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)); - - input_update_last_state(&input); - - camera_pos = v3f_add(camera_pos, dp); - - /* INFO(pryazha): render */ - glClearColor(0.15f, 0.15f, 0.15f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glUseProgram(shader); - MAT4 model, view, projection; - - view = look_at(camera_pos, v3f(0.0f, 0.0f, 0.0f), v3f(0.0f, 1.0f, 0.0f)); - shader_set_mat4fv(shader, "view", view); - - /* projection = ortho(-5.0f, 5.0f, -5.0f, 5.0f, 0.1f, 100.0f); */ - F32 fovx = 90.0f; - F32 near = 0.1f; - F32 far = 100.0f; - projection = perspective(fovx, (F32)WIDTH/(F32)HEIGHT, near, far); - - shader_set_mat4fv(shader, "projection", projection); - - glBindTexture(GL_TEXTURE_2D, marble_texture); - glBindVertexArray(VAO); - for (S32 transform_index = 0; - transform_index < (S32)ArrayCount(cube_positions); - ++transform_index) - { - model = transform_apply(cube_positions[transform_index]); - shader_set_mat4fv(shader, "model", model); - glDrawArrays(GL_TRIANGLES, 0, 36); - } - - glBindTexture(GL_TEXTURE_2D, metal_texture); - model = transform_apply(platform_position); - shader_set_mat4fv(shader, "model", model); - glDrawArrays(GL_TRIANGLES, 0, 36); - - 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); + glfwMakeContextCurrent(window); + + if (glewInit() != GLEW_OK) { + fprintf(stderr, "[ERROR] Failed to initialize glew.\n"); + glfwTerminate(); + return(1); + } + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + + U32 shader = create_shader_program("shaders/depth.vert", "shaders/depth.frag"); + U32 marble_texture = load_texture("../../data/textures/marble.jpg"); + U32 metal_texture = load_texture("../../data/textures/metal.png"); + + 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*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 VAO, VBO; + glGenVertexArrays(1, &VAO); + glBindVertexArray(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); + + state.camera = (Camera) { + v3f(0.0f, 0.0f, 3.0f), + 90.0f, 0.1f, 100.0f, + 0.0f, 0.0f + }; + + F32 target_fps = 60.0f; + F32 target_spf = 1.0f/target_fps; + F32 last_time = glfwGetTime(); + + while (!glfwWindowShouldClose(window)) { + glfwPollEvents(); + + process_glfw_keyboard(window, &input); + glfwGetFramebufferSize(window, &width, &height); + + /* INFO(pryazha): Update */ + V3F target = v3f_zero(); + + if (key_first_press(input.exit)) + glfwSetWindowShouldClose(window, GLFW_TRUE); + + V3F dv = get_dv_camera_orbital(&input, state.camera.pos, + target, state.dt, 3.0f); + state.camera.pos = v3f_add(state.camera.pos, dv); + + input_update_last_state(&input); + + /* INFO(pryazha): Render */ + MAT4 model, view, proj; + view = look_at(state.camera.pos, target, v3f(0.0f, 1.0f, 0.0f)); + /* proj = ortho(-5.0f, 5.0f, -5.0f, 5.0f, 0.1f, 100.0f); */ + proj = perspective(state.camera.fovx, (F32)width/(F32)height, + state.camera.near, state.camera.far); + + glViewport(0, 0, width, height); + glClearColor(0.15f, 0.15f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glUseProgram(shader); + shader_set_mat4fv(shader, "view", view); + shader_set_mat4fv(shader, "proj", proj); + glBindTexture(GL_TEXTURE_2D, marble_texture); + glBindVertexArray(VAO); + for (S32 i = 0; i < (S32)ArrayCount(cube_positions); ++i) { + model = transform_apply(cube_positions[i]); + shader_set_mat4fv(shader, "model", model); + glDrawArrays(GL_TRIANGLES, 0, 36); + } + glBindTexture(GL_TEXTURE_2D, metal_texture); + model = transform_apply(platform_transform); + shader_set_mat4fv(shader, "model", model); + 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/1.depth/shaders/depth.fs b/advanced_opengl/1.depth/shaders/depth.frag index a515c15..710b0d2 100644 --- a/advanced_opengl/1.depth/shaders/depth.fs +++ b/advanced_opengl/1.depth/shaders/depth.frag @@ -9,19 +9,15 @@ uniform sampler2D our_texture; float near = 0.1; float far = 100.0; -float -linearize_depth(float depth) +float linearize_depth(float depth) { float ndc_z = 2.0*depth-1.0; return((2.0*near*far)/(far+near-ndc_z*(far-near))); } -void -main(void) +void main(void) { - /* - frag_color = texture(our_texture, tex_coords); - */ + /* frag_color = texture(our_texture, tex_coords); */ float depth = linearize_depth(gl_FragCoord.z)/far; frag_color = vec4(vec3(depth), 1.0); } diff --git a/advanced_opengl/1.depth/shaders/depth.vs b/advanced_opengl/1.depth/shaders/depth.vert index 62e71a2..68b8fe2 100644 --- a/advanced_opengl/1.depth/shaders/depth.vs +++ b/advanced_opengl/1.depth/shaders/depth.vert @@ -4,13 +4,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; } |