/
main.cpp
278 lines (220 loc) · 7.42 KB
/
main.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
/*
* Adventures in OpenCL tutorial series
* Part 2
*
* author: Ian Johnson
* htt://enja.org
* code based on advisor Gordon Erlebacher's work
* NVIDIA's examples
* as well as various blogs and resources on the internet
*/
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <iomanip>
#include <math.h>
//OpenGL stuff
#include <GL/glew.h>
#if defined __APPLE__ || defined(MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
//Our OpenCL Particle Systemclass
#include "cll.h"
#define NUM_PARTICLES 20000
CL* example;
//GL related variables
int window_width = 800;
int window_height = 600;
int glutWindowHandle = 0;
float translate_z = -1.f;
// mouse controls
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
//main app helper functions
void init_gl(int argc, char** argv);
void appRender();
void appDestroy();
void timerCB(int ms);
void appKeyboard(unsigned char key, int x, int y);
void appMouse(int button, int state, int x, int y);
void appMotion(int x, int y);
//----------------------------------------------------------------------
//quick random function to distribute our initial points
float rand_float(float mn, float mx)
{
float r = random() / (float) RAND_MAX;
return mn + (mx-mn)*r;
}
//----------------------------------------------------------------------
int main(int argc, char** argv)
{
printf("Hello, OpenCL\n");
//Setup our GLUT window and OpenGL related things
//glut callback functions are setup here too
init_gl(argc, argv);
//initialize our CL object, this sets up the context
example = new CL();
//load and build our CL program from the file
#include "part2.cl" //std::string kernel_source is defined in this file
example->loadProgram(kernel_source);
//initialize our particle system with positions, velocities and color
int num = NUM_PARTICLES;
std::vector<Vec4> pos(num);
std::vector<Vec4> vel(num);
std::vector<Vec4> color(num);
//fill our vectors with initial data
for(int i = 0; i < num; i++)
{
//distribute the particles in a random circle around z axis
float rad = rand_float(.2, .5);
float x = rad*sin(2*3.14 * i/num);
float z = 0.0f;// -.1 + .2f * i/num;
float y = rad*cos(2*3.14 * i/num);
pos[i] = Vec4(x, y, z, 1.0f);
//give some initial velocity
//float xr = rand_float(-.1, .1);
//float yr = rand_float(1.f, 3.f);
//the life is the lifetime of the particle: 1 = alive 0 = dead
//as you will see in part2.cl we reset the particle when it dies
float life_r = rand_float(0.f, 1.f);
vel[i] = Vec4(0.0, 0.0, 3.0f, life_r);
//just make them red and full alpha
color[i] = Vec4(1.0f, 0.0f,0.0f, 1.0f);
}
//our load data function sends our initial values to the GPU
example->loadData(pos, vel, color);
//initialize the kernel
example->popCorn();
//this starts the GLUT program, from here on out everything we want
//to do needs to be done in glut callback functions
glutMainLoop();
}
//----------------------------------------------------------------------
void appRender()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//this updates the particle system by calling the kernel
example->runKernel();
//render the particles from VBOs
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_POINT_SMOOTH);
glPointSize(5.);
//printf("color buffer\n");
glBindBuffer(GL_ARRAY_BUFFER, example->c_vbo);
glColorPointer(4, GL_FLOAT, 0, 0);
//printf("vertex buffer\n");
glBindBuffer(GL_ARRAY_BUFFER, example->p_vbo);
glVertexPointer(4, GL_FLOAT, 0, 0);
//printf("enable client state\n");
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
//Need to disable these for blender
glDisableClientState(GL_NORMAL_ARRAY);
//printf("draw arrays\n");
glDrawArrays(GL_POINTS, 0, example->num);
//printf("disable stuff\n");
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glutSwapBuffers();
}
//----------------------------------------------------------------------
void init_gl(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(window_width, window_height);
glutInitWindowPosition (glutGet(GLUT_SCREEN_WIDTH)/2 - window_width/2,
glutGet(GLUT_SCREEN_HEIGHT)/2 - window_height/2);
std::stringstream ss;
ss << "Adventures in OpenCL: Part 2, " << NUM_PARTICLES << " particles" << std::ends;
glutWindowHandle = glutCreateWindow(ss.str().c_str());
glutDisplayFunc(appRender); //main rendering function
glutTimerFunc(30, timerCB, 30); //determin a minimum time between frames
glutKeyboardFunc(appKeyboard);
glutMouseFunc(appMouse);
glutMotionFunc(appMotion);
glewInit();
glClearColor(0.0, 0.0, 0.0, 1.0);
glDisable(GL_DEPTH_TEST);
// viewport
glViewport(0, 0, window_width, window_height);
// projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (GLfloat)window_width / (GLfloat) window_height, 0.1, 1000.0);
// set view matrix
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, translate_z);
}
//----------------------------------------------------------------------
void appDestroy()
{
//this makes sure we properly cleanup our OpenCL context
delete example;
if(glutWindowHandle)glutDestroyWindow(glutWindowHandle);
printf("about to exit!\n");
exit(0);
}
//----------------------------------------------------------------------
void timerCB(int ms)
{
//this makes sure the appRender function is called every ms miliseconds
glutTimerFunc(ms, timerCB, ms);
glutPostRedisplay();
}
//----------------------------------------------------------------------
void appKeyboard(unsigned char key, int x, int y)
{
//this way we can exit the program cleanly
switch(key)
{
case '\033': // escape quits
case '\015': // Enter quits
case 'Q': // Q quits
case 'q': // q (or escape) quits
// Cleanup up and quit
appDestroy();
break;
}
}
//----------------------------------------------------------------------
void appMouse(int button, int state, int x, int y)
{
//handle mouse interaction for rotating/zooming the view
if (state == GLUT_DOWN) {
mouse_buttons |= 1<<button;
} else if (state == GLUT_UP) {
mouse_buttons = 0;
}
mouse_old_x = x;
mouse_old_y = y;
}
//----------------------------------------------------------------------
void appMotion(int x, int y)
{
//hanlde the mouse motion for zooming and rotating the view
float dx, dy;
dx = x - mouse_old_x;
dy = y - mouse_old_y;
if (mouse_buttons & 1) {
rotate_x += dy * 0.2;
rotate_y += dx * 0.2;
} else if (mouse_buttons & 4) {
translate_z += dy * 0.1;
}
mouse_old_x = x;
mouse_old_y = y;
// set view matrix
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, translate_z);
glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);
}