#include "GL/glew.h" #include "GLFW/glfw3.h" #include "pwyazh.h" #include "pwyazh_GL.h" #include "common.h" void draw_cube(U32 cube_vao, U32 texture) { glBindVertexArray(cube_vao); glBindTexture(GL_TEXTURE_2D, texture); glDrawArrays(GL_TRIANGLES, 0, 36); glBindTexture(GL_TEXTURE_2D, 0); glBindVertexArray(0); } int main(void) { GLFWwindow *window; State state; S32 width, height; Input input; if (glfwInit() == GLFW_FALSE) { fprintf(stderr, "[ERROR] Failed to initialize glfw.\n"); return(1); } width = 1024; height = 768; 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); if (glewInit() != GLEW_OK) { fprintf(stderr, "[ERROR] Failed to initialize glew.\n"); glfwTerminate(); return(1); } glEnable(GL_DEPTH_TEST); U32 shader = create_shader_program_geom("shaders/base.vert", "shaders/base.frag", "shaders/base.geom"); U32 explosion_shader = create_shader_program_geom("shaders/explosion.vert", "shaders/explosion.frag", "shaders/explosion.geom"); U32 normals_shader = create_shader_program_geom("shaders/normals.vert", "shaders/normals.frag", "shaders/normals.geom"); U32 basic_shader = create_shader_program("shaders/basic.vert", "shaders/basic.frag"); U32 grid_texture = load_texture("../../data/textures/grid.png"); F32 points[] = { -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f }; GLuint points_vao, vbo; glGenVertexArrays(1, &points_vao); glBindVertexArray(points_vao); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)0); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5*sizeof(F32), (void *)(2*sizeof(F32))); glBindVertexArray(0); F32 a = 0.5f; /* NOTE(pryazha): Counter-clockwise order */ F32 cube_vertices[] = { /* NOTE(pryazha): Back face */ a, -a, -a, 0.0f, 0.0f, /* bottom-left */ -a, a, -a, 1.0f, 1.0f, /* top-right */ a, a, -a, 0.0f, 1.0f, /* top-left */ a, -a, -a, 0.0f, 0.0f, /* bottom-left */ -a, -a, -a, 1.0f, 0.0f, /* bottom-right */ -a, a, -a, 1.0f, 1.0f, /* top-right */ /* NOTE(pryazha): Front face */ -a, -a, a, 0.0f, 0.0f, /* bottom-left */ a, a, a, 1.0f, 1.0f, /* top-right */ -a, a, a, 0.0f, 1.0f, /* top-left */ -a, -a, a, 0.0f, 0.0f, /* bottom-left */ a, -a, a, 1.0f, 0.0f, /* bottom-right */ a, a, a, 1.0f, 1.0f, /* top-right */ /* NOTE(pryazha): Left face */ -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ -a, a, a, 1.0f, 1.0f, /* top-right */ -a, a, -a, 0.0f, 1.0f, /* top-left */ -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ -a, -a, a, 1.0f, 0.0f, /* bottom-right */ -a, a, a, 1.0f, 1.0f, /* top-right */ /* NOTE(pryazha): Right face */ a, -a, a, 0.0f, 0.0f, /* bottom-left */ a, a, -a, 1.0f, 1.0f, /* top-right */ a, a, a, 0.0f, 1.0f, /* top-left */ a, -a, a, 0.0f, 0.0f, /* bottom-left */ a, -a, -a, 1.0f, 0.0f, /* bottom-right */ a, a, -a, 1.0f, 1.0f, /* top-right */ /* NOTE(pryazha): Top face */ -a, a, a, 0.0f, 0.0f, /* bottom-left */ a, a, -a, 1.0f, 1.0f, /* top-right */ -a, a, -a, 0.0f, 1.0f, /* top-left */ -a, a, a, 0.0f, 0.0f, /* bottom-left */ a, a, a, 1.0f, 0.0f, /* bottom-right */ a, a, -a, 1.0f, 1.0f, /* top-right */ /* NOTE(pryazha): Bottom face */ -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ a, -a, a, 1.0f, 1.0f, /* top-right */ -a, -a, a, 0.0f, 1.0f, /* top-left */ -a, -a, -a, 0.0f, 0.0f, /* bottom-left */ a, -a, -a, 1.0f, 0.0f, /* bottom-right */ a, -a, a, 1.0f, 1.0f, /* top-right */ }; U32 cube_vao; glGenVertexArrays(1, &cube_vao); glBindVertexArray(cube_vao); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_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 vertices[] = { -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.5f, 0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 1.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.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(vertices), vertices, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8*sizeof(F32), (void *)0); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8*sizeof(F32), (void *)(3*sizeof(F32))); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8*sizeof(F32), (void *)(6*sizeof(F32))); glBindVertexArray(0); state.camera = (Camera) { v3f(2.0f, 1.0f, 2.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(); F32 time = last_time; S32 scene = 0; while (!glfwWindowShouldClose(window)) { glfwPollEvents(); process_glfw_keyboard(window, &input); glfwGetFramebufferSize(window, &width, &height); /* NOTE(pryazha): Update */ V3F target = v3f_zero(); if (key_first_press(input.exit)) glfwSetWindowShouldClose(window, GLFW_TRUE); if (key_first_press(input.jump)) { scene++; scene %= 3; } input_update_last_state(&input); /* NOTE(pryazha): Render */ MAT4 view, proj, model; view = look_at(state.camera.pos, target, v3f(0.0f, 1.0f, 0.0f)); proj = perspective(state.camera.fovx, (F32)width/(F32)height, state.camera.near, state.camera.far); model = mat4_identity(); glClearColor(0.15f, 0.15f, 0.15f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (scene == 0) { glUseProgram(shader); glBindVertexArray(points_vao); glDrawArrays(GL_POINTS, 0, 4); glBindVertexArray(0); } else if (scene == 1) { glUseProgram(explosion_shader); shader_set_mat4fv(explosion_shader, "proj", proj); shader_set_mat4fv(explosion_shader, "view", view); shader_set_mat4fv(explosion_shader, "model", model); shader_set_1f(explosion_shader, "time", time); glBindVertexArray(cube_vao); glBindTexture(GL_TEXTURE_2D, grid_texture); glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0); } else { glUseProgram(basic_shader); shader_set_mat4fv(basic_shader, "proj", proj); shader_set_mat4fv(basic_shader, "view", view); shader_set_mat4fv(basic_shader, "model", model); draw_cube(normals_cube_vao, grid_texture); glUseProgram(normals_shader); shader_set_mat4fv(normals_shader, "proj", proj); shader_set_mat4fv(normals_shader, "view", view); shader_set_mat4fv(normals_shader, "model", model); draw_cube(normals_cube_vao, grid_texture); } 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; time += state.dt; last_time = current_time; } glDeleteBuffers(1, &vbo); glDeleteVertexArrays(1, &points_vao); glfwTerminate(); return(0); }