Make the example program actually try to use the effect chain.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 Oct 2012 16:59:38 +0000 (18:59 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 1 Oct 2012 16:59:38 +0000 (18:59 +0200)
effect_chain.cpp
header.glsl
main.cpp

index 50fe599..a838150 100644 (file)
@@ -12,6 +12,7 @@
 #include "gamma_expansion_effect.h"
 #include "lift_gamma_gain_effect.h"
 #include "colorspace_conversion_effect.h"
+#include "texture_enum.h"
 
 EffectChain::EffectChain(unsigned width, unsigned height)
        : width(width), height(height), finalized(false) {}
@@ -124,18 +125,80 @@ void EffectChain::finalize()
                frag_shader += replace_prefix(effects[i]->output_glsl(), effect_id);
                frag_shader += "#undef PREFIX\n";
                frag_shader += "#undef FUNCNAME\n";
+               frag_shader += "#undef LAST_INPUT\n";
                frag_shader += std::string("#define LAST_INPUT ") + effect_id + "\n";
                frag_shader += "\n";
        }
+       frag_shader.append(read_file("footer.glsl"));
        printf("%s\n", frag_shader.c_str());
        
        glsl_program_num = glCreateProgram();
        GLhandleARB vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER);
        GLhandleARB fs_obj = compile_shader(frag_shader, GL_FRAGMENT_SHADER);
-       glAttachObjectARB(glsl_program_num, vs_obj);
+       glAttachShader(glsl_program_num, vs_obj);
        check_error();
-       glAttachObjectARB(glsl_program_num, fs_obj);
+       glAttachShader(glsl_program_num, fs_obj);
        check_error();
        glLinkProgram(glsl_program_num);
        check_error();
+
+       finalized = true;
+}
+
+void EffectChain::render_to_screen(unsigned char *src)
+{
+       assert(finalized);
+
+       check_error();
+       glUseProgram(glsl_program_num);
+       check_error();
+
+       glActiveTexture(GL_TEXTURE0);
+       glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
+
+       // TODO: use sRGB textures if applicable
+       if (input_format.pixel_format == FORMAT_RGB) {
+               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, src);
+       } else if (input_format.pixel_format == FORMAT_RGBA) {
+               glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, src);
+       } else {
+               assert(false);
+       }
+       check_error();
+       glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);
+
+       //for (unsigned i = 0; i < effects.size(); ++i) {
+       //      effects[i]->set_uniforms();
+       //}
+
+       glDisable(GL_BLEND);
+       check_error();
+       glDisable(GL_DEPTH_TEST);
+       check_error();
+       glDepthMask(GL_FALSE);
+       check_error();
+
+       glMatrixMode(GL_PROJECTION);
+       glLoadIdentity();
+       glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
+
+       glMatrixMode(GL_MODELVIEW);
+       glLoadIdentity();
+
+       glBegin(GL_QUADS);
+
+       glTexCoord2f(0.0f, 1.0f);
+       glVertex2f(0.0f, 0.0f);
+
+       glTexCoord2f(1.0f, 1.0f);
+       glVertex2f(1.0f, 0.0f);
+
+       glTexCoord2f(1.0f, 0.0f);
+       glVertex2f(1.0f, 1.0f);
+
+       glTexCoord2f(0.0f, 0.0f);
+       glVertex2f(0.0f, 1.0f);
+
+       glEnd();
+       check_error();
 }
index 274cb90..68d966b 100644 (file)
@@ -3,7 +3,7 @@ varying vec2 tc;
 
 vec4 read_input(vec2 tc)
 {
-       vec3 x = texture2D(input_tex, tc.st);
+       return texture2D(input_tex, tc.st);
 }
 
 #define LAST_INPUT read_input
index 256b29e..4853179 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -38,71 +38,6 @@ float lift_r = 0.0f, lift_g = 0.0f, lift_b = 0.0f;
 float gamma_r = 1.0f, gamma_g = 1.0f, gamma_b = 1.0f;
 float gain_r = 1.0f, gain_g = 1.0f, gain_b = 1.0f;
 
-void draw_picture_quad(GLint prog, int frame)
-{
-       glUseProgramObjectARB(prog);
-       check_error();
-
-       glActiveTexture(GL_TEXTURE0);
-       glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
-       glUniform1i(glGetUniformLocation(prog, "tex"), 0);
-
-       glActiveTexture(GL_TEXTURE1);
-       glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
-       glUniform1i(glGetUniformLocation(prog, "srgb_tex"), 1);
-
-       glActiveTexture(GL_TEXTURE2);
-       glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
-       glUniform1i(glGetUniformLocation(prog, "srgb_reverse_tex"), 2);
-
-       glUniform3f(glGetUniformLocation(prog, "lift"), lift_r, lift_g, lift_b);
-       //glUniform3f(glGetUniformLocation(prog, "gamma"), gamma_r, gamma_g, gamma_b);
-       glUniform3f(glGetUniformLocation(prog, "inv_gamma_22"),
-                   2.2f / gamma_r,
-                   2.2f / gamma_g,
-                   2.2f / gamma_b);
-       glUniform3f(glGetUniformLocation(prog, "gain_pow_inv_gamma"),
-                   pow(gain_r, 1.0f / gamma_r),
-                   pow(gain_g, 1.0f / gamma_g),
-                   pow(gain_b, 1.0f / gamma_b));
-       glUniform1f(glGetUniformLocation(prog, "saturation"), saturation);
-
-       glDisable(GL_BLEND);
-       check_error();
-       glDisable(GL_DEPTH_TEST);
-       check_error();
-       glDepthMask(GL_FALSE);
-       check_error();
-
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
-
-       glMatrixMode(GL_MODELVIEW);
-       glLoadIdentity();
-
-       glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
-//     glClear(GL_COLOR_BUFFER_BIT);
-       check_error();
-
-       glBegin(GL_QUADS);
-
-       glTexCoord2f(0.0f, 1.0f);
-       glVertex2f(0.0f, 0.0f);
-
-       glTexCoord2f(1.0f, 1.0f);
-       glVertex2f(1.0f, 0.0f);
-
-       glTexCoord2f(1.0f, 0.0f);
-       glVertex2f(1.0f, 1.0f);
-
-       glTexCoord2f(0.0f, 0.0f);
-       glVertex2f(0.0f, 1.0f);
-
-       glEnd();
-       check_error();
-}
-
 void update_hsv()
 {
        hsv2rgb(lift_theta, lift_rad, lift_v, &lift_r, &lift_g, &lift_b);
@@ -238,7 +173,8 @@ int main(int argc, char **argv)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        check_error();
 
-       load_texture("blg_wheels_woman_1.jpg");
+       unsigned img_w, img_h;
+       unsigned char *src_img = load_image("blg_wheels_woman_1.jpg", &img_w, &img_h);
 
        EffectChain chain(WIDTH, HEIGHT);
 
@@ -345,8 +281,7 @@ int main(int argc, char **argv)
 
                ++frame;
 
-               
-               draw_picture_quad(prog, frame);
+               chain.render_to_screen(src_img);
                
                glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
                check_error();