2012-02-14 208 views
1

我在使用OpenGL紋理映射時遇到問題。我試圖對場景中的不同對象應用不同的紋理,但我發現將第二個紋理應用於第二個對象正在改變第一個紋理座標(我認爲)。這兩個對象是一個立方體和一個圓柱體。OpenGL紋理座標問題

由於只有啓用一個紋理,場景是這樣的: enter image description here

如果我再啓用紋理1個或多個其他對象,我認爲這(我不是太困擾有關的不良紋理貼圖上在「輪子」現在): enter image description here

可能是什麼造成的?我的多維數據集和汽缸繪圖代碼如下:

立方:

//Alias the values for easier reading 
double X = Position().X; 
double Y = Position().Y; 
double Z = Position().Z; 
double W = _fWidth; 
double H = _fHeight; 
double D = _fDepth; 

//Set textures if necessary 
if(super._blHasTexture) 
{ 
    super._obTexture.bind(gl); 
} 

_obTexture.enable(gl); 
gl.glColor3f(0.5f, 0.5f, 0.7f); 
//Draw 

    gl.glBegin(GL2.GL_QUADS); 
     //Front 
    gl.glNormal3f(0f, 0f, 1f); 
    gl.glVertex3d(X, Y, Z);  gl.glTexCoord2f(0f, 0f); 
    gl.glVertex3d(X+W, Y, Z); gl.glTexCoord2f(1f, 0f); 
    gl.glVertex3d(X+W, Y-H, Z); gl.glTexCoord2f(1f, 1f); 
    gl.glVertex3d(X, Y-H, Z); gl.glTexCoord2f(0f, 1f); 

    //Back 
    gl.glNormal3f(0f, 0f, -1f); 
    gl.glVertex3d(X, Y, Z-D); gl.glTexCoord2f(0f, 0f); 
    gl.glVertex3d(X+W, Y, Z-D); gl.glTexCoord2f(1f, 0f); 
    gl.glVertex3d(X+W, Y-H, Z-D); gl.glTexCoord2f(1f, 1f); 
    gl.glVertex3d(X, Y-H, Z-D); gl.glTexCoord2f(0f, 1f); 

    //Left 
    gl.glNormal3f(-1f, 0f, 0f); 
    gl.glVertex3d(X, Y, Z);  gl.glTexCoord2f(0f, 0f); 
    gl.glVertex3d(X, Y, Z-D); gl.glTexCoord2f(1f, 0f); 
    gl.glVertex3d(X, Y-H, Z-D); gl.glTexCoord2f(1f, 1f); 
    gl.glVertex3d(X, Y-H, Z); gl.glTexCoord2f(0f, 1f);    

    //Right 
    gl.glNormal3f(1f, 0f, 0f); 
    gl.glVertex3d(X+W, Y, Z); gl.glTexCoord2f(0f, 0f); 
    gl.glVertex3d(X+W, Y, Z-D); gl.glTexCoord2f(1f, 0f); 
    gl.glVertex3d(X+W, Y-H, Z-D); gl.glTexCoord2f(1f, 1f); 
    gl.glVertex3d(X+W, Y-H, Z); gl.glTexCoord2f(0f, 1f); 

    //Top 
    gl.glNormal3f(0f, 1f, 0f); 
    gl.glVertex3d(X, Y, Z);  gl.glTexCoord2f(0f, 0f); 
    gl.glVertex3d(X+W, Y, Z); gl.glTexCoord2f(1f, 0f); 
    gl.glVertex3d(X+W, Y, Z-D); gl.glTexCoord2f(1f, 1f); 
    gl.glVertex3d(X, Y, Z-D); gl.glTexCoord2f(0f, 1f); 

    //Bottom 
    gl.glNormal3f(0f, -1f, 0f); 
    gl.glVertex3d(X, Y-H, Z); gl.glTexCoord2f(0f, 0f); 
    gl.glVertex3d(X+W, Y-H, Z); gl.glTexCoord2f(1f, 0f); 
    gl.glVertex3d(X+W, Y-H, Z-D); gl.glTexCoord2f(1f, 1f); 
    gl.glVertex3d(X, Y-H, Z-D); gl.glTexCoord2f(0f, 1f); 
gl.glEnd(); 

_obTexture.disable(gl); 

氣缸:

GLUquadric quad = glu.gluNewQuadric(); 
glu.gluQuadricTexture(quad, true); 

_obTexture.bind(gl); 
_obTexture.enable(gl); 

    //Draw the caps 
gl.glBegin(GL2.GL_TRIANGLE_FAN); 
    gl.glVertex3f(0f, 0f, 0.01f); 
    gl.glTexCoord2f(0f, 0f); 
    for(int t = 0; t < 20; t++) 
    { 
     float X = _fRadius * (float)Math.cos(t); 
     float Y = _fRadius * (float)Math.sin(t); 
     gl.glVertex3f(X, Y, 0.01f); 
    } 
    gl.glTexCoord2f(1f, 1f); 
gl.glEnd(); 

gl.glBegin(GL2.GL_TRIANGLE_FAN); 
    gl.glVertex3f(0f, 0f, -0.01f + _fHeight); 
    gl.glTexCoord2f(0f, 0f); 
    for(int t = 0; t < 20; t++) 
    { 
     float X = _fRadius * (float)Math.cos(t); 
     float Y = _fRadius * (float)Math.sin(t); 
     gl.glVertex3f(X, Y, -0.01f + _fHeight); 
    } 
     gl.glTexCoord2f(1f, 1f); 
gl.glEnd();   

glu.gluCylinder(quad, _fRadius, _fRadius, _fHeight, 20, 1); 
_obTexture.disable(gl); 

回答

4

將您的紋理座標每次調用glVertex之前,而不是之後 - 之後調用申請到以下的glVertex調用。

你正在做它的方式,最後的座標組正在下一渲染週期應用到第一頂點(即你的立方體的一個角落) - 因此,當你渲染其他對象的差異。

+0

太棒了!這一點馬上解決了,非常感謝。 – AndyBursh 2012-02-14 20:51:31