OpenGL ES2.0 颜色显示不正确

发布于 2024-10-31 05:54:55 字数 2597 浏览 1 评论 0原文

(iPhone) 我正在尝试在 ES2 中绘制一个立方体,每个面上都有不同的颜色。现在颜色不正确,我不明白为什么。这是相关代码:

- (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"
    "}

(iPhone) I'm trying to draw a cube in ES2 with a different color on each face. Right now the colors aren't coming out right and I can't figure out why. Here's the relevant code:

- (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"
    "}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

站稳脚跟 2024-11-07 05:54:56

只是提一下 - 如果您想使用字节来表示颜色,请使用:

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

(GL_TRUE 而不是 GL_FALSE)请参阅 处理顶点数据的技术

Just to mention it - if you wanna use bytes for colors, use:

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

(GL_TRUE instead of GL_FALSE) see Techniques for working with vertex data

撩人痒 2024-11-07 05:54:56

如果您通过嵌套数组定义顶点和元素数据,您会发现您的逻辑更清晰:

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%2 技巧,顺便说一句,您可能会在其中找到错误。您在第一个面之后递增颜色索引(此时 i 仍然为零,因此 i%2 == 0),因此顶面的第二个三角形获得与第一个颜色不同的颜色;所有面孔都会出现同样的问题。

You'll find that your logic is cleaner if you define vertex and element data via nested arrays:

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

It will also allow you to simply divide by two to get the color index, rather than using the awkward i%2 trick, which, BTW, is where you'll probably find the bug. You are incrementing the color index after the first face (i is still zero at that point, so i%2 == 0), so the second triangle of the top face gets a different color to the first one; the same problem will occur for all faces.

陌路黄昏 2024-11-07 05:54:56

您必须在引擎上启用 DEPTH_TEST,否则顶点的绘制顺序将覆盖您的眼睛期望看到的逻辑 Z 深度。非常非常常见的初始错误。

尝试在清除颜色和深度缓冲区后立即启用。

glEnable(GL_DEPTH_TEST);

干杯

You have to enable the DEPTH_TEST on your engine otherwise the sequence of drawing of the vertices will override the logical Z DEPTH your eyes expect to see. Very very common initial error.

Try to enable the just after you clear the color and depth buffer.

glEnable(GL_DEPTH_TEST);

Cheers

且行且努力 2024-11-07 05:54:55

您可能已经发现了这一点,但是当您将顶点属性数组设置为使用无符号字节时,您的颜色数据似乎被声明并初始化为浮点数。这可能就是为什么颜色似乎与您期望的不同的原因。

You have probably already found this, but it seems like your color data is being declared and initialized as floats while you're setting up the vertex attribute array to be using unsigned bytes. This may be why the colors don't seem to be what you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文