尝试使用简单的着色器+ VBO时出现问题

时间:2010-06-13 23:53:10

标签: graphics opengl-es vbo vertex-buffer

您好我正在尝试将以下函数转换为基于VBO的函数用于学习目的,它在屏幕上显示静态纹理。我在iPhone上使用带有着色器的OpenGL ES 2.0(在这种情况下应该与常规OpenGL几乎相同),这就是我的工作:

//Works!
- (void) drawAtPoint:(CGPoint)point depth:(CGFloat)depth
{
    GLfloat             coordinates[] = {
                            0,          1,
                            1,          1,
                            0,          0,
                            1,          0
                        };

    GLfloat             width = (GLfloat)_width * _maxS,
                        height = (GLfloat)_height * _maxT;

    GLfloat             vertices[] = {
                            -width / 2 + point.x,       -height / 2 + point.y,
                            width / 2 + point.x,        -height / 2 + point.y,
                            -width / 2 + point.x,       height / 2 + point.y,
                            width / 2 + point.x,        height / 2 + point.y,   
                        };


    glBindTexture(GL_TEXTURE_2D, _name);
    //Attrib position and attrib_tex coord are handles for the shader attributes
    glVertexAttribPointer(ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glEnableVertexAttribArray(ATTRIB_POSITION);
    glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
    glEnableVertexAttribArray(ATTRIB_TEXCOORD);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

我尝试这样做以转换为VBO但是我没有在这个版本的屏幕上看到任何显示:

//Doesn't display anything
- (void) drawAtPoint:(CGPoint)point depth:(CGFloat)depth
{
    GLfloat             width = (GLfloat)_width * _maxS,
                        height = (GLfloat)_height * _maxT;

    GLfloat             position[] = {
                            -width / 2 + point.x,       -height / 2 + point.y,
                            width / 2 + point.x,        -height / 2 + point.y,
                            -width / 2 + point.x,       height / 2 + point.y,
                            width / 2 + point.x,        height / 2 + point.y,   
                        }; //Texture on-screen position ( each vertex is x,y in on-screen coords )

    GLfloat             coordinates[] = {
        0,          1,
        1,          1,
        0,          0,
        1,          0
    }; // Texture coords from 0 to 1

    glBindVertexArrayOES(vao);
    glGenVertexArraysOES(1, &vao);
    glGenBuffers(2, vbo);

    //Buffer 1
    glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
    glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), position, GL_STATIC_DRAW);
    glEnableVertexAttribArray(ATTRIB_POSITION);
    glVertexAttribPointer(ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, position);

    //Buffer 2
    glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
    glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), coordinates, GL_DYNAMIC_DRAW);    
    glEnableVertexAttribArray(ATTRIB_TEXCOORD);
    glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, coordinates);

    //Draw
    glBindVertexArrayOES(vao);
    glBindTexture(GL_TEXTURE_2D, _name);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

在这两种情况下,我都使用这个简单的顶点着色器

//Vertex Shader
attribute vec2 position;//Bound to ATTRIB_POSITION
attribute vec4 color; 
attribute vec2 texcoord;//Bound to ATTRIB_TEXCOORD

varying vec2 texcoordVarying;

uniform mat4 mvp;

void main()
{
    //You CAN'T use transpose before in glUniformMatrix4fv so... here it goes.
    gl_Position = mvp * vec4(position.x, position.y, 0.0, 1.0);
    texcoordVarying = texcoord;
}

gl_Position等于mvp * vec4的乘积,因为我在2D中使用mvp模拟glOrthof

这个片段着色器

//Fragment Shader
uniform sampler2D sampler;

varying mediump vec2 texcoordVarying;

void main()
{
    gl_FragColor = texture2D(sampler, texcoordVarying);
}

我真的需要帮助,也许我的着色器在第二种情况下是错误的?

提前感谢。

1 个答案:

答案 0 :(得分:2)

一切都是正确的,除了glVertexAttribPointer调用。

当你有VBO绑定时,最后一个参数o glVertexAttribPointer用作VBO的偏移量,作为指针(指针值是偏移量)。因此,您的数据位于VBO的开头,因此两个调用的最后一个参数应为0(NULL)。