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 7 of 7

Thread: Drawing without Vertex Buffer Object

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2014
    Posts
    16

    Drawing without Vertex Buffer Object

    I am trying to draw without vertex buffer object but nothing is drawn. I do not know where I did wrong. I use Qt and OpenGL ES 2.0. This is my code:

    Code :
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,1);
        glEnableVertexAttribArray(m_posAttr);
        glEnableVertexAttribArray(m_distLocation);
     
        glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, vertices.size()*3 * sizeof(float), &vertices[0]);
     
        int offset = 2 * sizeof(float);
        glVertexAttribPointer(m_distLocation, 1, GL_FLOAT, GL_FALSE, 0, (void*)offset);
     
     
        glBindAttribLocation(m_program.programId(), m_posAttr, "posAttr" );
        glBindAttribLocation(m_program.programId(), m_distLocation, "distance" );
     
        glUniform1i(m_patternLocation, -1);
        glUniform4fv(m_colorAttr, 1, vColor);
        glUniform1f(m_depthAttr, 0.2f);
     
        glDrawElements(GL_TRIANGLES,indices.size(), GL_UNSIGNED_SHORT, &indices[0] );

    where vertices are vector of size 3 containing x and y coordinate and distance for each coordinate. Distance is used to do some calculations in shader.

    Any idea what I did wrong?

  2. #2
    Junior Member Regular Contributor
    Join Date
    Dec 2010
    Location
    Oakville, ON, CA
    Posts
    165
    Quote Originally Posted by MirzaSe View Post
    Any idea what I did wrong?
    Yep. Here is the answer:
    Quote Originally Posted by MirzaSe View Post
    I am trying to draw without vertex buffer object
    There must be a valid non-zero named VAO bound.

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2014
    Posts
    16
    Quote Originally Posted by Yandersen View Post
    Yep. Here is the answer:There must be a valid non-zero named VAO bound.
    I am a beginner and I wonder how to change it.

  4. #4
    Junior Member Regular Contributor
    Join Date
    May 2013
    Posts
    130
    Quote Originally Posted by myself
    Just use a single VAO that you create/bind at the beginning of your program after creating the gl context.

    Code cpp:
    GLuint lonelyVao;
    glGenVertexArrays(1, &lonelyVao);
    glBindVertexArray(lonelyVao)
    Edit: never mind that, you don't need VAOs in ES 2.0. MirzaSe use of the word VAO for Vertex Buffer Object confused my brain...

    This is trying to bind the buffer with the ID 1 as element source. Try to change it to 0.
    Code cpp:
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,1);
    Last edited by Osbios; 09-08-2014 at 05:18 AM.

  5. #5
    Junior Member Newbie
    Join Date
    Aug 2014
    Posts
    16
    Quote Originally Posted by Osbios View Post
    Try to change it to 0.
    Code cpp:
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,1);
    I tried, but it is the same. Thing is that I have in vertices x and y position coordinate and z is another attribute variable used for computing something in vertex shader for each position.

  6. #6
    Junior Member Regular Contributor
    Join Date
    May 2013
    Posts
    130
    Quote Originally Posted by MirzaSe View Post
    Thing is that I have in vertices x and y position coordinate and z is another attribute variable used for computing something in vertex shader for each position.
    I have no idea what you are talking about.

    But here is how I understand it:
    You have your data defined like this:
    Code cpp:
    #pragma pack(push)
    #pragma pack(1)
    struct xy_dist
    {
        float x;
        float y;
        float dist;
    };
    #pragma pack(pop)
     
    vector<xy_dist> vertices;
    vector<char> indices;

    Then your vertex layout code should look like this:
    Code cpp:
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glEnableVertexAttribArray(m_posAttr);
    glEnableVertexAttribArray(m_distLocation);
     
    int      posAttrSize           = sizeof(float) * 2;
    int      distLocationAttrSize  = sizeof(float) * 1;
    GLintptr posAttrStart          = reinterpret_cast<GLintptr>(&vertices[0]);
    GLintptr distLocationAttrStart = posAttrStart + posAttrSize;
    int      stride                = posAttrSize + distLocationAttrSize;
     
    glVertexAttribPointer(m_posAttr,      2, GL_FLOAT, GL_FALSE, stride, reinterpret_cast<GLvoid*>(posAttrStart));
    glVertexAttribPointer(m_distLocation, 1, GL_FLOAT, GL_FALSE, stride, reinterpret_cast<GLvoid*>(distLocationAttrStart));
     
    glBindAttribLocation(m_program.programId(), m_posAttr, "posAttr" );
    glBindAttribLocation(m_program.programId(), m_distLocation, "distance" );
     
    glUniform1i(m_patternLocation, -1);
    glUniform4fv(m_colorAttr, 1, vColor);
    glUniform1f(m_depthAttr, 0.2f);
     
    glDrawElements(GL_TRIANGLES,indices.size(), GL_UNSIGNED_SHORT, &indices[0] );

    In your original code the second glVertexAttribPointer still locked more like what you would use with a VBO. But it needs an exact memory address and not only the offset. Also the stride must be the same value. But I'm still not sure if you want the pos and distLocation from the same array. Because in that case you could just use a single attribute instead of two.

  7. #7
    Junior Member Newbie
    Join Date
    Aug 2014
    Posts
    16
    This is exacltly what I wanted. Thank you a lot. This was very helpful. Thank you.

Tags for this Thread

Posting Permissions

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