File: TestHandlerGL.h

package info (click to toggle)
transcend 0.3.dfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,768 kB
  • sloc: cpp: 26,891; ansic: 693; sh: 210; makefile: 96; perl: 67
file content (463 lines) | stat: -rw-r--r-- 11,834 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
 * Modification History
 *
 * 2000-December-19		Jason Rohrer
 * Created. 
 *
 * 2001-January-9		Jason Rohrer
 * Changed to use new Primitive3D implementations.  
 *
 * 2001-January-16		Jason Rohrer
 * Changed to use new Translate3D class for drawing primitives.
 *
 * 2001-January-30		Jason Rohrer
 * Updated to comply with new Primitive3D interface. 
 *
 * 2001-January-31		Jason Rohrer
 * Added definition of M_PI if not automatically defined.
 * Added a quit key handler.
 * Added multitexturing to central quad to test multitexture implementations.
 * Had to recommit because of lost log message.
 *
 * 2001-February-2		Jason Rohrer
 * Fixed a bug in the way textures were generated. 
 *
 * 2001-February-24		Jason Rohrer
 * Fixed incorrect delete usage.
 *
 * 2001-August-29		Jason Rohrer
 * Fixed to use new KeyboardHandler interface.  
 */
 
 
#ifndef TEST_HANDLER_GL_INCLUDED
#define TEST_HANDLER_GL_INCLUDED

#include <GL/gl.h>
#include <GL/glut.h>

#include <math.h>

#ifndef M_PI
#define M_PI		3.14159265358979323846
#endif

#include "ScreenGL.h"

#include "MouseHandlerGL.h"
#include "KeyboardHandlerGL.h"
#include "SceneHandlerGL.h"

#include "PrimitiveGL.h"
#include "ObjectGL.h"
#include "minorGems/graphics/3d/LathePrimitive3D.h"
#include "minorGems/graphics/3d/LandscapePrimitive3D.h"
#include "TextureGL.h"
#include "LightingGL.h"
#include "NoLightingGL.h"
#include "DirectionLightingGL.h"
#include "MultiLightingGL.h"
#include "minorGems/math/geometry/Vector3D.h"
#include "minorGems/math/geometry/Angle3D.h"


#include "minorGems/graphics/Color.h"
#include "minorGems/graphics/RGBAImage.h"
#include "minorGems/graphics/filters/BoxBlurFilter.h"
#include "minorGems/graphics/filters/ThresholdFilter.h"
#include "minorGems/graphics/filters/InvertFilter.h"

#include "minorGems/util/random/RandomSource.h"
#include "minorGems/util/random/Noise.h"

#include "minorGems/graphics/Image.h"

/**
 * Test class for ScreenGL implementation.
 *
 * Draws a simple polygon.
 *
 * @author Jason Rohrer 
 */
class TestHandlerGL : 
	public MouseHandlerGL, 
	public KeyboardHandlerGL,
	public SceneHandlerGL { 


	public:
		
		TestHandlerGL( RandomSource *inRandSource, int inNumTriangles );
		
		~TestHandlerGL();
		
		
		/**
		 * Sets up primitives.  Must be called *after* OpenGL context is
		 * constructed.
		 */
		void setupPrimitives();
		
		
		// implement the MouseHandlerGL interface
		void mouseMoved( int inX, int inY );
		void mouseDragged( int inX, int inY );
		void mousePressed( int inX, int inY );
		void mouseReleased( int inX, int inY );
		
		// implement the KeyboardHandlerGL interface
		void keyPressed( unsigned char inKey, int inX, int inY );
		void specialKeyPressed( int inKey, int inX, int inY );
		void keyReleased( unsigned char inKey, int inX, int inY );
		void specialKeyReleased( int inKey, int inX, int inY );
		
		// implement the SceneHandlerGL interface
		void drawScene();
	
	private:
		RandomSource *mRandSource;
		
		int mNumTriangles;
			
		Vector3D ***mTriangles;
		Color ***mColors;
		
		PrimitiveGL *mPrimitive; 
		PrimitiveGL *mPrimitive2;
		PrimitiveGL *mPrimitive3;
		
		LightingGL *mLightingA;
		LightingGL *mLightingB;
		MultiLightingGL *mLighting;
		
		double mCurrentAngle;
		double mAngleStep;
	};

TestHandlerGL::TestHandlerGL( RandomSource *inRandSource, 
	int inNumTriangles ) 
	: mRandSource( inRandSource ), mNumTriangles( inNumTriangles ),
	mTriangles( new Vector3D**[inNumTriangles] ),
	mColors( new Color**[inNumTriangles] ) {
	
	mLightingA = new DirectionLightingGL( new Color( 1.0, 1.0, 1.0 ), 
		new Vector3D( 0, 0, 1 ) );
	
	mLightingB = new DirectionLightingGL( new Color( 0, 0, 1.0 ), 
		new Vector3D( 1, 1, 0 ) );
	
	mLighting = new MultiLightingGL();
	mLighting->addLighting( mLightingA );
	mLighting->addLighting( mLightingB );
	
	for( int i=0; i<mNumTriangles; i++ ) {
		mColors[i] = new Color*[3];
		mTriangles[i] = new Vector3D*[3];
		
		for( int j=0; j<3; j++ ) {
			mTriangles[i][j] = 
				new Vector3D( mRandSource->getRandomDouble() * 10,
					mRandSource->getRandomDouble() * 10 - 5,
					mRandSource->getRandomDouble() * 10 );
			mColors[i][j] =
				new Color( mRandSource->getRandomFloat(),
					mRandSource->getRandomFloat(),
					mRandSource->getRandomFloat(),
					mRandSource->getRandomFloat() );
			}		
		}
	
	} 



TestHandlerGL::~TestHandlerGL() {

	for( int i=0; i<mNumTriangles; i++ ) { 
		for( int j=0; j<3; j++ ) {
			delete mTriangles[i][j];
			delete mColors[i][j];
			}
		delete [] mColors[i];
		delete [] mTriangles[i];		
		}
	delete [] mColors;
	delete [] mTriangles;	
	
	delete mPrimitive;
	delete mPrimitive2;
	delete mPrimitive3;
	
	delete mLightingA;
	delete mLightingB;
	delete mLighting;
	}



void TestHandlerGL::setupPrimitives() {
	// make a flat rectangle object
	Vector3D **corners = new Vector3D*[4];
	corners[0] = new Vector3D( -1, 1, 0 );
	corners[1] = new Vector3D( 1, 1, 0 );
	corners[2] = new Vector3D( -1,-1, 0 );
	corners[3] = new Vector3D( 1, -1, 0 );
	
	double *anchorsX = new double[4];
	double *anchorsY = new double[4];
	
	anchorsX[0] = 0;
	anchorsX[1] = 1;
	anchorsX[2] = 0;
	anchorsX[3] = 1;
	
	anchorsY[0] = 0;
	anchorsY[1] = 0;
	anchorsY[2] = 1;
	anchorsY[3] = 1;
	
	int textSize = 64;
	double fPower = .75;
	
	
	RGBAImage *noiseImage = new RGBAImage( textSize, textSize );
	//double *redNoiseBuffer = new double[ textSize * textSize ];
	//double *greenNoiseBuffer = new double[ textSize * textSize ];
	//double *blueNoiseBuffer = new double[ textSize * textSize ];
	genFractalNoise2d( noiseImage->getChannel(0), 
		textSize, textSize, fPower, true, mRandSource );
	genFractalNoise2d( noiseImage->getChannel(1), 
		textSize, textSize, fPower, true, mRandSource );
	genFractalNoise2d( noiseImage->getChannel(2), 
		textSize, textSize, fPower, true, mRandSource );
	
	Image *selection = new Image( textSize, textSize, 1 );
	genFractalNoise2d( selection->getChannel(0), 
		textSize, textSize, fPower, true, mRandSource );	
	BoxBlurFilter *blur = new BoxBlurFilter( 10 );
	ThresholdFilter *threshold = new ThresholdFilter( 0.5 );
	InvertFilter *invert = new InvertFilter();
	selection->filter( threshold );
	
	noiseImage->setSelection( selection );
	//noiseImage->filter(invert);
	
	delete blur;
	delete threshold;
	delete invert;
	
	noiseImage->clearSelection();
	delete selection;
	/*for( int t=0; t<textSize * textSize; t++ ) {
		unsigned char red = (unsigned char)( redNoiseBuffer[t] * 255 );
		unsigned char green = (unsigned char)( greenNoiseBuffer[t] * 255 );
		unsigned char blue = (unsigned char)( blueNoiseBuffer[t] * 255 );
		textureCharBuff[ t*4 ] = red;
		textureCharBuff[ t*4 + 1 ] = green;
		textureCharBuff[ t*4 + 2 ] = blue;
		textureCharBuff[ t*4 + 3 ] = 255;
		}
	*/
	
	
	
	//genFractalNoise2d( textureBuff, textSize, textSize );
	
	RGBAImage **imageArray = new RGBAImage*[2];
	imageArray[0] = noiseImage;
	imageArray[1] = noiseImage->copy();
	
	//double *alpha = imageArray[1]->getChannel(3);
	//int numPixels = imageArray[1]->getWidth() * imageArray[1]->getHeight();
	//for( int p=0; p<numPixels; p++ ) {
	//	alpha[p] = 0.25;
	//	}
	
	double **anchorArrayX = new double*[2];
	anchorArrayX[0] = anchorsX;
	anchorArrayX[1] = new double[4];
	
	anchorArrayX[1][0] = 0;
	anchorArrayX[1][1] = 5;
	anchorArrayX[1][2] = 0;
	anchorArrayX[1][3] = 5;
	
	
	double **anchorArrayY = new double*[2];
	anchorArrayY[0] = anchorsY;
	
	anchorArrayY[1] = new double[4];
	
	anchorArrayY[1][0] = 0;
	anchorArrayY[1][1] = 0;
	anchorArrayY[1][2] = 5;
	anchorArrayY[1][3] = 5;
	
	//Primitive3D *simplePrimitive3D = 
	//	new Primitive3D( 2, 2, corners, 1, &noiseImage, &anchorsX, &anchorsY );
	
	Primitive3D *simplePrimitive3D = 
		new Primitive3D( 2, 2, corners, 2, imageArray, 
			anchorArrayX, anchorArrayY );
	
	
	
	mPrimitive = new PrimitiveGL( simplePrimitive3D );
	
	//mPrimitive = new LathePrimitiveGL( 4, latheCurve, 5, 2 * M_PI, texture );
	
	//mPrimitive->setTransparent( true );
	
	// all passed in args will be destroyed when primitive is destroyed
	
	
	// setup second primitive
	Vector3D **latheCurve = new Vector3D*[4];
	latheCurve[0] = new Vector3D( 0.5, 1, 0 );
	latheCurve[1] = new Vector3D( 1, 0.5, 0 );
	latheCurve[2] = new Vector3D( 1, -0.5, 0 );
	latheCurve[3] = new Vector3D( 0.5, -1, 0 );
	
	noiseImage = new RGBAImage( textSize, textSize );
	genFractalNoise2d( noiseImage->getChannel(0), 
		textSize, textSize, fPower, true, mRandSource );
	genFractalNoise2d( noiseImage->getChannel(1), 
		textSize, textSize, fPower, true, mRandSource );
	genFractalNoise2d( noiseImage->getChannel(2), 
		textSize, textSize, fPower, true, mRandSource );

	Primitive3D *lathePrimitive3D = 
		new LathePrimitive3D( 4, latheCurve, 15, 2 * M_PI, noiseImage );
	
	lathePrimitive3D->setTransparent( false );
	
	mPrimitive2 = new PrimitiveGL( lathePrimitive3D );
	//mPrimitive2->setBackVisible( true );
	
	// all passed in args will be destroyed when primitive is destroyed
	
	
	noiseImage = new RGBAImage( textSize, textSize );
	genFractalNoise2d( noiseImage->getChannel(0), 
		textSize, textSize, fPower, true, mRandSource );
	genFractalNoise2d( noiseImage->getChannel(1), 
		textSize, textSize, fPower, true, mRandSource );
	genFractalNoise2d( noiseImage->getChannel(2), 
		textSize, textSize, fPower, true, mRandSource );
	
	int landSize = 20;
	unsigned long *intHeights = new unsigned long[ landSize * landSize ];
	genFractalNoise2d( intHeights, landSize, landSize );
	double *heights = new double[ landSize * landSize ];
	for( int l=0; l<landSize*landSize; l++ ) {
		// take one color component as the height
		heights[l] = (double)( intHeights[l] & 0xFF ) / 655;
		//heights[l] = 0;
		}
	
	
	LandscapePrimitive3D *landscapePrimitive3D = 
		new LandscapePrimitive3D( landSize, landSize, heights, noiseImage );
	
	mPrimitive3 = new PrimitiveGL( landscapePrimitive3D );
	
	mCurrentAngle = 0;
	mAngleStep = 0.01;
	}



void TestHandlerGL::mouseMoved( int inX, int inY ) {
	}

void TestHandlerGL::mouseDragged( int inX, int inY ) {
	}

void TestHandlerGL::mousePressed( int inX, int inY ) {
	}

void TestHandlerGL::mouseReleased( int inX, int inY ) {
	}
		
void TestHandlerGL::keyPressed( unsigned char inKey, int inX, int inY ) {
	if( inKey == 'q' || inKey == 'Q' ) {
		exit( 0 );
		}
	}

void TestHandlerGL::specialKeyPressed( int inKey, int inX, int inY ) {
	}



void TestHandlerGL::keyReleased( unsigned char inKey, int inX, int inY ) {
	}



void TestHandlerGL::specialKeyReleased( int inKey, int inX, int inY ) {
	}


		
void TestHandlerGL::drawScene() {	
/*	glDisable( GL_TEXTURE_2D );
	
	glBegin( GL_TRIANGLES );
		for( int p=0; p<mNumTriangles; p++ ) { 

		 
			for( int v=0; v<3; v++ ) {
	    		glColor4f( mColors[p][v]->r, 
					mColors[p][v]->g, mColors[p][v]->b, mColors[p][v]->a );
				glVertex3f( mTriangles[p][v]->mX, 
					mTriangles[p][v]->mY, mTriangles[p][v]->mZ );
	    		}
		
			}
	glEnd();
*/
	Vector3D *pos = new Vector3D( 0, 0, 10 );
	Angle3D *rot = new Angle3D( mCurrentAngle, mCurrentAngle, 0 );
	//Angle3D *rot = new Angle3D( 0, 0, 0 );
	Transform3D *trans = new Transform3D();
	trans->scale( 5 );
	trans->rotate( rot );
	trans->translate( pos );
	
	
	//mPrimitive->setBackVisible( false );
	mPrimitive->draw( trans, mLighting );
	delete rot;
	delete pos;
	delete trans;
	
	pos = new Vector3D( 0, -10, 10 );
	rot = new Angle3D( 0, mCurrentAngle, 0 );
	trans = new Transform3D();
	trans->scale( 20 );
	trans->rotate( rot );
	trans->translate( pos );
	
	mPrimitive3->draw( trans, mLighting );
	delete rot;
	delete pos;
	delete trans;
	
	pos = new Vector3D( 0, 5, 0 );
	rot = new Angle3D( M_PI, mCurrentAngle, 0 );
	trans = new Transform3D();
	trans->scale( 20 );
	trans->rotate( rot );
	trans->translate( pos );
	
	//mPrimitive3->setBackVisible( false );
	//mPrimitive3->draw( trans, mLighting );
	delete rot;
	delete pos;
	delete trans;
	
	mCurrentAngle += mAngleStep;	
	}
	
#endif