2011-04-07 101 views
1

(iPhone)我試圖在ES2中繪製一個立方體,每個面上都有不同的顏色。現在的顏色是不正確的,我不明白爲什麼。下面是相關的代碼:使用顏色的OpenGL ES2.0

- (void) DrawES2 { 
    glViewport (0, 0, backingWidth, backingHeight); 
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glUseProgram (programObject); 
    int colorIndex = 0; 
BOOL newFace = NO; 
for(int i = 0; i < 36; i += 3) 
{ 

    GLfloat faceColor[] = { faceColors[colorIndex], faceColors[colorIndex+1], faceColors[colorIndex+2], faceColors[colorIndex+3] }; 

    // Load the vertex data 
    glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, vVertices); 
    glEnableVertexAttribArray (0); 

    // Load the color data 
    glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_FALSE, 0, faceColor); 
    glEnableVertexAttribArray(1); 

    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, &indices[i]); 

    newFace = (i%2 == 0) ? NO : YES; 
    if(newFace) 
     colorIndex+=4; 
} 
} 
    GLfloat vVertices[] = { -0.5f, 0.5f, 0.5f, 
-0.5f, -0.5f, 0.5f, 
0.5f, -0.5f, 0.5f, 
0.5f, 0.5f, 0.5f, 
-0.5f, 0.5f, -0.5f, 
-0.5f, -0.5f, -0.5f, 
0.5f, -0.5f, -0.5f, 
0.5f, 0.5f, -0.5f }; 

// Used to draw cube more efficiently 
GLubyte indices[36] = { 
4, 7, 3, //top face 
4, 3, 0, 
5, 6, 7, //front face 
5, 7, 4, 
3, 2, 1, //back face 
0, 3, 1, 
6, 2, 3, //right face 
6, 3, 7, 
5, 0, 1, //left face 
5, 4, 0, 
5, 2, 6, //bottom face 
5, 1, 2 }; 
const GLfloat faceColors[] = { 
0, 1, 0, 1, 
1, 0.5f, 0, 1, 
1, 0, 0, 1, 
1, 1, 0, 1, 
0, 0, 1, 1, 
1, 0, 1, 1 
}; 
GLbyte vShaderStr[] = 
    "uniform mat4 t_matrix;   \n" 
    "uniform mat4 r_matrix;   \n" 
    "uniform mat4 u_proj_matrix; \n" 
    "attribute vec4 vPosition;  \n" 
    "attribute vec4 a_color;  \n" 
    "varying vec4 v_color;   \n" 
    "void main()          \n" 
    "{             \n" 
    " mat4 model_matrix = t_matrix * r_matrix;  \n" 
    " mat4 mvp_matrix = u_proj_matrix * model_matrix; \n" 
    " gl_Position = mvp_matrix * vPosition;   \n" 
    " v_color = a_color;        \n" 
    "}             \n"; 

    GLbyte fShaderStr[] = 
    "precision mediump float;  \n" 
    "varying vec4 v_color;   \n" 
    "void main()     \n" 
    "{        \n" 
    " gl_FragColor = v_color;  \n" 
    "} 

http://img684.imageshack.us/img684/2316/unled1y.png

回答

0

你會發現,你的邏輯是清潔的,如果你通過嵌套數組定義頂點和元數據:

GLubyte vVertices[][3] = { 
    { -0.5f, -0.5f, 0.5f }, 
    { 0.5f, -0.5f, 0.5f }, 
    ... 
}; 

GLubyte indices[][3] = { 
    { 4, 7, 3 }, //top face 
    { 4, 3, 0 }, 
    ... 
}; 

GLfloat faceColors[][4] = { 
    { 0, 1, 0, 1 }, 
    { 1, 0.5, 0, 1 }, 
    ... 
}; 

它也可以讓你只需除以二得到顏色索引,而不是使用尷尬的技巧,其中,順便說一句,你可能會發現錯誤。在第一張臉後(i在該點仍爲零,因此i%2 == 0),您正在遞增顏色索引,因此頂面的第二個三角形與第一個三角形的顏色不同。所有面孔都會出現同樣的問題。

+0

我喜歡這樣。我做了這些改變,但現在除了一個三角形是黑色外,它全部呈藍色:'code'( - (void)DrawES2 //加載顏色數據 \t \t glVertexAttribPointer(1,4,GL_UNSIGNED_BYTE,GL_FALSE, 0,faceColors [colorIndex/2]); \t \t glEnableVertexAttribArray(1); \t \t \t \t glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_BYTE,&指數[I]); \t \t \t \t colorIndex ++;) – user617123 2011-04-07 23:31:17

1

您可能已經發現了這一點,但似乎您的顏色數據正在聲明並初始化爲浮點數,而您將頂點屬性數組設置爲使用無符號字節。這可能是爲什麼這些顏色看起來不像你期望的那樣。

0

您必須在引擎上啓用DEPTH_TEST,否則頂點的繪製順序將覆蓋您的眼睛希望看到的邏輯Z DEPTH。非常非常常見的初始錯誤。

嘗試啓用剛清除顏色和深度緩衝區後。

glEnable(GL_DEPTH_TEST);

乾杯

1

就更不用說了 - 如果你想使用字節的顏色,使用:

glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, faceColor); 

(GL_TRUE而不是GL_FALSE)看到Techniques for working with vertex data