From e516095a425d1c8d5bd9f9004416a6c19bcaf84f Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 25 May 2018 19:49:37 +0200 Subject: Dynamically build compute shader from field function --- src/main.cc | 16 ++++++++++++++-- src/shader/compute.glsl | 11 ++--------- 2 files changed, 16 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main.cc b/src/main.cc index ceffe67..71d1cbd 100644 --- a/src/main.cc +++ b/src/main.cc @@ -22,7 +22,7 @@ #include "shader/display_vertex.glsl" #include "shader/display_fragment.glsl" -const unsigned int particle_count = 1000; +const unsigned int particle_count = 2500; const unsigned int max_ups = 100; const unsigned int texture_count = 20; @@ -83,6 +83,13 @@ std::vector makeInitialParticles(std::size_t count) { return buffer; } +std::string getShaderFunction(const std::string& fx, const std::string& fy) { + return COMPUTE_SHADER_CODE + + "vec2 f(vec2 v) {" + + "return vec2(" + fx + "," + fy + ");" + + "}"; +} + int main() { if( !glfwInit() ) { std::cerr << "Failed to initialize GLFW" << std::endl; @@ -123,7 +130,12 @@ int main() { GraphicShader sceneShader(VERTEX_SHADER_CODE, FRAGMENT_SHADER_CODE); - ComputeShader computeShader(COMPUTE_SHADER_CODE); + ComputeShader computeShader( + getShaderFunction( + "cos(v.x*sin(v.y))", + "sin(v.x-v.y)" + ) + ); computeShader.workOn(particleBuffer->getBuffer()); GraphicShader displayShader(DISPLAY_VERTEX_SHADER_CODE, diff --git a/src/shader/compute.glsl b/src/shader/compute.glsl index 148157b..eaf5579 100644 --- a/src/shader/compute.glsl +++ b/src/shader/compute.glsl @@ -6,17 +6,10 @@ layout (std430, binding=1) buffer bufferA{ float data[]; }; uniform vec2 world; -// vector field definition - -vec2 f(vec2 v) { - return vec2( - cos(v.x*sin(v.y)), - sin(v.x-v.y) - ); -} - // ODE solver +vec2 f(vec2); // field definition is to be appended + vec2 explicitEuler(float h, vec2 v) { return v + h * f(v); } -- cgit v1.2.3