View Full Version : Ortho problems
Kattmat
2003.10.10, 02:47 AM
Hiya, all!
I was playing around some with a strategy-game using Quartz, but speed was way too slow that way and I got the idea (from a guy at idevgames @ AIM ) to use an OpenGL ortho-view.
Setting up the OGL-view was not hard. First, I tried doing it with a perspective camera. Worked just fine! But then, when I used gluOrtho2D( 0, 512, 512, 0 ); or glOrtho( 0, 512, 512, -1, 1 ); it gave me the wrong result. My view is 512x512 pxl's and instead of giving me 512x512 units in the view. I got -1 to 1 on each axis, which is the default orthoview in OpenGL (if I haven't missunderstood something). It's like my glOrtho / gluOrtho2D function is never called! Why is that?
Huge thanks in advance for all the help!
Regards,
Anton Kiland.
OneSadCookie
2003.10.10, 05:09 AM
I think we're going to need source. Are you using GLUT or Cocoa? When do you change the projection? If you put a printf above that line does it get called :rolleyes: ... I can assure you that gluOrtho2D is bug-free...
Kattmat
2003.10.10, 10:55 AM
I'm using Cocoa, not GLUT. Hm, and for my code. Here it is:
- (id)initWithFrame:(NSRect)frameRect
{
NSOpenGLPixelFormat *nsglFormat;
NSOpenGLPixelFormatAttribute attr[] =
{
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
NSOpenGLPFAColorSize, BITS_PER_PIXEL,
NSOpenGLPFADepthSize, DEPTH_SIZE,
0
};
[self setPostsFrameChangedNotifications: YES];
nsglFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes: attr];
if ( !nsglFormat ) { NSLog( @"Invalid format... terminating." ); return nil; }
self = [super initWithFrame: frameRect pixelFormat: nsglFormat];
[nsglFormat release];
if ( !self ) { NSLog( @"Self not created... terminating." ); return nil; }
[[self openGLContext] makeCurrentContext];
[self initGL];
return self;
}
- (void)initGL
{
glClearColor( 0.0f, 0.8f, 0.0f, 1.0f );
glClearDepth( 1.0f );
glDepthFunc( GL_LESS );
glEnable( GL_DEPTH_TEST );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
glHint( GL_POLYGON_SMOOTH_HINT, GL_NICEST );
[self reshape];
}
- (void)reshape
{
float aspect;
NSSize bound = [self frame].size;
aspect = bound.width / bound.height;
glViewport( 0, 0, bound.width, bound.height);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, bound.width, bound.height, 0 );
}
- (void)drawFrame
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glTranslatef( 0.0f, 0.0f, 0.0f );
glBegin( GL_TRIANGLES );
glColor3f ( 1.0f, 0.0f, 0.0f );
glVertex2f( 0.1f, 0.1f );
glColor3f ( 0.0f, 1.0f, 0.0f );
glVertex2f(-0.1f, 0.1f );
glColor3f ( 0.0f, 0.0f, 1.0f );
glVertex2f( 0.0f,-0.1f );
glEnd();
[[self openGLContext] flushBuffer];
}
MattDiamond
2003.10.10, 11:49 AM
Just guessing, but maybe you need to set the matrix mode to model-view in your DrawFrame function?
glMatrixMode( GL_MODELVIEW );
If its still set to the projection matrix, your call to glLoadIdentity might be undo-ing the ortho projection you set up.
Originally posted by MattDiamond
Just guessing, but maybe you need to set the matrix mode to model-view in your DrawFrame function?
glMatrixMode( GL_MODELVIEW );
If its still set to the projection matrix, your call to glLoadIdentity might be undo-ing the ortho projection you set up.
kattmat :???: . This was one of the main points of the idevgames chat, to properly setup the matrix stacks. pay attention! :mad:
I suggest you, kattmat, read up on these things in the "Guide To OpenGL Programming" aka the "Redbook". Its good to read it all at least once, you will catch a lot of mistakes like this, even if you dont remember everything 100%, you'll know where to look first.
OneSadCookie
2003.10.10, 07:07 PM
Is this an NSOpenGLView subclass?
Kattmat
2003.10.12, 04:15 PM
Hum. Sorry I haven't answered in a while, but that has a natural reason: I was at my girlfriend's house 360 km away over the weekend :love:
Anyway, I came home 10 minutes ago and tried something out. Which, suprisingly, worked!
In the initGL code, I added:
gluOrtho2D( 0, 512.0, 512.0, 0 );
under the glHint's
and in the reshape function, I added:
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
under the gluOrtho-function.
And now all works fine! :D
Something tells me, though, that this is what you all have been saying to me all along.. :blush:
P-p-please don't kill me! :ohmy:
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.