summaryrefslogtreecommitdiffstats
path: root/examples/opengl/sharedbox
diff options
context:
space:
mode:
Diffstat (limited to 'examples/opengl/sharedbox')
-rw-r--r--examples/opengl/sharedbox/README8
-rw-r--r--examples/opengl/sharedbox/glbox.cpp211
-rw-r--r--examples/opengl/sharedbox/glbox.h58
-rw-r--r--examples/opengl/sharedbox/globjwin.cpp99
-rw-r--r--examples/opengl/sharedbox/globjwin.h39
-rw-r--r--examples/opengl/sharedbox/main.cpp42
-rw-r--r--examples/opengl/sharedbox/sharedbox.doc16
-rw-r--r--examples/opengl/sharedbox/sharedbox.pro14
8 files changed, 487 insertions, 0 deletions
diff --git a/examples/opengl/sharedbox/README b/examples/opengl/sharedbox/README
new file mode 100644
index 000000000..4959294d5
--- /dev/null
+++ b/examples/opengl/sharedbox/README
@@ -0,0 +1,8 @@
+
+The shared box example
+
+This example program is an extension of the "box" example program.
+
+It demonstrates how to use OpenGL display list sharing with QGLWidgets.
+
+
diff --git a/examples/opengl/sharedbox/glbox.cpp b/examples/opengl/sharedbox/glbox.cpp
new file mode 100644
index 000000000..f3ded8888
--- /dev/null
+++ b/examples/opengl/sharedbox/glbox.cpp
@@ -0,0 +1,211 @@
+/****************************************************************************
+**
+** 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 a box
+**
+** The OpenGL code is mostly borrowed from Brian Pauls "spin" example
+** in the Mesa distribution
+**
+****************************************************************************/
+
+#include "glbox.h"
+
+// Initialize static class variables:
+
+// Shared display list id:
+GLuint GLBox::sharedDisplayList = 0;
+
+// Counter keeping track of number of GLBox instances sharing
+// the display list, so that the last instance can delete it:
+int GLBox::sharedListUsers = 0;
+
+
+/*!
+ Create a GLBox widget
+*/
+
+GLBox::GLBox( TQWidget* parent, const char* name, const TQGLWidget* shareWidget )
+ : TQGLWidget( parent, name, shareWidget )
+{
+ xRot = yRot = zRot = 0.0; // default object rotation
+ scale = 1.0; // default object scale
+ object = 0;
+ localDisplayList = 0;
+}
+
+
+/*!
+ Set up the OpenGL rendering state. Robustly access shared display list.
+*/
+
+void GLBox::initializeGL()
+{
+ // Let OpenGL clear to black
+ qglClearColor( black );
+
+ glEnable(GL_DEPTH_TEST);
+
+ if ( sharedListUsers == 0 ) { // No shared list has been made yet
+ sharedDisplayList = makeObject(); // Make one
+ object = sharedDisplayList; // Use it
+ sharedListUsers++; // Keep reference count
+ qDebug( "GLBox %s created shared display list.", name() );
+ }
+ else { // There is a shared diplay list
+ if ( isSharing() ) { // Can we access it?
+ object = sharedDisplayList; // Yes, use it
+ sharedListUsers++; // Keep reference count
+ qDebug( "GLBox %s uses shared display list.", name() );
+ }
+ else {
+ localDisplayList = makeObject(); // No, roll our own
+ object = localDisplayList; // and use that
+ qDebug( "GLBox %s uses private display list.", name() );
+ }
+ }
+}
+
+
+
+/*!
+ Release allocated resources
+*/
+
+GLBox::~GLBox()
+{
+ makeCurrent(); // We're going to do gl calls
+ if ( localDisplayList != 0 ) { // Did we make our own?
+ glDeleteLists( localDisplayList, 1 ); // Yes, delete it
+ qDebug( "GLBox %s deleted private display list.", name() );
+ }
+ else {
+ sharedListUsers--; // No, we used the shared one; keep refcount
+ if ( sharedListUsers == 0 ) { // Any sharers left?
+ glDeleteLists( sharedDisplayList, 1 ); // No, delete it
+ sharedDisplayList = 0;
+ qDebug( "GLBox %s deleted shared display list.", name() );
+ }
+ }
+}
+
+
+/*!
+ Paint the box. The actual openGL commands for drawing the box are
+ performed here.
+*/
+
+void GLBox::paintGL()
+{
+ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
+
+ glMatrixMode( GL_MODELVIEW );
+ glLoadIdentity();
+ glTranslatef( 0.0, 0.0, -3.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 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, 1.0, 10.0);
+}
+
+
+/*!
+ 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 );
+
+ glBegin(GL_QUADS);
+ /* Front face */
+ qglColor( green );
+ glVertex3f(-1.0, 1.0, 1.0);
+ glVertex3f(1.0, 1.0, 1.0);
+ glVertex3f(1.0, -1.0, 1.0);
+ glVertex3f(-1.0, -1.0, 1.0);
+ /* Back face */
+ qglColor( yellow );
+ glVertex3f(-1.0, 1.0, -1.0);
+ glVertex3f(1.0, 1.0, -1.0);
+ glVertex3f(1.0, -1.0, -1.0);
+ glVertex3f(-1.0, -1.0, -1.0);
+ /* Top side face */
+ qglColor( blue );
+ glVertex3f(-1.0, 1.0, 1.0);
+ glVertex3f(1.0, 1.0, 1.0);
+ glVertex3f(1.0, 1.0, -1.0);
+ glVertex3f(-1.0, 1.0, -1.0);
+ /* Bottom side face */
+ qglColor( red );
+ glVertex3f(-1.0, -1.0, 1.0);
+ glVertex3f(1.0, -1.0, 1.0);
+ glVertex3f(1.0, -1.0, -1.0);
+ glVertex3f(-1.0, -1.0, -1.0);
+ 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/sharedbox/glbox.h b/examples/opengl/sharedbox/glbox.h
new file mode 100644
index 000000000..089513d33
--- /dev/null
+++ b/examples/opengl/sharedbox/glbox.h
@@ -0,0 +1,58 @@
+/****************************************************************************
+**
+** 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 a box
+**
+****************************************************************************/
+
+#ifndef GLBOX_H
+#define GLBOX_H
+
+#include <qgl.h>
+
+
+class GLBox : public TQGLWidget
+{
+ Q_OBJECT
+
+public:
+
+ GLBox( TQWidget* parent, const char* name, const TQGLWidget* shareWidget=0 );
+ ~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;
+ GLuint localDisplayList;
+
+ static GLuint sharedDisplayList;
+ static int sharedListUsers;
+
+ GLfloat xRot, yRot, zRot, scale;
+
+};
+
+
+#endif // GLBOX_H
diff --git a/examples/opengl/sharedbox/globjwin.cpp b/examples/opengl/sharedbox/globjwin.cpp
new file mode 100644
index 000000000..f908add4b
--- /dev/null
+++ b/examples/opengl/sharedbox/globjwin.cpp
@@ -0,0 +1,99 @@
+/****************************************************************************
+**
+** 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( "Delete Left TQGLWidget", this,
+ SLOT(deleteFirstWidget()) );
+ 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 nice frames to put around the OpenGL widgets
+ TQFrame* f1 = new TQFrame( this, "frame1" );
+ f1->setFrameStyle( TQFrame::Sunken | TQFrame::Panel );
+ f1->setLineWidth( 2 );
+ TQFrame* f2 = new TQFrame( this, "frame2" );
+ f2->setFrameStyle( TQFrame::Sunken | TQFrame::Panel );
+ f2->setLineWidth( 2 );
+
+ // Create an OpenGL widget
+ c1 = new GLBox( f1, "glbox1" );
+
+ // Create another OpenGL widget that shares display lists with the first
+ c2 = new GLBox( f2, "glbox2", c1 );
+
+ // Create the three sliders; one for each rotation axis
+ // Make them spin the boxes, but not in synch
+ TQSlider* x = new TQSlider ( 0, 360, 60, 0, TQSlider::Vertical, this, "xsl" );
+ x->setTickmarks( TQSlider::Left );
+ connect( x, SIGNAL(valueChanged(int)), c1, SLOT(setXRotation(int)) );
+ connect( x, SIGNAL(valueChanged(int)), c2, SLOT(setZRotation(int)) );
+
+ TQSlider* y = new TQSlider ( 0, 360, 60, 0, TQSlider::Vertical, this, "ysl" );
+ y->setTickmarks( TQSlider::Left );
+ connect( y, SIGNAL(valueChanged(int)), c1, SLOT(setYRotation(int)) );
+ connect( y, SIGNAL(valueChanged(int)), c2, SLOT(setXRotation(int)) );
+
+ TQSlider* z = new TQSlider ( 0, 360, 60, 0, TQSlider::Vertical, this, "zsl" );
+ z->setTickmarks( TQSlider::Left );
+ connect( z, SIGNAL(valueChanged(int)), c1, SLOT(setZRotation(int)) );
+ connect( z, SIGNAL(valueChanged(int)), c2, SLOT(setYRotation(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 widgets inside the frames
+ TQHBoxLayout* flayout1 = new TQHBoxLayout( f1, 2, 2, "flayout1");
+ flayout1->addWidget( c1, 1 );
+ TQHBoxLayout* flayout2 = new TQHBoxLayout( f2, 2, 2, "flayout2");
+ flayout2->addWidget( c2, 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( f1, 1 );
+ hlayout->addWidget( f2, 1 );
+
+}
+
+
+void GLObjectWindow::deleteFirstWidget()
+{
+ // Delete only c1; c2 will keep working and use the shared display list
+ if ( c1 ) {
+ delete c1;
+ c1 = 0;
+ }
+}
diff --git a/examples/opengl/sharedbox/globjwin.h b/examples/opengl/sharedbox/globjwin.h
new file mode 100644
index 000000000..c42f2ea72
--- /dev/null
+++ b/examples/opengl/sharedbox/globjwin.h
@@ -0,0 +1,39 @@
+/****************************************************************************
+**
+** 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 GLBox;
+
+class GLObjectWindow : public TQWidget
+{
+ Q_OBJECT
+public:
+ GLObjectWindow( TQWidget* parent = 0, const char* name = 0 );
+
+protected slots:
+
+ void deleteFirstWidget();
+
+private:
+ GLBox* c1;
+ GLBox* c2;
+};
+
+
+#endif // GLOBJWIN_H
diff --git a/examples/opengl/sharedbox/main.cpp b/examples/opengl/sharedbox/main.cpp
new file mode 100644
index 000000000..13e474007
--- /dev/null
+++ b/examples/opengl/sharedbox/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: Shared Box
+//
+// A small example showing how to use OpenGL display list sharing
+//
+// 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( 550, 350 );
+ a.setMainWidget( &w );
+ w.show();
+ return a.exec();
+}
diff --git a/examples/opengl/sharedbox/sharedbox.doc b/examples/opengl/sharedbox/sharedbox.doc
new file mode 100644
index 000000000..21e7a66a3
--- /dev/null
+++ b/examples/opengl/sharedbox/sharedbox.doc
@@ -0,0 +1,16 @@
+/*! \page opengl-sharedbox-example.html
+
+ \ingroup opengl-examples
+ \title OpenGL Shared Box Example
+
+
+This example program is an extension of the \link
+opengl-box-example.html Box example\endlink.
+
+It demonstrates how to use OpenGL display list sharing with
+QGLWidgets.
+
+See \c{$QTDIR/examples/opengl/sharedbox} for the source code.
+
+*/
+
diff --git a/examples/opengl/sharedbox/sharedbox.pro b/examples/opengl/sharedbox/sharedbox.pro
new file mode 100644
index 000000000..30b92e8ca
--- /dev/null
+++ b/examples/opengl/sharedbox/sharedbox.pro
@@ -0,0 +1,14 @@
+TEMPLATE = app
+TARGET = sharedbox
+
+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