From ce334547f0f0560386ca8b97f354d83da68019a5 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 16 Dec 2018 22:09:17 +0100 Subject: Generate fluid display using geometry shaders This should provide much more flexibility. For our purpose it would be useful if the vertex shader was executed after the geometry shader (to apply the projection matrix) but alas this is not the case. Thus the MVP matrix is applied during geometry construction and the vertex shader only provides density extraction. --- src/shader/code/collide.glsl | 6 +++--- src/shader/code/fragment.glsl | 7 ++++++- src/shader/code/geometry.glsl | 37 +++++++++++++++++++++++++++++++++++++ src/shader/code/vertex.glsl | 12 +++++++++--- 4 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 src/shader/code/geometry.glsl (limited to 'src/shader/code') diff --git a/src/shader/code/collide.glsl b/src/shader/code/collide.glsl index 90af535..936ca89 100644 --- a/src/shader/code/collide.glsl +++ b/src/shader/code/collide.glsl @@ -13,7 +13,7 @@ uniform uint nY; const uint q = 9; const float omega = 0.6; -const float velocityDisplayScalar = 500.; +const float displayAmplifier = 50.; float get(uint x, uint y, int i, int j) { return collideCells[q*nX*y + q*x + (i+1)*3 + j+1]; @@ -24,8 +24,8 @@ void set(uint x, uint y, int i, int j, float v) { } void setFluid(uint x, uint y, vec2 v, float d) { - fluidCells[3*nX*y + 3*x + 0] = float(x)-nX/2 + velocityDisplayScalar*v.x; - fluidCells[3*nX*y + 3*x + 1] = float(y)-nY/2 + velocityDisplayScalar*v.y; + fluidCells[3*nX*y + 3*x + 0] = float(x)-nX/2 + displayAmplifier*v.x; + fluidCells[3*nX*y + 3*x + 1] = float(y)-nY/2 + displayAmplifier*v.y; fluidCells[3*nX*y + 3*x + 2] = d; } diff --git a/src/shader/code/fragment.glsl b/src/shader/code/fragment.glsl index 37e18bd..a4121b8 100644 --- a/src/shader/code/fragment.glsl +++ b/src/shader/code/fragment.glsl @@ -1,5 +1,10 @@ static const std::string FRAGMENT_SHADER_CODE = R"( +#version 430 + +in vec3 color; +out vec4 FragColor; + void main() { - gl_FragColor = gl_Color; + FragColor = vec4(color.xyz, 0.0); } )"; diff --git a/src/shader/code/geometry.glsl b/src/shader/code/geometry.glsl new file mode 100644 index 0000000..6701b41 --- /dev/null +++ b/src/shader/code/geometry.glsl @@ -0,0 +1,37 @@ +static const std::string GEOMETRY_SHADER_CODE = R"( +#version 430 + +layout (points) in; +layout (triangle_strip, max_vertices=4) out; + +uniform mat4 MVP; + +in VS_OUT { + vec3 color; +} gs_in[]; + +out vec3 color; + +vec4 project(vec4 v) { + return MVP * v; +} + +void emitSquareAt(vec4 position) { + const float size = 0.2; + + gl_Position = project(position + vec4(-size, -size, 0.0, 0.0)); + EmitVertex(); + gl_Position = project(position + vec4( size, -size, 0.0, 0.0)); + EmitVertex(); + gl_Position = project(position + vec4(-size, size, 0.0, 0.0)); + EmitVertex(); + gl_Position = project(position + vec4( size, size, 0.0, 0.0)); + EmitVertex(); +} + +void main() { + color = gs_in[0].color; + emitSquareAt(gl_in[0].gl_Position); + EndPrimitive(); +} +)"; diff --git a/src/shader/code/vertex.glsl b/src/shader/code/vertex.glsl index 0eccad4..0727708 100644 --- a/src/shader/code/vertex.glsl +++ b/src/shader/code/vertex.glsl @@ -1,8 +1,14 @@ static const std::string VERTEX_SHADER_CODE = R"( -uniform mat4 MVP; +#version 430 + +layout (location=0) in vec3 VertexPosition; + +out VS_OUT { + vec3 color; +} vs_out; void main() { - gl_Position = MVP * vec4(gl_Vertex.xy, 0.0, 1.0); - gl_FrontColor = gl_Vertex.z * vec4(1., 0., 0., 0.); + gl_Position = vec4(VertexPosition.xy, 0., 1.); + vs_out.color = vec3(VertexPosition.z, 0., 0.); } )"; -- cgit v1.2.3