diff options
Diffstat (limited to 'examples/opengl/box')
-rw-r--r-- | examples/opengl/box/README | 8 | ||||
-rw-r--r-- | examples/opengl/box/box.doc | 18 | ||||
-rw-r--r-- | examples/opengl/box/box.pro | 15 | ||||
-rw-r--r-- | examples/opengl/box/glbox.cpp | 170 | ||||
-rw-r--r-- | examples/opengl/box/glbox.h | 53 | ||||
-rw-r--r-- | examples/opengl/box/globjwin.cpp | 74 | ||||
-rw-r--r-- | examples/opengl/box/globjwin.h | 32 | ||||
-rw-r--r-- | examples/opengl/box/main.cpp | 42 |
8 files changed, 412 insertions, 0 deletions
diff --git a/examples/opengl/box/README b/examples/opengl/box/README new file mode 100644 index 000000000..113f82d6c --- /dev/null +++ b/examples/opengl/box/README @@ -0,0 +1,8 @@ + +The box example + +This example program shows how to use OpenGL in Qt: Put your OpenGL +code in a class inherited from QGLWidget. The resulting subclass may +be used like any other Qt widget, with signals and slots, geometry +management, etc.. + diff --git a/examples/opengl/box/box.doc b/examples/opengl/box/box.doc new file mode 100644 index 000000000..51297fa35 --- /dev/null +++ b/examples/opengl/box/box.doc @@ -0,0 +1,18 @@ +/*! \page opengl-box-example.html + + \ingroup opengl-examples + \title OpenGL Box Example + +This example demonstrates how to use OpenGL in Qt. + +Essentially, all you do is put your OpenGL code in a class inherited +from QGLWidget. This class may then be used like any other Qt widget, +including the use of signals and slots and geometry management. + +See \c{$QTDIR/examples/opengl/box} for the source code. + +*/ + + + + diff --git a/examples/opengl/box/box.pro b/examples/opengl/box/box.pro new file mode 100644 index 000000000..80784531f --- /dev/null +++ b/examples/opengl/box/box.pro @@ -0,0 +1,15 @@ +TEMPLATE = app +TARGET = box + +CONFIG += qt opengl warn_on release +CONFIG -= dlopen_opengl + +DEPENDPATH = ../include + +REQUIRES = opengl + +HEADERS = glbox.h \ + globjwin.h +SOURCES = glbox.cpp \ + globjwin.cpp \ + main.cpp diff --git a/examples/opengl/box/glbox.cpp b/examples/opengl/box/glbox.cpp new file mode 100644 index 000000000..aa1792241 --- /dev/null +++ b/examples/opengl/box/glbox.cpp @@ -0,0 +1,170 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +/**************************************************************************** +** +** This is a simple TQGLWidget displaying an openGL wireframe box +** +** The OpenGL code is mostly borrowed from Brian Pauls "spin" example +** in the Mesa distribution +** +****************************************************************************/ + +#include "glbox.h" + +#if defined(Q_CC_MSVC) +#pragma warning(disable:4305) // init: truncation from const double to float +#endif + +/*! + Create a GLBox widget +*/ + +GLBox::GLBox( TQWidget* parent, const char* name ) + : TQGLWidget( parent, name ) +{ + xRot = yRot = zRot = 0.0; // default object rotation + scale = 1.25; // default object scale + object = 0; +} + + +/*! + Release allocated resources +*/ + +GLBox::~GLBox() +{ + makeCurrent(); + glDeleteLists( object, 1 ); +} + + +/*! + Paint the box. The actual openGL commands for drawing the box are + performed here. +*/ + +void GLBox::paintGL() +{ + glClear( GL_COLOR_BUFFER_BIT ); + + glLoadIdentity(); + glTranslatef( 0.0, 0.0, -10.0 ); + glScalef( scale, scale, scale ); + + glRotatef( xRot, 1.0, 0.0, 0.0 ); + glRotatef( yRot, 0.0, 1.0, 0.0 ); + glRotatef( zRot, 0.0, 0.0, 1.0 ); + + glCallList( object ); +} + + +/*! + Set up the OpenGL rendering state, and define display list +*/ + +void GLBox::initializeGL() +{ + 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 GLBox::resizeGL( int w, int h ) +{ + glViewport( 0, 0, (GLint)w, (GLint)h ); + glMatrixMode( GL_PROJECTION ); + glLoadIdentity(); + glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 15.0 ); + glMatrixMode( GL_MODELVIEW ); +} + + +/*! + Generate an OpenGL display list for the object to be shown, i.e. the box +*/ + +GLuint GLBox::makeObject() +{ + GLuint list; + + list = glGenLists( 1 ); + + glNewList( list, GL_COMPILE ); + + qglColor( white ); // Shorthand for glColor3f or glIndex + + glLineWidth( 2.0 ); + + glBegin( GL_LINE_LOOP ); + glVertex3f( 1.0, 0.5, -0.4 ); + glVertex3f( 1.0, -0.5, -0.4 ); + glVertex3f( -1.0, -0.5, -0.4 ); + glVertex3f( -1.0, 0.5, -0.4 ); + glEnd(); + + glBegin( GL_LINE_LOOP ); + glVertex3f( 1.0, 0.5, 0.4 ); + glVertex3f( 1.0, -0.5, 0.4 ); + glVertex3f( -1.0, -0.5, 0.4 ); + glVertex3f( -1.0, 0.5, 0.4 ); + glEnd(); + + glBegin( GL_LINES ); + glVertex3f( 1.0, 0.5, -0.4 ); glVertex3f( 1.0, 0.5, 0.4 ); + glVertex3f( 1.0, -0.5, -0.4 ); glVertex3f( 1.0, -0.5, 0.4 ); + glVertex3f( -1.0, -0.5, -0.4 ); glVertex3f( -1.0, -0.5, 0.4 ); + glVertex3f( -1.0, 0.5, -0.4 ); glVertex3f( -1.0, 0.5, 0.4 ); + glEnd(); + + glEndList(); + + return list; +} + + +/*! + Set the rotation angle of the object to \e degrees around the X axis. +*/ + +void GLBox::setXRotation( int degrees ) +{ + xRot = (GLfloat)(degrees % 360); + updateGL(); +} + + +/*! + Set the rotation angle of the object to \e degrees around the Y axis. +*/ + +void GLBox::setYRotation( int degrees ) +{ + yRot = (GLfloat)(degrees % 360); + updateGL(); +} + + +/*! + Set the rotation angle of the object to \e degrees around the Z axis. +*/ + +void GLBox::setZRotation( int degrees ) +{ + zRot = (GLfloat)(degrees % 360); + updateGL(); +} diff --git a/examples/opengl/box/glbox.h b/examples/opengl/box/glbox.h new file mode 100644 index 000000000..a04a8122a --- /dev/null +++ b/examples/opengl/box/glbox.h @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +/**************************************************************************** +** +** This is a simple TQGLWidget displaying an openGL wireframe box +** +****************************************************************************/ + +#ifndef GLBOX_H +#define GLBOX_H + +#include <qgl.h> + + +class GLBox : public TQGLWidget +{ + Q_OBJECT + +public: + + GLBox( TQWidget* parent, const char* name ); + ~GLBox(); + +public slots: + + void setXRotation( int degrees ); + void setYRotation( int degrees ); + void setZRotation( int degrees ); + +protected: + + void initializeGL(); + void paintGL(); + void resizeGL( int w, int h ); + + virtual GLuint makeObject(); + +private: + + GLuint object; + GLfloat xRot, yRot, zRot, scale; + +}; + + +#endif // GLBOX_H diff --git a/examples/opengl/box/globjwin.cpp b/examples/opengl/box/globjwin.cpp new file mode 100644 index 000000000..f4af52c36 --- /dev/null +++ b/examples/opengl/box/globjwin.cpp @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qpushbutton.h> +#include <qslider.h> +#include <qlayout.h> +#include <qframe.h> +#include <qmenubar.h> +#include <qpopupmenu.h> +#include <qapplication.h> +#include <qkeycode.h> +#include "globjwin.h" +#include "glbox.h" + + +GLObjectWindow::GLObjectWindow( TQWidget* parent, const char* name ) + : TQWidget( parent, name ) +{ + + // Create a menu + TQPopupMenu *file = new TQPopupMenu( this ); + file->insertItem( "Exit", qApp, SLOT(tquit()), CTRL+Key_Q ); + + // Create a menu bar + TQMenuBar *m = new TQMenuBar( this ); + m->setSeparator( TQMenuBar::InWindowsStyle ); + m->insertItem("&File", file ); + + // Create a nice frame to put around the OpenGL widget + TQFrame* f = new TQFrame( this, "frame" ); + f->setFrameStyle( TQFrame::Sunken | TQFrame::Panel ); + f->setLineWidth( 2 ); + + // Create our OpenGL widget + GLBox* c = new GLBox( f, "glbox"); + + // Create the three sliders; one for each rotation axis + TQSlider* x = new TQSlider ( 0, 360, 60, 0, TQSlider::Vertical, this, "xsl" ); + x->setTickmarks( TQSlider::Left ); + TQObject::connect( x, SIGNAL(valueChanged(int)),c,SLOT(setXRotation(int)) ); + + TQSlider* y = new TQSlider ( 0, 360, 60, 0, TQSlider::Vertical, this, "ysl" ); + y->setTickmarks( TQSlider::Left ); + TQObject::connect( y, SIGNAL(valueChanged(int)),c,SLOT(setYRotation(int)) ); + + TQSlider* z = new TQSlider ( 0, 360, 60, 0, TQSlider::Vertical, this, "zsl" ); + z->setTickmarks( TQSlider::Left ); + TQObject::connect( z, SIGNAL(valueChanged(int)),c,SLOT(setZRotation(int)) ); + + + // Now that we have all the widgets, put them into a nice layout + + // Put the sliders on top of each other + TQVBoxLayout* vlayout = new TQVBoxLayout( 20, "vlayout"); + vlayout->addWidget( x ); + vlayout->addWidget( y ); + vlayout->addWidget( z ); + + // Put the GL widget inside the frame + TQHBoxLayout* flayout = new TQHBoxLayout( f, 2, 2, "flayout"); + flayout->addWidget( c, 1 ); + + // Top level layout, puts the sliders to the left of the frame/GL widget + TQHBoxLayout* hlayout = new TQHBoxLayout( this, 20, 20, "hlayout"); + hlayout->setMenuBar( m ); + hlayout->addLayout( vlayout ); + hlayout->addWidget( f, 1 ); +} diff --git a/examples/opengl/box/globjwin.h b/examples/opengl/box/globjwin.h new file mode 100644 index 000000000..98dad489e --- /dev/null +++ b/examples/opengl/box/globjwin.h @@ -0,0 +1,32 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +/**************************************************************************** +** +** The GLObjectWindow contains a GLBox and three sliders connected to +** the GLBox's rotation slots. +** +****************************************************************************/ + +#ifndef GLOBJWIN_H +#define GLOBJWIN_H + +#include <qwidget.h> + + +class GLObjectWindow : public TQWidget +{ + Q_OBJECT +public: + GLObjectWindow( TQWidget* parent = 0, const char* name = 0 ); + +}; + + +#endif // GLOBJWIN_H diff --git a/examples/opengl/box/main.cpp b/examples/opengl/box/main.cpp new file mode 100644 index 000000000..4a3863232 --- /dev/null +++ b/examples/opengl/box/main.cpp @@ -0,0 +1,42 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for TQt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ +// +// TQt OpenGL example: Box +// +// A small example showing how a GLWidget can be used just as any TQt widget +// +// File: main.cpp +// +// The main() function +// + +#include "globjwin.h" +#include <qapplication.h> +#include <qgl.h> + +/* + The main program is here. +*/ + +int main( int argc, char **argv ) +{ + TQApplication::setColorSpec( TQApplication::CustomColor ); + TQApplication a(argc,argv); + + if ( !TQGLFormat::hasOpenGL() ) { + qWarning( "This system has no OpenGL support. Exiting." ); + return -1; + } + + GLObjectWindow w; + w.resize( 400, 350 ); + a.setMainWidget( &w ); + w.show(); + return a.exec(); +} |