diff options
author | pryazha <pryadeiniv@mail.ru> | 2025-01-19 17:33:44 +0500 |
---|---|---|
committer | pryazha <pryadeiniv@mail.ru> | 2025-01-19 17:33:44 +0500 |
commit | bd49bd525f4c6c6c15c4142bf42d1dd38be6fc16 (patch) | |
tree | 9e69f473c34b53e9e57d8af1873c39698bf5c80e /geometry_shader |
initial commit
Diffstat (limited to 'geometry_shader')
-rwxr-xr-x | geometry_shader/build.sh | 5 | ||||
-rwxr-xr-x | geometry_shader/geometry_shader | bin | 0 -> 1282768 bytes | |||
-rw-r--r-- | geometry_shader/geometry_shader.c | 337 | ||||
-rw-r--r-- | geometry_shader/shaders/base.fs | 11 | ||||
-rw-r--r-- | geometry_shader/shaders/base.gs | 27 | ||||
-rw-r--r-- | geometry_shader/shaders/base.vs | 14 | ||||
-rw-r--r-- | geometry_shader/shaders/basic.fs | 13 | ||||
-rw-r--r-- | geometry_shader/shaders/basic.vs | 16 | ||||
-rw-r--r-- | geometry_shader/shaders/explosion.fs | 13 | ||||
-rw-r--r-- | geometry_shader/shaders/explosion.gs | 44 | ||||
-rw-r--r-- | geometry_shader/shaders/explosion.vs | 18 | ||||
-rw-r--r-- | geometry_shader/shaders/normals.fs | 9 | ||||
-rw-r--r-- | geometry_shader/shaders/normals.gs | 44 | ||||
-rw-r--r-- | geometry_shader/shaders/normals.vs | 18 |
14 files changed, 569 insertions, 0 deletions
diff --git a/geometry_shader/build.sh b/geometry_shader/build.sh new file mode 100755 index 0000000..2d87dc0 --- /dev/null +++ b/geometry_shader/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +. ../config +TARGET='geometry_shader' +set -x +gcc -o $TARGET $CFLAGS $INCLUDE $LFLAGS $TARGET.c $LIBS && ./$TARGET diff --git a/geometry_shader/geometry_shader b/geometry_shader/geometry_shader Binary files differnew file mode 100755 index 0000000..53de7d2 --- /dev/null +++ b/geometry_shader/geometry_shader diff --git a/geometry_shader/geometry_shader.c b/geometry_shader/geometry_shader.c new file mode 100644 index 0000000..af2d494 --- /dev/null +++ b/geometry_shader/geometry_shader.c @@ -0,0 +1,337 @@ +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +#include "pwyazh.h" + +#include "common.h" + +#include <unistd.h> + +#define WIDTH 1024 +#define HEIGHT 768 + +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_SPACE: { + input.jump.state = KeyState_PRESS; + } break; + } + } break; + + case GLFW_RELEASE: { + switch (key) + { + case GLFW_KEY_SPACE: { + input.jump.state = KeyState_RELEASE; + } break; + } + } break; + } +} + +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; + + 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); + + U32 shader = create_shader_program_geom("shaders/base.vs", + "shaders/base.fs", + "shaders/base.gs"); + U32 explosion_shader = create_shader_program_geom("shaders/explosion.vs", + "shaders/explosion.fs", + "shaders/explosion.gs"); + U32 normals_shader = create_shader_program_geom("shaders/normals.vs", + "shaders/normals.fs", + "shaders/normals.gs"); + U32 basic_shader = create_shader_program("shaders/basic.vs", "shaders/basic.fs"); + + 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.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, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, + -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, + -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, + -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, + + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, + + -0.5f, -0.5f, -0.5f, 0.0f, -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, + + -0.5f, 0.5f, -0.5f, 0.0f, 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); + + 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(); + + /* NOTE(pryazha): Update */ + if (key_first_press(input.jump)) + { + scene++; + scene %= 3; + } + + 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); + + if (scene == 0) + { + glUseProgram(shader); + glBindVertexArray(points_vao); + glDrawArrays(GL_POINTS, 0, 4); + glBindVertexArray(0); + } + else if (scene == 1) + { + F32 radius = 2.0f; + F32 x = f32_sin(0.5*time)*radius; + F32 z = f32_cos(0.5*time)*radius; + MAT4 projection = perspective(90.0f, (F32)WIDTH/(F32)HEIGHT, 0.1f, 100.0f); + MAT4 view = look_at(v3f(x, 1.0f, z), v3f_zero(), v3f(0.0f, 1.0f, 0.0f)); + MAT4 model = mat4_identity(); + glUseProgram(explosion_shader); + shader_set_mat4fv(explosion_shader, "projection", projection); + 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 + { + MAT4 projection, view, model; + F32 radius, x, z; + + radius = 2.0f; + x = f32_sin(0.5*time)*radius; + z = f32_cos(0.5*time)*radius; + + projection = perspective(90.0f, (F32)WIDTH/(F32)HEIGHT, 0.1f, 100.0f); + view = look_at(v3f(x, 1.0f, z), v3f_zero(), v3f(0.0f, 1.0f, 0.0f)); + model = mat4_identity(); + + glUseProgram(basic_shader); + shader_set_mat4fv(basic_shader, "projection", projection); + 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, "projection", projection); + 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(); + dt = current_time-last_time; + time += dt; + last_time = current_time; + } + + glDeleteBuffers(1, &vbo); + glDeleteVertexArrays(1, &points_vao); + + glfwTerminate(); + return(0); +} diff --git a/geometry_shader/shaders/base.fs b/geometry_shader/shaders/base.fs new file mode 100644 index 0000000..283d885 --- /dev/null +++ b/geometry_shader/shaders/base.fs @@ -0,0 +1,11 @@ +#version 330 core + +in vec3 fcolor; + +out vec4 frag_color; + +void +main(void) +{ + frag_color = vec4(fcolor, 1.0f); +} diff --git a/geometry_shader/shaders/base.gs b/geometry_shader/shaders/base.gs new file mode 100644 index 0000000..6b140c5 --- /dev/null +++ b/geometry_shader/shaders/base.gs @@ -0,0 +1,27 @@ +#version 330 core +layout(points) in; +layout(triangle_strip, max_vertices = 5) out; + +in VS_OUT { + vec3 color; +} gs_in[]; + +out vec3 fcolor; + +void +main(void) +{ + fcolor = gs_in[0].color; + gl_Position = gl_in[0].gl_Position+vec4(-0.2, -0.2, 0.0, 0.0); + EmitVertex(); + gl_Position = gl_in[0].gl_Position+vec4(0.2, -0.2, 0.0, 0.0); + EmitVertex(); + gl_Position = gl_in[0].gl_Position+vec4(-0.2, 0.2, 0.0, 0.0); + EmitVertex(); + gl_Position = gl_in[0].gl_Position+vec4(0.2, 0.2, 0.0, 0.0); + EmitVertex(); + fcolor = vec3(1.0, 1.0, 1.0); + gl_Position = gl_in[0].gl_Position+vec4(0.0, 0.4, 0.0, 0.0); + EmitVertex(); + EndPrimitive(); +} diff --git a/geometry_shader/shaders/base.vs b/geometry_shader/shaders/base.vs new file mode 100644 index 0000000..2924e6c --- /dev/null +++ b/geometry_shader/shaders/base.vs @@ -0,0 +1,14 @@ +#version 330 core +layout(location = 0) in vec2 apos; +layout(location = 1) in vec3 acolor; + +out VS_OUT { + vec3 color; +} vs_out; + +void +main(void) +{ + vs_out.color = acolor; + gl_Position = vec4(apos, 0.0f, 1.0f); +} diff --git a/geometry_shader/shaders/basic.fs b/geometry_shader/shaders/basic.fs new file mode 100644 index 0000000..bdd4b9a --- /dev/null +++ b/geometry_shader/shaders/basic.fs @@ -0,0 +1,13 @@ +#version 330 core + +in vec2 tex_coords; + +out vec4 frag_color; + +uniform sampler2D texture1; + +void +main(void) +{ + frag_color = texture(texture1, tex_coords); +} diff --git a/geometry_shader/shaders/basic.vs b/geometry_shader/shaders/basic.vs new file mode 100644 index 0000000..9a630b6 --- /dev/null +++ b/geometry_shader/shaders/basic.vs @@ -0,0 +1,16 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 2) in vec2 atex_coords; + +out vec2 tex_coords; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void +main(void) +{ + gl_Position = projection*view*model*vec4(apos, 1.0f); + tex_coords = atex_coords; +} diff --git a/geometry_shader/shaders/explosion.fs b/geometry_shader/shaders/explosion.fs new file mode 100644 index 0000000..70e7151 --- /dev/null +++ b/geometry_shader/shaders/explosion.fs @@ -0,0 +1,13 @@ +#version 330 core + +in vec2 ftex_coords; + +out vec4 frag_color; + +uniform sampler2D texture0; + +void +main(void) +{ + frag_color = texture(texture0, ftex_coords); +} diff --git a/geometry_shader/shaders/explosion.gs b/geometry_shader/shaders/explosion.gs new file mode 100644 index 0000000..1883ad2 --- /dev/null +++ b/geometry_shader/shaders/explosion.gs @@ -0,0 +1,44 @@ +#version 330 core +layout(triangles) in; +layout(triangle_strip, max_vertices = 3) out; + +in VS_OUT { + vec2 gtex_coords; +} gs_in[]; + +out vec2 ftex_coords; + +uniform float time; + +vec3 +get_normal(void) +{ + vec3 a = vec3(gl_in[0].gl_Position)-vec3(gl_in[1].gl_Position); + vec3 b = vec3(gl_in[2].gl_Position)-vec3(gl_in[1].gl_Position); + return(normalize(cross(a, b))); +} + +vec4 +explode(vec4 position, vec3 normal) +{ + float magnitude = 0.2f; + vec3 direction = normal*magnitude*((sin(time)+1.0f)/2.0f); + vec4 result = position+vec4(direction, 0.0f); + return(result); +} + +void +main(void) +{ + vec3 normal = get_normal(); + gl_Position = explode(gl_in[0].gl_Position, normal); + ftex_coords = gs_in[0].gtex_coords; + EmitVertex(); + gl_Position = explode(gl_in[1].gl_Position, normal); + ftex_coords = gs_in[1].gtex_coords; + EmitVertex(); + gl_Position = explode(gl_in[2].gl_Position, normal); + ftex_coords = gs_in[2].gtex_coords; + EmitVertex(); + EndPrimitive(); +} diff --git a/geometry_shader/shaders/explosion.vs b/geometry_shader/shaders/explosion.vs new file mode 100644 index 0000000..5925b8f --- /dev/null +++ b/geometry_shader/shaders/explosion.vs @@ -0,0 +1,18 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec2 atex_coords; + +out VS_OUT { + vec2 gtex_coords; +} vs_out; + +uniform mat4 projection; +uniform mat4 view; +uniform mat4 model; + +void +main(void) +{ + vs_out.gtex_coords = atex_coords; + gl_Position = projection*view*model*vec4(apos, 1.0f); +} diff --git a/geometry_shader/shaders/normals.fs b/geometry_shader/shaders/normals.fs new file mode 100644 index 0000000..fbbafbf --- /dev/null +++ b/geometry_shader/shaders/normals.fs @@ -0,0 +1,9 @@ +#version 330 core + +out vec4 frag_color; + +void +main(void) +{ + frag_color = vec4(1.0, 0.0, 0.0, 1.0); +} diff --git a/geometry_shader/shaders/normals.gs b/geometry_shader/shaders/normals.gs new file mode 100644 index 0000000..3720a5f --- /dev/null +++ b/geometry_shader/shaders/normals.gs @@ -0,0 +1,44 @@ +#version 330 core +layout(triangles) in; +layout(line_strip, max_vertices = 6) out; + +in VS_OUT { + vec3 normal; +} gs_in[]; + +uniform mat4 projection; + +const float MAGNITUDE = 0.4; + +void +generate_line(int index) +{ + gl_Position = projection*gl_in[index].gl_Position; + EmitVertex(); + gl_Position = projection*(gl_in[index].gl_Position+ + vec4(gs_in[index].normal, 0.0)*MAGNITUDE); + EmitVertex(); + EndPrimitive(); +} + +void +main(void) +{ + /* + vec4 center = (gl_in[0].gl_Position+ + gl_in[1].gl_Position+ + gl_in[2].gl_Position)/3.0; + vec4 end = center+vec4(((gs_in[0].normal+ + gs_in[1].normal+ + gs_in[2].normal)/3.0), + 0.0)*MAGNITUDE; + gl_Position = projection*center; + EmitVertex(); + gl_Position = projection*end; + EmitVertex(); + EndPrimitive(); + */ + generate_line(0); + generate_line(1); + generate_line(2); +} diff --git a/geometry_shader/shaders/normals.vs b/geometry_shader/shaders/normals.vs new file mode 100644 index 0000000..3b728e5 --- /dev/null +++ b/geometry_shader/shaders/normals.vs @@ -0,0 +1,18 @@ +#version 330 core +layout(location = 0) in vec3 apos; +layout(location = 1) in vec3 anormal; + +out VS_OUT { + vec3 normal; +} vs_out; + +uniform mat4 view; +uniform mat4 model; + +void +main(void) +{ + mat3 normal_matrix = mat3(transpose(inverse(view*model))); + vs_out.normal = normalize(normal_matrix*anormal); + gl_Position = view*model*vec4(apos, 1.0); +} |