-
Notifications
You must be signed in to change notification settings - Fork 0
/
four.cpp
1505 lines (1177 loc) · 37.3 KB
/
four.cpp
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <omp.h>
#define _USE_MATH_DEFINES
#include <math.h>
#ifdef WIN32
#include <windows.h>
#pragma warning(disable:4996)
#endif
#ifdef WIN32
#include "glew.h"
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include "glut.h"
#include "glui.h"
#include "cl.h"
#include "cl_gl.h"
// title of these windows:
const char *WINDOWTITLE = { "OpenCL/OpenGL Particle System -- Joe Parallel" };
const char *GLUITITLE = { "User Interface Window" };
// random parameters:
const float XMIN = { -100.0 };
const float XMAX = { 100.0 };
const float YMIN = { -100.0 };
const float YMAX = { 100.0 };
const float ZMIN = { -100.0 };
const float ZMAX = { 100.0 };
const float VMIN = { -100. };
const float VMAX = { 100. };
const int NUM_PARTICLES = 1024*1024;
const int LOCAL_SIZE = 64;
const char *CL_FILE_NAME = { "particles.cl" };
const char *CL_BINARY_NAME = { "particles.nv" };
const int GLUITRUE = { true };
const int GLUIFALSE = { false };
#define ESCAPE 0x1b
const int INIT_WINDOW_SIZE = { 700 }; // window size in pixels
const float ANGFACT = { 1. };
const float SCLFACT = { 0.005f };
const float MINSCALE = { 0.001f };
const int LEFT = { 4 };
const int MIDDLE = { 2 };
const int RIGHT = { 1 };
enum Projections
{
ORTHO,
PERSP
};
enum ButtonVals
{
GO,
PAUSE,
RESET,
QUIT
};
const float BACKCOLOR[ ] = { 0., 0., 0., 0. };
const GLfloat AXES_COLOR[ ] = { 1., .5, 0. };
const GLfloat AXES_WIDTH = { 3. };
//
// structs we will need later:
struct xyzw
{
float x, y, z, w;
};
struct rgba
{
float r, g, b, a;
};
// non-constant global variables:
int ActiveButton; // current button that is down
GLuint AxesList; // list to hold the axes
int AxesOn; // ON or OFF
GLUI * Glui; // instance of glui window
int GluiWindow; // the glut id for the glui window
int MainWindow; // window id for main graphics window
int Paused;
GLfloat RotMatrix[4][4]; // set by glui rotation widget
float Scale, Scale2; // scaling factors
GLuint SphereList;
int WhichProjection; // ORTHO or PERSP
int Xmouse, Ymouse; // mouse values
float Xrot, Yrot; // rotation angles in degrees
float TransXYZ[3]; // set by glui translation widgets
double ElapsedTime;
int ShowPerformance;
size_t GlobalWorkSize[3] = { NUM_PARTICLES, 1, 1 };
size_t LocalWorkSize[3] = { LOCAL_SIZE, 1, 1 };
GLuint hPobj;
GLuint hCobj;
cl_mem dPobj;
cl_mem dCobj;
struct xyzw * hVel;
cl_mem dVel;
cl_command_queue CmdQueue;
cl_device_id Device;
cl_kernel Kernel;
cl_platform_id Platform;
cl_program Program;
cl_platform_id PlatformID;
// function prototypes:
void PrintOpenclInfo();
void SelectOpenclDevice();
char * Vendor( cl_uint );
char * Type( cl_device_type );
inline
float
SQR( float x )
{
return x * x;
}
void Animate( );
void Axes( float );
void Buttons( int );
void Display( );
void DoRasterString( float, float, float, char * );
void DoStrokeString( float, float, float, float, char * );
void InitCL( );
void InitGlui( );
void InitGraphics( );
void InitLists( );
bool IsCLExtensionSupported( const char * );
void Keyboard( unsigned char, int, int );
void MouseButton( int, int, int, int );
void MouseMotion( int, int );
void PrintCLError( cl_int, char * = "", FILE * = stderr );
void PrintOpenclInfo();
void Quit( );
float Ranf( float, float );
void Reset( );
void ResetParticles( );
void Resize( int, int );
void SelectOpenclDevice();
void Traces( int );
void Visibility( int );
//
// main Program:
//
int
main( int argc, char *argv[ ] )
{
glutInit( &argc, argv );
InitGraphics( );
InitLists( );
InitCL( );
Reset( );
InitGlui( );
glutMainLoop( );
return 0;
}
void
Animate( )
{
cl_int status;
double time0, time1;
// acquire the vertex buffers from opengl:
glutSetWindow( MainWindow );
glFinish( );
status = clEnqueueAcquireGLObjects( CmdQueue, 1, &dPobj, 0, NULL, NULL );
PrintCLError( status, "clEnqueueAcquireGLObjects (1): " );
status = clEnqueueAcquireGLObjects( CmdQueue, 1, &dCobj, 0, NULL, NULL );
PrintCLError( status, "clEnqueueAcquireGLObjects (2): " );
if( ShowPerformance )
time0 = omp_get_wtime( );
// 11. enqueue the Kernel object for execution:
cl_event wait;
status = clEnqueueNDRangeKernel( CmdQueue, Kernel, 1, NULL, GlobalWorkSize, LocalWorkSize, 0, NULL, &wait );
PrintCLError( status, "clEnqueueNDRangeKernel: " );
if( ShowPerformance )
{
status = clWaitForEvents( 1, &wait );
PrintCLError( status, "clWaitForEvents: " );
time1 = omp_get_wtime( );
ElapsedTime = time1 - time0;
}
clFinish( CmdQueue );
status = clEnqueueReleaseGLObjects( CmdQueue, 1, &dCobj, 0, NULL, NULL );
PrintCLError( status, "clEnqueueReleaseGLObjects (2): " );
status = clEnqueueReleaseGLObjects( CmdQueue, 1, &dPobj, 0, NULL, NULL );
PrintCLError( status, "clEnqueueReleaseGLObjects (2): " );
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
//
// glui buttons callback:
//
void
Buttons( int id )
{
cl_int status;
switch( id )
{
case GO:
GLUI_Master.set_glutIdleFunc( Animate );
break;
case PAUSE:
Paused = ! Paused;
if( Paused )
GLUI_Master.set_glutIdleFunc( NULL );
else
GLUI_Master.set_glutIdleFunc( Animate );
break;
case RESET:
Reset( );
ResetParticles( );
status = clEnqueueWriteBuffer( CmdQueue, dVel, CL_FALSE, 0, 4*sizeof(float)*NUM_PARTICLES, hVel, 0, NULL, NULL );
PrintCLError( status, "clEneueueWriteBuffer: " );
GLUI_Master.set_glutIdleFunc( NULL );
Glui->sync_live( );
glutSetWindow( MainWindow );
glutPostRedisplay( );
break;
case QUIT:
Quit( );
break;
default:
fprintf( stderr, "Don't know what to do with Button ID %d\n", id );
}
}
//
// draw the complete scene:
//
void
Display( )
{
glutSetWindow( MainWindow );
glDrawBuffer( GL_BACK );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glEnable( GL_DEPTH_TEST );
glShadeModel( GL_FLAT );
GLsizei vx = glutGet( GLUT_WINDOW_WIDTH );
GLsizei vy = glutGet( GLUT_WINDOW_HEIGHT );
GLsizei v = vx < vy ? vx : vy; // minimum dimension
GLint xl = ( vx - v ) / 2;
GLint yb = ( vy - v ) / 2;
glViewport( xl, yb, v, v );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
if( WhichProjection == ORTHO )
glOrtho( -300., 300., -300., 300., 0.1, 2000. );
else
gluPerspective( 50., 1., 0.1, 2000. );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
gluLookAt( 0., -100., 800., 0., -100., 0., 0., 1., 0. );
glTranslatef( (GLfloat)TransXYZ[0], (GLfloat)TransXYZ[1], -(GLfloat)TransXYZ[2] );
glRotatef( (GLfloat)Yrot, 0., 1., 0. );
glRotatef( (GLfloat)Xrot, 1., 0., 0. );
glMultMatrixf( (const GLfloat *) RotMatrix );
glScalef( (GLfloat)Scale, (GLfloat)Scale, (GLfloat)Scale );
float scale2 = 1.f + Scale2; // because glui translation starts at 0.
if( scale2 < MINSCALE )
scale2 = MINSCALE;
glScalef( (GLfloat)scale2, (GLfloat)scale2, (GLfloat)scale2 );
glDisable( GL_FOG );
if( AxesOn != GLUIFALSE )
glCallList( AxesList );
// ****************************************
// Here is where you draw the current state of the particles:
// ****************************************
glBindBuffer( GL_ARRAY_BUFFER, hPobj );
glVertexPointer( 4, GL_FLOAT, 0, (void *)0 );
glEnableClientState( GL_VERTEX_ARRAY );
glBindBuffer( GL_ARRAY_BUFFER, hCobj );
glColorPointer( 4, GL_FLOAT, 0, (void *)0 );
glEnableClientState( GL_COLOR_ARRAY );
glPointSize( 3. );
glDrawArrays( GL_POINTS, 0, NUM_PARTICLES );
glPointSize( 1. );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
// ****************************************
// Here is where you draw your spheres. You define them, however, in the InitLists( ) function.
// ****************************************
glCallList( SphereList );
if( ShowPerformance )
{
char str[128];
sprintf( str, "%6.1f GigaParticles/Sec", (float)NUM_PARTICLES/ElapsedTime/1000000000. );
glDisable( GL_DEPTH_TEST );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0., 100., 0., 100. );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3f( 1., 1., 1. );
DoRasterString( 5., 95., 0., str );
}
glutSwapBuffers( );
glFlush( );
}
//
// use glut to display a string of characters using a raster font:
//
void
DoRasterString( float x, float y, float z, char *s )
{
char c; // one character to print
glRasterPos3f( (GLfloat)x, (GLfloat)y, (GLfloat)z );
for( ; ( c = *s ) != '\0'; s++ )
{
glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_24, c );
}
}
//
// use glut to display a string of characters using a stroke font:
//
void
DoStrokeString( float x, float y, float z, float ht, char *s )
{
char c; // one character to print
glPushMatrix( );
glTranslatef( (GLfloat)x, (GLfloat)y, (GLfloat)z );
float sf = ht / ( 119.05f + 33.33f );
glScalef( (GLfloat)sf, (GLfloat)sf, (GLfloat)sf );
for( ; ( c = *s ) != '\0'; s++ )
{
glutStrokeCharacter( GLUT_STROKE_ROMAN, c );
}
glPopMatrix( );
}
//
// initialize the opencl stuff:
//
void
InitCL( )
{
// show us what we've got here:
PrintOpenclInfo();
// pick which opencl device to use
// this fills the globals Platform and Device:
SelectOpenclDevice();
// see if we can even open the opencl Kernel Program
// (no point going on if we can't):
FILE *fp = fopen( CL_FILE_NAME, "r" );
if( fp == NULL )
{
fprintf( stderr, "Cannot open OpenCL source file '%s'\n", CL_FILE_NAME );
return;
}
// 2. allocate the host memory buffers:
cl_int status; // returned status from opencl calls
// test against CL_SUCCESS
// since this is an opengl interoperability program,
// check if the opengl sharing extension is supported,
// (no point going on if it isn't):
// (we need the Device in order to ask, so can't do it any sooner than here)
if( IsCLExtensionSupported( "cl_khr_gl_sharing" ) )
{
fprintf( stderr, "cl_khr_gl_sharing is supported.\n" );
}
else
{
fprintf( stderr, "cl_khr_gl_sharing is not supported -- sorry.\n" );
return;
}
// 3. create an opencl context based on the opengl context:
cl_context_properties props[ ] =
{
CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext( ),
CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC( ),
CL_CONTEXT_PLATFORM, (cl_context_properties) Platform,
0
};
cl_context Context = clCreateContext( props, 1, &Device, NULL, NULL, &status );
PrintCLError( status, "clCreateContext: " );
// 4. create an opencl command queue:
CmdQueue = clCreateCommandQueue( Context, Device, 0, &status );
if( status != CL_SUCCESS )
fprintf( stderr, "clCreateCommandQueue failed\n" );
// create the velocity array and the opengl vertex array buffer and color array buffer:
delete [ ] hVel;
hVel = new struct xyzw [ NUM_PARTICLES ];
glGenBuffers( 1, &hPobj );
glBindBuffer( GL_ARRAY_BUFFER, hPobj );
glBufferData( GL_ARRAY_BUFFER, 4 * NUM_PARTICLES * sizeof(float), NULL, GL_STATIC_DRAW );
glGenBuffers( 1, &hCobj );
glBindBuffer( GL_ARRAY_BUFFER, hCobj );
glBufferData( GL_ARRAY_BUFFER, 4 * NUM_PARTICLES * sizeof(float), NULL, GL_STATIC_DRAW );
glBindBuffer( GL_ARRAY_BUFFER, 0 ); // unbind the buffer
// fill those arrays and buffers:
ResetParticles( );
// 5. create the opencl version of the opengl buffers:
dPobj = clCreateFromGLBuffer( Context, CL_MEM_READ_WRITE, hPobj, &status );
PrintCLError( status, "clCreateFromGLBuffer (1)" );
dCobj = clCreateFromGLBuffer( Context, CL_MEM_READ_WRITE, hCobj, &status );
PrintCLError( status, "clCreateFromGLBuffer (2)" );
// 5. create the opencl version of the velocity array:
dVel = clCreateBuffer( Context, CL_MEM_READ_WRITE, 4*sizeof(float)*NUM_PARTICLES, NULL, &status );
PrintCLError( status, "clCreateBuffer: " );
// 6. enqueue the command to write the data from the host buffers to the Device buffers:
status = clEnqueueWriteBuffer( CmdQueue, dVel, CL_FALSE, 0, 4*sizeof(float)*NUM_PARTICLES, hVel, 0, NULL, NULL );
PrintCLError( status, "clEneueueWriteBuffer: " );
// 7. read the Kernel code from a file:
fseek( fp, 0, SEEK_END );
size_t fileSize = ftell( fp );
fseek( fp, 0, SEEK_SET );
char *clProgramText = new char[ fileSize+1 ]; // leave room for '\0'
size_t n = fread( clProgramText, 1, fileSize, fp );
clProgramText[fileSize] = '\0';
fclose( fp );
// create the text for the Kernel Program:
char *strings[1];
strings[0] = clProgramText;
Program = clCreateProgramWithSource( Context, 1, (const char **)strings, NULL, &status );
if( status != CL_SUCCESS )
fprintf( stderr, "clCreateProgramWithSource failed\n" );
delete [ ] clProgramText;
// 8. compile and link the Kernel code:
char *options = { "" };
status = clBuildProgram( Program, 1, &Device, options, NULL, NULL );
if( status != CL_SUCCESS )
{
size_t size;
clGetProgramBuildInfo( Program, Device, CL_PROGRAM_BUILD_LOG, 0, NULL, &size );
cl_char *log = new cl_char[ size ];
clGetProgramBuildInfo( Program, Device, CL_PROGRAM_BUILD_LOG, size, log, NULL );
fprintf( stderr, "clBuildProgram failed:\n%s\n", log );
delete [ ] log;
}
// 9. create the Kernel object:
Kernel = clCreateKernel( Program, "Particle", &status );
PrintCLError( status, "clCreateKernel failed: " );
// 10. setup the arguments to the Kernel object:
status = clSetKernelArg( Kernel, 0, sizeof(cl_mem), &dPobj );
PrintCLError( status, "clSetKernelArg (1): " );
status = clSetKernelArg( Kernel, 1, sizeof(cl_mem), &dVel );
PrintCLError( status, "clSetKernelArg (2): " );
status = clSetKernelArg( Kernel, 2, sizeof(cl_mem), &dCobj );
PrintCLError( status, "clSetKernelArg (3): " );
}
//
// initialize the glui window:
//
void
InitGlui( )
{
glutInitWindowPosition( INIT_WINDOW_SIZE + 50, 0 );
Glui = GLUI_Master.create_glui( (char *) GLUITITLE );
Glui->add_statictext( (char *) GLUITITLE );
Glui->add_separator( );
Glui->add_checkbox( "Axes", &AxesOn );
Glui->add_checkbox( "Perspective", &WhichProjection );
Glui->add_checkbox( "Show Performance", &ShowPerformance );
GLUI_Panel *panel = Glui->add_panel( "Object Transformation" );
GLUI_Rotation *rot = Glui->add_rotation_to_panel( panel, "Rotation", (float *) RotMatrix );
rot->set_spin( 1.0 );
Glui->add_column_to_panel( panel, GLUIFALSE );
GLUI_Translation *scale = Glui->add_translation_to_panel( panel, "Scale", GLUI_TRANSLATION_Y , &Scale2 );
scale->set_speed( 0.01f );
Glui->add_column_to_panel( panel, FALSE );
GLUI_Translation *trans = Glui->add_translation_to_panel( panel, "Trans XY", GLUI_TRANSLATION_XY, &TransXYZ[0] );
trans->set_speed( 1.1f );
Glui->add_column_to_panel( panel, FALSE );
trans = Glui->add_translation_to_panel( panel, "Trans Z", GLUI_TRANSLATION_Z , &TransXYZ[2] );
trans->set_speed( 1.1f );
panel = Glui->add_panel( "", FALSE );
Glui->add_button_to_panel( panel, "Go !", GO, (GLUI_Update_CB) Buttons );
Glui->add_column_to_panel( panel, FALSE );
Glui->add_button_to_panel( panel, "Pause", PAUSE, (GLUI_Update_CB) Buttons );
Glui->add_column_to_panel( panel, FALSE );
Glui->add_button_to_panel( panel, "Reset", RESET, (GLUI_Update_CB) Buttons );
Glui->add_column_to_panel( panel, FALSE );
Glui->add_button_to_panel( panel, "Quit", QUIT, (GLUI_Update_CB) Buttons );
Glui->set_main_gfx_window( MainWindow );
GLUI_Master.set_glutIdleFunc( NULL );
}
//
// initialize the glut and OpenGL libraries:
// also setup display lists and callback functions
//
void
InitGraphics( )
{
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( INIT_WINDOW_SIZE, INIT_WINDOW_SIZE );
MainWindow = glutCreateWindow( WINDOWTITLE );
glutSetWindowTitle( WINDOWTITLE );
glClearColor( BACKCOLOR[0], BACKCOLOR[1], BACKCOLOR[2], BACKCOLOR[3] );
// setup the callback routines:
glutSetWindow( MainWindow );
glutDisplayFunc( Display );
glutReshapeFunc( Resize );
glutKeyboardFunc( Keyboard );
glutMouseFunc( MouseButton );
glutMotionFunc( MouseMotion );
glutVisibilityFunc( Visibility );
#ifdef WIN32
GLenum err = glewInit();
if( err != GLEW_OK )
{
fprintf( stderr, "glewInit Error\n" );
}
#endif
}
//
// initialize the display lists that will not change:
//
void
InitLists( )
{
SphereList = glGenLists( 1 );
glNewList( SphereList, GL_COMPILE );
// ****************************************
// Here is where the first sphere is drawn.
// It is centered at (-100,-800.,0.) with a radius of 600.
// This had better match the definition of the first sphere
// in the .cl file
// ****************************************
glColor3f( 0., .9f, .9f ); // 0. <= r,g,b <= 1.
glPushMatrix( );
glTranslatef( -300.f, -400.f, 0.f );
glutWireSphere( 200.f, 100, 100 );
glPopMatrix( );
glColor3f(.9f, .9f, 0.); // 0. <= r,g,b <= 1.
glPushMatrix();
glTranslatef(300.f, -400.f, 0.f);
glutWireSphere(200.f, 100, 100);
glPopMatrix();
glColor3f(0., .9f, 0.); // 0. <= r,g,b <= 1.
glPushMatrix();
glTranslatef(0., -400.f, 300.f);
glutWireSphere(200.f, 100, 100);
glPopMatrix();
glColor3f(.9f, 0., 0.); // 0. <= r,g,b <= 1.
glPushMatrix();
glTranslatef(0., -400.f, -300.f);
glutWireSphere(200.f, 100, 100);
glPopMatrix();
// ****************************************
// Here is where the second sphere is drawn.
// This had better match the definition of the second sphere
// in the .cl file
// ****************************************
// do this for yourself...
glEndList( );
AxesList = glGenLists( 1 );
glNewList( AxesList, GL_COMPILE );
glColor3fv( AXES_COLOR );
glLineWidth( AXES_WIDTH );
Axes( 150. );
glLineWidth( 1. );
glEndList( );
}
//
// the keyboard callback:
//
void
Keyboard( unsigned char c, int x, int y )
{
switch( c )
{
case 'o':
case 'O':
WhichProjection = ORTHO;
break;
case 'p':
case 'P':
WhichProjection = PERSP;
break;
case 'q':
case 'Q':
case ESCAPE:
Buttons( QUIT ); // will not return here
break; // happy compiler
default:
fprintf( stderr, "Don't know what to do with keyboard hit: '%c' (0x%0x)\n", c, c );
}
Glui->sync_live( );
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
//
// called when the mouse button transitions down or up:
//
void
MouseButton( int button, int state, int x, int y )
{
int b; // LEFT, MIDDLE, or RIGHT
switch( button )
{
case GLUT_LEFT_BUTTON:
b = LEFT; break;
case GLUT_MIDDLE_BUTTON:
b = MIDDLE; break;
case GLUT_RIGHT_BUTTON:
b = RIGHT; break;
default:
b = 0;
fprintf( stderr, "Unknown mouse button: %d\n", button );
}
// button down sets the bit, up clears the bit:
if( state == GLUT_DOWN )
{
Xmouse = x;
Ymouse = y;
ActiveButton |= b; // set the proper bit
}
else
{
ActiveButton &= ~b; // clear the proper bit
}
}
//
// called when the mouse moves while a button is down:
//
void
MouseMotion( int x, int y )
{
int dx = x - Xmouse; // change in mouse coords
int dy = y - Ymouse;
if( ActiveButton & LEFT )
{
Xrot += ( ANGFACT*dy );
Yrot += ( ANGFACT*dx );
}
if( ActiveButton & MIDDLE )
{
Scale += SCLFACT * (float) ( dx - dy );
// keep object from turning inside-out or disappearing:
if( Scale < MINSCALE )
Scale = MINSCALE;
}
Xmouse = x; // new current position
Ymouse = y;
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
//
// reset the transformations and the colors:
//
// this only sets the global variables --
// the glut main loop is responsible for redrawing the scene
//
void
Reset( )
{
ActiveButton = 0;
AxesOn = GLUIFALSE;
Paused = GLUIFALSE;
Scale = 1.0;
Scale2 = 0.0; // because add 1. to it in Display( )
ShowPerformance = GLUIFALSE;
WhichProjection = PERSP;
Xrot = Yrot = 0.;
TransXYZ[0] = TransXYZ[1] = TransXYZ[2] = 0.;
RotMatrix[0][1] = RotMatrix[0][2] = RotMatrix[0][3] = 0.;
RotMatrix[1][0] = RotMatrix[1][2] = RotMatrix[1][3] = 0.;
RotMatrix[2][0] = RotMatrix[2][1] = RotMatrix[2][3] = 0.;
RotMatrix[3][0] = RotMatrix[3][1] = RotMatrix[3][3] = 0.;
RotMatrix[0][0] = RotMatrix[1][1] = RotMatrix[2][2] = RotMatrix[3][3] = 1.;
}
void
ResetParticles( )
{
glBindBuffer( GL_ARRAY_BUFFER, hPobj );
struct xyzw *points = (struct xyzw *) glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY );
for( int i = 0; i < NUM_PARTICLES; i++ )
{
points[i].x = Ranf( XMIN, XMAX );
points[i].y = Ranf( YMIN, YMAX );
points[i].z = Ranf( ZMIN, ZMAX );
points[i].w = 1.;
}
glUnmapBuffer( GL_ARRAY_BUFFER );
glBindBuffer( GL_ARRAY_BUFFER, hCobj );
struct rgba *colors = (struct rgba *) glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY );
for( int i = 0; i < NUM_PARTICLES; i++ )
{
colors[i].r = Ranf( .3f, 1. );
colors[i].g = Ranf( .3f, 1. );
colors[i].b = Ranf( .3f, 1. );
colors[i].a = 1.;
}
glUnmapBuffer( GL_ARRAY_BUFFER );
for( int i = 0; i < NUM_PARTICLES; i++ )
{
hVel[i].x = Ranf( VMIN, VMAX );
hVel[i].y = Ranf( 0., VMAX );
hVel[i].z = Ranf( VMIN, VMAX );
}
}
void
Resize( int width, int height )
{
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
void
Visibility ( int state )
{
if( state == GLUT_VISIBLE )
{
glutSetWindow( MainWindow );
glutPostRedisplay( );
}
else
{
// could optimize by keeping track of the fact
// that the window is not visible and avoid
// animating or redrawing it ...
}
}
// the stroke characters 'X' 'Y' 'Z' :
static float xx[ ] = {
0., 1., 0., 1.
};
static float xy[ ] = {
-.5, .5, .5, -.5
};
static int xorder[ ] = {
1, 2, -3, 4
};
static float yx[ ] = {
0., 0., -.5, .5
};
static float yy[ ] = {
0., .6f, 1., 1.
};
static int yorder[ ] = {
1, 2, 3, -2, 4
};
static float zx[ ] = {
1., 0., 1., 0., .25, .75
};
static float zy[ ] = {
.5, .5, -.5, -.5, 0., 0.
};
static int zorder[ ] = {
1, 2, 3, 4, -5, 6
};
// fraction of the length to use as height of the characters:
#define LENFRAC 0.10
// fraction of length to use as start location of the characters:
#define BASEFRAC 1.10
//
// Draw a set of 3D axes:
// (length is the axis length in world coordinates)
//
void
Axes( float length )
{
glBegin( GL_LINE_STRIP );
glVertex3f( length, 0., 0. );
glVertex3f( 0., 0., 0. );
glVertex3f( 0., length, 0. );
glEnd( );
glBegin( GL_LINE_STRIP );
glVertex3f( 0., 0., 0. );
glVertex3f( 0., 0., length );