-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraphics.cpp
More file actions
203 lines (164 loc) · 5.44 KB
/
graphics.cpp
File metadata and controls
203 lines (164 loc) · 5.44 KB
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
#include "graphics.h"
float g_time = 0.0;
// Text buffer
char text_buff[100];
// FPS timers
static unsigned int fps_start = 0;
static unsigned int fps_frames = 0;
double ms;
// camera controls
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
float translate_x = 0.0, translate_y = 0.0, translate_z = -3.0;
void graphics::renderString(int x, int y, void* font, unsigned char* string, float3 rgb) {
glColor3f(rgb.x, rgb.y, rgb.z);
glWindowPos2d(x, y);
glutBitmapString(font, string);
}
void graphics::refreshDisplay(int value) {
if (glutGetWindow()) {
glutPostRedisplay();
glutTimerFunc(REFRESH_DELAY, refreshDisplay, 0);
}
}
void graphics::display() {
// calculate FPS
fps_frames++;
int delta_t = glutGet(GLUT_ELAPSED_TIME) - fps_start;
if (delta_t > 1000) {
ms = delta_t / (float)fps_frames;
fps_frames = 0;
fps_start = glutGet(GLUT_ELAPSED_TIME);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);
glTranslatef(translate_x, translate_y, translate_z);
// render from the vbo
glBindBuffer(GL_ARRAY_BUFFER, vbo::vbo);
glVertexPointer(3, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glColor3f(1.0, 0.0, 0.0);
glDrawArrays(GL_POINTS, 0, N * N);
glDisableClientState(GL_VERTEX_ARRAY);
// Print specs
sprintf(text_buff, "N: %d | L: %2.2f | V: %2.2f", N, LENGTH, V);
renderString(20, WINDOW_HEIGHT - 50, GLUT_BITMAP_9_BY_15, (unsigned char*)text_buff, make_float3(1.0f, 1.0f, 0.0f));
// Print timers
sprintf(text_buff, " FPS: %3.3f\nms/frame: %3.3f\n VBO(ms): %3.3f", 1000 / ms, ms, vbo_time);
renderString(20, WINDOW_HEIGHT - 80, GLUT_BITMAP_9_BY_15, (unsigned char*)text_buff, make_float3(1.0f, 1.0f, 1.0f));
glutSwapBuffers();
// Update time
g_time += TIMESTEP / (float)1000;
}
void graphics::keyboard(unsigned char key, int /*x*/, int /*y*/) {
float speed = 0.5f;
float sprint = 10.0f;
switch (key) {
case 'q':
glutDestroyWindow(glutGetWindow());
exit(0);
case 'A': speed *= sprint;
case 'a':
translate_x -= speed; break;
case 'D': speed *= sprint;
case 'd':
translate_x += speed; break;
case 'W': speed *= sprint;
case 'w':
translate_z -= speed; break;
case 'S': speed *= sprint;
case 's':
translate_z += sprint; break;
case 'R': speed *= sprint;
case 'r':
translate_y -= speed; break;
case 'F': speed *= sprint;
case 'f':
translate_y += speed; break;
default:
printf("Unkown key: %d\n", (int)key);
}
}
void graphics::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;
}
void graphics::motion(int x, int y) {
float dx, dy;
dx = (float)(x - mouse_old_x);
dy = (float)(y - mouse_old_y);
if (mouse_buttons & 1) {
rotate_x += dy * 0.2f;
rotate_y += dx * 0.2f;
}
else if (mouse_buttons & 4)
translate_z += dy * 0.01f;
mouse_old_x = x;
mouse_old_y = y;
}
bool graphics::initGL(int* argc, char** argv, char *title) {
glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow(title);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutTimerFunc(REFRESH_DELAY, refreshDisplay, 0);
// initialize necessary OpenGL extensions
if (!isGLVersionSupported(2, 0)) {
fprintf(stderr, "ERROR: Support for necessary OpenGL extensions missing.\n");
return false;
}
// default initialization
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(60.0, (GLfloat)WINDOW_WIDTH / (GLfloat)WINDOW_HEIGHT, 0.1, 1000.0);
return true;
}
void graphics::cpu::cleanup() {
if (vbo::vbo)
vbo::cpu::deleteVBO(&vbo::vbo);
}
void graphics::cpu::display() {
// Calculate vbo
switch (VBO_MODE) {
case(VBO_DIRECT_ONE_STEP): ocean::cpu::direct::calculate_vbo(&vbo::vbo, g_time); break;
case(VBO_FFT): ocean::cpu::fft::calculate_vbo(&vbo::vbo, g_time); break;
}
graphics::display();
}
bool graphics::cpu::initGL(int* argc, char** argv) {
return graphics::initGL(argc, argv, "CPU: Wave Simulation");
}
void graphics::gpu::cleanup() {
if (vbo::vbo)
vbo::gpu::deleteVBO(&vbo::vbo, vbo::cuda_vbo_resource);
}
void graphics::gpu::display() {
// run CUDA kernel to generate vertex positions
//ocean::gpu::calculate_vbo(&vbo::cuda_vbo_resource, g_time);
switch (VBO_MODE) {
case VBO_DIRECT_ONE_STEP: ocean::gpu::direct::one_step::calculate_vbo(&vbo::cuda_vbo_resource, g_time); break;
case VBO_DIRECT_TWO_STEP: ocean::gpu::direct::two_step::calculate_vbo(&vbo::cuda_vbo_resource, g_time); break;
case VBO_FFT: ocean::gpu::fft::calculate_vbo(&vbo::cuda_vbo_resource, g_time);
}
graphics::display();
}
bool graphics::gpu::initGL(int* argc, char** argv) {
return graphics::initGL(argc, argv, "GPU: Wave Simulation");
}