试图弄清楚为什么我的程序中附加的着色器为零。我遇到了另一个问题。我的片段和顶点着色器等于零(它们为 NULL)
NSString *vertexShaderPath = [[NSBundle mainBundle] pathForResource:@"VertexShader" ofType:@"glsl"];
NSString *vertexShaderString = [NSString stringWithContentsOfFile:vertexShaderPath encoding:NSUTF8StringEncoding error:nil];
const char * vertexShaderCString = [vertexShaderString cStringUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%s",vertexShaderCString);
NSString *fragmentShaderPath = [[NSBundle mainBundle]pathForResource:@"FragmentShader" ofType:@"glsl"];
NSString *fragmentShaderString = [NSString stringWithContentsOfFile:fragmentShaderPath encoding:NSUTF8StringEncoding error:nil];
const char* fragmentShaderCString = [fragmentShaderString cStringUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%s", fragmentShaderCString);
int vertexShaderCStringLength = strlen(vertexShaderCString);
NSLog(@"%i",vertexShaderCStringLength);
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderCString, &vertexShaderCStringLength);
glCompileShader(vertexShader);
int fragmentShaderCStringLength = strlen(fragmentShaderCString);
NSLog(@"%i",fragmentShaderCStringLength);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderCString, &fragmentShaderCStringLength);
glCompileShader(fragmentShader);
programHandle = glCreateProgram();
glAttachShader(programHandle, vertexShader);
glAttachShader(programHandle, fragmentShader);
glLinkProgram(programHandle);
glGetProgramiv(programHandle, GL_ATTACHED_SHADERS, &attachedShaders);
NSLog(@"Attched shaders: %i",attachedShaders);
if (fragmentShader == 0 ) {
NSLog(@"Some sort of fragment error");
}
if(vertexShader == 0){
NSLog(@"Some sort of vertex error");
}
上面的代码在一个 GLKViewController 的 viewDidLoad 中。在我进行编码寻找编译错误(为了便于阅读而删除)之前 - 没有这些错误,而且我还在 NSLog 中打印出片段和顶点着色器,所以我知道我的路径是正确的。
我不知道为什么我的顶点和片段着色器等于零。我在这个程序上花了很多时间,我不想重新开始。
任何帮助都非常感谢!
谢谢!