PDA

View Full Version : perspective view problem



angelwarrior
04-16-2008, 01:20 AM
I am coding a small test application in Qt opengl and here is the code
/************************************************** *********/

#ifndef _gmopenglmain_cpp_
#define _gmopenglmain_cpp_


#include<gmopenglmain.h>
#include<ui_xandyunits.h>
#include<QtGui/QDialog>
#include<QtGui/QMessageBox>
#include<QTGui/QColor>



gmopenglmain::
gmopenglmain( QWidget* parent , gmmainwidget *source )
:QGLWidget( parent )
{

object = 0;

m_source = source;

xydisplay = source->getXYDisplayEdit();

Ui_xandyunits *xyobj = new Ui_xandyunits();

QDialog *xyunits = new QDialog(this);

xyobj->setupUi( xyunits );

xyobj->xmeters->setText("50");

xyobj->ymeters->setText("50");

xyunits->exec();;

m_xmeters = (xyobj->xmeters->text()).toInt();

m_ymeters = (xyobj->ymeters->text()).toInt();

unitinmetersx = ((m_xmeters*.1) + 1 + 3);
unitinmetersy = ((m_ymeters*.1) + 1 + 3);


setMouseTracking(true);

}



gmopenglmain::
~gmopenglmain()
{
makeCurrent();
glDeleteLists( object, 1 );
}


/*!
Paint the box. The actual openGL commands for drawing the box are
performed here.
*/

void
gmopenglmain::
paintGL()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glLoadIdentity();

glCallList( object );
}


/*!
Set up the OpenGL rendering state, and define display list
*/

void
gmopenglmain::
initializeGL()
{
QColor black(0,0,0,255);
qglClearColor( black ); // Let OpenGL clear to black
object = makeObject();// Generate an OpenGL display list
glShadeModel( GL_FLAT );
}



/*!
Set up the OpenGL view port, matrix mode, etc.
*/

void
gmopenglmain::
resizeGL( int w, int h )
{
glViewport( 0, 0, (GLint)w, (GLint)h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, unitinmetersx , //left and right
0.0,unitinmetersy, //bottom and top
150.0, 100.0 ); //near and far
glMatrixMode( GL_MODELVIEW );
}


/*!
Generate an OpenGL display list for the object to be shown, i.e. the box
*/

GLuint
gmopenglmain::
makeObject()
{
GLuint list;

list = glGenLists( 1 );

glNewList( list, GL_COMPILE );

qglColor( Qt::blue ); // Shorthand for glColor3f or glIndex
glTranslatef(0.0f,0.0f,0.0f);

glBegin(GL_QUADS); // Draw A Quad
glVertex3f( 0.0f, 1.0f ,0.0f);// Top Left
glVertex3f( 1.0f, 1.0f , 0.0f); // Top Right
glVertex3f( 1.0f, 0.0f, 0.0f);// Bottom Right
glVertex3f( 0.0f,0.0f, 0.0f);// Bottom Left
glEnd(); // Done Drawing The Quad

glEndList();

return list;
}


#endif


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

I am only able to see the box only when my znear is set to 100 in glortho or in glfrustrum

dletozeun
04-16-2008, 02:57 AM
glOrtho( 0.0, unitinmetersx , //left and right
0.0,unitinmetersy, //bottom and top
150.0, 100.0 ); //near and far

This can not work because the near clipping plane must be in front of the far clipping plane. You must remember that znear and zfar are in fact negative values because z axis points toward the eye (eye is in (0,0,0)).
Usually znear is 0.1 and zfar 100

angelwarrior
04-16-2008, 09:06 AM
I am very much thank full for your reply
I have one more doubt on this
You mean to say that the near and far is always on negative z plane.Why it cannot be on the positive z plane

dletozeun
04-16-2008, 09:48 AM
the near and far clipping planes must be in front of the eye which coordinates are (0,0,0). Like I said before the Z axis points toward the eye direction (out of the screen) so all clipping planes must be in the z negative area.

I did not mean that the znear and zfar values must be negative in the glOrtho function. opengl inverts these values automatically.

Bob
04-16-2008, 11:42 AM
dletozeun, what you say about near and far clip planes are true for perspective projections. For orthographic projections, the values can be anything, except being equal.

The mapping between depth and clip planes is linear in orthographic projections, but not in perspective and the mapping doesn't allow for arbitrary clip plane values in the same way. It's a mathematical limitation, not a logical limitation (as in "near clip plane should be closer than the far one", for example).

dletozeun
04-16-2008, 02:17 PM
Ah ok I did not know that... Actually I had never took care of the opengl projection formula.

What I said before is what is more logical in my opinion when I use projections in opengl. Maybe AngelWarrior want to do a special projection. in this case he should take care of where he draws the scene objects.