3

您好我正在尝试将以下函数转换为基于 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 的乘积,因为我正在使用该 mvp 在 2D 中模拟 glOrthof

还有这个片段着色器

//Fragment Shader
uniform sampler2D sampler;

varying mediump vec2 texcoordVarying;

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

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

提前致谢。

4

1 回答 1

2

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

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

于 2010-06-14T03:38:48.907 回答