Multiple OpenGL Windows in Cocoa

Adger Stokvisch
Post: #1
Dear Developers,

Can someone tell me how I can create multiple OpenGL windows in a Cocoa application?
As a starting point I have used the samplecode provided by Apple at:

I would like to make a second (third ..(N-th)) window which displays a different
visualization using OpenGL.

Kind regards,

Quote this message in a reply
Post: #2
I haven't used cocoa in a while, but have you tried just creating new instances of the first window?
Quote this message in a reply
Posts: 78
Joined: 2002.06
Post: #3
If you are just wanting many opengl views not linked, just create a sub-class of NSOpenGLView and write opengl code in the - (void)drawRect: (NSRect)r method. Then in IB create as many views as you want and set them to your custom opengl view. Ignore that sample code it is over complex.

Otherwise if you want many opengl views linked together

Have a look at
Or dig through my code

I used multiple contexts to share all the data between each menu item. Each item is a separate opengl view created in IB.

If you want more details I can problem put it more clearly, but your question leaves the answer open.

Quote this message in a reply
Posts: 1,234
Joined: 2002.10
Post: #4
If your window layout is simple (i.e. the view fills the entire window) you can of course create all windows and views programatically and not bother with the nib. Either way you'll have to manage a GL context for each view.
Quote this message in a reply
Adger Stokvisch
Post: #5
It will take me some more time to study this case more thorougly.
For now let me try to pose my question more clear and specific:

The goal is to achieve a second window which displays different
vertex object(s) each with it's own GL context.

( arekkusu your right, but I really would like to have separate windows.)

I sub-classed the NSOpenGLView, and it named BasicOpenGLView2.
So now I have BasicOpenGLView and BasicOpenGLView2.

As simple example how do you draw a Cube in BasicOpenGLView and
a Sphere in BasicOpenGLView2..
( or using the same geometric information, and display it
from a different camera point of view in each window as seen in
many 3D modelling programs.., but that's another question.)

The - (void)drawRect: (NSRect)r method, would need a to know
what to draw in which window.. (switch the context)

How do I implement this in - (void)drawRect: (NSRect)r.

(sticking to the sample code how to implement it in CustomOpenGLView.m )

willThimbleby (impressive coding, haven managed to install all libs yet,
but I shall try studying it in depth later, Xcode couldn't find the music.mp3 file)
but have run the application, beautiful product!

What do you mean by setting it to the custom view?
, and how do I accomplish this?


To keep things simple and didactically sound, let's take a very basic code example :
(the Mac OS X/Cocoa Code).
How to extend this so that the triangle is viewed in window #1 and the square in
window #2 ?

Can someone show/tell me how to do this??

Still it would be great if I could keep on working on that sample code
which I filled up with features by now, but maybe I have to start
from scratch, it sure is didactically wrong, but I get curious what is
really happening in this coding example and how to extend it succesfully.

, Adger
Quote this message in a reply
Posts: 78
Joined: 2002.06
Post: #6
cheers. In interface builder you can set a custom class for a nsview control. You define your classes like any others as sub-classes of openglviews.

I uploaded a simpler example here that uses IB views, it has two opengl windows and is easier to understand than nehe's code.
Quote this message in a reply
Posts: 78
Joined: 2002.06
Post: #7
Sorry. There was a small bug, fixed now. --Will
Quote this message in a reply
Adger Stokvisch
Post: #8
Thank you!, that works fine.

I have a new question now:
How to implement an animation timer, and update
both windows?


I took Lesson04
(Mac OS X/Cocoa)
as example constaining a simple animation timer.


How to extend this code so that it enables updating two windows?

or how to modify the OpenGLDemo (see above), to have the triangle
and square rotating?

cheers, Adger
Quote this message in a reply
Posts: 78
Joined: 2002.06
Post: #9
Again nehe's code is more complicated than you need. You could have two separate timers in each openglview. So something like:

- (void) awakeFromNib

    [NSTimer scheduledTimerWithTimeInterval:timeInterval
                             selector:@selector( update: )
                             userInfo:nil repeats:YES ];

- (void) update:(NSTimer *)timer
      [self setNeedsDisplay:YES];

Or if you wanted them to really update in step. Create a controller object in IB, create outlets to the two opengl views, and use the above code once but have update call setNeedsDisplay on the two opengl views.
Quote this message in a reply
Adger Stokvisch
Post: #10
Thanks again,

It has been a pleasure, to meet you all at the forum.

, Adger
Quote this message in a reply
Posts: 1,199
Joined: 2004.10
Post: #11
It's perhaps a little late, but last night I wrote a tool for my use in examining how well, or poorly, texture maps tile ( since I'm writing a game with a terrain engine, and ugly repeating textures need photoshopping ).

The program is a document-based Cocoa app, allowing multiple windows, each with an NSOpenGLView showing a particular texture map, with a slider to set the repeat level.

It's not an example of high quality Cocoa programming -- I wrote it in about 2 hours. But the source is included and may be of use.

That said, it's actually a slightly useful tool in that it monitors the timestamp of the texture(s) being displayed and will quietly reload the texture in the background if you edit and save it from another program. This means you can have it showing a texture that repeats poorly and it will update as you edit it in photoshop or whatever tool you're using.

Quote this message in a reply
Adger Stokvisch
Post: #12
It works fine with seperate timers, but I've not accomplish to make it update in step.
(I initiated a controller object, but don't now how to use it properly)

Can somebody show/help me to make it update in step?
maybe show it in code and modify the OpenGLDemo provided by willThimlberly?

(P.S, Am I able to upload the code I have sofar, via an Attachment to the iDevForum?
this would be more communicative and sociable, I have no server at the moment)

, Adger
Quote this message in a reply
Posts: 78
Joined: 2002.06
Post: #13
Short attempt (or how to create new classes and outlets in IB). Might be worth doing the tutorial on Cocoa/IB as-well.

1. Open your MainMenu.nib in IB
2. Click on the Classes tab
3. Select NSObject and press return call it something sensible (ViewController)
4. Get info on your class you created (cmd-1)
5. Select Outlets and add two new ones, called view1 and view2
6. From the classes menu create the files for your new class
7. Instantiate your new class (same menu)
8. Control-click-drag from your instantiated class (a blue cube) to your two custom views
9. In XCode/PB add an init method and put in your timer code.
10. In the timer method call [view1 setNeedsDisplay:YES]; and [view1 setNeedsDisplay:YES];

Quote this message in a reply
Adger Stokvisch
Post: #14
Now I have it working, I got stuck at step 9..,
Still having problems thinking Cocoa(..)

Now I have two separate classes, suppose I would like to visualize
a single process running in an update loop, generating say two values;
x = r * cos(th); and y = r * sin(th); (just as example..)

How do I connected x to the scale of the triangle object,
and the y value to the scale of the square object.

How can this (inheritance) problem be solved best

Cheers, Adger
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Opengl/Cocoa text rendering tesil 15 20,089 Mar 20, 2012 11:16 AM
Last Post: OneSadCookie
  OpenGL Text Rendering (in Cocoa) daveh84 5 8,772 Feb 19, 2009 12:44 PM
Last Post: TomorrowPlusX
  OpenGL & Cocoa - Improving frame rate daveh84 4 6,251 Feb 2, 2009 06:53 AM
Last Post: backslash
  bad depth sorting in Cocoa OpenGL aldermoore 2 5,135 Dec 30, 2008 03:07 PM
Last Post: ThemsAllTook
  Loading and using textures with alpha in OpenGL with Cocoa corporatenewt 4 8,053 Dec 8, 2007 02:06 PM
Last Post: Malarkey