سؤال

I read this tutorial arc synthesis, vertex attributes, chapter 2 playing with colors and decided to play around with the code. To sum up, this tutorial explain how to pass vertices colors and coordinates to vertex and fragment shaders to put some color on a triangle.

Here is a code of the display function (from the tutorial with some changes) that works as expected :

void display()
{
    // cleaning screen
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    //using expected program and binding to expected buffer
    glUseProgram(theProgram);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

    //setting data to attrib 0
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
    //glDisableVertexAttribArray(0); // if uncommenting this out, it does not work anymore

    //setting data to attrib 1
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)48);

    //cleaning and rendering
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glUseProgram(0);

    glfwSwapBuffers();
}

Now if uncommenting the line //glDisableVertexAttribArray(0); before setting data to attribute 1 this does not work anymore. Why is that ? Plus, I don't get it why attribute 0 can be set without 1 enabled and not the contrary. By the way, what is the usefulness of enabling/disabling vertices attributes ? I mean you (at least I) will probably end up enabling all vertices attributes so why they are off by default ?

هل كانت مفيدة؟

المحلول

It's at the glDrawArrays call that currently enabled attributes are read and passed to the renderer. If you disable them before that then they won't get passed from your buffer.

There can be a lot of potential attributes available (up to glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &result); at least 16) and most applications don't need that many.

The position attribute in the shader is set to index 0 and if it doesn't get assigned then the shader gets all points with the same location (typically 0,0,0,1). Whereas index 1 is the color data and if that is missing it's not a big deal.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top