2011-02-16 76 views
4

我在下面的代碼中定義了一個立方體。我想每個6個面像的指定顏色:如何在OpenGL ES應用程序中指定立方體的每面顏色?

前臉:COLOR1

背面:COLOR2

左臉:COLOR3

右臉:color4

頂部face: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); 

回答

3

這是桌面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; 
} 
+0

謝謝,但這對我不起作用,因爲我擁有的多維數據集的定義與此代碼中定義的多維數據集非常不同。 – ace 2011-02-16 20:59:35

相關問題