简体   繁体   中英

OpenGL ES 2 on iOS: NULL Fragment and Vertex Shaders

In attempt to figure out why I have zero attached shaders to my program. I have come across another problem. My fragment and vertex shaders are equal to zero (they're 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");
}

The above code is in the viewDidLoad of a GLKViewController. Before I had coding looking for compiling errors (removed for readability) -- which there were none and I am also printing out the Fragment and Vertex Shader in an NSLog, so I know my paths are correct.

I have no idea why my vertex and fragment shaders are equal to zero. I've spent so much time on this program and I don't want to start over.

Any help at all is extremely and gratefully appreciated!

Thanks!

As seen here - http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/DeterminingOpenGLESCapabilities/DeterminingOpenGLESCapabilities.html#//apple_ref/doc/uid/TP40008793-CH102-SW1

Check for Extensions Before Using Them

Is the encoding (for shader files) ASCII or UTF-8?

Call glGetError to Test for Errors

Because your code seems perfectly normal, try calling glGetError(...) in place of NSLog(...) to find more about errors (if there are any).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM