PDA

View Full Version : Textures stretching



CPPMaster
05-05-2004, 02:41 AM
My textures begin to stretch whenever the polygon takes about 1/2 of screen space. The texcoords are correct. Screenshots, using a quad (tri strip) with a perlin noise texture:
Everything is ok (http://img31.photobucket.com/albums/v92/CPPMaster/Screenshot0001.jpg) .
Moving closer (http://img31.photobucket.com/albums/v92/CPPMaster/Screenshot0002.jpg) . And closer (http://img31.photobucket.com/albums/v92/CPPMaster/Screenshot0003.jpg) .

Any help will be greatly appreciated.

-NiCo-
05-05-2004, 02:56 AM
Apparently there's still something wrong with the way you generate the texture coordinates. If you look closely you can see that the quad is divided into two triangles. The effect you see is a typical problem that results from using perspective correct texcoord interpolation instead of projective correct interpolation.

The figures at page 2 of the document at
http://developer.nvidia.com/object/Projective_Texture_Mapping.html
illustrates this problem.

Nico

CPPMaster
05-05-2004, 03:02 AM
As I said the quad is made using triangle strips, but I'm 100% sure that the texcoords are correct (well, every thing is correct as long as the "camera" is not getting to close).

-NiCo-
05-05-2004, 03:09 AM
In theory, it will always look ok if your quad is almost parrallel to your viewing plane, because in that case the projective interpolation will be a perspective interpolation due to the constant q coordinate.

Exactly how do you generate the texture coordinates,?
Do you specify them explicitely by using texcoord2f or implicitly by using texgen?
An are you using fragment shaders? If so, do you perform texture lookups with tex or txp?

N.

CPPMaster
05-05-2004, 03:09 AM
I'm not using projected textures, just 2D textures.

-NiCo-
05-05-2004, 03:16 AM
Can you post the code where you draw the quad and specify the texcoords? Would make things a bit easier...

N.

CPPMaster
05-05-2004, 03:16 AM
Originally posted by -NiCo-:
Exactly how do you generate the texture coordinates,?
Do you specify them explicitely by using texcoord2f or implicitly by using texgen?
An are you using fragment shaders? If so, do you perform texture lookups with tex or txp?

N.I'm using fragement programs, but my problem effects all shaded and unshaded polygons. I'm passing texcoords using VBO (glTexCoordPointer(2, GL_FLOAT, 0, SomeBufferOffset) , and they are correct. My terrain is using a fragement program and the textures are applied in the fragement program. The texture coordinates, however, are passed in an array and are interpolated wrongly, like on every other poly. :(

CPPMaster
05-05-2004, 03:20 AM
Originally posted by -NiCo-:
Can you post the code where you draw the quad and specify the texcoords? Would make things a bit easier...

N.Here:



glBindTexture(GL_TEXTURE_2D,texture);
GLEnable(GL_TEXTURE_2D);

glBindBufferARB(GL_ARRAY_BUFFER_ARB,VBOBuffer);

glTexCoordPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(0));

glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(8*sizeof(float)));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);


glBindBufferARB(GL_ARRAY_BUFFER_ARB,0);

-NiCo-
05-05-2004, 03:31 AM
Have you tested it without using VBO's, just using

glBegin(GL_TRIANGLE_STRIP);
glTexcoord2f(.,.);
glVertex3f(.,.,.);
...
glEnd();

Does that make a difference? I'm not that experienced in using VBOs other than plain evrtex data.

N.

CPPMaster
05-05-2004, 03:36 AM
Originally posted by -NiCo-:
Have you tested it without using VBO's, just using

glBegin(GL_TRIANGLE_STRIP);
glTexcoord2f(.,.);
glVertex3f(.,.,.);
...
glEnd();

Does that make a difference? I'm not that experienced in using VBOs other than plain evrtex data.

N.Tested it just now. No difference. :confused:

-NiCo-
05-05-2004, 03:51 AM
Does it make a difference if you use GL_QUAD instead of GL_TRIANGLE STRIP?

This is a long shot, but did you change the TEXTURE_MATRIX somewhere?

If you're using texcoord2f, using tex or txp shouldn't make a difference in your fragment program, so that can't be the problem

N.

yooyo
05-05-2004, 04:03 AM
Looks like your texcoords are screw-up. If you want plain texture mapping set only S and T coords. Leave R and Q to 0.0.

Take a look in your fragment shader. Maybe you are change R and Q while doing some calculations, or because driver bug maybe fragment shader have some trash in R and Q.

yooyo

-NiCo-
05-05-2004, 04:32 AM
As you can see from the figure at page 2 of http://developer.nvidia.com/object/Projective_Texture_Mapping.html
it's possible that your rectangle isn't a pure rectangle but a trapezoid.
This figure also does not make use of texture coordinate generation, but explicit coordinate definition. A small difference will result in a greater distortion at closer range and oblique angles

N.

CPPMaster
05-05-2004, 04:42 AM
Originally posted by -NiCo-:
Does it make a difference if you use GL_QUAD instead of GL_TRIANGLE STRIP?

This is a long shot, but did you change the TEXTURE_MATRIX somewhere?

If you're using texcoord2f, using tex or txp shouldn't make a difference in your fragment program, so that can't be the problem

N.GL_QUAD doesn't makes a difference. I didn't change the texture matrix. And as I said before my problem effects unshaded polygons as well.

CPPMaster
05-05-2004, 04:44 AM
Originally posted by yooyo:
Looks like your texcoords are screw-up. If you want plain texture mapping set only S and T coords. Leave R and Q to 0.0.

Take a look in your fragment shader. Maybe you are change R and Q while doing some calculations, or because driver bug maybe fragment shader have some trash in R and Q.

yooyoR and Q are 0. I'm using 2d texture coordinates.

CPPMaster
05-05-2004, 04:45 AM
Originally posted by -NiCo-:
As you can see from the figure at page 2 of http://developer.nvidia.com/object/Projective_Texture_Mapping.html
it's possible that your rectangle isn't a pure rectangle but a trapezoid.
This figure also does not make use of texture coordinate generation, but explicit coordinate definition. A small difference will result in a greater distortion at closer range and oblique angles

N.I'm using 2 triangles so it shouldn't make a difference.

plasmonster
05-05-2004, 04:52 AM
Just for kicks, add this line somewhere:

glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );

Those pics look like my first attempts at software texture mapping long ago. :)

CPPMaster
05-05-2004, 05:37 AM
Originally posted by Portal:
Just for kicks, add this line somewhere:

glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );

Those pics look like my first attempts at software texture mapping long ago. :) I'm already calling this. :(

-NiCo-
05-05-2004, 05:48 AM
I'm using 2 triangles so it shouldn't make a difference. This has got nothing to do with using triangles.
Take a look at http://www.esat.kuleuven.ac.be/~ncorneli/

In undistorted.bmp I drew a simple perfect rectangle using standard texture coordinates. The red color represents the s coordinate for texture lookup and green the t coordinate.

In undistorted.bmp I left the texture coordinates unchanged, but put one vertex at a different position such that it isn't a rectangle anymore. Notice the diagonal line that appears.

It's hard to find out the problem without having seen the full code.

N.

CPPMaster
05-05-2004, 06:01 AM
[quote]Originally posted by -NiCo-:
[B]

glGenTextures(1, &(texture));
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_LINEAR);

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8, width, height, GL_RED, GL_UNSIGNED_BYTE, buffer); VBO init code:


float *Data=new float[20];

Data[8]=-50;
Data[9]=50;
Data[10]=-50;
Data[11]=50;
Data[12]=50;
Data[13]=-50;
Data[14]=50;
Data[15]=50;
Data[16]=-50;
Data[17]=-50;
Data[18]=50;
Data[19]=-50;

Data[0]=0.0f;
Data[1]=0.0f;
Data[2]=1.0f;
Data[3]=0.0f;
Data[4]=0.0f;
Data[5]=1.0f;
Data[6]=1.0f;
Data[7]=1.0f;

glGenBuffersARB(1,&VBOBuffer);
glBindBufferARB(GL_ARRAY_BUFFER_ARB,VBOBuffer);
glBufferDataARB(GL_ARRAY_BUFFER_ARB,20*sizeof(floa t),Data,GL_STATIC_DRAW_ARB);
glBindBufferARB(GL_ARRAY_BUFFER_ARB,0);

delete [] Data;
I've already posted the drawing code.

-NiCo-
05-05-2004, 06:15 AM
In your code, aren't

Data[8]=-50;
Data[9]=50;
Data[10]=-50;

and

Data[17]=-50;
Data[18]=50;
Data[19]=-50;

the same vertices?

The same goes for

Data[14]=50;
Data[15]=50;
Data[16]=-50;

and

Data[11]=50;
Data[12]=50;
Data[13]=-50;

?

-NiCo-
05-05-2004, 06:27 AM
Another remark


glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR_MIPMAP_LINEAR); You can not specify a mipmap filtering mode for magnification filters, only for minification filtering.

N.

Overmind
05-05-2004, 06:28 AM
Just an idea, I didn't actually test this:
Do you do your camera transformation on the projection matrix instead of the modelview matrix? If yes, this could cause a problem with perspective correct interpolation...

CPPMaster
05-05-2004, 07:09 AM
Originally posted by -NiCo-:
In your code, aren't

Data[8]=-50;
Data[9]=50;
Data[10]=-50;

and

Data[17]=-50;
Data[18]=50;
Data[19]=-50;

the same vertices?

The same goes for

Data[14]=50;
Data[15]=50;
Data[16]=-50;

and

Data[11]=50;
Data[12]=50;
Data[13]=-50;

?Oh, I copied the - wrongly.

CPPMaster
05-05-2004, 07:13 AM
Originally posted by -NiCo-:
Another remark


glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR_MIPMAP_LINEAR); You can not specify a mipmap filtering mode for magnification filters, only for minification filtering.

N.Hey I didn't know that. Thanks.

CPPMaster
05-05-2004, 07:16 AM
Originally posted by Overmind:
Just an idea, I didn't actually test this:
Do you do your camera transformation on the projection matrix instead of the modelview matrix? If yes, this could cause a problem with perspective correct interpolation...No, everything (except the perspective calculation and calling glfrustom) is done in the ModelView matrix.
I just don't get it.. :confused: :confused:

gdewan
05-05-2004, 07:39 AM
Are you seeing this with other programs? I am not familier with the 9600 driver settings you can mess with, but could there be something there forcing linear (instead of perstective correct) interpolation on? Have you tried the executable on a non-radeon card?

CPPMaster
05-05-2004, 07:52 AM
Originally posted by gdewan:
Are you seeing this with other programs? I am not familier with the 9600 driver settings you can mess with, but could there be something there forcing linear (instead of perstective correct) interpolation on? Have you tried the executable on a non-radeon card?Games work fine. I didn't try on a non-Radeon, mainly because my other GF4Ti doesn't support PS2.

gdewan
05-05-2004, 07:57 AM
What are you doing in the shader then? Sounds like the problem might be there

CPPMaster
05-05-2004, 08:03 AM
Originally posted by gdewan:
What are you doing in the shader then? Sounds like the problem might be thereI told you. The problem effects non-shaded polygons as well.

CPPMaster
05-05-2004, 08:12 AM
I've just tested it with the same quad, but I've applied a shader to it. The fragement program outputs the texcoords as color, and as expected when I move the mouse all the color on the quad slides randomaly. :mad:

-NiCo-
05-05-2004, 08:17 AM
Maybe it's best to write a little test code that simply draws a textured quad without anything else. Just to check if it's a driver bug or not.

N.

CPPMaster
05-05-2004, 08:20 AM
Originally posted by -NiCo-:
Maybe it's best to write a little test code that simply draws a textured quad without anything else. Just to check if it's a driver bug or not.

N.New program? Hang on, I will just test my app on my second computer with the no fragememnt programs codepath.

-NiCo-
05-05-2004, 08:27 AM
If it's a bug, it's possible that games still run fine as most of them use directx.
Just try compiling some code of http://nehe.gamedev.net or some other site to see if the problem still persists.

N.

CPPMaster
05-05-2004, 08:39 AM
I was talking about OpenGL games of course (Counter Strike using the OpenGL renderer). Nut even Nehe's examples work correctly. :(

knackered
05-05-2004, 08:50 AM
Try not using VOB....it's almost like you've done no debugging yourself before...is this the first bug you've ever encountered or something? The people here are just suggesting the obvious, eliminate factors until you find your bug.

CPPMaster
05-05-2004, 08:56 AM
Originally posted by knackered:
Try not using VOB....it's almost like you've done no debugging yourself before...is this the first bug you've ever encountered or something? The people here are just suggesting the obvious, eliminate factors until you find your bug.Do you think I have wrote more than 35000 lines and never encountered a bug? I have already tried without VBOs, I have tried different texture loading ways, but using the fragement program I see that the problem are the tex coords being changed.

-NiCo-
05-05-2004, 08:59 AM
If Nehe's tutorials don't work, the problem is definetly specific to your workenvironment.
Try reinstalling the graphics card drivers and make sure the libs you're linking to are not corrupted. Also try disabling al environment settings like Ati's TruForm, antialiasing, anisotropic filtering, etc.

N.

CPPMaster
05-05-2004, 09:01 AM
Originally posted by -NiCo-:
If Nehe's tutorials don't work, the problem is definetly specific to your workenvironment.
Try reinstalling the graphics card drivers and make sure the libs you're linking to are not corrupted. Also try disabling al environment settings like Ati's TruForm, antialiasing, anisotropic filtering, etc.

N.No Nehe's examples do work.

-NiCo-
05-05-2004, 09:06 AM
Nut even Nehe's examples work correctly. So 'Nut' means 'yes'? :)

Anyway, it's indeed time to do some debugging at every step and see if the results are what you expect.

Good luck!

N.

CPPMaster
05-05-2004, 09:13 AM
Originally posted by -NiCo-:

Nut even Nehe's examples work correctly. So 'Nut' means 'yes'? :)

Anyway, it's indeed time to do some debugging at every step and see if the results are what you expect.

Good luck!

N.'Nut' means 'But'. :)
I'll gladly debug each step, but WHAT STEPS SHOULD I DEBUG?! The texture coordinates are correct, but somewhere in the pipeline before the fragement proccessor something goes wrong.. :confused:

-NiCo-
05-05-2004, 09:17 AM
Start with the NeHe tutorial for a textured quad, then gradually ad parts of your code to that tutorial that would effect the execution and verify at each point that the results are still correct.

N.

CPPMaster
05-05-2004, 09:17 AM
Heck, the texture coordinates get changed even beore the rasterizer. What except the texture matrix can be causing that?!

CPPMaster
05-05-2004, 09:19 AM
Originally posted by -NiCo-:
Start with the NeHe tutorial for a textured quad, then gradually ad parts of your code to that tutorial that would effect the execution and verify at each point that the results are still correct.

N.Yeah, I think I'll do that. Thanks for your help and time.

yooyo
05-05-2004, 09:53 AM
On your screenshots I can see bad texture mapping only when hw do clipping. Check your vertex shader. Maybe your transform code doesn't work as well.

yooyo

CPPMaster
05-05-2004, 10:22 AM
Originally posted by yooyo:
On your screenshots I can see bad texture mapping only when hw do clipping. Check your vertex shader. Maybe your transform code doesn't work as well.

yooyoI use no shaders for that quad.

harsman
05-05-2004, 11:50 PM
This might help: http://www.r3.nu/~cass/qcoord/

yooyo
05-06-2004, 12:30 AM
Check for texcoord generation. Maybe you forget to call glDisable(GL_TEXTURE_GEN_R) and glDisable(GL_TEXTURE_GEN_Q).

Or.. try change driver...

yooyo

Christian Schüler
05-06-2004, 05:46 AM
Make sure that the texture coordinates of all 4 vertices of the quad lie on a plane.

...?

That is that if you view your texcoords as positions, they must form a parallelogram.

CPPMaster
05-06-2004, 07:30 AM
Strange. It does work correctly on the GF4Ti using my ARB code path, however forcing exactly the same code path on my Radeon doesn't solve the problem. :mad:

ox1d_47
05-06-2004, 09:24 AM
I am also having trouble with my radeon. I have a problem with texture-coords "damaged", too. But my problem appeares when i use point-sprites. My code worked on a geforce-3.
Strange, strange. Someone should report all these bugs to ATI. Hopefully the will release a new driver. I am getting crazy. :eek:

CPPMaster
05-06-2004, 12:24 PM
I doubt it is a driver bug, because other OpenGL games (CS) work correctly.

zeckensack
05-07-2004, 01:03 AM
Originally posted by CPPMaster:

Originally posted by Overmind:
Just an idea, I didn't actually test this:
Do you do your camera transformation on the projection matrix instead of the modelview matrix? If yes, this could cause a problem with perspective correct interpolation...No, everything (except the perspective calculation and calling glfrustom) is done in the ModelView matrix.
I just don't get it.. :confused: :confused: I second the idea that the most likely cause of these issues is that your matrix stacks are botched. You might produce constant w or something like that.

I take it that you construct some (all?) matrices yourself? (because you said "perspective calculation and calling glFrustum")

Can you insert code that gets both the modelview and projection matrices during a "broken" frame and just post the results?

CPPMaster
05-07-2004, 07:40 AM
Fixed it. It does looks like a driver bug. I've commented out glPushMatrix() and glPopMatrix() in the particle rendering code, and it looks like calling glTransform a few thousends of times (probably out of precision of float) causes some matrix bugs or something. Anyway adding the missing push and pop matrix calls fixes it, thanks everybody. :cool: