2011-02-13 66 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); 
     } 

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