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

Thread: glEnableVertexAttribArray

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2013
    Location
    Paris
    Posts
    17

    glEnableVertexAttribArray

    Hi,
    I am trying to render using VAO, everything works fine but I just want to understand something.
    When I initialize my opengl context using Qt I do also create a VAO this way :

    Code :
        glGenVertexArrays(1, &vao);
    	glGenBuffers(1, &vertexBuffer);
    	glGenBuffers(1, &indexBuffer);
     
    	glBindVertexArray(vao);
    		glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    		glBufferData(GL_ARRAY_BUFFER, 3*vertices.size() * sizeof(GL_FLOAT), vertices.constData(), GL_STATIC_DRAW);
    		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    		glEnableVertexAttribArray(0);
    		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    		glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3*indices.size() * sizeof(GL_FLOAT), indices.constData(), GL_STATIC_DRAW);
    	glBindVertexArray(0);

    You see that glEnableVertexAttribArray(0); is already there.
    However when I want to render (after compiling, linking my shader), I have to use this code in order to have something rendered at my screen.

    Code :
    	shaderProgram.bind();
    		//UNIFORMS
    		shaderProgram.setUniformValue("mvpMatrix", pMatrix * view * model);
    		shaderProgram.setUniformValue("color", QColor(255,244,255));
    		//ARRAYS
    		//VERTEX ARRAY
    		glBindVertexArray(vao);
    			glEnableVertexAttribArray(0);
    			glDrawElements(rendStyle, indices.size(), GL_UNSIGNED_INT, 0);
    		glDisableVertexAttribArray(0);
    	//RELEASE SHADER
    	shaderProgram.release();

    So there is a repetition of glEnableVertexAttribArray(0), If I remove this latter from the rendering part nothing is drawn at the screen.

    Now, I don't understand why I have to use glEnableVertexAttribArray(0) in the rendering part since it was already set up in the vao (initilization part).

    Thanks.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    do not call
    Code :
     
    glDisableVertexAttribArray(0);

    AFAIK this will change the state in the currently bound VAO.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2013
    Location
    Paris
    Posts
    17
    Quote Originally Posted by tonyo_au View Post
    do not call
    Code :
     
    glDisableVertexAttribArray(0);

    AFAIK this will change the state in the currently bound VAO.
    What should I use then ?
    I have to use it because otherwise it will not render at all.
    Last edited by holyll; 01-16-2013 at 07:51 AM.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948
    His point is that you shouldn't be calling either of the Enables or the Disables in your rendering function. Just bind the VAO and go.

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2013
    Location
    Paris
    Posts
    17
    If I do so I have nothing on the screen.

    I am using Qt.
    This is the content of my initiliaze function :

    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
    /* Problem: glewInit failed, something is seriously wrong. */
    qDebug() << glewGetErrorString(err);
    }
    qDebug() << "GLEW VERSION" << glewGetString(GLEW_VERSION);


    qglClearColor(QColor(163,73,163)); // Choose clear Color
    //Shader add, compile and link
    shaderProgram.addShaderFromSourceFile(QGLShader::V ertex, "vertex.txt");
    shaderProgram.addShaderFromSourceFile(QGLShader::F ragment, "fragment.txt");
    shaderProgram.link();

    for(int i=0 ; i < N; i++)
    {
    for(int j=0; j<N; j++)
    {
    QVector3D vertice = QVector3D(i-N/2,0,j-N/2);
    vertices << vertice;
    }
    }

    initIndices();
    //VAO, VBO Part
    glGenVertexArrays(1, &vao);
    glGenBuffers(1, &vertexBuffer);
    glGenBuffers(1, &indexBuffer);

    glBindVertexArray(vao);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, 3*vertices.size() * sizeof(GL_FLOAT), vertices.constData(), GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3*indices.size() * sizeof(GL_FLOAT), indices.constData(), GL_STATIC_DRAW);
    glBindVertexArray(0);


    And this is the render part
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    view.setToIdentity();
    model.setToIdentity();
    model.rotate(xr / 16.0, 1.0, 0.0, 0.0);
    model.rotate(yr/ 16.0, 0.0, 1.0, 0.0);
    model.rotate(zr / 16.0, 0.0, 0.0, 1.0);
    view.translate(0.0, 0.0, distance);
    //Bind Shader
    shaderProgram.bind();
    //UNIFORMS
    shaderProgram.setUniformValue("mvpMatrix", pMatrix * view * model);
    shaderProgram.setUniformValue("color", QColor(255,244,255));
    //ARRAYS
    //VERTEX ARRAY
    glBindVertexArray(vao);
    glEnableVertexAttribArray(0);
    glDrawElements(rendStyle, indices.size(), GL_UNSIGNED_INT, 0);
    glDisableVertexAttribArray(0);

    //RELEASE SHADER
    shaderProgram.release();

    If I remove the blue line I have nothing on the screen... it just can't work without it.

  6. #6
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948
    Code :
    glDisableVertexAttribArray(0);

    You need to remove this line also.

    Oh, and why is it that, in your updated code, you removed the glEnableVertexAttribArray call from your initialize routine? That was correct; the enable state of an array is part of the VAO. You set it there, once, and you don't have to set it again.

Posting Permissions

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