Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OpenGL:是否需要启用所有顶点属性?_Opengl_Attributes_Vertex - Fatal编程技术网

OpenGL:是否需要启用所有顶点属性?

OpenGL:是否需要启用所有顶点属性?,opengl,attributes,vertex,Opengl,Attributes,Vertex,我阅读了本教程,并决定尝试一下代码。总之,本教程解释了如何将顶点颜色和坐标传递给顶点和片段着色器,以便在三角形上添加一些颜色 以下是显示功能的代码(来自教程中的一些更改),其工作原理与预期一致: void display() { // cleaning screen glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); //using expected program and bi

我阅读了本教程,并决定尝试一下代码。总之,本教程解释了如何将顶点颜色和坐标传递给顶点和片段着色器,以便在三角形上添加一些颜色

以下是显示功能的代码(来自教程中的一些更改),其工作原理与预期一致:

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();
}
现在如果取消对该行的注释 //glDisableVertexAttributeArray(0);
在将数据设置为属性1之前,此操作不再有效。为什么呢?另外,我不明白为什么属性0可以在不启用1的情况下设置,反之亦然。顺便问一下,启用/禁用顶点属性有什么用处?我的意思是,您(至少我)可能会最终启用所有顶点属性,因此为什么默认情况下它们处于禁用状态?

glDrawArrays
调用中,当前启用的属性将被读取并传递给渲染器。如果在此之前禁用它们,则它们将无法从缓冲区中传递

可能有很多潜在属性可用(最多
glGetIntegerv(GL\u MAX\u VERTEX\u ATTRIBS,&result);
至少16个),大多数应用程序不需要那么多


着色器中的
position
属性设置为index
0
,如果未指定该属性,则着色器将获取具有相同位置(通常为0,0,0,1)的所有点。而索引1是颜色数据,如果没有,那也没什么大不了的。

事实上,GlenableVertexAttributeArray似乎只需要在GLDrawArray之前调用。从教程中,我认为glEnableVertexAttribArray是使glVertexAttribPointer工作的必需条件,但事实显然并非如此