iOS上的OpenGL ES 2:NULL片段和顶点着色器

时间:2013-07-16 21:57:14

标签: ios opengl-es opengl-es-2.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中打印出片段和顶点着色器,所以我知道我的路径是正确的。

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

任何帮助都非常值得感谢!

谢谢!

1 个答案:

答案 0 :(得分: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(...)以查找有关错误的更多信息(如果有的话)。