Help with NSOpenGLView

srcleaves
Unregistered
 
Post: #1
Hi,

I'm trying to learn OpenGL and am interested in using NSOpenGLView and not GLUT. Of course many of the examples available on the web use GLUT so I have to partially figure things out on my own. I have a very simple triangle app that draws a single triangle into the view. This works just fine as long as I don't try to do a gluLookAt or glViewport. But the code I am creating this from does both of those calls along with a gluPerspective. I have used the GLUT version of the code and it works fine. But I really don't understand how to get that code to work with the NSOpenGLView subclass.

This code...
//glViewport( 0, 0, size.width, size.height );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();

gluPerspective( 45, (GLfloat)size.width / (GLfloat)size.height, 1.0, 150 );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

inside the NSOpenGLView subclasses prepareOpenGL method doesn't have any negative effect. Uncomment the glViewport call though it I don't see the triangle anymore.

This code...
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glLoadIdentity();

// Set the camera position
//gluLookAt( 0, 0, 6, 0, 0, 0, 0, 1, 0 );

glBegin( GL_TRIANGLES );
{
glVertex3f( 0, 1, 0 ); // top point of the triangle
glVertex3f( -1, 0, 0 ); // left point
glVertex3f( 1, 0, 0 ); // right point
}
glEnd();

inside the drawRect method works fine unless the gluLookAt call is uncommented.

As I said these two calls are used in the original GLUT code and so I want to understand either why they don't work here or what else I need to do when translating GLUT to NSOpenGLView.

Thanks in advance!

Stephan
Quote this message in a reply
nabobnick
Unregistered
 
Post: #2
Have you checked out Apple's NSOpenGL Fullscreen and Cocoa OpenGL source examples? They are were I learned from.

http://developer.apple.com/samplecode/NSOpenGL_Fullscreen/NSOpenGL_Fullscreen.html

http://developer.apple.com/samplecode/Cocoa_OpenGL/Cocoa_OpenGL.html
Quote this message in a reply
srcleaves
Unregistered
 
Post: #3
nabobnick Wrote:Have you checked out Apple's NSOpenGL Fullscreen and Cocoa OpenGL source examples? They are were I learned from.

I downloaded these and took a look. Both are more complex than the very simple problem I'm working on. Neither really helped me answer why the problem I have exists.

The demo with the spinning globe actually seems to be closer to what I have in that it overrides the reshape method and that is where it sets up the viewport and perspective. The interesting thing is that I don't see that it even uses gluLookAt. What is truly annoying however is that leaving the glViewport call in and the gluLookAt call out results in my code being pretty much the same setup as this demo. The problem is that it still won't show the triangle. Again if I comment out glViewport everything is just happy and works.

The only real difference I see is that in IB when I get the info on my NSOpenGLView subclass I see a way to specify buffers and those attributes do NOT appear for Apple's demo NIB. I am thinking is is because their NIB was created with an older IB.

Stephan
Quote this message in a reply
srcleaves
Unregistered
 
Post: #4
Maybe I need to rephrase the question. If I have a very simple subclass of NSOpenGLView that sets up its NSOpenGLPixelFormat in its constructor and draws a triangle in its drawRect method what do I need to do in order to be able to use gluLookAt?
Quote this message in a reply
nabobnick
Unregistered
 
Post: #5
Blatant stupity follows please ignore my post and read the much more informative next post!


<IGNORE>
Any function with glu on the front requires GLUT, maybe that is the source of confusion. If you are trying to get rid of GLUT you can no longer use glu functions. I am no expert but last night I saw an example in the OpenGL Red Book that did not use gluLookAt instead it used glOrtho which requires more work to use and is probably harder to understand. After that I am no help, but might point you in the right direction.
</IGNORE>
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #6
Quote:Any function with glu on the front requires GLUT, maybe that is the source of confusion. If you are trying to get rid of GLUT you can no longer use glu functions. I am no expert but last night I saw an example in the OpenGL Red Book that did not use gluLookAt instead it used glOrtho which requires more work to use and is probably harder to understand. After that I am no help, but might point you in the right direction.

Actually, glu* functions are not part of GLUT. There is no problem with using gluLookAt in a non-GLUT project, and glOrtho is not a replacement for gluLookAt.
Quote this message in a reply
srcleaves
Unregistered
 
Post: #7
Problem Solved!!! Oh my gosh, why the heck doesn't Xcode tell you you haven't included a header? It happily compiles and runs but doesn't work. ARGH. I needed to include glu.h. Sorry I'm used to Java where you get very clear compile time indication of missing imports.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #8
Turning off ZeroLink will catch undefined C functions.

And I think this has been covered, but
1) <OpenGL/glu.h> != <GLUT/glut.h>
2) you should put your viewport/projection setup into the reshape method.
3) an NSOpenGLView subclass will have the renderer attributes in IB. A custom subclass won't. You have to build the pixel format manually in the second case, but it's not very hard.
Quote this message in a reply
srcleaves
Unregistered
 
Post: #9
Thanks arekkusa! I understood that glu was not GLUT. I also placed the viewport/projection stuff in the reshape method. I have been setting the pixel format manually because other code I have seen in the past did the same. Indeed it seems a better idea for documentation purposes. The ZeroLink info is definitely useful!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Nothing showing up in NSOpenGLView [newb] binaryinsomnia 6 6,456 Nov 29, 2011 07:04 PM
Last Post: binaryinsomnia
  Cocoa controls on top of NSOpenGLView wadesworld 5 5,911 Apr 6, 2009 01:38 AM
Last Post: arekkusu
  Antialiasing and NSOpenGLView attributes Jar445 2 6,546 Jan 20, 2009 10:42 AM
Last Post: maximile
  Adding NSOpenGLView class in Xcode/Interface Builder 3.0 Graphic Ace 2 3,704 Dec 5, 2007 02:15 PM
Last Post: Blacktiger
  Double-Buffering with NSOpenGLView DesertPenguin 3 5,725 Aug 1, 2006 07:17 AM
Last Post: DesertPenguin