2011-02-13 67 views
3

我有一个立方体定义为:什么是OpenGL中立方体的纹理坐标?

float vertices[] = { -width, -height, -depth, // 0 
           width, -height, -depth, // 1 
           width, height, -depth, // 2 
          -width, height, -depth, // 3 
          -width, -height, depth, // 4 
           width, -height, depth, // 5 
           width, height, depth, // 6 
          -width, height, depth // 7 
     }; 

,我有像128×128,我只是想在每一个立方体,别无其他的6个面的涂漆。那么什么是纹理cooridinates?我需要实际的价值。

这是绘图代码:

// Counter-clockwise winding. 
     gl.glFrontFace(GL10.GL_CCW); 
     // Enable face culling. 
     gl.glEnable(GL10.GL_CULL_FACE); 
     // What faces to remove with the face culling. 
     gl.glCullFace(GL10.GL_BACK); 
     // Enabled the vertices buffer for writing and to be used during 
     // rendering. 
     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 

     // Specifies the location and data format of an array of vertex 
     // coordinates to use when rendering. 
     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVerticesBuffer); 


      // Bind the texture according to the set texture filter 
      gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[filter]); 
      gl.glEnable(GL10.GL_TEXTURE_2D); 



      // Enable the texture state 
      gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 

      // Point to our buffers 
      gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer); 



     // Set flat color 
     gl.glColor4f(red, green, blue, alpha); 


     gl.glDrawElements(GL10.GL_TRIANGLES, mNumOfIndices, 
       GL10.GL_UNSIGNED_SHORT, mIndicesBuffer); 

     // ALL the DRAWING IS DONE NOW 

     // Disable the vertices buffer. 
     gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 


      gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); 


     // Disable face culling. 
     gl.glDisable(GL10.GL_CULL_FACE); 

这是该指数数组:

short indices[] = { 0, 2, 1, 
       0, 3, 2, 

       1,2,6, 
       6,5,1, 

       4,5,6, 
       6,7,4, 

       2,3,6, 
       6,3,7, 

       0,7,3, 
       0,4,7, 

       0,1,5, 
       0,5,4 


       }; 

我不知道是否需要索引阵列发现TEX坐标。请注意,我给出的立方体顶点数组是使用索引数组的最有效的立方体表示。立方体绘制完美,但不是纹理。只有一面显示正确的图像,但其他面都搞乱了。我使用了各种纹理在线教程中描述的方法,但它不起作用。

+1

你或许应该发表您的绘制代码太... – ltjax 2011-02-13 10:30:41

+0

根据以上信息,我们需要多少个tex坐标,技术上是正确的?用tex坐标表示tex坐标数组中的2d点像1,0。 – ace 2011-02-13 11:07:45

回答

3
  1. 您需要定义每个面你想要的方向(这将改变其纹理坐标放在每个顶点)
  2. 你需要复制的顶点位置为同一立体角都会有不同的纹理坐标取决于它是哪个面的一部分
  3. 如果您想要每个面上的完整纹理,那么纹理坐标为(0,0)(0,1)(1,1)(1,0)。你如何将它们映射到特定的顶点(其中24个,每个面4个)取决于你想要的方向。
2

对我来说,更容易考虑你的verticies为width = x,height = y和depth = z。 然后这是一个简单的事情,得到6面。

float vertices[] = { -x, -y, -z, // 0 
           x, -y, -z, // 1 
           x, y, -z, // 2 
          -x, y, -z, // 3 
          -x, -y, z, // 4 
           x, -y, z, // 5 
           x, y, z, // 6 
          -x, y, z// 7 
     }; 

例如您的多维数据集的前脸将产生积极的深度(这个立方体的中心是在0,0,0从你给的verticies),现在因为有8分4个正深度,你的正面是4,5,6,7,这是从-x,-y反时针到-x,y。

好吧,所以你的背部脸部都是负深度或-z,所以它只是0,1,2,3。

查看照片?你的左脸全是负宽或-x所以0,3,4,7,你的右脸是正X,所以1,2,5,6。

我会让你找出立方体的顶部和底部。

2

你的顶点数组只描述了一个立方体的两面,但为了讨论各种情形,说顶点[0] - 顶点[3]描述1下侧,然后你的纹理坐标可能是:

float texCoords[] = { 0.0, 0.0, //bottom left of texture 
         1.0, 0.0, //bottom right " " 
         1.0, 1.0, //top right " " 
         0.0, 1.0 //top left  " " 
        }; 

您可以使用这些坐标用于纹理整个纹理的每个后续面。

6

你在找什么是cube map。在OpenGL中,您可以一次定义6个纹理(代表立方体的大小),并使用3D纹理坐标而不是通用2D纹理坐标来映射它们。对于简单的立方体,纹理坐标将与顶点各自的法线相同。 (如果您只是以这种方式构建平面立方体,则还可以在顶点着色器中合并法线和纹理坐标!)立方体地图比尝试将相同的纹理应用于重复四边形(额外不必要的绘制步骤)要简单得多。

GLuint mHandle; 
glGenTextures(1, &mHandle); // create your texture normally 

// Note the target being used instead of GL_TEXTURE_2D! 
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); 
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 

glBindTexture(GL_TEXTURE_CUBE_MAP, mHandle); 

// Now, load in your six distinct images. They need to be the same dimensions! 
// Notice the targets being specified: the six sides of the cube map. 
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, width, height, 0, 
    format, GL_UNSIGNED_BYTE, data1); 
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, width, height, 0, 
    format, GL_UNSIGNED_BYTE, data2); 
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, width, height, 0, 
    format, GL_UNSIGNED_BYTE, data3); 
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, width, height, 0, 
    format, GL_UNSIGNED_BYTE, data4); 
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, width, height, 0, 
    format, GL_UNSIGNED_BYTE, data5); 
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, width, height, 0, 
    format, GL_UNSIGNED_BYTE, data6); 

glGenerateMipmap(GL_TEXTURE_CUBE_MAP); 
glTextParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 

// And of course, after you are all done using the textures... 
glDeleteTextures(1, &mHandle); 

当指定的纹理坐标,然后你会使用套的3个坐标,而不是套2.在一个简单的立方体,你点使用标准化向量的8个角。如果N = 1.0/sqrt(3.0),那么一个角将是N,N,N;另一个是N,N,-N;等

0

要渲染天空盒(立方体贴图),下面的着色器为我工作:

Cubemap vertexshader:: 
     attribute vec4 a_position;    
     varying vec3 v_cubemapTexture;   
     vec3 texture_pos;       
     uniform vec3 u_cubeCenterPt;    
     uniform mat4 mvp;      
     void main(void)      
     {          
     gl_Position = mvp * a_position;   
     texture_pos = vec3(a_position.x - u_cubeCenterPt.x, a_position.y - u_cubeCenterPt.y, a_position.z - u_cubeCenterPt.z); 
     v_cubemapTexture = normalize(texture_pos.xyz); 
     }          

Cubemap fragmentshader:: 
     precision highp float;            
     varying vec3 v_cubemapTexture;          
     uniform samplerCube cubeMapTextureSample;       
     void main(void)             
     {                 
      gl_FragColor = textureCube(cubeMapTextureSample, v_cubemapTexture); 
     } 

希望它是有用的......