OpenGL ES的 - 纹理贴图的8个顶点立方体的所有面? [英] OpenGL ES - texture map all faces of an 8 vertex cube?

查看:309
本文介绍了OpenGL ES的 - 纹理贴图的8个顶点立方体的所有面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工作通过一些的OpenGL ES教程,使用Android模拟器。我已经得到了纹理映射和时遇到一些麻烦映射到多维数据集。有没有可能是如下所述纹理映射到一个多维数据集有8个顶点和12个三角形的六个面的所有面?

  //使用一半我们要为0,0,0中心。
    宽度/ = 2;
    高度/ = 2;
    深度/ = 2;

    浮动顶点[] = { - 宽度,-height,深度,// 0
                          宽度,-height,深度,// 1
                          宽度,高度,深度,// 2
                          - 宽度,高度,深度,// 3
                         -Width,-height,-depth,// 4
                          宽度,-height,-depth,// 5
                          宽度,高度,-depth,// 6
                          - 宽度,高度,-depth,// 7
    };

    短期指数[] = {
            // 面前
            0,1,2,
            0,2,3,
            // 背部
            5,4,7,
            5,7,6,
            // 剩下
            4,0,3,
            4,3,7,
            // 对
            1,5,6,
            1,6,2,
            // 最佳
            3,2,6,
            3,6,7,
            // 底部
            4,5,1,
            4,1,0,
    };

   浮texCoords [] = {
        1.0F,1.0F,
        0.0,1.0F,
        0.0,0.0,
        1.0F,0.0,

        1.0F,1.0F,
        0.0,1.0F,
        0.0,0.0,
        1.0F,0.0,

        1.0F,1.0F,
        0.0,1.0F,
        0.0,0.0,
        1.0F,0.0,

        1.0F,1.0F,
        0.0,1.0F,
        0.0,0.0,
        1.0F,0.0,

        1.0F,1.0F,
        0.0,1.0F,
        0.0,0.0,
        1.0F,0.0,

        1.0F,1.0F,
        0.0,1.0F,
        0.0,0.0,
        1.0F,0.0,
    };
 

我已经得到的前和后端面正常工作,但是,没有任何其他脸部的显示纹理

绘图code

 公共无效画(GL10 GL){
    //逆时针绕。
    gl.glFrontFace(GL10.GL_CCW);

    //启用脸部剔除。
    gl.glEnable(GL10.GL_CULL_FACE);

    //什么面孔,除去与脸部剔除。
    gl.glCullFace(GL10.GL_BACK);

    //启用顶点缓冲器写入和过程中使用的
    //渲染。
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

    //指定顶点的阵列的位置和数据格式
    //坐标绘制时使用。
    gl.glVertexPointer(3,GL10.GL_FLOAT,0,verticesBuffer);

    如果(normalsBuffer!= NULL){
        //启用用于写入的正常缓冲和渲染过程中使用。
        gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);

        //指定法线呈现时要使用的阵列的位置和数据格式。
        gl.glNormalPointer(GL10.GL_FLOAT,0,normalsBuffer);
    }

    //设置平面彩色
    gl.glColor4f(RGBA [0],RGBA [1],RGBA [2],RGBA [3]);

    //平滑的色彩
    如果(colorBuffer!= NULL){
        //启用渲染过程中所使用的彩色阵列的缓冲区。
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        //点​​出其中的颜色缓冲区。
        gl.glColorPointer(4,GL10.GL_FLOAT,0,colorBuffer);
    }

    //使用纹理?
    如果(textureBuffer!= NULL){
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glTexCoordPointer(2,GL10.GL_FLOAT,0,textureBuffer);
    }

    //绘制平移和旋转前
    gl.glTranslatef(X,Y,Z);
    gl.glRotatef(RX,1,0,0);
    gl.glRotatef(RY,0,1,0);
    gl.glRotatef(RZ,0,0,1);

    gl.glDrawElements(GL10.GL_TRIANGLES,numOfIndices,GL10.GL_UNSIGNED_SHORT,indicesBuffer);

    //禁用顶点缓冲区。
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
    gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    //禁用脸部剔除。
    gl.glDisable(GL10.GL_CULL_FACE);
}
 

解决方案

您必须使用24顶点。在OpenGL中,一个顶点不仅仅是位置,它是所有顶点的集合属性。每个顶点阵列被访问具有相同索引

臭名昭著的立方体的例子是一些几乎每个人都觉得是开始使用OpenGL时效率不高,但在现实世界中,更复杂的模型,复制的程度是相当低的。

Working through some OpenGL-ES tutorials, using the Android emulator. I've gotten up to texture mapping and am having some trouble mapping to a cube. Is it possible to map a texture to all faces of a cube that has 8 vertices and 12 triangles for the 6 faces as described below?

// Use half as we are going for a 0,0,0 centre.
    width  /= 2;
    height /= 2;
    depth  /= 2;

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

    short indices[] = { 
            // Front
            0,1,2,
            0,2,3,
            // Back
            5,4,7,
            5,7,6,
            // Left
            4,0,3,
            4,3,7,
            // Right
            1,5,6,
            1,6,2,
            // Top
            3,2,6,
            3,6,7,
            // Bottom
            4,5,1,
            4,1,0,
    };

   float texCoords[] = {
        1.0f, 1.0f,
        0.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f,

        1.0f, 1.0f,
        0.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f,

        1.0f, 1.0f,
        0.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f,

        1.0f, 1.0f,
        0.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f,

        1.0f, 1.0f,
        0.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f,

        1.0f, 1.0f,
        0.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f,
    };

I have gotten the front and back faces working correctly, however, none of the other faces are showing the texture.

Drawing code

public void draw(GL10 gl) {
    // 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, verticesBuffer);

    if (normalsBuffer != null) {
        // Enabled the normal buffer for writing and to be used during rendering.
        gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);

        // Specifies the location and data format of an array of normals to use when rendering.
        gl.glNormalPointer(GL10.GL_FLOAT, 0, normalsBuffer);
    }

    // Set flat color
    gl.glColor4f(rgba[0], rgba[1], rgba[2], rgba[3]);

    // Smooth color
    if ( colorBuffer != null ) {
        // Enable the color array buffer to be used during rendering.
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        // Point out the where the color buffer is.
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
    }

    // Use textures?
    if ( textureBuffer != null) {
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
    }

    // Translation and rotation before drawing
    gl.glTranslatef(x, y, z);
    gl.glRotatef(rx, 1, 0, 0);
    gl.glRotatef(ry, 0, 1, 0);
    gl.glRotatef(rz, 0, 0, 1);

    gl.glDrawElements(GL10.GL_TRIANGLES, numOfIndices, GL10.GL_UNSIGNED_SHORT, indicesBuffer);

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

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

解决方案

You have to use 24 vertexes. In OpenGL, a vertex is more than just position, it is the collection of all vertex attributes. Every vertex array is accessed with the same index.

The infamous cube example is something almost everyone feels is inefficient when starting to use OpenGL, but in real-world, more complex models, the degree of duplication is quite low.

这篇关于OpenGL ES的 - 纹理贴图的8个顶点立方体的所有面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆