OpenGL中立方体的纹理坐标是什么?

OpenGL中立方体的纹理坐标是什么?,opengl,Opengl,我将立方体定义为: float vertices[] = { -width, -height, -depth, // 0 width, -height, -depth, // 1 width, height, -depth, // 2 -width, height, -depth, // 3

我将立方体定义为:

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
        };  
我有一张128x128的图片,我只想画在立方体的6个面上,别的什么都不画。那么,共ridinates的结构是什么呢?我需要实际值

这是图纸代码:

// 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坐标。注意,我给出的立方体顶点数组是使用索引数组表示立方体的最有效方法。立方体绘制得非常完美,但纹理却不完美。只有一面显示了正确的图片,但另一面却乱七八糟。我使用了各种在线纹理教程中描述的方法,但它不起作用

  • 您需要定义每个面的方向(这将更改每个顶点上放置的纹理坐标)
  • 您需要复制顶点位置,因为同一个立方体角点将具有不同的纹理坐标,这取决于它所属的面
  • 如果要在每个面上显示完整纹理,则纹理坐标为(0,0)(0,1)(1,1)(1,0)。如何将它们映射到特定顶点(其中24个,每个面4个)取决于所需的方向

  • <>对于我来说,更容易把你的垂直视为宽度=x,高度=y和深度=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),现在因为有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


    我将让您计算出立方体的顶部和底部。

    您的顶点数组仅描述立方体的两个面,但出于论证的目的,假设顶点[0]-顶点[3]描述一个面,则您的纹理坐标可能是:

    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     "    "
                        };
    

    您可以使用这些坐标对整个纹理的每个后续边进行纹理处理。

    您要查找的是一个。在OpenGL中,您可以一次定义六个纹理(表示立方体的大小边),并使用3D纹理坐标(而不是常见的2D纹理坐标)对它们进行映射。对于简单立方体,纹理坐标将与顶点各自的法线相同。(如果仅以这种方式对平面立方体进行纹理处理,也可以在顶点着色器中合并法线和纹理坐标!)立方体贴图比尝试将相同纹理应用于重复的四边形(额外不必要的绘制步骤)简单得多

    指定纹理坐标时,将使用3个坐标集而不是2个坐标集。在一个简单的立方体中,使用规格化向量指向8个角。如果N=1.0/sqrt(3.0),则一个角为N,N,N;另一个是N,N,-N;等等。

    要渲染skybox(立方体贴图),下面的着色器对我很有用:

    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);  
            }
    

    希望它有用…

    您可能也应该发布您的绘图代码…鉴于上述信息,我们需要多少tex坐标,在技术上是正确的?我所说的tex坐标是指tex坐标数组中像1,0这样的2d点。这些是纹理坐标还是顶点的索引?这些是顶点坐标,在他大量编辑之后,我没有更新这个问题。tex坐标非常简单,因为他在每个面上使用128x128图像,所以对于每个面,您映射整个图像,即每个面tex坐标(0,0)、(0128)、(128,128)、(128,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);  
            }