Jump to content
  • Advertisement
Sign in to follow this  
Guy Meh

OpenGL gluPerspective not doing anything [Cocoa (Mac OS X)] [solved]

This topic is 3596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've started playing around with OpenGL in Cocoa (Mac OS X's "official" programming language) where I've created a subclass of NSOpenGLView. The problem is in my reshape method. Here I call gluPerspective (though I'm just drawing a square so far). gluPerspective doesn't appear to anything; my image gets distorted when I resize the window. glOrtho works, but not gluPerspective My implementation:
#import "MyOpenGLView.h"

@implementation MyOpenGLView

-(void) awakeFromNib
{
    fovy = 72.0;
    zNear = -1.0;
    zFar = 100.0;
}

- (void) prepareOpenGL
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
}

- (void) reshape
{
    int width = [self bounds].size.width;
    int height = [self bounds].size.height;

    if (height == 0)
    {
        height = 1;
    }

    glViewport(0, 0, width, height);

    GLdouble fAspect = (GLdouble)width / (GLdouble)height;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    /*if (width <= height)
    {
        glOrtho(-10.0, 10.0, -10.0 * ((GLfloat)height / (GLfloat)width),
          10.0 * ((GLfloat)height / (GLfloat)width), -10.0, 10.0 );
    }
    else
    {
        glOrtho(-10.0 * fAspect, 10.0 * fAspect, -10.0, 10.0, -10.0, 10.0 );
    }*/
    gluPerspective(72.0, fAspect, -1.0, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

- (void) drawRect: (NSRect) bounds
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(1.0f, 0.85f, 0.35f);
    glBegin(GL_QUADS);
    {
        glVertex3f(-0.50, -0.50, 0.0);
        glVertex3f( 0.50, -0.50, 0.0);
        glVertex3f( 0.50,  0.50, 0.0);
        glVertex3f(-0.50,  0.50, 0.0);
    }
    glEnd();

    glFlush();
}

@end
[Edited by - Guy Meh on January 15, 2009 9:31:23 PM]

Share this post


Link to post
Share on other sites
Advertisement
try setting the near value to 1.0 rather then -1.0.
Also, thought im not sure if this is needed, try moveing the box's z value.. but I know the near value is erroneous, it must be >0.0

Share this post


Link to post
Share on other sites
That's what was wrong, my zNear value was below 1.0 (is this specified in any documentation?).

I had to change the z-values for my square to -1 so that I could see it, but that won't matter since I eventually want to draw 3D objects anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by Guy Meh
is this specified in any documentation?




http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/glu/perspective.html

States that both near and far must be positive, Near also cannot be 0.0, and far must be greater then near. You hear all the time of people setting near as close to 0.0 as possible (0.00001), but I've heard that you really want it far away from 0.0 as you can stand.. I normally just keep it at 1.0, with a far of about 1000.0 but yeah..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!