C++ 当不使用某些顶点属性时,GLD元素崩溃

C++ 当不使用某些顶点属性时,GLD元素崩溃,c++,opengl,shader,vertex-attributes,C++,Opengl,Shader,Vertex Attributes,在我的OpenGL程序中,我有两个着色器。一个渲染纹理,另一个渲染纯色。编译和链接着色器后,我会根据天气启用纹理坐标顶点属性数组,这取决于着色器是否包含该属性 //This code is called after the shaders are compiled. //Get the handles textureU = glGetUniformLocation(program,"texture"); tintU = glGetUniformLocation(program,"tint");

在我的OpenGL程序中,我有两个着色器。一个渲染纹理,另一个渲染纯色。编译和链接着色器后,我会根据天气启用纹理坐标顶点属性数组,这取决于着色器是否包含该属性

//This code is called after the shaders are compiled.

//Get the handles
textureU = glGetUniformLocation(program,"texture");
tintU = glGetUniformLocation(program,"tint");
viewMatrixU = glGetUniformLocation(program,"viewMatrix");
transformMatrixU = glGetUniformLocation(program,"transformMatrix");
positionA = glGetAttribLocation(program,"position");
texcoordA = glGetAttribLocation(program,"texcoord");

//Detect if this shader can handle textures
if(texcoordA < 0 || textureU < 0) hasTexture = false;
else hasTexture = true;

//Enable Attributes
glEnableVertexAttribArray(positionA);
if(hasTexture) glEnableVertexAttribArray(texcoordA);
下面是上面看到的设置着色器的函数

glUseProgram(program); currentShader = program;
GLsizei stride = 12;
if(hasTexture) stride = 20;
glVertexAttribPointer(positionA,3,GL_FLOAT,GL_FALSE,stride,0);
if(hasTexture)
    glVertexAttribPointer(texcoordA,2,GL_FLOAT,GL_FALSE,stride,(void*)12);
据我所知,这个问题在英特尔集成显卡上并不明显,它似乎相当宽容


编辑:如果有必要知道,我正在使用GLFW和GLEW。

尝试在draw调用后添加相应的
GLDisableVertexAttributeArray()
s:

glEnableVertexAttribArray(positionA);
if(hasTexture) glEnableVertexAttribArray(texcoordA);
// draw
glDisableVertexAttribArray(positionA);
if(hasTexture) glDisableVertexAttribArray(texcoordA);

问题是我在编译着色器后启用了顶点属性数组。我不应该在这里启用它们

我在编译着色器时启用了texcoord属性,但由于第一项没有使用它,因此glDrawerElements会出错

我通过在设置着色器时启用属性修复了此问题

glUseProgram(program); currentShader = program;
GLsizei stride = 12;
if(hasTexture) stride = 20;
glVertexAttribPointer(positionA,3,GL_FLOAT,GL_FALSE,stride,0);
if(hasTexture)
    glVertexAttribPointer(texcoordA,2,GL_FLOAT,GL_FALSE,stride,(void*)12);