Chain together vertex shaders, like with fragment shaders.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 2 Oct 2012 11:46:25 +0000 (13:46 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 2 Oct 2012 11:46:25 +0000 (13:46 +0200)
effect.cpp
effect.h
effect_chain.cpp
fs-footer.glsl [moved from footer.glsl with 100% similarity]
fs-header.glsl [moved from header.glsl with 100% similarity]
identity-vs.glsl [new file with mode: 0644]
vs-footer.glsl [new file with mode: 0644]
vs-header.glsl [new file with mode: 0644]
vs.glsl [deleted file]

index 3a4f8c8..15fff8c 100644 (file)
@@ -152,3 +152,8 @@ void Effect::set_uniforms(GLuint glsl_program_num, const std::string& prefix)
                set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
        }
 }
+
+std::string Effect::output_vertex_shader()
+{
+       return read_file("identity-vs.glsl");
+}
index 1478c6e..79209b8 100644 (file)
--- a/effect.h
+++ b/effect.h
@@ -35,6 +35,7 @@ public:
        virtual bool needs_mipmaps() { return false; }
 
        virtual std::string output_convenience_uniforms();
+       virtual std::string output_vertex_shader();
        virtual std::string output_fragment_shader() = 0;
 
        virtual void set_uniforms(GLuint glsl_program_num, const std::string& prefix);
index 5c1ba69..b3c9099 100644 (file)
@@ -155,9 +155,26 @@ void EffectChain::finalize()
                effects.push_back(gamma_conversion);
                current_gamma_curve = output_format.gamma_curve;
        }
+       
+       std::string vert_shader = read_file("vs-header.glsl");
+       for (unsigned i = 0; i < effects.size(); ++i) {
+               char effect_id[256];
+               sprintf(effect_id, "eff%d", i);
+       
+               vert_shader += "\n";
+               vert_shader += std::string("#define FUNCNAME ") + effect_id + "\n";
+               vert_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id);
+               vert_shader += replace_prefix(effects[i]->output_vertex_shader(), effect_id);
+               vert_shader += "#undef PREFIX\n";
+               vert_shader += "#undef FUNCNAME\n";
+               vert_shader += "#undef LAST_INPUT\n";
+               vert_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
+               vert_shader += "\n";
+       }
+       vert_shader.append(read_file("vs-footer.glsl"));
+       printf("%s\n", vert_shader.c_str());
 
-       std::string frag_shader = read_file("header.glsl");
-
+       std::string frag_shader = read_file("fs-header.glsl");
        for (unsigned i = 0; i < effects.size(); ++i) {
                char effect_id[256];
                sprintf(effect_id, "eff%d", i);
@@ -172,11 +189,11 @@ void EffectChain::finalize()
                frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
                frag_shader += "\n";
        }
-       frag_shader.append(read_file("footer.glsl"));
+       frag_shader.append(read_file("fs-footer.glsl"));
        printf("%s\n", frag_shader.c_str());
        
        glsl_program_num = glCreateProgram();
-       GLuint vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER);
+       GLuint vs_obj = compile_shader(vert_shader, GL_VERTEX_SHADER);
        GLuint fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
        glAttachShader(glsl_program_num, vs_obj);
        check_error();
similarity index 100%
rename from footer.glsl
rename to fs-footer.glsl
similarity index 100%
rename from header.glsl
rename to fs-header.glsl
diff --git a/identity-vs.glsl b/identity-vs.glsl
new file mode 100644 (file)
index 0000000..10e19e7
--- /dev/null
@@ -0,0 +1,5 @@
+// Identity transformation (sometimes useful to do nothing).
+vec2 FUNCNAME(vec2 tc)
+{
+       return LAST_INPUT(tc);
+}
diff --git a/vs-footer.glsl b/vs-footer.glsl
new file mode 100644 (file)
index 0000000..916eea9
--- /dev/null
@@ -0,0 +1,7 @@
+varying vec2 tc;
+
+void main()
+{
+       tc = LAST_INPUT(tc);
+        gl_Position = ftransform();
+}
diff --git a/vs-header.glsl b/vs-header.glsl
new file mode 100644 (file)
index 0000000..c171645
--- /dev/null
@@ -0,0 +1,6 @@
+vec2 read_input(vec2 tc)
+{
+       return gl_MultiTexCoord0.st;
+}
+
+#define LAST_INPUT read_input
diff --git a/vs.glsl b/vs.glsl
deleted file mode 100644 (file)
index c7e54e2..0000000
--- a/vs.glsl
+++ /dev/null
@@ -1,9 +0,0 @@
-#version 120
-varying vec2 tc;
-
-void main()
-{
-        tc = gl_MultiTexCoord0.st;
-        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
-}
-