Skip to content

Commit

Permalink
Add geometry shader to pipeline
Browse files Browse the repository at this point in the history
 - This follows on the previous commit; rendering modes can now be
   switched by pressing 'M'. Currently, the alternate mode only moves
   some processing to the geometry shader, and adds an annoying
   time-dependent movement of line segments so I can verify it's working
   and the normal calculation looks alright.

 - Next up: trying to generate triangles in the geometry shader instead
   of just copying the line segments.
  • Loading branch information
yalue committed Feb 28, 2022
1 parent a64f714 commit 0f6094f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 45 deletions.
6 changes: 4 additions & 2 deletions l_system_3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static void FreeApplicationState(ApplicationState *s) {
// Recomputes the scene's projection matrix based on s->aspect_ratio.
static void UpdateProjectionMatrix(ApplicationState *s) {
glm_mat4_identity(s->shared_uniforms.projection);
glm_perspective(45.0, s->aspect_ratio, 0.01, 100.0,
glm_perspective(45.0, s->aspect_ratio, 1.0, 100.0,
s->shared_uniforms.projection);
}

Expand Down Expand Up @@ -297,7 +297,9 @@ static void UpdateCamera(ApplicationState *s) {
glm_vec3_zero(target);
glm_vec3_zero(up);
up[1] = 1.0;
tmp = glfwGetTime() / 4.0;
tmp = glfwGetTime();
s->shared_uniforms.current_time = tmp;
tmp /= 4.0;
position[0] = sin(tmp) * 5.0;
position[1] = 2.0;
position[2] = cos(tmp) * 5.0;
Expand Down
3 changes: 2 additions & 1 deletion l_system_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ typedef struct {
vec4 camera_position;
float size_scale;
float geometry_thickness;
float pad[2];
float current_time;
float pad[1];
} SharedUniforms;

// Maintains global data about the running program.
Expand Down
53 changes: 23 additions & 30 deletions pipes_shader.geom
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#version 330 core
layout (lines) in;
layout (triangle_strip, max_vertices = 4) out;
layout (line_strip, max_vertices = 2) out;

in VS_OUT {
vec3 position;
Expand All @@ -18,40 +18,33 @@ out GS_OUT {
vec4 color;
} gs_out;

uniform mat4 model;
uniform mat3 normal;

//INCLUDE_SHARED_UNIFORMS

// Returns a value that oscillates between 0 and 1 based on time.
float timeOffset() {
return (sin(shared_uniforms.current_time * 3) + 1.0) * 0.5;
}

void main() {
vec3 right = gs_in[0].right;
vec3 forward = gs_in[0].forward;
vec3 bottom = gs_in[0].position;
vec3 top = gs_in[0].position;
// NOTE: verify this is in the correct order.
gs_out.forward = normal * gs_in[0].forward;
gs_out.up = normal * gs_in[0].up;
gs_out.right = normal * gs_in[0].right;
gs_out.color = gs_in[1].color;

mat4 projView = shared_uniforms.projection * shared_uniforms.view;
// The normal vectors don't change.
gs_out.forward = forward;
gs_out.up = gs_in[0].up;
gs_out.right = right;
//gs_out.color = gs_in[1].color;
gs_out.color = vec4(0.5, 0.3, 1.0, 1.0);

float half_rect_width = 0.5 * shared_uniforms.geometry_thickness *
shared_uniforms.size_scale;

// Bottom left
gs_out.frag_position = bottom - (right * half_rect_width);
gl_Position = projView * vec4(gs_out.frag_position, 1.0);
vec4 pos_tmp = model * vec4(gs_in[0].position, 1);
pos_tmp += normalize(vec4(gs_out.right, 1.0)) * timeOffset();
gs_out.frag_position = pos_tmp.xyz;
gl_Position = projView * pos_tmp;
EmitVertex();
// Bottom right
gs_out.frag_position = bottom + (right * half_rect_width);
gl_Position = projView * vec4(gs_out.frag_position, 1.0);
EmitVertex();
// Top right
gs_out.frag_position = top + (right * half_rect_width);
gl_Position = projView * vec4(gs_out.frag_position, 1.0);
EmitVertex();
// Top left
gs_out.frag_position = top - (right * half_rect_width);
gl_Position = projView * vec4(gs_out.frag_position, 1.0);

pos_tmp = model * vec4(gs_in[1].position, 1);
pos_tmp += normalize(vec4(gs_out.right, 1.0)) * timeOffset();
gs_out.frag_position = pos_tmp.xyz;
gl_Position = projView * pos_tmp;
EmitVertex();
EndPrimitive();
}
11 changes: 4 additions & 7 deletions pipes_shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ layout (location = 3) in vec4 color_in;
// Added to the location of each vertex to center the overall mesh on 0,0,0.
uniform vec3 location_offset;

uniform mat4 model;
uniform mat3 normal;

out VS_OUT {
vec3 position;
vec3 forward;
Expand All @@ -21,9 +18,9 @@ out VS_OUT {
//INCLUDE_SHARED_UNIFORMS

void main() {
vs_out.position = (model * vec4(position_in + location_offset, 1.0)).xyz;
vs_out.forward = normalize(normal * forward_in);
vs_out.up = normalize(normal * up_in);
vs_out.right = normalize(cross(vs_out.forward, vs_out.up));
vs_out.position = position_in + location_offset;
vs_out.forward = forward_in;
vs_out.up = up_in;
vs_out.color = color_in;
vs_out.right = normalize(cross(forward_in, up_in));
}
3 changes: 2 additions & 1 deletion shared_uniforms.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout(std140) uniform SharedUniforms {
vec4 camera_position;
float size_scale;
float geometry_thickness;
float pad[2];
float current_time;
float pad[1];
} shared_uniforms;

7 changes: 3 additions & 4 deletions simple_shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ out VS_OUT {
//INCLUDE_SHARED_UNIFORMS

void main() {
gl_Position = shared_uniforms.projection * shared_uniforms.view * model *
vec4(position_in + location_offset, 1.0);
vec4 loc_tmp = model * vec4(position_in + location_offset, 1.0);
gl_Position = shared_uniforms.projection * shared_uniforms.view * loc_tmp;
vs_out.frag_position = loc_tmp.xyz;
vs_out.color = color_in;
vs_out.forward = normal * forward_in;
vs_out.up = normal * up_in;
Expand All @@ -34,6 +35,4 @@ void main() {
if (normal[0][0] == 0.0001) {
vs_out.color[0] *= 0.9999;
}

vs_out.frag_position = vec3(model * vec4(position_in + location_offset, 1.0));
}

0 comments on commit 0f6094f

Please sign in to comment.