aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdrian Kummerlaender2018-12-16 22:09:17 +0100
committerAdrian Kummerlaender2018-12-16 22:09:17 +0100
commitce334547f0f0560386ca8b97f354d83da68019a5 (patch)
tree2c4e148a7e8052779da70d9cf1767cea5c44926d /src
parent52cbbff72bd5516b5f325d1c88e4fcd8ca67f989 (diff)
downloadcompustream-ce334547f0f0560386ca8b97f354d83da68019a5.tar
compustream-ce334547f0f0560386ca8b97f354d83da68019a5.tar.gz
compustream-ce334547f0f0560386ca8b97f354d83da68019a5.tar.bz2
compustream-ce334547f0f0560386ca8b97f354d83da68019a5.tar.lz
compustream-ce334547f0f0560386ca8b97f354d83da68019a5.tar.xz
compustream-ce334547f0f0560386ca8b97f354d83da68019a5.tar.zst
compustream-ce334547f0f0560386ca8b97f354d83da68019a5.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/main.cc3
-rw-r--r--src/shader/code/collide.glsl6
-rw-r--r--src/shader/code/fragment.glsl7
-rw-r--r--src/shader/code/geometry.glsl37
-rw-r--r--src/shader/code/vertex.glsl12
-rw-r--r--src/shader/wrap/graphic_shader.cc10
-rw-r--r--src/shader/wrap/graphic_shader.h1
7 files changed, 68 insertions, 8 deletions
diff --git a/src/main.cc b/src/main.cc
index 5ac18e1..20d7666 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -14,6 +14,7 @@
#include "shader/wrap/graphic_shader.h"
#include "shader/wrap/compute_shader.h"
+#include "shader/code/geometry.glsl"
#include "shader/code/vertex.glsl"
#include "shader/code/fragment.glsl"
@@ -72,7 +73,7 @@ int renderWindow() {
window.init([&]() {
scene_shader = std::make_unique<GraphicShader>(
- VERTEX_SHADER_CODE, FRAGMENT_SHADER_CODE);
+ VERTEX_SHADER_CODE, GEOMETRY_SHADER_CODE, FRAGMENT_SHADER_CODE);
lattice_a = std::make_unique<LatticeCellBuffer>(nX, nY);
lattice_b = std::make_unique<LatticeCellBuffer>(nX, nY);
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.);
}
)";
diff --git a/src/shader/wrap/graphic_shader.cc b/src/shader/wrap/graphic_shader.cc
index 0ed37ff..c891730 100644
--- a/src/shader/wrap/graphic_shader.cc
+++ b/src/shader/wrap/graphic_shader.cc
@@ -15,6 +15,16 @@ GraphicShader::Guard GraphicShader::use() const {
return Guard(_id);
}
+GraphicShader::GraphicShader(const std::string& vertex,
+ const std::string& geometry,
+ const std::string fragment):
+ _id(glCreateProgram()) {
+ glAttachShader(_id, util::compileShader(vertex, GL_VERTEX_SHADER));
+ glAttachShader(_id, util::compileShader(geometry, GL_GEOMETRY_SHADER));
+ glAttachShader(_id, util::compileShader(fragment, GL_FRAGMENT_SHADER));
+ glLinkProgram(_id);
+}
+
GraphicShader::GraphicShader(const std::string& vertex, const std::string fragment):
_id(glCreateProgram()) {
glAttachShader(_id, util::compileShader(vertex, GL_VERTEX_SHADER));
diff --git a/src/shader/wrap/graphic_shader.h b/src/shader/wrap/graphic_shader.h
index 25a5efb..dcae6db 100644
--- a/src/shader/wrap/graphic_shader.h
+++ b/src/shader/wrap/graphic_shader.h
@@ -20,6 +20,7 @@ public:
Guard use() const;
+ GraphicShader(const std::string& vertex, const std::string& geometry, const std::string fragment);
GraphicShader(const std::string& vertex, const std::string fragment);
~GraphicShader();