Advertisement

Perspective > ortho > perspective etc

Started by April 01, 2002 05:42 PM
3 comments, last by BauerGL 22 years, 10 months ago
Okey, I''ve made a program which renders stuff . And in my render function I have a switch between perspective and ortho mode. These functions look like this:
  
void COpenGL::StartOrthoMode(int width, int height)
{
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
    glLoadIdentity();
    glOrtho(0, width, 0, height, -1, 1);
    glMatrixMode(GL_MODELVIEW);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
    glEnable(GL_TEXTURE_2D);
    glPolygonMode(GL_FRONT, GL_FILL);
    glPolygonMode(GL_BACK, GL_FILL);
}
-----------------------------------------------
void COpenGL::EndOrthoMode()
{
  glDisable(GL_BLEND);
    glMatrixMode(GL_PROJECTION);
  glPopMatrix();
  glMatrixMode(GL_MODELVIEW);
}
  
But then in my render function which looks like this:
  
void CGame::MenuRender()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();


  // ***************************************

  // DRAW 2D

  // ***************************************

  m_pOpenGL->StartOrthoMode(WINDOW_WIDTH, WINDOW_HEIGHT);
  
  // Print some text


  m_pOpenGL->EndOrthoMode();
  // ****************************************

  // END DRAW 2D

  // ****************************************

}
  
If I try to render something outside StartOrthoMode() and EndOrthoMode() it won''t get rendered... so that is my problem. And this is my resize function:
  
bool CApplication::ResizeScene(int width, int height)
{
  // Prevent a divide by zero

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

  // Reset current view

  glViewport(0,0, width, height);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  // Calculate the aspect ratio of the window

  gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 1000.0f);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  
  return true;
}
  
So what can be wrong with my changes, and how do I fix it? Thank you very much and keep up the good work! CUselessStuff::NiftyQuote();

@mikaelbauer

Super Sportmatchen - A retro multiplayer competitive sports game project!

There is only one thing that I can think of and that is your resize function sets the render moder to perspective and the first time you switch to ortho you never switch back to perspective mode. You have to make a funct like your StartOrthoMode but for Perspective mode other wise it wont render anything. Or Perhaps this might work as well...

Revise your source to this:


  void COpenGL::EndOrthoMode(){    glDisable(GL_BLEND);  glMatrixMode(GL_PROJECTION);  glPopMatrix();  gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 1000.0f);}void COpenGL::StartOrthoMode(int width, int height){  glPushMatrix();    glLoadIdentity();    glOrtho(0, width, 0, height, -1, 1);    glMatrixMode(GL_MODELVIEW);    glEnable(GL_BLEND);    glBlendFunc(GL_SRC_ALPHA, GL_ONE);    glEnable(GL_TEXTURE_2D);    glPolygonMode(GL_FRONT, GL_FILL);    glPolygonMode(GL_BACK, GL_FILL);}  


That should work... although I''m not quite sure why you want to switch between ortho and perspective every frame but I believe that should work... I hope I was of some assistance...

I'm don't know much but I can try to help... just email me at... Shadow_0f_Light@Yahoo.com(the '0' in 'Of' is a zero :P)
Advertisement
The reason I want to do this is that it becomes easier to draw 2d stuff, such as HUD etcetera... but maybe I'm doing it all wrong?

Thanks for the tip, I'll try it right away.

CUselessStuff::NiftyQuote();

EDIT: Doesn't seem to work :[. In NeHe's gametutorials where he loads a md2 object, he uses an almost identic method. Hmmmmmmmm..... :]

[edited by - BauerGL on April 2, 2002 7:12:10 AM]

@mikaelbauer

Super Sportmatchen - A retro multiplayer competitive sports game project!

In my display routine, I first do a 3d perspective mode setup, and render my scene.

Then I setup 2d ortho, and do all my text/gui/fullscreen effects.

2 distinct phases in each frame.

I have found it much easier to work in.

zin

zintel.com - 3d graphics & more or less
zintel.com - 3d graphics & more or less
BauerGL : the most baffling thing is that your code looks perfectly fine to me :|

Maybe you should double check that glViewport and gluPerspective are being given valid/useful height and width values in your resize handler... it may be that its being given something weird/very small (sorry, that''s all I could come up with )

Shadow_of_Light : he is switching projections every frame to render 2D overlays on a 3D scene (as zin pointed out). This is the correct way to do this task.

His code does exactly the same as yours, only more efficiently (as he uses the matrix stack rather than recalculate the perspective projection matrix each frame).

This topic is closed to new replies.

Advertisement