Sign in to follow this  

glsl texture-uv set with GL_TEXTURE_COORD_ARRAY wrong in shader

This topic is 2541 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I'm trying to render a full-screen textured quad to my window with the following code.

In the shader, if I use the "TextureCoordIn" it appears that I'm only sampling 1 texel (ie my screen is only 1 color).
However, if I calculate the texture coordinate (code commented out), then everything works perfectly...

So apparently the input-texture-coordinate is "not reaching" the shader (or maybe is always set to 0.0, 0.0) - but I can't figure out why...

Any ideas anyone?


const vec2 madd=vec2(0.5,0.5);

attribute vec2 Position;
attribute vec2 TextureCoordIn;

varying vec2 TextureCoord;

void main(void)
{
gl_Position = vec4(Position.xy, 0.0, 1.0);
TextureCoord = TextureCoordIn; <= screen in 1 color, wrong
//TextureCoord = Position.xy*madd+madd; <= this works
}





void render_fullscreen_rectangle(RenderDevice* _context, RenderTarget* _rt)
{
typedef struct _Vertex_V2fT2f
{
float x,y;
float u,v;
} Vertex_V2fT2f;


Vertex_V2fT2f flat[4] =
{
{ -1, -1, 0.0f, 0.0f },
{ 1, -1, 1.0f, 0.0f },
{ -1, 1, 0.0f, 1.0f },
{ 1, 1, 1.0f, 1.0f },
};
glDisable (GL_COLOR_MATERIAL);

// render fullscreen textured quad
glVertexPointer (2,GL_FLOAT,(GLsizei)sizeof(Vertex_V2fT2f),&(flat[0].x));
glTexCoordPointer(2,GL_FLOAT,(GLsizei)sizeof(Vertex_V2fT2f),&(flat[0].u));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

void render (void)
{
// render scene to texture/fbo

// render texture to screen
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _context->rt->texture);
glActiveTexture(GL_TEXTURE0);

glUseProgram (_context->m_blitting.program);
glDisable(GL_DEPTH_TEST);
render_fullscreen_rectangle(_context, _context->rt);
glUseProgram (0);
}




Share this post


Link to post
Share on other sites
If you have arbitrary input attributes (e.g. not the opengl builtin variables), then you shouldn't be using any of these functions:

glVertexPointer
glTexCoordPointer
glEnableClientArray
glDisableClientArray

You need to query your shader for the attribute locations of "Position" and "TextureCoordIn", and then use these locations as inputs to these functions:

glVertexAttribPointer
glEnableVertexAttribArray


The fact that you're getting anything to render at all is pretty much luck I think, as you're using undefined behavior. Otherwise how is your program supposed to know that you want "TextureCoordIn" to be the texcoord pointer, and "Position" to be the vertex pointer?

Share this post


Link to post
Share on other sites
I was staring at that code for 2 hours, wondering why it wasn't working -
your tip took only 15 minutes of implementing and getting it working ^^

thanks!

Share this post


Link to post
Share on other sites

This topic is 2541 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this