Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Pass two attributes into vertex shader(Qt5.0.2)
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stereomatching
    wrote on 9 May 2013, 00:48 last edited by
    #1

    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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 9 May 2013, 01:17 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stereomatching
        wrote on 9 May 2013, 01:23 last edited by
        #3

        [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

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 9 May 2013, 01:52 last edited by
          #4

          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).

          1 Reply Last reply
          0
          • S Offline
            S Offline
            stereomatching
            wrote on 9 May 2013, 02:30 last edited by
            #5

            [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)

            1 Reply Last reply
            0

            1/5

            9 May 2013, 00:48

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved