OpenGL: Switching between 2D and 3D
Here is where I stand. Im looking to optimize this because I have 2D and 3D elements every frame. I have considered that after the first call to Perspective#D I save the Projection Matrix with glGetMatrix() into a local variable, then just reload it with glLoadMatrix(). Has anyone tried anything like that before? I am concerned that there may be more Matrices other than GL_PROJECTION that I need to save... What about glViewport, do I need to call it if I load the Projection Matrix?
Code:
void cRenderEngine::Perspective3D(float p_fov, float p_near, float p_far, float width, float height) {
renderMode = RENDER_3D;
fov3d = p_fov;
near3d = p_near;
far3d = p_far;
width3d = width;
height3d = height;
float lw = (float)width, lh = (float)height;
if(height == 0) height = 1;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(p_fov,lw/lh,p_near,p_far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void cRenderEngine::Render3D() {
if(renderMode==RENDER_3D) return;
Perspective3D(fov3d,near3d,far3d,width3d,height3d);
}
void cRenderEngine::Perspective2D(RECT size) {
renderMode = RENDER_2D;
per2D = size;
glViewport(size.left, size.bottom, size.right, size.top);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(size.left, size.right, size.bottom, size.top);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void cRenderEngine::Render2D() {
if(renderMode==RENDER_2D) return;
Perspective2D(per2D);
}