我有一个带有多个使用 OpenGL ES 2.0 和自定义片段着色器的 OpenGL-ES 视图的 iPad 应用程序。目前,着色器已针对每个单独的 OpenGL 视图进行编译和链接。我想编译和链接一次着色器,并为每个视图重新使用它们。从理论上讲,这应该只是一个调用的问题
gluseProgram(gProgramHandle);
在我的 render() 方法中,在渲染之前,并加载一次 gProgramHandle,对吧?但这不起作用。当我切换到使用单个 gProgramHandle(在初始化时设置为 -1)时,只有一个 OpenGL 视图有效,其他视图显示为深绿色矩形。我究竟做错了什么?
- (void)loadShaders
{
if (gProgramHandle == -1)
{
NSLog(@"Compiling shaders...");
GLuint vertexShader = [self compileShader:@"AIMGsiVertexShader" withType:GL_VERTEX_SHADER];
GLuint fragmentShader = [self compileShader:@"AIMGsiFragmentShader" withType:GL_FRAGMENT_SHADER];
gProgramHandle = glCreateProgram();
glAttachShader(gProgramHandle, vertexShader);
glAttachShader(gProgramHandle, fragmentShader);
glLinkProgram(gProgramHandle);
GLint linkSuccess;
glGetProgramiv(gProgramHandle, GL_LINK_STATUS, &linkSuccess);
if (linkSuccess == GL_FALSE)
{
// If there was an error when compiling the gsls shaders, report the compile error and quit.
GLchar messages[256];
glGetProgramInfoLog(gProgramHandle, sizeof(messages), 0, &messages[0]);
NSString *messageString = [NSString stringWithUTF8String:messages];
NSLog(@"%@", messageString);
exit(1);
}
NSLog(@"Done compiling and linking shaders.");
}
// We can efficiently switch between shaders by calling glUseProgram() to use the program with the shaders we want to use.
glUseProgram(gProgramHandle);
// Gradient map values are sent in this vector: numValuesInCache, factor, offset
_gradientValsUniform = glGetUniformLocation(gProgramHandle, "GradientVals");
_numColorsInGradient = glGetUniformLocation(gProgramHandle, "NumColorsInCache");
_gradientColorsArray = glGetUniformLocation(gProgramHandle, "ColorsArray");
_positionSlot = glGetAttribLocation(gProgramHandle, "Position");
glEnableVertexAttribArray(_positionSlot);
_projectionUniform = glGetUniformLocation(gProgramHandle, "Projection");
_modelViewUniform = glGetUniformLocation(gProgramHandle, "Modelview");
_texCoordSlot = glGetAttribLocation(gProgramHandle, "TexCoordIn");
glEnableVertexAttribArray(_texCoordSlot);
_textureUniform = glGetUniformLocation(gProgramHandle, "Texture");
}