summaryrefslogtreecommitdiff
path: root/stencil/stencil.c
diff options
context:
space:
mode:
authorpryazha <pryadeiniv@mail.ru>2025-01-23 19:39:54 +0500
committerpryazha <pryadeiniv@mail.ru>2025-01-23 19:39:54 +0500
commitbf1c59565096ac9774493846cfb15e259d3b0e66 (patch)
tree82e8182af128580b6f38437feccba8c0f48abdc1 /stencil/stencil.c
parent1ee094199af9c169e1ccaa53c7b0c186c4a3639e (diff)
restructure
Diffstat (limited to 'stencil/stencil.c')
-rw-r--r--stencil/stencil.c297
1 files changed, 0 insertions, 297 deletions
diff --git a/stencil/stencil.c b/stencil/stencil.c
deleted file mode 100644
index a7c58f3..0000000
--- a/stencil/stencil.c
+++ /dev/null
@@ -1,297 +0,0 @@
-#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_STENCIL_TEST);
- glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
-
- U32 shader = create_shader_program("shaders/stencil.vs", "shaders/stencil.fs");
- U32 outline_shader = create_shader_program("shaders/stencil.vs", "shaders/outline.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 | GL_STENCIL_BUFFER_BIT);
-
- /* NOTE(pryazha): Update stencil buffer with ones while drawing the cubes */
- glStencilFunc(GL_ALWAYS, 1, 0xFF);
- glStencilMask(0xFF);
-
- MAT4 model, view, projection;
- 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);
- shader_set_mat4fv(shader, "view", view);
- 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);
- }
-
- /* NOTE(pryazha): Don't update stencil buffer while drawing the floor */
- glStencilMask(0x00);
-
- glBindTexture(GL_TEXTURE_2D, metal_texture);
- model = transform_apply(platform_position);
- shader_set_mat4fv(shader, "model", model);
- glDrawArrays(GL_TRIANGLES, 0, 36);
-
- glStencilFunc(GL_NOTEQUAL, 1, 0xFF);
- glStencilMask(0x00);
- glDisable(GL_DEPTH_TEST);
-
- glUseProgram(outline_shader);
- shader_set_mat4fv(outline_shader, "view", view);
- shader_set_mat4fv(outline_shader, "projection", projection);
-
- for (S32 transform_index = 0;
- transform_index < (S32)ArrayCount(cube_positions);
- ++transform_index)
- {
- Transform transform = cube_positions[transform_index];
- transform = transform_scale(transform, v3f(1.1f, 1.1f, 1.1f));
- shader_set_mat4fv(outline_shader, "model", transform_apply(transform));
- glDrawArrays(GL_TRIANGLES, 0, 36);
- }
-
- glStencilMask(0xFF);
- glStencilFunc(GL_ALWAYS, 1, 0xFF);
- glEnable(GL_DEPTH_TEST);
-
- 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);
-}