0
votes

I have an existing openGL context, using an OpenGL 2.1 core profile. I am able to draw objects/textures/etc no problem. However, now I want to be able to have my application to launch a separate NSWindow, with an NSOpenGLView, that displays part of a texture I drew in the original renderer's view. After some reading, I eventually bumped into the topic of context sharing, which I think may be the route I have to take if I want to pull this off.

My shared openGL context is of type - CGLContextObj, but I don't know what to do with it as my window resides in a different process. I've read the Apple documentation on rendering contexts, but I am unable to apply the concepts they laid out if there's barely any examples for me to go through. Any advice will be really appreciated, thank you in advance.

EDIT:

Perhaps I did not give enough description, my apologies. I subclass my NSOpenGLView, and it's init I do the following:

// *** irrelevant initialization stuff above inside init *** //

// Get pixel format from first context to be used for NSOpenGLView when it's finally initialized later
_pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:(void*)_attribs];

// We will create CGPixelFormatObj from our C array of pixel format ttributes
GLint nPix;
CGPixelFormatObj myCgPixObj;
CGLChoosePixelFormat(_attribs, &myCgPixOPbj, &nPix);

// Now that we have the pixel format in CGPixelFormatObj form, create CGLContextObj to be passed in later when we init NSOpenGLView
CGLContextObj newContext;
CGLCreateContext(myCgPixObj, mainRenderingContext, &newContext);

// Create an NSOpenGLContext object here to feed into NSOpenGLView
NSOpenGLContext* _contextForGLView = [[NSOpenGLContext alloc] initWithCGLContextObj:newContext];

[newContext setView:self];
[self setOpenGLContext:newContext];

// We don't need this anymore
CGLDestroyPixelFormat(myCgPixObj); 

return self;

I am able to draw objects in this view just fine. But I get a blank white rectangle whenever I try to use the textures created in the main rendering context. I'm a little lost on how to proceed from here, I have never dealt with shared contexts before.

1
What do you mean by "my window resides in a different process"? Are these 2 different apps? - user1118321
Assuming this is all in 1 application, have you verified that the texture you get from window 1 contains what you think it contains? There are a number of reasons why a texture might not have the right pixels in it, but without more information about what you're doing, it's hard to say what's going wrong. Can you post the texture creation code? - user1118321
@user1118321 - Having the window and its view in another process was initially my plan, but currently I am trying to see if I can all do it in one application. As for your second question, yes, I am 100% sure that the texture id is what I wanted. This texture was already created internally and I have no access to the actual code it was created unfortunately. - JDBones
OK, in that case, I think you need to post the code you have for setting OpenGL to use the proper texture, and the shaders you're using for the geometry. - user1118321

1 Answers

0
votes

Seems like I got it working, partially at least since I had to force the view to redraw by moving my Window around to actually render the texture from the main context (another problem for another time!). Anyways, here's how I did it:

  • My main rendering context is supplied by a host application (yes, I'm working on a plugin), and is of type CGLContextObj. I wrap that context in an NSOpenGLContext object via calling initWithCGLContextObj
  • Next step was to create an NSOpenGLPixelFormat object, initializing it with the pixel format attributes used by the host application's renderer. This step is important as it ensures that the rendering context that will be used in my view will have the same OpenGL core profile, along with other attributes used by the host application.
  • Then in my subclassed NSOpenGLView, I create a new NSOpenGLContext object, preferably in the prepareOpenGL method, by using initWithFormat:shareContext: for allocation. I used the NSOpenGLPixelFormat and NSOpenGLContext objects created previously to pass as parameters.

Upon assigning the newly created context to my view, I was able to render the textures from the main rendering context.