在我運行在iPad模擬器上的iOS項目中,我有3個着色器程序,每個着色器程序使用類似但不相同的屬性和制服集。前兩種着色器編譯和工作完美,在編譯或繪圖過程中任何時候都不會出現gl錯誤。我現在需要添加第三個着色器,並且這引起了一個問題:現在,其他着色器都不繪製任何東西,並且當我嘗試使用glUniform傳遞制服時,glGetError返回1282(GL_INVALID_OPERATION)。 glGetError在glUniform被調用之前返回0行。編譯GLSL着色器打破其他着色器
我相信在我嘗試調用glUniform之前將程序設置爲正確的程序,因爲當我在傳遞制服之前使用GL_CURRENT_PROGRAM調用glGetIntegerv時,它匹配我嘗試使用的程序的gl名稱(I調用glUseProgram與我想要在glUniform調用問題的方法開始時使用的程序)。 glGetError在編譯問題着色器之前和之後返回0。編輯代碼從Apple的示例項目中略有修改,我沒有看到任何內容會影響其他着色器的狀態。
- (BOOL)loadGLProgramWithVertexShader: (NSString*) vertexShaderName fragmentShader: (NSString*) fragementShaderName
{
GLuint vertShader, normFragShader;
NSString *vertShaderPathname, *normFragShaderPathName;
// Create shader program.
shaderProgram = glCreateProgram();
// Create and compile vertex shader.
vertShaderPathname = [[NSBundle mainBundle] pathForResource: vertexShaderName ofType:@"vsh"];
if (![self compileShader:&vertShader type:GL_VERTEX_SHADER file:vertShaderPathname]) {
NSLog(@"Failed to compile vertex shader");
return NO;
}
// Create and compile normal mapping fragment shader.
normFragShaderPathName = [[NSBundle mainBundle] pathForResource: fragementShaderName ofType:@"fsh"];
if (![self compileShader:&normFragShader type:GL_FRAGMENT_SHADER file:normFragShaderPathName]) {
NSLog(@"Failed to compile fragment shader");
return NO;
}
// Attach vertex shader to program.
glAttachShader(shaderProgram, vertShader);
// Attach fragment shader to program.
glAttachShader(shaderProgram, normFragShader);
// Bind attribute locations.
// This needs to be done prior to linking.
glBindAttribLocation(shaderProgram, ATTRIB_VERTEX, "position");
glBindAttribLocation(shaderProgram, ATTRIB_TEXTURECOORD, "texCoord");
// Link program.
if (![self linkProgram:shaderProgram]) {
NSLog(@"Failed to link program: %d", shaderProgram);
if (vertShader) {
glDeleteShader(vertShader);
vertShader = 0;
}
if (normFragShader) {
glDeleteShader(normFragShader);
normFragShader = 0;
}
if (shaderProgram) {
glDeleteProgram(shaderProgram);
shaderProgram = 0;
}
return NO;
}
// Get uniform locations.
uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX] = glGetUniformLocation(shaderProgram, "modelViewProjectionMatrix");
uniforms[UNIFORM_TEXTURE] = glGetUniformLocation(shaderProgram, "texture");
// Release vertex and fragment shaders.
if (vertShader) {
glDetachShader(shaderProgram, vertShader);
glDeleteShader(vertShader);
}
if (normFragShader) {
glDetachShader(shaderProgram, normFragShader);
glDeleteShader(normFragShader);
}
return YES;
}