0
\$\begingroup\$

I'm new and trying to draw a very simple quad with VAO and GLSL.

My definitions:

typedef struct SPos
{
    float x;
    float y;
} SPos;
SPos mVertices[6];
SPos mIndicies[6];
GLuint mVao;
GLuint mVertexVbo;
GLuint mIbo;

These are my steps:

  1. Load Vertex, Fragment code (I'm sure that no mistake in this step) and get programID
  2. Create VBO

    glGenBuffers(1, &mVertexVbo);
    glBindBuffer(GL_ARRAY_BUFFER, mVertexVbo);
    glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(SPos), mVertices, GL_STATIC_DRAW);
    
  3. Create IBO.

    mIndices[0] = 0;
    mIndices[1] = 1;
    mIndices[2] = 2;
    mIndices[3] = 3;
    glGenBuffers(1, &mIbo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIbo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * sizeof(GLuint), (GLuint*) mIndices,\
        GL_STATIC_DRAW);
    
  4. Create VAO

    glGenVertexArrays(1, &mVao);
    glBindVertexArray(mVao);
    glUseProgram(mProgram);
    
    GLint posLoc = glGetAttribLocation(mShader.program, "a_posL");
    glEnableVertexAttribArray(posLoc);
    glBindBuffer( GL_ARRAY_BUFFER, mVertexVbo );
    glVertexAttribPointer( posLoc, 2, GL_FLOAT, GL_FALSE, 0, 0 );
    
    glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mIbo );
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    
  5. Render loop.

    mVertices[0].x = -50.0f;
    mVertices[0].y = -50.0f;
    
    mVertices[1].x = 50.0f;
    mVertices[1].y = -50.0f;
    
    mVertices[2].x = 50.0f;
    mVertices[2].y = 50.0f;
    
    mVertices[3].x = -50.0f;
    mVertices[3].y = 50.0f;
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBindVertexArray(mVao);
    glDrawElements( GL_TRIANGLE_FAN, 4, GL_UNSIGNED_INT, 0 );
    // GLES2 swap function.
    eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
    

These are my shaders

fragment:

#version 300 es
precision mediump float;

out vec4 oColor;
uniform vec4 unifColor;

void main()
{
    oColor = vec4(1);
}

vertex:

#version 300 es

in vec2 a_posL;

void main()
{
    gl_Position = vec4(a_posL, 1.0, 1.0);
}

Nothing is draw in the screen. What's wrong?

All above is follows a VAO tutorial on the internet.

I spend 1 day to debug and scan it very carefully but not work.

EDIT:

Thanks GOD. The problem is that I setted the data of mVertices in render step, have to set it before create VBO.

I'm still confuse that glBufferData need a pointer for data but Why I cannot pass the address of data then assign its value in later? Why I have to assign its value before bind it to VBO?

\$\endgroup\$
  • \$\begingroup\$ Because buffer is(usually) in GPU memory, how the hell GPU should know when to fetch the data? Before every gl call copy everything back(and forth)? That does not sound very efficient. However, you can actually do something similar - you can ask driver for address where you can write to, when you are done, you just tell driver to send the data to the (GPU) VBO. \$\endgroup\$ – wondra Feb 11 '15 at 19:46
  • \$\begingroup\$ Yeah, before went to sleep on last night, I figured out that glBufferData purpose to trans data from CPU to GPU. I didn't think that before. From starting to learn OpenGL until now, I thought that data only transfers to GPU whenever any glDraw function are called. \$\endgroup\$ – LongLT Feb 12 '15 at 4:02
1
\$\begingroup\$

glBufferData makes a copy of the data and stores it in the VBO. This will then be uploaded to the gpu and the client can free/reuse the memory.

\$\endgroup\$
  • \$\begingroup\$ yeah, thanks. In my situation, using VAO is not effective as I thought. How do programmer uses VAO? I mean draw with dynamic position. Does they let shader do that? what is most right way to use VAO? too much question :) sorry that almost demo or example on the internet just teach me how to do, not how to use. GLSL is very hard. \$\endgroup\$ – LongLT Feb 12 '15 at 3:55
  • \$\begingroup\$ @LongLT you can update the buffer even when it is bound to a VAO, though otherwise they use a uniform to change the end location of vertices \$\endgroup\$ – ratchet freak Feb 12 '15 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.