Switching from glOrtho to glFrustum
I am working on a simple 3D game. However the menu part of the complete game 2D. I use 2 function to switch the 2D or 3D.
My problem is: I got both function working fine when they are called initially but when I Switch from on to other I get some problem.
When I setup 2d view a to show the menu first and then change the view to 3D, the view is completely lost in space and is drawn crazy n far away.
- (void) Setup3DView {
glLoadIdentity();
glViewport(0, 0, 320, 480);
const GLfloat lightAmbient[] = {0.2, 0.2, 0.2, 1.0};
const GLfloat lightDiffuse[] = {1.0, 1.0, 1.0, 1.0};
const GLfloat lightPosition[] = {5.0, 5.0, 15.0, 0.0};
const GLfloat light2Position[] = {-5.0, -5.0, 15.0, 0.0};
const GLfloat lightShininess = 0.0;
const GLfloat zNear = 0.01, zFar = 800.0, fieldOfView = 45.0;
GLfloat size;
glEnable(GLDEPTHTEST);
glMatrixMode(GL_PROJECTION);
size = zNear * tanf(DEGREESTORADIANS(fieldOfView) / 2.0);
glFrustumf(-size, size, -size / (320.0/480.0), size / (320.0/480.0), zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glLightfv(GL_LIGHT0, GL_SHININESS, &lightShininess);
glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT2, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT2, GL_POSITION, light2Position);
glLightfv(GL_LIGHT2, GL_SHININESS, &lightShininess);
glLoadIdentity();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
printf("Moving to 3D View ");
glBlendFunc(GL_ONE, GLONE_MINUS_SRCALPHA);
glEnable(GLTEXTURE2D);
glTexEnvi(GLTEXTUREENV, GLTEXTURE_ENVMODE, GL_REPLACE);
glEnableClientState(GLVERTEXARRAY);
glEnableClientState(GLTEXTURE_COORDARRAY);
glEnable(GL_BLEND);
glBlendFunc(GLSRCALPHA, GLONE_MINUS_SRCALPHA);
}
- (void) Setup2DView {
CGRect rect = CGRectMake(0, 0, 320, 480);
glViewport(0, 0, rect.size.width, rect.size.height);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glOrthof(0, rect.size.width, 0, rect.size.height, -1, 1);
glMatrixMode(GL_MODELVIEW);
glBlendFunc(GL_ONE, GLONE_MINUS_SRCALPHA);
glEnable(GLTEXTURE2D);
glTexEnvi(GLTEXTUREENV, GLTEXTURE_ENVMODE, GL_REPLACE);
glEnableClientState(GLVERTEXARRAY);
glEnableClientState(GLTEXTURE_COORDARRAY);
glEnable(GL_BLEND);
glBlendFunc(GLSRCALPHA, GLONE_MINUS_SRCALPHA);
}
My problem is: I got both function working fine when they are called initially but when I Switch from on to other I get some problem.
When I setup 2d view a to show the menu first and then change the view to 3D, the view is completely lost in space and is drawn crazy n far away.
Mac OS X (10.5.4)