0

I'm trying to write a Wavefront OBJ file viewer in Objective-C, that is capable of loading meshes/materials/shaders from files. I've created classes for shaders and shader programs, and I'm trying to create an OpenGL shader program object as part of my shader program class's init method:

- (id)initWithVertexShader:(NSString *)vshader FragmentShader:(NSString *)fshader {
self = [super init];
if (self) {
    SRShader* shaders[2] = {
        [[SRShader alloc] initWithFilename:vshader Type:GL_VERTEX_SHADER Source:nil],
        [[SRShader alloc] initWithFilename:fshader Type:GL_FRAGMENT_SHADER Source:nil]
    };

    program = glCreateProgram();

    for (int i = 0; i < 2; i++) {
        SRShader* s = shaders[i];
        NSError* e = nil;
        s.source = [NSString stringWithContentsOfFile:s.filename encoding:NSUTF8StringEncoding error:&e];
        if (!e) {
            NSLog(@"Failed to read shader file: %@\n", s.filename);
            exit(-1);
        }

        GLuint shader = [s compile];
... and so on.

However, calling glCreateProgram results in EXC_BAD_ACCESS, as does the call to [SRShader compile], which in turn calls glCreateShader. Does anyone know of any issues with these function calls in Objective-C? Maybe something to do with ARC or calling them in an initialization function?

4

2 回答 2

5

如果您使用的是 NSOpenGLView,则需要在调用之前添加以下代码行,因此如果您从 Interface Builder 加载视图,则glCreateProgram()可能是这样:-(id)initWithCoder

NSOpenGLContext* context = [self openGLContext];

[context makeCurrentContext];

非常基本,我知道这个帖子已经过时了,但是当他们谷歌并找到这个帖子(就像我刚才所做的那样)时,也许它会节省一两分钟。

于 2013-01-22T00:04:15.410 回答
0

正如 Trevor 在另一篇文章中回答的那样,您应该初始化 glew。

glewInit();

答案来源: https ://gamedev.stackexchange.com/a/22788

于 2022-01-31T17:19:44.273 回答