Ortho problems

Kattmat
Unregistered
 
Post: #1
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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
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...
Quote this message in a reply
Kattmat
Unregistered
 
Post: #3
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];
}
Quote this message in a reply
Moderator
Posts: 439
Joined: 2002.09
Post: #4
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.

Measure twice, cut once, curse three or four times.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #5
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 Huh . 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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
Is this an NSOpenGLView subclass?
Quote this message in a reply
Kattmat
Unregistered
 
Post: #7
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! Grin


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:
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  3D in Ortho bmantzey 5 3,965 Nov 20, 2008 02:34 AM
Last Post: DoG
  Ortho + Aspect Ratios jSTIN 2 2,983 Apr 6, 2005 09:21 AM
Last Post: jSTIN
  2D to 3D and back (Ortho and Perspective) Phate 5 5,508 Nov 30, 2003 10:56 AM
Last Post: AnotherJake
  Interpolating Ortho projection to 3d arekkusu 8 5,828 Feb 27, 2003 04:53 AM
Last Post: codemattic