PDA

View Full Version : glBegin/glEnd with custom shader effect



alvisedt
04-08-2015, 09:40 AM
Hello everyone, my problem is as follow: I want to render a textured quad on screen and apply an effect to its texture.
I dont want to fill my code with shader uniform and matrix stuff so I rather keep on the glBegin/glEnd method.

Currently I render my geomtry like this:



glBegin(GL_QUADS);

glTexCoord2f(0.0f, 0.0f);
glVertex3i(5, h - 5, 0); // The bottom left corner

glTexCoord2f(0.0f, 1.0f);
glVertex3i(5, 5, 0); // The top left corner

glTexCoord2f(1.0f, 1.0f);
glVertex3i(w - 5, 5, 0); // The top right corner

glTexCoord2f(1.0f, 0.0f);
glVertex3i(w - 5, h - 5, 0); // The bottom right corner

glEnd();


and I have set up a very easy shader like so:

VERTEX SHADER:


#version 150 core

uniform mat4 pvm;

layout(location = 0) in vec3 in_Position;
layout(location = 1) in vec2 texCoord;

varying vec2 texCoordV;

void main(void)
{
texCoordV = texCoord;
gl_Position = ftransform();
}


FRAGMENT SHADER:


#version 150 core

uniform sampler2D text;

varying vec2 texCoordV;

out vec4 out_Color;

void main(void)
{
out_Color = vec4(texCoordV, 1.0, 1.0);
}


I can see now a perfectly blue quare as the texCoordV are all 0s. I cant find on the internet any way to "bond" also to the


glTexCoord2f(0.0f, 0.0f);

and thus keeping my shader and my code easy.

Any guru has the answer?

Thanks!

Alfonse Reinheart
04-08-2015, 09:56 AM
#version 150 core

... You are using compatibility stuff. So you shouldn't be compiling your shaders as though you were using core OpenGL. Also, ftransform doesn't exist in core OpenGL, so the compiler should have complained about that.

In any case, you cannot alias generic attribute indices with non-generic attributes (ie: values provided by glVertex, glTexCoord, etc). If you want to use the non-generic attributes in your program, you need to use the old, built-in per-vertex inputs. Variables like gl_Vertex, gl_TexCoord (an array), etc.

And you also should be compiling your shaders as compatibility, not core.

Alternatively, you could simply change your code to use the generic vertex attribute functions. Namely, glVertexAttrib. It works exactly like glVertex/glTexCoord/etc, except that it feeds specific attribute indices. The glVertex behavior of issuing the current vertex is provided by glVertexAttrib when sending attribute index 0. So you would change your glTexCoord call to glVertexAttrib*(1), and your glVertex call to glVertexAttrib*(0) (which issues the vertex).

alvisedt
04-08-2015, 01:57 PM
... You are using compatibility stuff. So you shouldn't be compiling your shaders as though you were using core OpenGL. Also, ftransform doesn't exist in core OpenGL, so the compiler should have complained about that.

In any case, you cannot alias generic attribute indices with non-generic attributes (ie: values provided by glVertex, glTexCoord, etc). If you want to use the non-generic attributes in your program, you need to use the old, built-in per-vertex inputs. Variables like gl_Vertex, gl_TexCoord (an array), etc.

And you also should be compiling your shaders as compatibility, not core.

Alternatively, you could simply change your code to use the generic vertex attribute functions. Namely, glVertexAttrib. It works exactly like glVertex/glTexCoord/etc, except that it feeds specific attribute indices. The glVertex behavior of issuing the current vertex is provided by glVertexAttrib when sending attribute index 0. So you would change your glTexCoord call to glVertexAttrib*(1), and your glVertex call to glVertexAttrib*(0) (which issues the vertex).

Thanks a lot, I tought about something like that but I had no idea. Anyway, you answered correctly to my question, and for anyone who is intrested here are my vertex and fragment shader:

VERTEX:


void main(void)
{
gl_Position = ftransform();
gl_TexCoord [0] = gl_MultiTexCoord0;
}


FRAGMENT


uniform sampler2D text_in;

void main(void)
{
gl_FragColor = texture2D(text_in, gl_TexCoord[0].st);
}


That's it, nothing to bind (only the uniform for the texture) and nothing more to do. Easy. Thanks!