0

试图弄清楚为什么我的程序中附加的着色器为零。我遇到了另一个问题。我的片段和顶点着色器等于零(它们为 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 中打印出片段和顶点着色器,所以我知道我的路径是正确的。

我不知道为什么我的顶点和片段着色器等于零。我在这个程序上花了很多时间,我不想重新开始。

任何帮助都非常感谢!

谢谢!

4

1 回答 1

0

如此处所示 - http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/DeterminingOpenGLESCapabilities/DeterminingOpenGLESCapabilities.html#//apple_ref/doc/uid/TP40008793-CH102-SW1

在使用扩展程序之前检查它们

编码(对于着色器文件)是 ASCII 还是 UTF-8?

调用 glGetError 来测试错误

因为您的代码看起来完全正常,请尝试调用glGetError(...)代替NSLog(...)以查找有关错误的更多信息(如果有的话)。

于 2013-07-17T03:58:38.030 回答