Reputation: 33
I wrote a simple OpenGL code, and it worked normally in visual Studio. But when it was moved to Xcode. it was a compilation success, but the results were different. There are nothing.
I only changed OpenGL, GLUT header file.
I suspect that the range of gluPerspective
is different in Xcode, but I don't know the exact cause.
How is it workable?
Here is my code In xcode
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
#include <OpenGL/glu.h>
#include <math.h>
#include <stdio.h>
double RotAngle = 0.0;
void OctPyramid(void)
{
int N = 8;
double angle = 2 * 3.1415 / N;
int i;
glBegin(GL_LINE_LOOP);
for (i = 0; i < N; i++)
glVertex3f(cos(i * angle), -1.0, sin(i * angle));
glEnd();
glBegin(GL_LINES);
for (i = 0; i < N; i++)
{
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(cos(i * angle), -1.0, sin(i * angle));
}
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -6.0);
glRotatef(RotAngle, 0, 1, 0);
glTranslatef(2.0, 0.0, 0.0);
OctPyramid();
glFlush();
}
void IncAngle(void)
{
RotAngle = RotAngle + 0.08;
if (RotAngle > 360.0)
RotAngle = RotAngle - 360.0;
glutPostRedisplay();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowPosition(0, 0);
glutInitWindowSize(400, 400);
glutInitDisplayMode(GLUT_RGBA);
glutCreateWindow("main");
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 1.0, 0.0, 10.0);
glutDisplayFunc(display);
glutIdleFunc(IncAngle);
glutMainLoop();
}
Upvotes: 1
Views: 126
Reputation: 211126
The distance to the near plane zNear
and the distance to the far plane zFar
have to be greater than 0:
Set zNear
greater than 0.0:
gluPerspective(45, 1.0, 0.0, 10.0);
gluPerspective(45, 1.0, 0.1, 10.0);
Note, gluPerspective
sets a projection matrix like the following:
ta = tan(fovy / 2)
a = aspect
n = zNear
f = zFar
x: 1/(ta*a) 0 0 0
y: 0 1/ta 0 0
z: 0 0 -(f+n)/(f-n) -1
t: 0 0 -2*f*n/(f-n) 0
If zNear = 0
this leads to:
x: 1/(ta*a) 0 0 0
y: 0 1/ta 0 0
z: 0 0 -1 -1
t: 0 0 0 0
If a Homogeneous coordinates is transformed by such a matrix, then the z
component of the result will be always equal the w
component.
After the Perspective divide the z
component of the normalized device coordinate is 1, regardless of the coordinate.
This means that all the vertex coordinates of the geometry are "projected" to the same depth, all the geometry has the maximum depth of 1.0.
Because of that zNear
has to be greater than 0.0, else the behavior is not specified and depends on the internal implementation of the library.
Upvotes: 3