Welcome to MacForumz.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

cocoa and opengl

 
   Macintosh computer (Home) -> General Discussion RSS
Next:  RDC window size  
Author Message
Matteo Cavalleri

External


Since: Apr 24, 2005
Posts: 6



(Msg. 1) Posted: Sun Apr 24, 2005 5:55 pm
Post subject: cocoa and opengl
Archived from groups: comp>sys>mac>programmer>misc (more info?)

I'm trying to learn a bit of opengl with a custom opengl class, and
here's a strange problem I found. All the tutorials I find use these
code to setup and/or resize the opengl view:

glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, w/h, 0.1f, 100.0f);

If I do the same the poligons are displayed correctly, but as soon as I
resize the window they disappear. If I do the following:

glViewport(0, 0, w, h);
gluPerspective(45.0f, w/h, 0.1f, 100.0f); // moved this one
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

then the poligons are displayed even when I resize the window. I can't
understand why it happens...

Moreover if I move the modelview matrix along the z axis they are no
longer displayed (I'm following the nehe tutorials) so I may suspect
that has to do with some wrong inizialization, but I can't guess what's
wrong.

any help is really appreciated

TIA

 >> Stay informed about: cocoa and opengl 
Back to top
Login to vote
David Phillip Oste

External


Since: Apr 25, 2004
Posts: 1083



(Msg. 2) Posted: Mon Apr 25, 2005 1:55 am
Post subject: Re: cocoa and opengl [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Study the example:

/Developer/Examples/OpenGL/Cocoa/UnderlaySurface/

particularly

- (void) drawGLScene;

in GLView.mm

--
David Phillip Oster

 >> Stay informed about: cocoa and opengl 
Back to top
Login to vote
Matteo Cavalleri

External


Since: Apr 24, 2005
Posts: 6



(Msg. 3) Posted: Mon Apr 25, 2005 6:55 am
Post subject: Re: cocoa and opengl [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

David Phillip Oster <oster DeleteThis @ieee.org> wrote:

 > Study the example:
 >
 > /Developer/Examples/OpenGL/Cocoa/UnderlaySurface/

I already looked that source, but I'll try to study it better and see
what I missed. thanks for the suggestion.


--
Questa firma e' frutto della tua immaginazione.<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: cocoa and opengl 
Back to top
Login to vote
Matteo Cavalleri

External


Since: Apr 24, 2005
Posts: 6



(Msg. 4) Posted: Mon Apr 25, 2005 9:55 am
Post subject: Re: cocoa and opengl [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Matteo Cavalleri <shiva.brahma.DeleteThis@tiscali.invalid> wrote:

  > > Study the example:
  > >
  > > /Developer/Examples/OpenGL/Cocoa/UnderlaySurface/

Ok, I've studied it but I haven't still understood what I do wrong, but
I've another question now. The opengl view of this example is a subclass
of NSOpenGLView. AFAIK initWithFrame: is never called when you subclass
NSOpenGLView but in this example it is called. I suppose the author of
the demo used a NSView object when he built the interface with IB and
then assigned it to a subclass of NSOpenGLView instead of a subclass of
NSView. Is it possible? I tried to check with IB but I'm not sure if
this is correct.

Anyway, my program still shows nothing. I tried both a custom opengl
class (subclassing NSView) and a NSOpenGLView subclass. The only thing
that changed is that now I don't need to resize the window to see the
poligons disappear, I just never see them....

Here's my current code:



#import "MyOpenGLView.h"

@implementation MyOpenGLView

- (void) awakeFromNib
{
setupDone = NO;
}


- (void) drawRect:(NSRect)rect
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

if (!setupDone) {
setupDone = YES;
[self setupGL];
}

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//glTranslatef(-1.5f,0.0f,-6.0f);
glTranslatef(-1.5f,0.0f,0.0f);

glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd();

glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Lef
glEnd();

// i'm not using doublebuffering, so i should not need flushBuffer:
//if ([self inLiveResize])
glFlush();
//else
// [[self openGLContext] flushBuffer];
}

- (void) reshape
{
NSRect rect = [self bounds];
GLdouble aspect;

glViewport(0, 0, (GLsizei) rect.size.width, (GLsizei)
rect.size.height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

aspect = rect.size.width / rect.size.height;
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}


- (void) setupGL
{
// reshape: has already been called by os x and the modelview matrix
// has already been selected in drawRect
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);

// do i need this? I already setup depth buffer within IB
//glEnable(GL_DEPTH_TEST);
//glDepthFunc(GL_LEQUAL);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

@end



--
Questa firma e' frutto della tua immaginazione.<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: cocoa and opengl 
Back to top
Login to vote
David Phillip Oste

External


Since: Apr 25, 2004
Posts: 1083



(Msg. 5) Posted: Mon Apr 25, 2005 12:55 pm
Post subject: Re: cocoa and opengl [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <1gvkw2r.1ym2f5ynsuujqN%shiva.brahma@tiscali.invalid>,
shiva.brahma RemoveThis @tiscali.invalid (Matteo Cavalleri) wrote:

 > Matteo Cavalleri <shiva.brahma RemoveThis @tiscali.invalid> wrote:
 >
   > > > Study the example:
   > > >
   > > > /Developer/Examples/OpenGL/Cocoa/UnderlaySurface/
 >
 > Ok, I've studied it but I haven't still understood what I do wrong, but
 > I've another question now.

You were setting up your transformation matrices once. The example was
re-setting them up on each call to drawRect. As soon as the window was
resized, your transformation matrix was inappropriate. The example would
have set up an appropriate matrix on the next call to drawRect.

 > // i'm not using doublebuffering, so i should not need flushBuffer:
 > //if ([self inLiveResize])
 > glFlush();
 > //else
 > // [[self openGLContext] flushBuffer];
 > }

Unless you specifically tell OpenGL to draw to the front buffer, (which
you'd never want to do, since you'd see ugly partial draws), you are
always drawing to the back buffer.

Since you never call - (void) flushBuffer;, nothing is ever drawn.

--
David Phillip Oster<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: cocoa and opengl 
Back to top
Login to vote
Matteo Cavalleri

External


Since: Apr 24, 2005
Posts: 6



(Msg. 6) Posted: Mon Apr 25, 2005 1:55 pm
Post subject: Re: cocoa and opengl [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

David Phillip Oster <oster DeleteThis @ieee.org> wrote:


 > You were setting up your transformation matrices once. The example was
 > re-setting them up on each call to drawRect. As soon as the window was

you mean calling these inside drawRect?

glViewport(...);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(...);

i tried, it still doesn't work. i also tried putting

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

at the end of the reshape method. it still doesn't work


 > Unless you specifically tell OpenGL to draw to the front buffer, (which
 > you'd never want to do, since you'd see ugly partial draws), you are
 > always drawing to the back buffer.
 >
 > Since you never call - (void) flushBuffer;, nothing is ever drawn.

the documentation says "If the receiver is not a double-buffered
context, this call does nothing". since i did not set double buffer i
thought I would not need this, in any case I tried and all I get is a
white windows that becomes black when I resize it.

thanks for your help.

--
Questa firma e' frutto della tua immaginazione.<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: cocoa and opengl 
Back to top
Login to vote
David Phillip Oste

External


Since: Apr 25, 2004
Posts: 1083



(Msg. 7) Posted: Tue Apr 26, 2005 6:15 am
Post subject: Re: cocoa and opengl [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <1gvkw2r.1ym2f5ynsuujqN%shiva.brahma@tiscali.invalid>,
shiva.brahma.RemoveThis@tiscali.invalid (Matteo Cavalleri) wrote:

 > Matteo Cavalleri <shiva.brahma.RemoveThis@tiscali.invalid> wrote:
 > Here's my current code:

....

 > - (void) drawRect:(NSRect)rect
 > {
 > glMatrixMode(GL_MODELVIEW);
 > glLoadIdentity();
 >
 > if (!setupDone) {
 > setupDone = YES;
 > [self setupGL];
 > }
 >
 > glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 >
 > //glTranslatef(-1.5f,0.0f,-6.0f);
 > glTranslatef(-1.5f,0.0f,0.0f);
 >


glColor3f(1., 0., 0.); // add this line

 > glBegin(GL_TRIANGLES);
 > glVertex3f( 0.0f, 1.0f, 0.0f); // Top
 > glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
 > glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
 > glEnd();
 >

glColor3f(.2, .4, 0.); // add this line

 > glBegin(GL_QUADS);
 > glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
 > glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
 > glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
 > glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Lef
 > glEnd();
 >
 > // i'm not using doublebuffering, so i should not need flushBuffer:
 > //if ([self inLiveResize])
 > glFlush();
 > //else
 > // [[self openGLContext] flushBuffer];
 > }
 >


You weren't getting anything because you were drawing black on black.
When I pasted your code into a fresh project, and added a few calls to
glColor3f(), I had no trouble getting it to draw and resize.

--
David Phillip Oster<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: cocoa and opengl 
Back to top
Login to vote
David Phillip Oste

External


Since: Apr 25, 2004
Posts: 1083



(Msg. 8) Posted: Tue Apr 26, 2005 6:23 am
Post subject: Re: cocoa and opengl [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <1gvl7st.fczknwam5t5sN%shiva.brahma@tiscali.invalid>,
shiva.brahma.TakeThisOut@tiscali.invalid (Matteo Cavalleri) wrote:

 > thanks for your help.

I've put a full working example, based on your post that started this
thread in <a style='text-decoration: underline;' href="http://www.turbozen.com/sourcecode/opengl/OpenGLTest.zip" target="_blank">http://www.turbozen.com/sourcecode/opengl/OpenGLTest.zip</a>

--
David Phillip Oster<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: cocoa and opengl 
Back to top
Login to vote
Matteo Cavalleri

External


Since: Apr 24, 2005
Posts: 6



(Msg. 9) Posted: Tue Apr 26, 2005 10:50 am
Post subject: Re: cocoa and opengl [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

David Phillip Oster <oster DeleteThis @ieee.org> wrote:


 > I've put a full working example, based on your post that started this
<font color=purple> > thread in <a style='text-decoration: underline;' href="http://www.turbozen.com/sourcecode/opengl/OpenGLTest.zip</font" target="_blank">http://www.turbozen.com/sourcecode/opengl/OpenGLTest.zip</font</a>>

thanks. anyway i was able to make my code work too. it seems I had
different problems. first the polygons were in fact not drawn and when i
was able to fix that they were simply drawn out of sight, so I just
didn't see them. moving them fixed that ;) that's kinda stupid, I know,
but as a test I recompiled my previous example and even moving the
poligons they were still not visible, so I'm pretty sure there was
another problem (maybe the transformation matrix problem you told me).
And last but not least, as suggested by another person on another
newsgroup, it seems I hit a problem with zerolink, because somehow it
seems I fixed another problem just by recompiling the same code two or
three times...

thanks again for the time you spent on my problem.

--
Questa firma e' frutto della tua immaginazione.<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: cocoa and opengl 
Back to top
Login to vote
David Phillip Oste

External


Since: Apr 25, 2004
Posts: 1083



(Msg. 10) Posted: Tue Apr 26, 2005 5:07 pm
Post subject: Re: cocoa and opengl [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <1gvmges.1o88d26bp1prkN%shiva.brahma@tiscali.invalid>,
shiva.brahma DeleteThis @tiscali.invalid (Matteo Cavalleri) wrote:

 > thanks again for the time you spent on my problem.

I'm glad you got past your problem.

I built a working Cocoa program from your snippet in an amazingly short
time:

* Create a new Cocoa App project in XCode
* Double click on its MainMenu.nib to open it in Interface Builder
* Make the window contents a NSOpenGLView
* In the pallete MainMenu.nib window on the lower left, Classes panel,
search for NSOpenGLView and use the contextual menu to make a subclass
named MyOpenGLView
* use the contextual menu to make files for MyOpenGLView.
* Click on the NSOpenGLView in the "Window" window, and in the "Get
Info" window, custom class pane, change it to MyOpenGLView
* Switch back to XCode.
* Paste in the sample snippet.
* add my fixes, compile and run.

--
David Phillip Oster<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: cocoa and opengl 
Back to top
Login to vote
Matteo Cavalleri

External


Since: Apr 24, 2005
Posts: 6



(Msg. 11) Posted: Tue Apr 26, 2005 8:54 pm
Post subject: Re: cocoa and opengl [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

David Phillip Oster <oster RemoveThis @ieee.org> wrote:


 > * Create a new Cocoa App project in XCode
[...]
 > * Switch back to XCode.

I made this fast too. displaying those damn poligons was a different
matter ;)

anyway I'm thinking maybe I should stop nehe tutorials for a while and
study a bit better opengl matrices.

--
Questa firma e' frutto della tua immaginazione.<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: cocoa and opengl 
Back to top
Login to vote
Display posts from previous:   
   Macintosh computer (Home) -> General Discussion All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]