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

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

## 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);

/*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);
}

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

glColor3f(1.0f, 0.85f, 0.35f);
{
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 on other sites
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 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 on other sites
Quote:
 Original post by Guy Mehis 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..

1. 1
2. 2
JoeJ
17
3. 3
4. 4
frob
11
5. 5

• 13
• 16
• 13
• 20
• 13
• ### Forum Statistics

• Total Topics
632181
• Total Posts
3004625

×