PDA

View Full Version : texturematrix gluPerspective shader



Quark
12-04-2010, 03:46 AM
In my project I'm using the texture-matrix like this:

glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glScalef(...);
glTranslatef(...);
gluPerspective(...);
glTranslatef(...);
gluLookAt(...);

The strange thing is, that with this code everything worked perfectly as long as I didn't use a shaderprogram:

VS:
void main(void)
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
}

FS:
uniform sampler2D TextureSampler0;
void main(void)
{
gl_FragColor = texture2D (TextureSampler0,
vec2(gl_TexCoord[0]));
}

The shaderprogram is quite simple and I think i handled the texturematrix correctly...
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

after some try i found out that as long as I don't use gluPerspective(...), I get the same result using the shaderprogram as without the shaderprogram:

glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glScalef(...);
glTranslatef(...);
//gluPerspective(...); <- here is the problem .
glTranslatef(...);
gluLookAt(...);

I've no idea why my code doesn't work. I also tryed it on another computer...

I hope you can find the mistake I made...

ravage
12-04-2010, 06:03 PM
Are you sure that the matrix you are setting is in gl_TextureMatrix[0] and not some other?

meaning do you call glActiveTexture anywhere that may have changed it from GL_TEXTURE0?

Quark
12-05-2010, 02:26 AM
Yes I'm sure this isn't the problem. I'm using glActiveTexture(GL_TEXTURE0) right before glMatrixMode(GL_TEXTURE).

Besides every Matrix manipulation but gluPerspective(...) works with my shader.

gluPerspective(...) does change the result when running the aplication with the shaderprogram, but it doesn't change it the same way as without a shader. So I think that the multiplication of the texturecoordinates with the texturemarix must be wrong..

Iulian B
12-05-2010, 04:05 AM
Let's get back to basics (the basics of debugging, that is).


Try using


float matrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, &amp;matrix);


before and after the gluPerspective call, both with and without shaders, see if you get the same values. if you don't, it's an glu/shaders interaction problem, if you do, there's something wrong in your shaders... try, try, try

Quark
12-05-2010, 10:02 AM
Thanks for your idea...

I tryed it and I got the same results with and without shader..
So somethig is wrong in my shader? But my Shaderpair is so simple.. Do I have to do something else with the texturecoordinates than
"gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;"?

Quark
12-09-2010, 12:58 PM
I just made another test:

glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glScalef(...);
glTranslatef(...);
gluPerspective(...);
glTranslatef(...);
gluLookAt(...);
glGetFloatv(GL_TEXTURE_MATRIX, &amp;matrix);

I compared the resulds in "matrix" whith those in the shader and they were identical... So that means that
"gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
is wrong or the standard shaderprogram does something else with the Texturecoordinates...

Anyone an idea?

Iulian B
12-30-2010, 06:28 PM
Yes I'm sure this isn't the problem. I'm using glActiveTexture(GL_TEXTURE0) right before glMatrixMode(GL_TEXTURE).


Try also calling glClientActiveTexture, just for fun... or, if you go for the shaders, just let go of the old way and (calculate and) upload the matrices to your shaders by hand in custom variables just to be sure...

mfort
01-01-2011, 05:18 AM
The bug is your fragment shader. If your texture matrix is not trivial then you end up with homogenous texture coordinates with w != 1. So your fragment shader must use textureProj() with full vec4 coordinate.

It will look like this:


gl_FragColor = textureProj(TextureSampler0, gl_TexCoord[0]);

Quark
01-09-2011, 04:04 PM
Wowww, mfort, I just tried what you wrote.. and it immediately worked as I wanted it.. Thank you very much. I almost gave up on my project, because it didn't work.

I do not realy know the difference between textureProj() and texture2D() yet, but I'll read about it soon...

I thank everyone who took some time thinking about my problem..