PDA

View Full Version : opengl



rick_st3
16th June 2008, 11:58
I wrote a simple code to create an opengl window with a square in it...
here are the relevent files....




#ifndef HEADER_H
#define HEADER_H

#include<QtOpenGl>

class QGLWidget;


class ric:public QGLWidget
{

public:


GLvoid glInitialize();
GLvoid glResize(int width,int height);
GLvoid paintGL();

};

#endif







#include<QGLWidget>

#include"header.h"


GLvoid ric::glInitialize()
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
glShadeModel(GL_SMOOTH);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}

GLvoid ric::glResize(int width,int height)
{
if (height==0)
height = 1;

glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

GLvoid ric::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glTranslatef(-1.5f,0.0f,-6.0f);

glColor3f(0.0f,1.0f,0.25f);
glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25f,0.25f,0.0f);
glVertex3f(0.25f,0.75f,0.0f);
glVertex3f(0.75f,0.75f,0.0f);
glVertex3f(0.75f,0.25f,0.0f);
glEnd();
glFlush();
}






#include<QApplication>

#include"header.h"

int main(int argc,char *argv[])
{
QApplication app(argc,argv);

ric window;
window.glInitialize();
window.glResize(640,480);
window.paintGL();
window.show();

return app.exec();
}



now after compiling and running the project, all I get is a plane blank window... I cannot understand the problem... please help..!! thanks..

Rayven
16th June 2008, 14:50
Try changing the method definitions to:



#ifndef HEADER_H
#define HEADER_H

#include<QtOpenGl>

class QGLWidget;


class ric:public QGLWidget
{

protected: // Notice these are protected, they are called automatically by QGlWidget
void initializeGL();
void resizeGL();
void paintGL( );
};

#endif
OpenGL is looking for these functions to be overloaded specifically. You dont need to call initializeGL, resizeGL or paintGL directly as they will be handled by the QGlWidget calls. If you need to repaint the scene, call updateGL( ). So your main would look like:

#include<QApplication>

#include"header.h"

int main(int argc,char *argv[])
{
QApplication app(argc,argv);

ric window;
window.show(); // Show calls updateGL( ) for you, no need to for this example

return app.exec();
}

See the API docs http://doc.trolltech.com/4.4/qglwidget.html for a good setup example, assuming you are using Qt4.4

rick_st3
16th June 2008, 15:31
but i am using the resize function with 2 arguments... so if i dont provide the values explicitly then how would the compiler react..?? i haven't initialized the height & width as well...

and i made the changes as suggested...i am still getting the blank window... please help..!!

Rayven
16th June 2008, 15:54
Whoops, forgot to put the width and height into the resize.


void resizeGL(int w, int h)

Also, try setting the matrix mode before and after setting the ortho in paintGL:


glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity ( ) ;

rick_st3
16th June 2008, 16:00
there you go...!!! thanks a ton mate... but can you jus explain to me what you just did..??

spud
16th June 2008, 16:34
You need to read the documentation for QGLWidget(and probably for QWidget as well)
The three funcions initializeGL(), resizeGL() and paintGL( ) are event handlers that get called by the Qt framework when appropriate.

initializeGL, once before the widget is shown the first time
resizeGL, every time the user changes the size of the widget
paintGL, every time the widget needs to redraw itself

You never need to call these functions explicitely.
If you want to change the size of the window, you need to call QWidget::resize()

rick_st3
16th June 2008, 17:12
ahh... got it... thanks mate...