We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Textures + QuadStrip + Warping
Page Index Toggle Pages: 1
Textures + QuadStrip + Warping (Read 3202 times)
Textures + QuadStrip + Warping
Apr 13th, 2008, 1:10am
 
I am doing something I have done many times before but am now seeing a bug.  I have reverted to using v125, but the problem is there in v135 too.  Actually, not sure if calling it a bug is right because It is probably something stupid I have done wrong.  Check this image:

...

Here you can see a bunch of quad strips that taper at both ends.  Next up, you see me texture the quad strips but remove the taper.

...

But if I put the taper back in, I get odd warps in the textures.

...

I have a sneaky suspicion that hardcorepawn had this problem when texturing the buildings in his zombie game, but I think he was using P3D and not OpenGL.  Anyhoo, I couldn't find any mention of it in the forums.  So, what the hell am I doing wrong?  I have done this process a few times and dont recall doing anything different here.  Ideas?
Re: Textures + QuadStrip + Warping
Reply #1 - Apr 13th, 2008, 7:01pm
 
So i couldn't reproduce this. Are your texture coordinate's tapered like the end of the quads? It sort of just looks like the texture mapping is just a little screwy. Have you tried this out side of processing or after reseting the matrix?

I really can;t imagine what it could be, though I am using java 1.5 because I played around with sunflow. Sometimes I wish we had a little more control over what happens before and after the Draw() function without having to make and support an alternate PGraphics object. I'll dp a little more investigating.
Re: Textures + QuadStrip + Warping
Reply #2 - Apr 13th, 2008, 8:45pm
 
Here is the code in question.  Again, I am thinking I am doing something wrong here. Anything stand out?

BTW, I tried the glHint for fixing texture perspective... no effect.

Code:

pgl.beginGL();
gl.glEnable( GL.GL_TEXTURE_2D );
pgl.bindTexture( images.fish );
gl.glBegin( GL.GL_QUAD_STRIP );

for ( int i=0; i<len; i++ ){
float per = (float)i/(float)( len-1 );
float sinPer = sin( per * PI );

if ( i < len - 1 ){
Vec3D perp0 = loc[i].sub( loc[i+1] );
Vec3D perp1 = perp0.cross( pov.eyeNormal ).normalize();
Vec3D perp2 = perp0.cross( perp1 ).normalize();
perp1 = perp0.cross( perp2 ).normalize();

xOff = radius * perp1.x * sinPer;
yOff = radius * perp1.y * sinPer;
zOff = radius * perp1.z * sinPer;

gl.glColor4f( 1.0, 1.0, 1.0, 1.0 );
gl.glTexCoord2f( 0, per );
gl.glVertex3f( loc[i].x - xOff, loc[i].y - yOff, loc[i].z - zOff );
gl.glTexCoord2f( 1, per );
gl.glVertex3f( loc[i].x + xOff, loc[i].y + yOff, loc[i].z + zOff );
}
}

gl.glEnd();
gl.glDisable(GL.GL_TEXTURE_2D);
pgl.endGL();
Re: Textures + QuadStrip + Warping
Reply #3 - Apr 13th, 2008, 9:06pm
 
i think? it's because in your trapezoids (quads) the diagonal won't split the through the center vertex, so your triangles are entirely different shapes, so will have different uv slopes, so the textures appear misaligned along the shared edge.  (so it looks like one tri is winding one way and following bottom slope, while other winds the other way and follows top slope)  first thing i'd try is splitting your quads into a 4 triangle fan around the midpoint - that should? get neighboring triangles to have same uv slope along shared edge.

[edit] or perhaps just split each quad in half lengthwise so you have two symmetrical pieces (again thinking that might match slopes)
Re: Textures + QuadStrip + Warping
Reply #4 - Apr 27th, 2008, 11:41pm
 
I've run into the same problem as Robert. As a test i made a textured sphere constructed of quads, and used a basic grid texture.  The grid lines are somewhat straight along the equator but as you get close to the poles and the quads start to taper the textures start to get warped. I thought that the culprit might be in the pgl.bindTexture() method but I tried using native opengl commands to bind the textures and got the exact same results, so it's not a processing issue.  As Dave stated you can kind of fix this by subdividing the geometry but this only lessens the texture warping and it also adds a lot of unnecessary geometry. Anybody find a better solution?
Re: Textures + QuadStrip + Warping
Reply #5 - Apr 28th, 2008, 12:04am
 
I'm not sure if it'll make a difference, but you could try:

gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT,GL.GL_NICEST);
Re: Textures + QuadStrip + Warping
Reply #6 - Apr 28th, 2008, 12:32am
 
Nope that didn't seem to do anything, but i found this article that explains the issue and provides a solution, it even has a cool little sample to demonstrate the problem:

http://www.r3.nu/~cass/qcoord/

another way of bypassing this problem would be to converting your quads to triangles.
Re: Textures + QuadStrip + Warping
Reply #7 - Apr 30th, 2008, 1:57pm
 
I thought the glTexCoord4f would solve the problem on my side, but I don't manage to set the values in a way that it works.
I studied the article at http://www.r3.nu/~cass/qcoord/ and also played around with the given example but couldn't figure out how I could apply the solution to any quad.
In the below code I try to apply this texture:
...
with this result:
...
Here's the code in question. The texture can be downloaded at
http://www.christianschneider.ch/uv/checker.jpg
Quote:
import processing.opengl.*;
import javax.media.opengl.*;
import com.sun.opengl.util.texture.*;

Texture tex;  

void setup(){
 size(400,300,OPENGL);
 try{
   tex=TextureIO.newTexture(new File(dataPath("checker.jpg")),true);
 }
 catch(Exception e) {
   println(e);
 }
}


void draw(){

 background(255);

 PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;  
 GL gl = pgl.beginGL();  

 tex.bind();  
 tex.enable();  

 gl.glBegin(GL.GL_QUADS);
 gl.glTexCoord2f(0, 0);
 gl.glVertex2f  (10, 200);

 gl.glTexCoord4f( 1, 0,0,1);
 gl.glVertex2f  ( 300, 200);

 gl.glTexCoord4f(1,  1,0,1);
 gl.glVertex2f  ( 200,  10);

 gl.glTexCoord2f(0, 1);
 gl.glVertex2f  (10,  10);
 gl.glEnd();


 tex.disable();
 pgl.endGL();
}

Re: Textures + QuadStrip + Warping
Reply #8 - Apr 30th, 2008, 11:13pm
 
Quote:
 gl.glBegin(GL.GL_QUADS);
 gl.glTexCoord2f(0, 0);
 gl.glVertex2f  (10, 200);

 gl.glTexCoord2f( 0,1);
 gl.glVertex2f  ( 200, 200);

 gl.glTexCoord2f(1, 1);
 gl.glVertex2f  ( 200,  10);

 gl.glTexCoord2f(1,0);
 gl.glVertex2f  (10, 10);
 gl.glEnd();


Is it what you want to do ?
Re: Textures + QuadStrip + Warping
Reply #9 - May 1st, 2008, 12:20am
 
nope, I want to make the edge dissapear in the texture, so that the quad would display like that:
...
instead of something like that
...
This is what the article at http://www.r3.nu/~cass/qcoord/ explains by using
glTexCoord4f in order to get right slopes. I just have a hard time to make this work with the code I put in my last post. I am sure that by using glTexCoord4f you can solve the problem but I don't find the right values. The texture mapping looks fine as long as the quad is rectangular, but the warping doesn't work well when applied to an irregular quad.
Re: Textures + QuadStrip + Warping
Reply #10 - May 1st, 2008, 1:56am
 
Quote:
import processing.opengl.*;
import javax.media.opengl.*;
import com.sun.opengl.util.texture.*;

Texture tex;  

void setup(){
 size(400,400,OPENGL);
 try{
   tex=TextureIO.newTexture(new File(dataPath("checker.jpg")),true);  
 }
 catch(Exception e) {  
   println(e);  
 }
}

float scale_texcoord = 2.f;
float top = 400.0;
float tx = scale_texcoord * top;

void draw(){

 background(255);

 PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;  
 GL gl = pgl.beginGL();  

 tex.bind();  
 tex.enable();  
 gl.glBegin(GL.GL_QUADS);
 gl.glTexCoord2f(-400, -400);
 gl.glVertex2f  (-400, -400);

 gl.glTexCoord4f( -tx, tx,0,tx);
 gl.glVertex2f  ( -top, 400);

 gl.glTexCoord4f(tx,  tx,0,tx);
 gl.glVertex2f  ( top,  400);  

 gl.glTexCoord2f(400, -400);
 gl.glVertex2f  (400,  -400);  
 gl.glEnd();



 tex.disable();
 pgl.endGL();
}


After tweaking, this is what i've got so far.

...
Re: Textures + QuadStrip + Warping
Reply #11 - May 2nd, 2008, 5:52pm
 
The way this works is by projecting the texture onto the quad using the screen space coordinates (not regular texture coordinates) and using the q variable as a scaling factor.  Here's another article that explains it further:

http://www.cs.unc.edu/~hoff/techrep/projtextures.html

Honestly, this seems like way to much work.  This issue is really only apparent when dealing with low poly counts and larger quads.  The easiest thing to do is to subdivide the Quad into four triangles that meet at a vertex in the middle (basically forming an X in the quad).



Page Index Toggle Pages: 1