-
Notifications
You must be signed in to change notification settings - Fork 0
/
lb.cpp
278 lines (222 loc) · 5.96 KB
/
lb.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
#include "common.h"
#include "grass.cpp"
#include "pool.cpp"
#include "water.cpp"
#include "cubemap.h"
const unsigned int window_width = 1024;
const unsigned int window_height = 768;
const unsigned int mesh_width = 256;
const unsigned int mesh_height = 256;
glm::mat4 projectionMatrix; // Store the projection matrix
glm::mat4 viewMatrix; // Store the view matrix
glm::mat4 modelMatrix; // Store the model matrix
int mouse_old_x, mouse_old_y, mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0, translate_x = 0.0, translate_y = -0.2, translate_z = -3.0;
int add=1;
float *d_fin, *d_fout;
int mode=1;
bool fscreen = false;
bool init(int argc, char** argv);
bool initGL(int *argc, char** argv);
grassProgram grass;
poolProgram pool;
waterProgram water;
cubemapProgram cubemap;
void display();
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);
void initArrays();
void initialize();
GLhandleARB shaderProgramBuild(const GLchar *vertex, const GLchar *fragment, int attach);
//void runCuda(struct cudaGraphicsResource **vbo_resource);
//GLuint vbo_cube_vertices;
GLhandleARB shaderProgram;
GLhandleARB shaderProgramCubemap;
GLchar *fragmentShaderSource, *vertexShaderSource;
GLchar *fragmentShaderSourceCubemap, *vertexShaderSourceCubemap;
using namespace std;
int main(int argc, char** argv)
{
init(argc, argv);
cudaThreadExit();
cudaDeviceReset();
exit(0);
}
bool initGL(int *argc, char **argv)
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(window_width, window_height);
glutCreateWindow("Lattice Boltzmann - Cuda GL Interop (VBO)");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
#ifdef _WIN32
//FreeConsole();
#endif
// glutFullScreen();
glewInit();
glClearColor(0.0, 0.0, 0.0, 1.0);
glDisable(GL_DEPTH_TEST);
glViewport(0, 0, window_width, window_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)window_width / (GLfloat) window_height, 0.1, 10.0);
//initShaders();
return true;
}
bool checkErrors() {
switch ( glGetError() ) {
case GL_NO_ERROR: {
return true;
}
case GL_INVALID_ENUM: {
cout << "GL_INVALID_ENUM" << endl;
break;
}
case GL_INVALID_VALUE: {
cout << "GL_INVALID_VALUE" << endl;
break;
}
case GL_INVALID_OPERATION: {
cout << "GL_INVALID_OPERATION" << endl;
break;
}
case GL_INVALID_FRAMEBUFFER_OPERATION: {
cout << "GL_INVALID_FRAMEBUFFER_OPERATION" << endl;
break;
}
case GL_OUT_OF_MEMORY: {
cout << "GL_OUT_OF_MEMORY" << endl;
break;
}
}
return false;
}
bool init(int argc, char** argv)
{
//sdkCreateTimer( &timer );
if (false == initGL(&argc, argv))
{
cudaDeviceReset();
return false;
}
findCudaGLDevice(argc, (const char **)argv);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
grass.prepare();
pool.prepare();
cubemap.prepare();
//skybox = new SkyBox();
/*glEnable(GL_LIGHTING);
// Set Diffuse color component
GLfloat LightColor [] = { 0.0f,0.0f,1.0f,0.5f };
// white
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightColor);
// Set Position
GLfloat LightPos[] = { 2.0f, 2.0f, 2.0f, 0.5f};
glLightfv(GL_LIGHT0, GL_POSITION, LightPos);
// Turn it on
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);*/
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glBlendColor(0.0, 0.0, 1.0, 0.5);
water.prepare();
glutMainLoop();
cudaThreadExit();
return true;
}
void display()
{
water.runCuda();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
// added this static boolean do do one-time OpenGL related initialization.
static bool doInitialize = true;
if (doInitialize == true) {
doInitialize = false;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f) ;
grass.compileShaders();
pool.compileShaders();
water.compileShaders();
cubemap.compileShaders();
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
modelMatrix = glm::mat4(1.0f);
viewMatrix = glm::mat4(1.0f);
projectionMatrix = glm::mat4(1.0f);
modelMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(1.0f)); // Create our model matrix which will halve the size of our model
viewMatrix = glm::rotate(viewMatrix, rotate_x, glm::vec3(1.0f, 0.0f, 0.0f));
viewMatrix = glm::rotate(viewMatrix, rotate_y, glm::vec3(0.0f, 1.0f, 0.0f));
viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0, -0.4, translate_z)); // Create our view matrix which will translate us back 5 units
projectionMatrix = glm::perspective(30.0f, (float)window_width / (float)window_height, 0.1f, 100.f); // Create our perspective projection matrix
checkErrors();
pool.draw();
grass.draw();
water.draw();
cubemap.draw();
glutSwapBuffers();
glutPostRedisplay();
glUseProgram(0);
glFlush();
glutReportErrors();
//return ;
//computeFPS();
}
void keyboard(unsigned char key, int /*x*/, int /*y*/)
{
if (key >= 48 && key <=57)
add |= int(pow(2.0, key-48));
switch(key) {
case(38): //UP
translate_y += 1.;
break;
case(40): //DOWN
translate_y -= 1.;
break;
case(37): //LEFT
translate_x -= 1.;
break;
case(39): //RIGHT
translate_x += 1.;
break;
case(27) : // ESC
exit(0);
break;
case(32) :
fscreen = !fscreen;
if (fscreen)
glutFullScreen();
else
glutReshapeWindow(window_width, window_height);
}
}
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN) {
mouse_buttons |= 1<<button;
} else if (state == GLUT_UP) {
mouse_buttons = 0;
}
mouse_old_x = x;
mouse_old_y = y;
glutPostRedisplay();
}
void motion(int x, int y)
{
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.01;
}
mouse_old_x = x;
mouse_old_y = y;
}