views:

52

answers:

1

I am a bit of an openGL novice and I am trying to get texture sharing to work between two openGL views. I created two NSOpenGL view classes and then in the interface builder I created a window that had one of each openGL view. The first view has code in its prepareOpenGl that sets up the texture and assigns it to a global variable, the second view re-intializes its openGlContext to be one that is shared with the orginal view that created the texture, then in the prepareOpenGL of the second view I call glBindTexture on the global texture. When I run my application I only see a texture on the first view and the second view just shows an untextured object. It seems my texture sharing is not working. I am wondering if my general strategy is flawed or if I am just missing a little detail. I have included what seems to be the most important code snipets.

-(void)prepareOpenGL {
    //prepareOPenGL of the first view
    glEnable( GL_TEXTURE_2D );
    glEnable(GL_DEPTH_TEST);
    //makeImage() creates a silly little texture pattern
    makeImage();
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures(1, &texName);
    glBindTexture(GL_TEXTURE_2D, &texName);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    glShadeModel(GL_SMOOTH);
}
-(void)prepareOpenGL {
    //prepareOpenGL of the second view
    glEnable( GL_TEXTURE_2D );
    glEnable(GL_DEPTH_TEST);
    glBindTexture(GL_TEXTURE_2D, &texName);
    glShadeModel(GL_SMOOTH);
}
- (id)initWithFrame:(NSRect)frame { 
    if(self = [super initWithFrame:frame])
   {
            printf("InitWithFrame: \n");
            NSOpenGLContext* context = [[[NSOpenGLContext alloc] initWithFormat:[NSOpenGLView defaultPixelFormat] shareContext:[otherView openGLContext]] autorelease];
           [self setOpenGLContext:context];
    }
    return self;
}

When I refer to the "second view" I mean the one the is trying to use the texture of the original view.

A: 

Don't send an init message to an object you didn't just get from alloc.

What you're doing there is receiving a context that has already been initialized, and then re-initializing it. That's bad: Most, probably all, classes do not expect to be re-initialized, so this is likely to cause memory leaks.

What you should do is create the context yourself using alloc, initWithFormat:shareContext:, and autorelease, and set it as your context.

I have no way of knowing whether this will completely solve your problem, but it'll help, and it will at least cut out some possible memory leaks that you have in your current code.

You're also doing your initWithFrame: implementation wrong. See this blog post and my comment on it.

More generally, I think you need to re-read the Objective-C Programming Language and Cocoa Fundamentals Guide documents. The Objective-C document, in particular, covers init methods in one of its chapters.

Peter Hosey
Thank you Peter, both your suggestion and the blog you linked too were very helpful. I made the changes in my code and my included snipets. Unfortunately I still get the same behavior
Mike2012