【问题标题】:How to specify color per side of a cube in OpenGL ES application?如何在 OpenGL ES 应用程序中指定立方体每边的颜色?
【发布时间】:2011-02-16 17:05:00
【问题描述】:

我在下面的代码中定义了一个立方体。我想为 6 个面中的每一个指定颜色,例如:

正面:color1

背面:color2

左脸:color3

右脸:color4

顶面:color5

底面:color6

请注意,在 OpenGL ES 中,我们只有三角形而不是四边形。我正在寻找可以解决我的问题的工作示例代码,因为工作示例代码价值超过一千字,就像他们说的图片一样。

这是立方体:

 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[] = { 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
                         };

这是绘图代码:

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


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



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

【问题讨论】:

    标签: opengl-es


    【解决方案1】:

    这是桌面 GLUT 代码,但核心逻辑应直接转换为 OpenGL ES:

    #include <GL/glut.h>
    #include <cstdlib>
    
    void glutTexturedCube(GLdouble size)
    {
        GLfloat color1[] = { 255,   0,   0 };
        GLfloat color2[] = {   0, 255,   0 };
        GLfloat color3[] = {   0,   0, 255 };
        GLfloat color4[] = { 255, 255,   0 };
        GLfloat color5[] = {   0, 255, 255 };
        GLfloat color6[] = { 255, 255, 255 };
    
        GLfloat color[] = {
            color1[0], color1[1], color1[2],
            color1[0], color1[1], color1[2],
            color1[0], color1[1], color1[2],
            color1[0], color1[1], color1[2],
            color1[0], color1[1], color1[2],
            color1[0], color1[1], color1[2],
    
            color2[0], color2[1], color2[2],
            color2[0], color2[1], color2[2],
            color2[0], color2[1], color2[2],
            color2[0], color2[1], color2[2],
            color2[0], color2[1], color2[2],
            color2[0], color2[1], color2[2],
    
            color3[0], color3[1], color3[2],
            color3[0], color3[1], color3[2],
            color3[0], color3[1], color3[2],
            color3[0], color3[1], color3[2],
            color3[0], color3[1], color3[2],
            color3[0], color3[1], color3[2],
    
            color4[0], color4[1], color4[2],
            color4[0], color4[1], color4[2],
            color4[0], color4[1], color4[2],
            color4[0], color4[1], color4[2],
            color4[0], color4[1], color4[2],
            color4[0], color4[1], color4[2],
    
            color5[0], color5[1], color5[2],
            color5[0], color5[1], color5[2],
            color5[0], color5[1], color5[2],
            color5[0], color5[1], color5[2],
            color5[0], color5[1], color5[2],
            color5[0], color5[1], color5[2],
    
            color6[0], color6[1], color6[2],
            color6[0], color6[1], color6[2],
            color6[0], color6[1], color6[2],
            color6[0], color6[1], color6[2],
            color6[0], color6[1], color6[2],
            color6[0], color6[1], color6[2],
        };
    
        GLfloat vert[] = {
            // top (+z)
            -1, -1,  1,
             1, -1,  1,
            -1,  1,  1,
            -1,  1,  1,
             1, -1,  1,
             1,  1,  1, 
    
            // bottom (-z)
            -1, -1, -1,
            -1,  1, -1,
             1, -1, -1,
             1, -1, -1,
            -1,  1, -1,
             1,  1, -1,
    
            // right (+x)
             1, -1, -1, 
             1,  1, -1, 
             1, -1,  1, 
             1, -1,  1, 
             1,  1, -1, 
             1,  1,  1, 
    
            // left (-x)
            -1, -1, -1, 
            -1, -1,  1, 
            -1,  1, -1, 
            -1,  1, -1, 
            -1, -1,  1, 
            -1,  1,  1, 
    
            // front (+y)
            -1, -1, -1,
             1, -1, -1,
            -1, -1,  1,
            -1, -1,  1,
             1, -1, -1,
             1, -1,  1,
    
            // back (-y)
            -1,  1, -1,
            -1,  1,  1,
             1,  1, -1,
             1,  1, -1,
            -1,  1,  1,
             1,  1,  1,
        };
    
        GLushort idxs[] = { 
             0, 1, 2, 
             3, 4, 5,
    
             6, 7, 8,
             9,10,11,
    
            12,13,14,
            15,16,17,
    
            18,19,20,
            21,22,23,
    
            24,25,26,
            27,28,29,
    
            30,31,32,
            33,34,35
        };
    
        glEnableClientState(GL_COLOR_ARRAY);
        glEnableClientState(GL_VERTEX_ARRAY);
        glColorPointer(3, GL_FLOAT, 0, color);
        glVertexPointer(3, GL_FLOAT, 0, vert);
    
        glPushMatrix();
        glColor4f(1, 1, 1, 1);
        glScaled(size, size, size);
        glDrawElements(GL_TRIANGLES, sizeof(idxs)/sizeof(idxs[0]), GL_UNSIGNED_SHORT, idxs);
        glPopMatrix();
    
        glDisableClientState(GL_COLOR_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);
    } 
    
    static GLuint texName;
    
    void init(void)
    {
        glClearColor(0,0,0,0);
    
        glEnable(GL_DEPTH_TEST);
        glShadeModel(GL_SMOOTH);
        glEnable(GL_CULL_FACE);
    }
    
    void display(void)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        // projection
        glMatrixMode(GL_PROJECTION); glLoadIdentity();
        int viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport);
        gluPerspective(60.0, (GLdouble)viewport[2]/(GLdouble)viewport[3], 1.0, 100.0 );
    
        // view transform
        glMatrixMode(GL_MODELVIEW); glLoadIdentity();
        glTranslatef(0,0,-4);
    
        // render 
        glPushMatrix();
        const float ANGLE_SPEED = 30; // degrees/sec
        float angle = ANGLE_SPEED * (glutGet(GLUT_ELAPSED_TIME) / 1000.0f);
        glRotatef(angle*0.5f, 1, 0, 0);
        glRotatef(angle, 0, 1, 0);
        glRotatef(angle*0.7f, 0, 0, 1);
    
        glutTexturedCube(1);
    
        glutSwapBuffers();
    }
    
    void reshape(int w, int h)
    {
        glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    }
    
    void keyboard (unsigned char key, int x, int y)
    {
        switch (key) 
            { 
            case 27: exit(0); break;
            default: break; 
            }
    }
    
    void idle()
    {
        glutPostRedisplay();
    }
    
    int main(int argc, char** argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
        glutInitWindowSize(640, 480);
        glutInitWindowPosition(100, 100);
        glutCreateWindow (argv[0]);
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
        glutKeyboardFunc(keyboard);
        glutIdleFunc(idle);
    
        init();
        glutMainLoop();
        return 0;
    }
    

    【讨论】:

    • 谢谢,但这对我不起作用,因为我拥有的多维数据集的定义与此代码中定义的多维数据集非常不同。
    • @Alan:那么可能想完善你的问题。
    • @Alan - 实际上,他的 color 数组将满足您的需求。忽略立方体设计的其余部分,并使用它。就像您的立方体一样,他使用六个索引为每个面指定两个三角形,因此它将以不同的颜色绘制立方体的每个面。您可能需要更改整体颜色的顺序以将它们与您想要的面部编号对齐,但是一旦您将其与您的应用程序集成并绘制立方体,这将是微不足道的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多