views:

210

answers:

1

Hello I'm trying to convert the following functions to a VBO based function for learning purposes, it displays a static texture on screen. I'm using OpenGL ES 2.0 with shaders on the iPhone (should be almost the same than regular OpenGL in this case), this is what I got working:

//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);
}

I tried to do this to convert to a VBO however I don't see anything displaying on-screen with this version:

//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);
}

In both cases I'm using this simple Vertex Shader

//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;
}

The gl_Position is equal to product of mvp * vec4 because I'm simulating glOrthof in 2D with that mvp

And this Fragment Shader

//Fragment Shader
uniform sampler2D sampler;

varying mediump vec2 texcoordVarying;

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

I really need help with this, maybe my shaders are wrong for the second case ?

thanks in advance.

+2  A: 

Everything is right, except the glVertexAttribPointer call.

When you have a VBO bound, the last parameter o glVertexAttribPointer is used as an offset into the VBO, as a pointer (the pointer value is the offset). So, your data is at the start of the VBO, so the last parameter should be 0 (NULL) for both calls.

Matias Valdenegro