Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: how to bind two vertex buffers to vertex shader input, as in DirectX

  1. #1

    how to bind two vertex buffers to vertex shader input, as in DirectX

    I am porting a code from DirectX/HLSL to OpenGL/GLSL. I am stuck with inputting two vertex buffers into vertex shader. My code in HLSL is as below:

    HLSL:
    Code :
    float4 vs_main(float4 pos0 : POSITION0, float4 pos1 : POSITION1)
    {
       return something;
    }


    I need to get two positions, two texture coords and ... and i need the GLSL and C++ code to bind the vertex buffers. I googled this and searched my opengl book, but no luck.

    Any help is appreciated.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    I assume you mean that one vertex will have 2 positions, 2 texture coordinates etc associated with it.

    You do this when you construct the vertex array object.

    Code :
    glGenVertexArrays(1, &VAO_ID);
    glBindVertexArray(VAO_ID);
     
    glBindBuffer(GL_ARRAY_BUFFER,buffer_0_id);
     glVertexAttribPointer(POSITION_0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
    ...
     
     
    glBindBuffer(GL_ARRAY_BUFFER,buffer_1_id);
     glVertexAttribPointer(POSITION_1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
    ...

    Code :
    layout(location = POSITION_0) in  vec3  Position0;
    layout(location = POSITION_1) in  vec3  Position1;

  3. #3
    thank you so much, I will do test it tomorrow.

  4. #4
    Quote Originally Posted by tonyo_au View Post
    I assume you mean that one vertex will have 2 positions, 2 texture coordinates etc associated with it.

    You do this when you construct the vertex array object.

    Code :
    glGenVertexArrays(1, &VAO_ID);
    glBindVertexArray(VAO_ID);
     
    glBindBuffer(GL_ARRAY_BUFFER,buffer_0_id);
     glVertexAttribPointer(POSITION_0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
    ...
     
     
    glBindBuffer(GL_ARRAY_BUFFER,buffer_1_id);
     glVertexAttribPointer(POSITION_1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
    ...

    Code :
    layout(location = POSITION_0) in  vec3  Position0;
    layout(location = POSITION_1) in  vec3  Position1;
    glVertexAttribPointer(POSITION_0, 3, GL_FLOAT, GL_FALSE, sizeof(vertices), 0);
    POSITION_0 & POSITION_1 are not defined. Only GL_POSITION exists!

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,128
    POSITION_0 & POSITION_1 are not defined. Only GL_POSITION exists!
    Hold on. First of all, of course POSITION_0 and POSITION_1 aren't defined. They're supposed to be integers in the range [0 .. MAX_VERTEX_ATTRIBS]. Therefore, if you add

    Code :
    #define POSITION_0 0
    #define POSITION_1 1

    to your shader is good. Of course, you need to make exactly the same definition in your application, or otherwise you will have potential mismatches between what the shader defines and what your application uses as arguments to glVertexAttribPointer.

    EDIT: BTW, GL_POSITION doesn't exist either. gl_Position does. Remember, we're very case sensitive about such things.

  6. #6
    Thanks. I get the index of variables vertex and colors using the following function:
    shaderProgram->setAttributeLocation("vertex")
    I think the logic of my code is correct, However what it draws is absolutely garbage.

    Code :
    void PlanPositionIndicator::render(QGLShaderProgram* shaderProgram)
    {
        if(!initialized)
        {
            GLuint vertexId = shaderProgram->attributeLocation("vertex");
     
            GLuint colorId  = shaderProgram->attributeLocation("color");
     
            glGenVertexArrays(1, &VAO_ID);
            glBindVertexArray(VAO_ID);
     
            glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[0]);
            glVertexAttribPointer(vertexId, 2, GL_FLOAT, GL_FALSE, sizeof(vertices), 0);
            glBufferData(GL_ARRAY_BUFFER, sizeof(QVector2D)*vertices.size(), vertices.constData(), GL_STATIC_DRAW);
     
            glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[1]);
            glVertexAttribPointer(colorId, 1, GL_FLOAT, GL_FALSE, sizeof(vertices), 0);
            glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vertices.size(), colors, GL_STATIC_DRAW);
     
            initialized = 1;
        }
     
        glBindBuffer(GL_ARRAY_BUFFER, VAO_ID);
     
        //    shaderProgram->setAttributeArray("vertex", vertices.constData());
     
        int vertexLocation = shaderProgram->attributeLocation("vertex");
     
     
        //shaderProgram->setAttributeArray("color", GL_FLOAT,colors,1);
     
        shaderProgram->enableAttributeArray(vertexLocation);
     
    //    glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, sizeof(vertices), NULL);
     
        int colorLocation = shaderProgram->attributeLocation("color");
     
        shaderProgram->enableAttributeArray(colorLocation);
     
    //    glVertexAttribPointer(colorLocation, 3, GL_FLOAT, GL_FALSE, sizeof(colors), (const char*)(sizeof(float)*3));
     
    //    glEnable    (GL_BLEND);
     
    //    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     
        glDrawArrays(GL_TRIANGLES, 0, numVerts);
    }

    Does anyone have any idea?

  7. #7
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    455
    You shouldn't have the line:
    Code :
    glBindBuffer(GL_ARRAY_BUFFER, VAO_ID);
    It should be:
    Code :
    glBindVertexArray(VAO_ID);

    Also, is your shader program compiled + linked successfully prior to querying attribute locations?

  8. #8
    Click image for larger version. 

Name:	Screenshot from 2013-06-29 13:57:55.jpg 
Views:	69 
Size:	11.8 KB 
ID:	1063
    Code :
        if(!initialized)
        {
            GLuint vertexId = shaderProgram->attributeLocation("vertex");
     
            GLuint colorId  = shaderProgram->attributeLocation("color");
     
            glGenVertexArrays(1, &VAO_ID);
            glBindVertexArray(VAO_ID);
     
            glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[0]);
            glVertexAttribPointer(vertexId, 2, GL_FLOAT, GL_FALSE, sizeof(vertices), 0);
            glBufferData(GL_ARRAY_BUFFER, sizeof(QVector2D)*vertices.size(), vertices.constData(), GL_STATIC_DRAW);
     
            glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[1]);
            glVertexAttribPointer(colorId, 1, GL_FLOAT, GL_FALSE, sizeof(vertices), 0);
            glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vertices.size(), colors, GL_STATIC_DRAW);
     
            initialized = 1;
        }
     
        //glBindBuffer(GL_ARRAY_BUFFER, VAO_ID);
        glBindVertexArray(VAO_ID);
     
        int vertexLocation = shaderProgram->attributeLocation("vertex");
        shaderProgram->enableAttributeArray(vertexLocation);
        int colorLocation = shaderProgram->attributeLocation("color");
        shaderProgram->enableAttributeArray(colorLocation);
     
        glDrawArrays(GL_TRIANGLES, 0, numVerts);

    I changed the function. thanks. Yes, I receive no error regarding the shader program not linked. Besides, the shader program was working properly without VBO.

    this is my shader program:
    Code :
    //#version 130
     
    precision mediump float;
     
    uniform mat4 mvpMatrix;
     
    attribute vec4 vertex;
    attribute float color;
     
    varying float varyingColor;
     
    void main(void)
    {
        varyingColor = color;
        gl_Position = mvpMatrix * vertex;
    }


    Code :
    //#version 130
     
    precision mediump float;
     
    varying float varyingColor;
     
    void main(void)
    {
        gl_FragColor = vec4(varyingColor * vec3(0, 1, 0), 0.5);
    }

    The program should draw a ppi, but what it draws is quite irrelevant, I attached a screen shot.
    Last edited by saman_artorious; 06-29-2013 at 02:29 AM.

  9. #9
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    I notice you have an alpha value - have you enabled blending?

  10. #10
    Quote Originally Posted by tonyo_au View Post
    I notice you have an alpha value - have you enabled blending?
    Even if I enable it, it has no effect:
    Code :
        glEnable    (GL_BLEND);
        glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glDrawArrays(GL_TRIANGLES, 0, numVerts);

Posting Permissions

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