Results 1 to 1 of 1

Thread: Pass two attributes into vertex shader(Qt5.0.2)

  1. #1
    Join Date
    Jan 2011
    Posts
    127
    Thanks
    42
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: 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
    Qt Code:
    1. attribute vec4 position;
    2. attribute vec4 color;
    3.  
    4. varying vec4 theColor;
    5.  
    6. void main() {
    7. gl_Position = position;
    8. theColor = color;
    9. //theColor = vec4(0.5, 0.8, 0.3, 1.0);
    10. }
    To copy to clipboard, switch view to plain text mode 

    fragment shader
    Qt Code:
    1. varying vec4 theColor;
    2.  
    3. void main()
    4. {
    5. gl_FragColor = theColor;
    6. }
    To copy to clipboard, switch view to plain text mode 

    initialize buffer

    Qt Code:
    1. std::vector<GLfloat> const vertexPositions{
    2. 0.75f, 0.75f, 0.0f, 1.0f,
    3. 0.75f, -0.75f, 0.0f, 1.0f,
    4. -0.75f, -0.75f, 0.0f, 1.0f,
    5. 1.0f, 0.0f, 0.0f, 1.0f,
    6. 0.0f, 1.0f, 0.0f, 1.0f,
    7. 0.0f, 0.0f, 1.0f, 1.0f
    8. };
    9.  
    10. positionBufferObject.push_back(0);
    11.  
    12. glGenBuffers(1, &positionBufferObject.back());
    13. glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject.back());
    14.  
    15. glBufferData(GL_ARRAY_BUFFER, buffer.size() * sizeof(GLfloat), &buffer[0], GL_STATIC_DRAW);
    16. glBindBuffer(GL_ARRAY_BUFFER, 0);
    To copy to clipboard, switch view to plain text mode 

    paintGL

    Qt Code:
    1. void ch2FragPosition::paintGL()
    2. {
    3. glClear(GL_COLOR_BUFFER_BIT);
    4. glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject[0]);
    5.  
    6. int vertexLocation = program.attributeLocation("position");
    7. program.enableAttributeArray(vertexLocation);
    8. glVertexAttribPointer(vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
    9.  
    10. int colorLocation = program.attributeLocation("color");
    11. program.enableAttributeArray(colorLocation);
    12. glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid const*)(12 * sizeof(GLfloat)));
    13.  
    14. glDrawArrays(GL_TRIANGLES, 0, 3);
    15.  
    16. glBindBuffer(GL_ARRAY_BUFFER, 0);
    17. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. attribute vec4 position;
    2. attribute vec4 color;
    3.  
    4. varying vec4 theColor;
    5.  
    6. void main() {
    7. gl_Position = position;
    8. //theColor = color;
    9. theColor = vec4(0.5, 0.8, 0.3, 1.0);
    10. }
    To copy to clipboard, switch view to plain text mode 

    How could I fix this problem?Thanks


    Added after 1 7 minutes:


    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

    Qt Code:
    1. std::unique_ptr<ch2FragPosition> ch2_frag_position(new ch2FragPosition(":ch2/modernOpenGLShader/ch2/positionVertex",
    2. ":ch2/modernOpenGLShader/ch2/positionFrag"));
    3. ch2_frag_position->show();
    To copy to clipboard, switch view to plain text mode 
    But it should be

    Qt Code:
    1. std::unique_ptr<ch1HelloTriangle> ch2_frag_position(new ch2FragPosition(":ch2/modernOpenGLShader/ch2/positionVertex",
    2. ":ch2/modernOpenGLShader/ch2/positionFrag"));
    3. ch2_frag_position->show();
    To copy to clipboard, switch view to plain text mode 
    Thanks for your helps
    Last edited by stereoMatching; 9th May 2013 at 03:33.

Similar Threads

  1. Replies: 8
    Last Post: 28th January 2014, 08:09
  2. Replies: 0
    Last Post: 16th April 2012, 22:58
  3. QGLBuffer instead of Vertex Buffer Object -> How To?
    By FlashMuller in forum Qt Programming
    Replies: 0
    Last Post: 18th November 2010, 11:41
  4. OpenGL Vertex Buffer Array/Object
    By Denarius in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2010, 08:59
  5. Replies: 1
    Last Post: 9th November 2009, 14:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.