summaryrefslogtreecommitdiff
path: root/face_culling
diff options
context:
space:
mode:
Diffstat (limited to 'face_culling')
-rwxr-xr-xface_culling/build.sh5
-rwxr-xr-xface_culling/face_cullingbin0 -> 1283016 bytes
-rw-r--r--face_culling/face_culling.c304
-rw-r--r--face_culling/shaders/face_culling.fs13
-rw-r--r--face_culling/shaders/face_culling.vs16
5 files changed, 338 insertions, 0 deletions
diff --git a/face_culling/build.sh b/face_culling/build.sh
new file mode 100755
index 0000000..571cdc2
--- /dev/null
+++ b/face_culling/build.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+. ../config
+TARGET='face_culling'
+set -x
+gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET
diff --git a/face_culling/face_culling b/face_culling/face_culling
new file mode 100755
index 0000000..7a816d4
--- /dev/null
+++ b/face_culling/face_culling
Binary files differ
diff --git a/face_culling/face_culling.c b/face_culling/face_culling.c
new file mode 100644
index 0000000..01c2325
--- /dev/null
+++ b/face_culling/face_culling.c
@@ -0,0 +1,304 @@
+#include "GL/glew.h"
+#include "GLFW/glfw3.h"
+
+#include "pwyazh.h"
+
+#include "common.h"
+
+#include <unistd.h>
+
+#define WIDTH 1280
+#define HEIGHT 720
+
+static V3F camera_pos;
+static F32 dt;
+static Input input;
+
+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;
+ }
+}
+
+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);
+ glEnable(GL_CULL_FACE);
+ glFrontFace(GL_CW);
+ glCullFace(GL_BACK);
+
+ U32 shader = create_shader_program("shaders/face_culling.vs", "shaders/face_culling.fs");
+
+ U32 grid_texture = load_texture("../data/textures/grid.png");
+
+ F32 a = 0.5f;
+ /* NOTE(pryazha): Counter-clockwise order */
+ F32 cube_vertices_ccw[] = {
+ /* 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 */
+ };
+
+ F32 cube_vertices_cw[] = {
+ /* 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, 1.0f, 0.0f, /* bottom-right */
+ a, -a, -a, 0.0f, 0.0f, /* bottom-left */
+ a, a, -a, 0.0f, 1.0f, /* top-left */
+ -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, 1.0f, 0.0f, /* bottom-right */
+ -a, -a, a, 0.0f, 0.0f, /* bottom-left */
+ -a, a, a, 0.0f, 1.0f, /* top-left */
+ 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, 1.0f, 0.0f, /* bottom-right */
+ -a, -a, -a, 0.0f, 0.0f, /* bottom-left */
+ -a, a, -a, 0.0f, 1.0f, /* top-left */
+ -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, 1.0f, 0.0f, /* bottom-right */
+ a, -a, a, 0.0f, 0.0f, /* bottom-left */
+ a, a, a, 0.0f, 1.0f, /* top-left */
+ 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, 1.0f, 0.0f, /* bottom-right */
+ -a, a, a, 0.0f, 0.0f, /* bottom-left */
+ -a, a, -a, 0.0f, 1.0f, /* top-left */
+ 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, 1.0f, 0.0f, /* bottom-right */
+ -a, -a, -a, 0.0f, 0.0f, /* bottom-left */
+ -a, -a, a, 0.0f, 1.0f, /* top-left */
+ a, -a, a, 1.0f, 1.0f, /* top-right */
+ };
+
+ U32 cube_vao, vbo;
+ glGenVertexArrays(1, &cube_vao);
+ glBindVertexArray(cube_vao);
+ glGenBuffers(1, &vbo);
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices_cw), cube_vertices_cw, 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);
+
+ Transform cube_transform = transform_default();
+
+ F32 target_fps = 60.0f;
+ F32 target_spf = 1.0f/target_fps;
+ camera_pos = v3f(0.0f, 1.0f, 3.0f);
+
+ F32 last_time = glfwGetTime();
+
+ F32 d_angle = 45.0f;
+
+ 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);
+
+ MAT4 model, view, projection;
+ cube_transform = transform_rotate(cube_transform, v3f(0.0f, d_angle*dt, 0.0f));
+ model = transform_apply(cube_transform);
+ view = look_at(camera_pos, v3f(0.0f, 0.0f, 0.0f), v3f(0.0f, 1.0f, 0.0f));
+ projection = perspective(90.0f, (F32)WIDTH/(F32)HEIGHT, 0.1f, 100.0f);
+
+ glUseProgram(shader);
+ glBindTexture(GL_TEXTURE_2D, grid_texture);
+ shader_set_mat4fv(shader, "model", model);
+ shader_set_mat4fv(shader, "view", view);
+ shader_set_mat4fv(shader, "projection", projection);
+ glBindVertexArray(cube_vao);
+ 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);
+}
diff --git a/face_culling/shaders/face_culling.fs b/face_culling/shaders/face_culling.fs
new file mode 100644
index 0000000..f58c5d3
--- /dev/null
+++ b/face_culling/shaders/face_culling.fs
@@ -0,0 +1,13 @@
+#version 330 core
+
+out vec4 frag_color;
+
+in vec2 tex_coords;
+
+uniform sampler2D texture1;
+
+void
+main(void)
+{
+ frag_color = texture(texture1, tex_coords);
+}
diff --git a/face_culling/shaders/face_culling.vs b/face_culling/shaders/face_culling.vs
new file mode 100644
index 0000000..d3d92d1
--- /dev/null
+++ b/face_culling/shaders/face_culling.vs
@@ -0,0 +1,16 @@
+#version 330 core
+layout(location = 0) in vec3 apos;
+layout(location = 1) in vec2 atex_coords;
+
+uniform mat4 model;
+uniform mat4 view;
+uniform mat4 projection;
+
+out vec2 tex_coords;
+
+void
+main(void)
+{
+ gl_Position = projection*view*model*vec4(apos, 1.0f);
+ tex_coords = atex_coords;
+}