如何在OpenGL ES应用程序中为立方体的每一侧指定颜色? [英] How to specify color per side of a cube in OpenGL ES application?

查看:60
本文介绍了如何在OpenGL ES应用程序中为立方体的每一侧指定颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中定义了一个多维数据集.我想为6张面孔中的每张面孔指定颜色,例如:

I have a cube define in the code below. I want to specify color for each of the 6 faces like:

正面:color1

背面:color2

左脸:color3

右脸:color4

顶面:color5

底脸:color6

请注意,在OpenGL ES中,我们只有三角形,而没有四边形.我正在寻找可以解决我的问题的示例代码,因为示例代码的价值超过一千个单词,就像他们对图片所说的那样.

Note that in OpenGL ES we only have triangles and not quads. I am looking for working sample code that solves my problem because working sample code is worth more than a thousand words like they say about pictures.

这里是立方体:

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

推荐答案

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

This is desktop GLUT code but the core logic should translate directly to 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;
}

这篇关于如何在OpenGL ES应用程序中为立方体的每一侧指定颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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