Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Problem using VBOs

  1. #1
    Intern Contributor
    Join Date
    Jan 2012
    Posts
    81

    Problem using VBOs

    Hello All,

    I was mostly using the old glBegin() method for a long time. Now, im learning to use modern ways of specifying vertices. Here is some sample code i found online. I modified a little:
    What i tried to do is, in the same VBO, use color values also:

    Code :
    static const char* pVS = "                                                    \n\
    #version 330                                                                  \n\
                                                                                  \n\
    layout (location = 0) in vec3 Position;                                       \n\
    layout (location = 1) in vec3 Col;                                            \n\
    out vec3 col_out;                                                             \n\
                                                                                  \n\
    void main()                                                                   \n\
    {                                                                             \n\
        gl_Position = vec4(0.5 * Position.x, 0.5 * Position.y, Position.z, 1.0);  \n\
        col_out = Col;                                                            \n\
    }";
     
    static const char* pFS = "                                                    \n\
    #version 330                                                                  \n\
                                                                                  \n\
    out vec4 FragColor;                                                           \n\
    in vec3 col_out;                                                              \n\
                                                                                  \n\
    void main()                                                                   \n\
    {                                                                             \n\
       FragColor = vec4(col_out, 1.0);                                            \n\
    }";
     
     
    typedef struct {
       Vector3f vert;
       Vector3f col;
    } Vertex;
     
    Vertex Vertices[3];
     
    static void RenderSceneCB()
    {
        glClear(GL_COLOR_BUFFER_BIT);
     
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
     
        glEnableVertexAttribArray(1);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), &(Vertices[0].col));
     
        glDrawArrays(GL_TRIANGLES, 0, 3);
     
        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);
     
        glutSwapBuffers();
    }
     
     
    static void InitializeGlutCallbacks()
    {
        glutDisplayFunc(RenderSceneCB);
    }
     
    static void CreateVertexBuffer()
    {     
     
        Vertices[0].vert = Vector3f(-1.0f, -1.0f, 0.0f);
        Vertices[0].col = Vector3f(1.0f, 0.0f, 0.0f);
     
        Vertices[1].vert = Vector3f(1.0f, -1.0f, 0.0f);
        Vertices[1].col  = Vector3f(1.0f, 0.0f, 0.0f);
     
        Vertices[2].vert = Vector3f(0.0f, 1.0f, 0.0f);
        Vertices[2].col  = Vector3f(1.0f, 0.0f, 0.0f);
     
         glGenBuffers(1, &VBO);
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
    }


    Here is the essential part. But i get a blank screen. Can anyone please let me know what i'm doing wrong?

    Thanks in advance!

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,101
    The only thing I can see that looks odd is your glBindBuffer(GL_ARRAY_BUFFER, VBO);.
    I would only do this once prior to enabling and setting any vertex attributes

  3. #3
    Intern Contributor
    Join Date
    Jan 2012
    Posts
    81
    Thanks for the reply tonyo_au. i tried doing it just once. i added the glBindBuffer() once , just before


    glEnableVertexAttribArray(0);

    But, it doesn't seem to work. I still get the black screen.

  4. #4
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    433
    The last argument of:
    Code :
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), &(Vertices[0].col));

    should be the offset of the .col field in the Vertex structure (which is probably 12) as you are using a buffer object, and shouldn't be a client-side address.

    There's different ways you could get this offset value including manually providing it, calculating the offset using the difference in address between a field and the start of a struct (eg. between &
    Vertices[0] and &Vertices[0].col) or using the offsetof macro.

  5. #5
    Intern Contributor
    Join Date
    Jan 2012
    Posts
    81
    Thanks Dan Bartlett!

    It works fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •