Ortho problems
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.
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.
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
... I can assure you that gluOrtho2D is bug-free...
... I can assure you that gluOrtho2D is bug-free...
I'm using Cocoa, not GLUT. Hm, and for my code. Here it is:
Code:
- (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];
}
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.
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.
Measure twice, cut once, curse three or four times.
Quote: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!
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.
Is this an NSOpenGLView subclass?
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
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!
Something tells me, though, that this is what you all have been saying to me all along..
P-p-please don't kill me! :ohmy:
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!
Something tells me, though, that this is what you all have been saying to me all along..
P-p-please don't kill me! :ohmy:
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| 3D in Ortho | bmantzey | 5 | 3,420 |
Nov 20, 2008 02:34 AM Last Post: DoG |
|
| Ortho + Aspect Ratios | jSTIN | 2 | 2,598 |
Apr 6, 2005 09:21 AM Last Post: jSTIN |
|
| 2D to 3D and back (Ortho and Perspective) | Phate | 5 | 4,724 |
Nov 30, 2003 10:56 AM Last Post: AnotherJake |
|
| Interpolating Ortho projection to 3d | arekkusu | 8 | 4,998 |
Feb 27, 2003 04:53 AM Last Post: codemattic |
|

