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