Advertisement

Switching between ortho and perspective :: n00bie

Started by May 13, 2002 11:41 AM
2 comments, last by BauerGL 22 years, 9 months ago
In my render function which is very simple at the moment I have this code:
  
void CGame::GameRender()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	gluLookAt(m_pCamera->mPosition.x, m_pCamera->mPosition.y, m_pCamera->mPosition.z,
			  m_pCamera->mView.x, m_pCamera->mView.y, m_pCamera->mView.z,
			  m_pCamera->mUpVector.x, m_pCamera->mUpVector.y, m_pCamera->mUpVector.z);

	glColor3f(1.0,0.0,0.0);
	glBegin(GL_TRIANGLES);
		glVertex3f(0.0,0.0,0.0);
		glVertex3f(1.0,0.0,0.0);
		glVertex3f(0.0,1.0,0.0);
	glEnd();

	m_pOpenGL->StartOrtho(m_WindowConfig.width, m_WindowConfig.height);

	glColor3f(1.0,1.0,1.0);
	m_pFont->Print2D(10.0,10.0, "hej");

	glColor3f(1.0,1.0,0.0);
	glBegin(GL_TRIANGLES);
		glVertex3f(1.0,0.0,0.0);
		glVertex3f(2.0,0.0,0.0);
		glVertex3f(1.0,1.0,0.0);
	glEnd();

	m_pOpenGL->EndOrtho();

}
  
And my two "switch" functions look like this:
  
void COpenGL::StartOrtho(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::EndOrtho()
{
	glDisable(GL_BLEND);
    glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
}
  
The problem is that in my render function, anything that is supposed to be drawn between the Start/Endortho calls won''t be drawn. So is there anything wrong with my code? I have tried to check and compare with different tutorials but from what I''ve made of them, this should work. CUselessStuff::NiftyQuote();

@mikaelbauer

Super Sportmatchen - A retro multiplayer competitive sports game project!

you call gluLookAt, meaning that you have changed your modelview matrix, then you switch to an ortho with the clip planes at 1 and -1, but you never load identity into the modelview matrix, meaning that you probably translated so far that your coordinates are not between -1 and 1. Try loading the identity into the modelview before the 2d stuff
Advertisement
You mean like this:

  void COpenGL::StartOrtho(int width, int height){	glMatrixMode(GL_PROJECTION);	glPushMatrix();    glLoadIdentity();    glOrtho(0, width, 0, height, -1, 1);    glMatrixMode(GL_MODELVIEW); 	glLoadIdentity();             // NEW ***    glEnable(GL_BLEND);    glBlendFunc(GL_SRC_ALPHA, GL_ONE);    glEnable(GL_TEXTURE_2D);    glPolygonMode(GL_FRONT, GL_FILL);	glPolygonMode(GL_BACK, GL_FILL);}  


If that''s the case, nope it still doesn''t work Thanks anyway.

CUselessStuff::NiftyQuote();

@mikaelbauer

Super Sportmatchen - A retro multiplayer competitive sports game project!

You should also be pushing and popping the Modelview matrix...


  void COpenGL::StartOrtho(int width, int height){	  glMatrixMode(GL_PROJECTION);	  glPushMatrix();      glLoadIdentity();      glOrtho(0, width, 0, height, -1, 1);      glMatrixMode(GL_MODELVIEW);  glPushMatrix();    // ** NEW **  glLoadIdentity();  // ** NEW **    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::EndOrtho(){	  glDisable(GL_BLEND);  glMatrixMode(GL_MODELVIEW);  glPopMatrix();  glMatrixMode(GL_PROJECTION);  glPopMatrix();}  


Also, as you''re now clipping with a volume as wide and high as the screen (eg 800.0 in X, and 600.0 in Y), maybe try to draw a larger triangle in the centre of the window...

glVertex2f(m_WindowConfig.width/2, m_WindowConfig.height/2);
glVertex2f(m_WindowConfig.width/2 + 10.0, m_WindowConfig.height/2);
glVertex2f(m_WindowConfig.width/2, m_WindowConfig.height/2 + 10);


Other than that, I can''t see anything that would stop your code from working. As a side note, do you realise that you have set you projection so that you must specify Y coordinate in your 2D mode starting from the bottom of the window? (you may be aware of this, but just checking )
The way to set it so you can specify coordinates in the same fashion as regular windowing-style coordinates (ie y=0 at the top) is:

glOrtho(0, width, height, 0, -1, 1);

Hope that helps you out

This topic is closed to new replies.

Advertisement