From f420ba91b26aad7701f781bf371f47662a19f452 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 8 Mar 2014 18:33:17 +0100 Subject: [PATCH] Use GLSL 1.30-style attributes for the main shader. This is in preparation for removing fixed-function use altogether, for OpenGL 3.2+ core context support. --- effect_chain.cpp | 55 +++++++++++++++++++++++++++++------------------- vs.vert | 12 +++++++++-- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/effect_chain.cpp b/effect_chain.cpp index 9aa092b..c803c62 100644 --- a/effect_chain.cpp +++ b/effect_chain.cpp @@ -1446,13 +1446,6 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height 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(); - if (phases.size() > 1) { glGenFramebuffers(1, &fbo); check_error(); @@ -1476,7 +1469,8 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height output_textures.insert(make_pair(phases[phase], tex_num)); } - glUseProgram(phases[phase]->glsl_program_num); + const GLuint glsl_program_num = phases[phase]->glsl_program_num; + glUseProgram(glsl_program_num); check_error(); // Set up RTT inputs for this phase. @@ -1503,7 +1497,7 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height check_error(); string texture_name = string("tex_") + phases[phase]->effect_ids[input]; - glUniform1i(glGetUniformLocation(phases[phase]->glsl_program_num, texture_name.c_str()), sampler); + glUniform1i(glGetUniformLocation(glsl_program_num, texture_name.c_str()), sampler); check_error(); } @@ -1536,27 +1530,44 @@ void EffectChain::render_to_fbo(GLuint dest_fbo, unsigned width, unsigned height unsigned sampler_num = phases[phase]->inputs.size(); for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) { Node *node = phases[phase]->effects[i]; - node->effect->set_gl_state(phases[phase]->glsl_program_num, phases[phase]->effect_ids[node], &sampler_num); + node->effect->set_gl_state(glsl_program_num, phases[phase]->effect_ids[node], &sampler_num); check_error(); } // Now draw! - glBegin(GL_QUADS); - - glTexCoord2f(0.0f, 0.0f); - glVertex2f(0.0f, 0.0f); - - glTexCoord2f(1.0f, 0.0f); - glVertex2f(1.0f, 0.0f); + float vertices[] = { + 0.0f, 0.0f, + 1.0f, 0.0f, + 1.0f, 1.0f, + 0.0f, 1.0f + }; + + int position_attrib = glGetAttribLocation(glsl_program_num, "position"); + assert(position_attrib != -1); + glEnableVertexAttribArray(position_attrib); + check_error(); + glVertexAttribPointer(position_attrib, 2, GL_FLOAT, GL_FALSE, 0, vertices); + check_error(); - glTexCoord2f(1.0f, 1.0f); - glVertex2f(1.0f, 1.0f); + int texcoord_attrib = glGetAttribLocation(glsl_program_num, "texcoord"); + if (texcoord_attrib != -1) { + glEnableVertexAttribArray(texcoord_attrib); + check_error(); + glVertexAttribPointer(texcoord_attrib, 2, GL_FLOAT, GL_FALSE, 0, vertices); // Same as texcoords. + check_error(); + } - glTexCoord2f(0.0f, 1.0f); - glVertex2f(0.0f, 1.0f); + glDrawArrays(GL_QUADS, 0, 4); + check_error(); - glEnd(); + glUseProgram(0); check_error(); + glDisableVertexAttribArray(position_attrib); + check_error(); + if (texcoord_attrib != -1) { + glDisableVertexAttribArray(texcoord_attrib); + check_error(); + } for (unsigned i = 0; i < phases[phase]->effects.size(); ++i) { Node *node = phases[phase]->effects[i]; diff --git a/vs.vert b/vs.vert index 1f48069..0ba2fdc 100644 --- a/vs.vert +++ b/vs.vert @@ -1,7 +1,15 @@ +attribute vec2 position; +attribute vec2 texcoord; varying vec2 tc; void main() { - tc = gl_MultiTexCoord0.st; - gl_Position = ftransform(); + // The result of glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0) is: + // + // 2.000 0.000 0.000 -1.000 + // 0.000 2.000 0.000 -1.000 + // 0.000 0.000 -2.000 -1.000 + // 0.000 0.000 0.000 1.000 + gl_Position = vec4(2.0 * position.x - 1.0, 2.0 * position.y - 1.0, -1.0, 1.0); + tc = texcoord; } -- 2.39.2