PDA

View Full Version : Opengl



manmohan
12th February 2009, 16:56
Hi to every one I am't a good programmer with the qt but I am trying to design a widget which has 3 Opengl widgets....

can any one help me with any kind of information that will help to create this... widget...

This widget has a main widget which will be expanding on the hoizontal way only... and then I would like to display 3 cylinders in all the 3 widgets....

I was successful in drawing a single cylinder in the main widget... but after that I have used the same code in the other 2 widgets but there's no display on the screen....

please help me out... I will post my code.... also if necessary....

manmohan
13th February 2009, 06:44
Here is the code in the zipped format....
This program was compiled in Linux...
I have created a cpp file and a header file for each of the three widgets so this might be big....

//GLWidget1 this is the main window header file.....
#ifndef GLWIDGET1_H
#define GLWIDGET1_H
#include <QGLWidget>
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
GLWidget(QWidget *parent = 0);
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
private:
GLUquadricObj *pobj;
QPoint lastPos;
GLfloat rotationX, rotationY, rotationZ;
};
#endif

Main windows cpp file...

#include <QtGui>
#include <QtOpenGL>
#include <math.h>
#include "glwidget1.h"
GLWidget::GLWidget(QWidget *parent)
: QGLWidget(parent)
{ }
void GLWidget::initializeGL()
{ qglClearColor(Qt::gray);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE); }
void GLWidget::paintGL()
{ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -15.0);
glRotatef(rotationX, 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);
glRotatef(rotationZ, 0.0, 0.0, 1.0);
pobj = gluNewQuadric();
gluQuadricDrawStyle(pobj, GLU_LINE);
gluQuadricNormals(pobj, GLU_SMOOTH);
qglColor(Qt::red);
gluCylinder(pobj, 1, 1, 3, 30, 30); }
void GLWidget::resizeGL(int width, int height)
{ glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat x = GLfloat(width) / height;
glFrustum(-x, +x, -1.0, +1.0, 4.0, 20.0);
glMatrixMode(GL_MODELVIEW); }
void GLWidget::mousePressEvent(QMouseEvent *event)
{ lastPos = event->pos(); }
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{ GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();
if (event->buttons() & Qt::LeftButton) {
rotationX += 180 * dy;
rotationY += 180 * dx;
updateGL();
} else if (event->buttons() & Qt::RightButton) {
rotationX += 180 * dy;
rotationZ += 180 * dx;
updateGL(); }
lastPos = event->pos(); }

this code is for the other window............. //glwidget2.cpp file...


#include "glwidget2.h"
#include <QtGui>
#include <QtOpenGL>
#include <math.h>
GLWidget_fin::GLWidget_fin(QWidget *parent)
: QGLWidget(parent)
{ }
void GLWidget_fin::initializeGL()
{ qglClearColor(Qt::gray);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE); }
void GLWidget_fin::paintGL()
{ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -15.0);
glRotatef(rotationX, 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);
glRotatef(rotationZ, 0.0, 0.0, 1.0);
pobj = gluNewQuadric();
gluQuadricDrawStyle(pobj, GLU_LINE);
gluQuadricNormals(pobj, GLU_SMOOTH);
qglColor(Qt::red);
gluCylinder(pobj, 1, 1, 1, 30, 30); }
void GLWidget_fin::resizeGL(int width, int height)
{ glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat x = GLfloat(width) / height;
glFrustum(-x, +x, -1.0, +1.0, 10.0, 40.0);
glMatrixMode(GL_MODELVIEW); }
/*void GLWidget_fin::mousePressEvent(QMouseEvent *event)
{ lastPos = event->pos(); }
void GLWidget_fin::mouseMoveEvent(QMouseEvent *event)
{ GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();
if (event->buttons() & Qt::LeftButton) {
rotationX += 180 * dy;
rotationY += 180 * dx;
updateGL();
} else if (event->buttons() & Qt::RightButton) {
rotationX += 180 * dy;
rotationZ += 180 * dx;
updateGL(); }
lastPos = event->pos(); }*/
glwidget2.h header file...

#ifndef GLWIDGET2_H
#define GLWIDGET2_H
#include <QGLWidget>
class GLWidget_fin : public QGLWidget
{ Q_OBJECT
public:
GLWidget_fin(QWidget *parent = 0);
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
/* void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);*/
private:
GLUquadricObj *pobj;
QPoint lastPos;
GLfloat rotationX, rotationY, rotationZ; };
#endif // GLWIDGET2_H
glwidget3.h header file for the third GLWidget

#ifndef GLWIDGET3_H
#define GLWIDGET3_H
#include <QGLWidget>
class GLWidget_rear : public QGLWidget
{ Q_OBJECT
public:
GLWidget_rear(QWidget *parent = 0);
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
/*void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);*/
private:
GLUquadricObj *pobj;
QPoint lastPos;
GLfloat rotationX, rotationY, rotationZ; };
#endif // GLWIDGET3_H
glwidget3.cpp file ..... third widgets cpp file...

#include "glwidget3.h"
#include <QtGui>
#include <QtOpenGL>
#include <math.h>
GLWidget_rear::GLWidget_rear(QWidget *parent)
: QGLWidget(parent)
{ }
void GLWidget_rear::initializeGL()
{ qglClearColor(Qt::gray);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE); }
void GLWidget_rear::paintGL()
{ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -15.0);
glRotatef(rotationX, 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);
glRotatef(rotationZ, 0.0, 0.0, 1.0);
pobj = gluNewQuadric();
gluQuadricDrawStyle(pobj, GLU_LINE);
gluQuadricNormals(pobj, GLU_SMOOTH);
qglColor(Qt::red);
gluCylinder(pobj, 1, 1, 3, 30, 30); }
void GLWidget_rear::resizeGL(int width, int height)
{ glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat x = GLfloat(width) / height;
glFrustum(-x, +x, -1.0, +1.0, 4.0, 10.0);
glMatrixMode(GL_MODELVIEW); }
/*void GLWidget_rear::mousePressEvent(QMouseEvent *event)
{ lastPos = event->pos(); }
void GLWidget_rear::mouseMoveEvent(QMouseEvent *event)
{ GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();
if (event->buttons() & Qt::LeftButton) {
rotationX += 180 * dy;
rotationY += 180 * dx;
updateGL();
} else if (event->buttons() & Qt::RightButton) {
rotationX += 180 * dy;
rotationZ += 180 * dx;
updateGL(); }
lastPos = event->pos();
}*/
main.cpp file for the program...

#include <QApplication>
#include <QDesktopWidget>
#include "window.h"
int main(int argc, char *argv[])
{ QApplication app(argc, argv);
Window window;
window.show();
return app.exec(); }
this 3 widgets are put in together into a window widgets whos code is
window.h

#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
#include <QDialog>
class GLWidget;
class Window : public QWidget
{ Q_OBJECT
public:
Window();
private:
GLWidget *glWidget1, //glWidget1 -> main Design Widget
*glWidget2, //glWidget2 -> Fin Design Widget
*glWidget3 ; //glWidget3 -> rear View Design Widget */
};
#endif
and the .cpp file of the main window is

#include <QtGui>
#include "glwidget1.h"
#include "glwidget2.h"
#include "glwidget3.h"
#include "window.h"
Window::Window()
{ glWidget1 = new GLWidget;
glWidget2 = new GLWidget;
glWidget3 = new GLWidget;
QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->addWidget(glWidget2);
vLayout->addWidget(glWidget3);
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addWidget(glWidget1);
hLayout->addLayout(vLayout);
setLayout(hLayout);
QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Expanding);
glWidget1->setSizePolicy(sizePolicy1);
QSizePolicy sizePolicy2(QSizePolicy::Fixed, QSizePolicy::Fixed);
glWidget2->setSizePolicy(sizePolicy2);
QSizePolicy sizePolicy3(QSizePolicy::Fixed, QSizePolicy::Fixed);
glWidget2->setSizePolicy(sizePolicy3);
glWidget1->setMaximumSize(QSize(1000, 1000));
glWidget1->setMinimumSize(QSize(800, 800));
glWidget2->setMaximumSize(QSize(400,400));
glWidget2->setMinimumSize(QSize(400,400));
glWidget2->resize(400,400);
glWidget3->setMaximumSize(QSize(400,400));
glWidget3->setMinimumSize(QSize(400,400));
setWindowTitle(tr("Hello GL")); }
this the pro file that was generated to which I have added QT += opengl for running the opengl program...


################################################## ####################
# Automatically generated by qmake (2.01a) Wed Feb 11 14:50:13 2009
################################################## ####################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
QT += opengl
# Input
HEADERS += glwidget1.h glwidget2.h glwidget3.h window.h
SOURCES += glwidget1.cpp glwidget2.cpp glwidget3.cpp main.cpp window.cpp

john_god
14th February 2009, 19:32
I've notice some issues in your code, you define 3 classes, GLWidget, GLWidget_fin, GLWidget_rear, but the 3 variables glWidget1, glWidget2, and glWidget3 are all from
GLWidget type.
Also don't see the point of declaring 3 classes with the same code.
I compile the code of your program and the widget doesnt fit in my screen, is your intention to do the widget with fixed dimensions ?
As far only one cilinder being show, sorry I also dond't know :confused:
Gurus where are you ?

manmohan
16th February 2009, 05:52
Thanks dude for checking, but I have declared the same class because once I get the display then I will be changing the display of the widgets in a different way.....


Any body please help...............

john_god
16th February 2009, 19:15
Also you may checkout in the Qt Assistant the "GLWidget" and read it. I think there's a issue with the widget's OpenGL rendering context, witch has to be made current with the makeCurrent() function. This would explain why only the first cilinder is being draw.

manmohan
24th February 2009, 10:44
Thanks dude I have tried that too but there was no improvement.....

Please help me yar....

this will help me in developing a g8t app....:)

Thanks for the help.....

manmohan
27th February 2009, 10:23
Hey dude is there any forum from where I could get help regarding this program please let me know......

Thank's
Mohan