[Solved]Pass two attributes into vertex shader(Qt5.0.2)



  • platform : mac osx 10.8.3
    compiler : clang3.2

    Learning opengl from this website(ch2, playing with colors)
    "opengl":http://www.arcsynthesis.org/gltut/

    Trying to pass my data into two attributes, but the second attributes can't have the data correctly

    vertex shader
    @attribute vec4 position;
    attribute vec4 color;

    varying vec4 theColor;

    void main() {
    gl_Position = position;
    theColor = color;
    //theColor = vec4(0.5, 0.8, 0.3, 1.0);
    }@

    fragment shader
    @varying vec4 theColor;

    void main()
    {
    gl_FragColor = theColor;
    }@

    initialize buffer

    @std::vector<GLfloat> const vertexPositions{
    0.75f, 0.75f, 0.0f, 1.0f,
    0.75f, -0.75f, 0.0f, 1.0f,
    -0.75f, -0.75f, 0.0f, 1.0f,
    1.0f, 0.0f, 0.0f, 1.0f,
    0.0f, 1.0f, 0.0f, 1.0f,
    0.0f, 0.0f, 1.0f, 1.0f
    };

    positionBufferObject.push_back(0);
    
    glGenBuffers(1, &positionBufferObject.back());
    glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject.back());
    
    glBufferData(GL_ARRAY_BUFFER, buffer.size() * sizeof(GLfloat), &buffer[0], GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);@
    

    paintGL

    @void ch2FragPosition::paintGL()
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject[0]);

    int vertexLocation = program.attributeLocation("position");
    program.enableAttributeArray(vertexLocation);
    glVertexAttribPointer(vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
    
    int colorLocation = program.attributeLocation("color");
    program.enableAttributeArray(colorLocation);
    glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid const*)(12 * sizeof(GLfloat)));
    
    glDrawArrays(GL_TRIANGLES, 0, 3);
    
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    

    }@

    The QGLWidget show me a black window, if I alter the vertex shader like these
    QGLWidget will show me a triangle with color

    @attribute vec4 position;
    attribute vec4 color;

    varying vec4 theColor;

    void main() {
    gl_Position = position;
    //theColor = color;
    theColor = vec4(0.5, 0.8, 0.3, 1.0);
    }@

    How could I fix this problem?Thanks


  • Moderators

    Unless I miss something - you initialize the positionBufferObject with the vertex positions array but then specify the attribute pointer of the colors to point into the same array starting past the vertices?

    The fix is to actually put the color data into the array.



  • [quote author="Chris Kawa" date="1368062238"]Unless I miss something - you initialize the positionBufferObject with the vertex positions array but then specify the attribute pointer of the colors to point into the same array starting past the vertices?

    The fix is to actually put the color data into the array.[/quote]

    Yes, this is the way the website do it, I only follow the instruction.
    I edited the question because I forgot to put the color into the vector.
    Besides, put the color into the vector do not fix the problem, I don't know why
    looks like the color attribute do not receive the data correctly


  • Moderators

    Ok, this might be stupid question but are you enabling the shader via glUseProgram?
    If yes then maybe check if the returned vertexLocation and colorLocation are valid (>=0).



  • [quote author="Chris Kawa" date="1368064340"]Ok, this might be stupid question but are you enabling the shader via glUseProgram?
    If yes then maybe check if the returned vertexLocation and colorLocation are valid (>=0).[/quote]

    The question is not stupid, I am a beginner of opengl, it is possible for me to commit even the most
    impossible mistake.

    About your question, I enable it already, the vertexLocation and colorLocation are valid(>=0).

    Edit : I found the problem already, it has nothing to do with the opengl, I forgot to point the
    class by a proper base class.

    base class is : ch1HelloTriangle
    derived class : ch2FragPosition

    I declared the class liked this

    @std::unique_ptr<ch2FragPosition> ch2_frag_position(new ch2FragPosition(":ch2/modernOpenGLShader/ch2/positionVertex",
    ":ch2/modernOpenGLShader/ch2/positionFrag"));
    ch2_frag_position->show();@

    But it should be

    @std::unique_ptr<ch1HelloTriangle> ch2_frag_position(new ch2FragPosition(":ch2/modernOpenGLShader/ch2/positionVertex",
    ":ch2/modernOpenGLShader/ch2/positionFrag"));
    ch2_frag_position->show();@

    Thanks for your helps(as you wonder, a really stupid mistake)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.