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>
{
Q_OBJECT
public:
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
private:
GLUquadricObj *pobj;
GLfloat rotationX, rotationY, rotationZ;
};
#endif
//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
To copy to clipboard, switch view to plain text mode
Main windows cpp file...
#include <QtGui>
#include <QtOpenGL>
#include <math.h>
#include "glwidget1.h"
GLWidget
::GLWidget(QWidget *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); }
{ lastPos = event->pos(); }
{ 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(); }
#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(); }
To copy to clipboard, switch view to plain text mode
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
) { }
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(); }*/
#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(); }*/
To copy to clipboard, switch view to plain text mode
glwidget2.h header file...
#ifndef GLWIDGET2_H
#define GLWIDGET2_H
#include <QGLWidget>
{ Q_OBJECT
public:
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
/* void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);*/
private:
GLUquadricObj *pobj;
GLfloat rotationX, rotationY, rotationZ; };
#endif // GLWIDGET2_H
#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
To copy to clipboard, switch view to plain text mode
glwidget3.h header file for the third GLWidget
#ifndef GLWIDGET3_H
#define GLWIDGET3_H
#include <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;
GLfloat rotationX, rotationY, rotationZ; };
#endif // GLWIDGET3_H
#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
To copy to clipboard, switch view to plain text mode
glwidget3.cpp file ..... third widgets cpp file...
#include "glwidget3.h"
#include <QtGui>
#include <QtOpenGL>
#include <math.h>
GLWidget_rear
::GLWidget_rear(QWidget *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();
}*/
#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();
}*/
To copy to clipboard, switch view to plain text mode
main.cpp file for the program...
#include <QApplication>
#include <QDesktopWidget>
#include "window.h"
int main(int argc, char *argv[])
Window window;
window.show();
return app.exec(); }
#include <QApplication>
#include <QDesktopWidget>
#include "window.h"
int main(int argc, char *argv[])
{ QApplication app(argc, argv);
Window window;
window.show();
return app.exec(); }
To copy to clipboard, switch view to plain text mode
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;
{ Q_OBJECT
public:
Window();
private:
GLWidget *glWidget1, //glWidget1 -> main Design Widget
*glWidget2, //glWidget2 -> Fin Design Widget
*glWidget3 ; //glWidget3 -> rear View Design Widget */
};
#endif
#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
To copy to clipboard, switch view to plain text mode
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;
vLayout->addWidget(glWidget2);
vLayout->addWidget(glWidget3);
hLayout->addWidget(glWidget1);
hLayout->addLayout(vLayout);
setLayout(hLayout);
glWidget1->setSizePolicy(sizePolicy1);
glWidget2->setSizePolicy(sizePolicy2);
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")); }
#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")); }
To copy to clipboard, switch view to plain text mode
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
######################################################################
# 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
To copy to clipboard, switch view to plain text mode
Bookmarks