source: trunk/examples/demo/opengl/gltexobj.cpp

Last change on this file was 160, checked in by dmik, 19 years ago

Imported table and iconview modules and a bunch of dependent examples from the official release 3.3.1 from Trolltech.

  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1/****************************************************************************
2** $Id: gltexobj.cpp 160 2006-12-11 20:15:57Z dmik $
3**
4** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
5**
6** This file is part of an example program for Qt. This example
7** program may be used, distributed and modified without limitation.
8**
9*****************************************************************************/
10
11/****************************************************************************
12**
13** This is a simple QGLWidget demonstrating the use of QImages for textures.
14**
15** Much of the GL code is inspired by the 'spectex' and 'texcyl'
16** public domain demo programs by Brian Paul.
17**
18****************************************************************************/
19
20#include "gltexobj.h"
21#include <qimage.h>
22
23/*!
24 Create a GLTexobj widget
25*/
26
27GLTexobj::GLTexobj( QWidget* parent, const char* name, WFlags f )
28 : GLControlWidget( parent, name, 0, f ), impX( -2 ), impY( 0.5 ), impZ( 1 )
29{
30 object = 0;
31}
32
33/*!
34 Release allocated resources
35*/
36
37GLTexobj::~GLTexobj()
38{
39 makeCurrent();
40 glDeleteLists( object, 1 );
41}
42
43
44/*!
45 Paint the texobj. The actual openGL commands for drawing the texobj are
46 performed here.
47*/
48
49void GLTexobj::paintGL()
50{
51 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
52 glPushMatrix();
53 transform();
54 glCallList( object );
55 drawText();
56 glPushAttrib( GL_LIGHTING_BIT | GL_TEXTURE_BIT );
57 glDisable( GL_LIGHTING );
58 glDisable( GL_TEXTURE_2D );
59 qglColor( green );
60 glLineWidth( 1.0 );
61 glBegin( GL_LINES );
62 {
63 glVertex3f( 0.0f, 0.0f, 1.0f );
64 glVertex3f( 0.98f, 0.98f, 0.98f );
65 }
66 glEnd();
67 renderText( 1.0, 1.0, 1.0, "Can", QFont( "helvetica", 12, QFont::Bold, TRUE ) );
68 glPopAttrib();
69 glPopMatrix();
70}
71
72
73/*!
74 Set up the OpenGL rendering state, and define display list
75*/
76
77void GLTexobj::initializeGL()
78{
79 // Set up the lights
80
81 GLfloat whiteDir[4] = {2.0, 2.0, 2.0, 1.0};
82 GLfloat whiteAmb[4] = {1.0, 1.0, 1.0, 1.0};
83 GLfloat lightPos[4] = {30.0, 30.0, 30.0, 1.0};
84
85 glEnable(GL_LIGHTING);
86 glEnable(GL_LIGHT0);
87 glEnable(GL_DEPTH_TEST);
88 glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
89 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, whiteAmb);
90
91 glMaterialfv(GL_FRONT, GL_DIFFUSE, whiteDir);
92 glMaterialfv(GL_FRONT, GL_SPECULAR, whiteDir);
93 glMaterialf(GL_FRONT, GL_SHININESS, 20.0);
94
95 glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteDir); // enable diffuse
96 glLightfv(GL_LIGHT0, GL_SPECULAR, whiteDir); // enable specular
97 glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
98
99 // Set up the textures
100
101 QImage tex1, tex2, buf;
102
103 if ( !buf.load( "opengl/gllogo.bmp" ) ) { // Load first image from file
104 qWarning( "Could not read image file, using single-color instead." );
105 QImage dummy( 128, 128, 32 );
106 dummy.fill( Qt::green.rgb() );
107 buf = dummy;
108 }
109 tex1 = QGLWidget::convertToGLFormat( buf ); // flipped 32bit RGBA
110
111 if ( !buf.load( "opengl/qtlogo.bmp" ) ) { // Load first image from file
112 qWarning( "Could not read image file, using single-color instead." );
113 QImage dummy( 128, 128, 32 );
114 dummy.fill( Qt::red.rgb() );
115 buf = dummy;
116 }
117 tex2 = QGLWidget::convertToGLFormat( buf ); // flipped 32bit RGBA
118
119 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
120 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
121 glEnable( GL_TEXTURE_2D );
122
123 // Set up various other stuff
124
125 glClearColor( 0.0, 0.0, 0.0, 0.0 ); // Let OpenGL clear to black
126 glEnable( GL_CULL_FACE ); // don't need Z testing for convex objects
127 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
128
129 // Make the object display list
130
131 object = makeObject( tex1, tex2 ); // Generate an OpenGL display list
132}
133
134
135
136/*!
137 Set up the OpenGL view port, matrix mode, etc.
138*/
139
140void GLTexobj::resizeGL( int w, int h )
141{
142 glViewport( 0, 0, w, h );
143 glMatrixMode( GL_PROJECTION );
144 glLoadIdentity();
145 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
146 glMatrixMode( GL_MODELVIEW );
147 glLoadIdentity();
148 glTranslatef( 0.0, 0.0, -70.0 );
149}
150
151
152/*!
153 Generate an OpenGL display list for the object to be shown, i.e. the texobj
154*/
155
156GLuint GLTexobj::makeObject( const QImage& tex1, const QImage& tex2 )
157{
158 GLUquadricObj* q = gluNewQuadric();
159 GLuint cylinderObj = glGenLists(1);
160 glNewList( cylinderObj, GL_COMPILE );
161
162 glTranslatef( 0.0, 0.0, -1.0 );
163
164 // cylinder
165 glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1.width(), tex1.height(), 0,
166 GL_RGBA, GL_UNSIGNED_BYTE, tex1.bits() );
167 gluQuadricTexture( q, GL_TRUE );
168 gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
169
170 // end cap
171 glTexImage2D( GL_TEXTURE_2D, 0, 3, tex2.width(), tex2.height(), 0,
172 GL_RGBA, GL_UNSIGNED_BYTE, tex2.bits() );
173 glTranslatef( 0.0, 0.0, 2.0 );
174 gluDisk( q, 0.0, 0.6, 24, 1 );
175
176 // other end cap
177 glTranslatef( 0.0, 0.0, -2.0 );
178 gluQuadricOrientation( q, (GLenum)GLU_INSIDE );
179 gluDisk( q, 0.0, 0.6, 24, 1 );
180
181 glEndList();
182 gluDeleteQuadric( q );
183
184 return cylinderObj;
185}
186
187void GLTexobj::animate()
188{
189 xRot += impX;
190 yRot += impY;
191 zRot -= impZ;
192
193 impX -= impX * 0.05;
194 impY -= impY * 0.05;
195 impZ -= impZ * 0.05;
196
197 if ( impX > 0.1 || impY > 0.1 || impZ > 0.1 ||
198 impX < -0.1 || impY < -0.1 || impZ < -0.1 )
199 updateGL();
200}
201
202void GLTexobj::setRotationImpulse( double x, double y, double z )
203{
204 if ( animation ) {
205 impX += 180*x;
206 impY += 180*y;
207 impZ += 180*z;
208 } else {
209 GLControlWidget::setRotationImpulse( x, y, z );
210 }
211}
Note: See TracBrowser for help on using the repository browser.