@@ -20,219 +20,214 @@ GLint GL::m_uniformSphereRadius;
20
20
21
21
char title[50 ];
22
22
23
- GL::GL ( int width, int height, info_t *info, bool fullscreen = false ) {
24
- m_info = info;
25
-
26
- #pragma region Create GLUT Window
27
- glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
28
- glutInitWindowSize ( width, height);
29
- glutInitWindowPosition ( glutGet ( GLUT_SCREEN_WIDTH ) / 2 - width/2 , glutGet ( GLUT_SCREEN_HEIGHT ) / 2 - height/2 );
30
- glutCreateWindow ( " N-Body" );
31
- glewInit ( );
32
-
33
- if (fullscreen )
34
- glutFullScreen ();
35
- #pragma endregion
36
-
37
- #pragma region Register GLUT Callbacks
38
- glutDisplayFunc ( GL::Display );
39
- glutTimerFunc ( REFRESH_EVERY_X_MS, GL::Refresh, REFRESH_EVERY_X_MS ); // determine a minimum time between frames
40
- glutKeyboardFunc ( Keyboard );
41
- glutSpecialFunc ( KeyboardSpecial );
42
- #pragma endregion
43
-
44
- #pragma region Initialize CL
45
- CL = new WOCL ( CL_DEVICE_TYPE_GPU, true );
46
- CL->SetWorkSize ( info->local_item_size , WOCL::CalculateNumOfGroups ( info->local_item_size , info->n ), 0 );
47
- CL->CreateAndBuildKernel ( " res/kernelVec.cl" , " kernelVec" );
48
- #pragma endregion
23
+ GL::GL (const int width, const int height, info_t *info, const bool fullscreen = false ) {
24
+ m_info = info;
25
+
26
+ // Create GLUT Window
27
+ glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
28
+ glutInitWindowSize ( width, height);
29
+ glutInitWindowPosition ( glutGet ( GLUT_SCREEN_WIDTH ) / 2 - width/2 , glutGet ( GLUT_SCREEN_HEIGHT ) / 2 - height/2 );
30
+ glutCreateWindow ( " N-Body" );
31
+ glewInit ( );
32
+
33
+ if (fullscreen)
34
+ glutFullScreen ();
35
+
36
+ // Register GLUT Callbacks
37
+ glutDisplayFunc ( GL::Display );
38
+ glutTimerFunc ( REFRESH_EVERY_X_MS, GL::Refresh, REFRESH_EVERY_X_MS ); // determine a minimum time between frames
39
+ glutKeyboardFunc ( Keyboard );
40
+ glutSpecialFunc ( KeyboardSpecial );
41
+
42
+ // Initialize CL
43
+ CL = new WOCL ( CL_DEVICE_TYPE_GPU, true );
44
+ CL->SetWorkSize ( info->local_item_size , WOCL::CalculateNumOfGroups ( info->local_item_size , info->n ), 0 );
45
+ CL->CreateAndBuildKernel ( " res/kernelVec.cl" , " kernelVec" );
49
46
50
47
}
51
48
52
49
GL::~GL () {
53
- if ( CL != nullptr ) delete CL;
50
+ if ( CL != nullptr ) delete CL;
54
51
}
55
52
56
53
void GL::Init () {
57
- glClearColor ( 0.0 , 0.0 , 0.0 , 1.0 );
58
- glDisable ( GL_DEPTH_TEST );
59
-
60
- glEnable ( GL_BLEND );
61
- glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
62
- glEnable ( GL_POINT_SMOOTH );
63
- glPointSize ( 2 . );
64
- glColor3f ( 1 .0f , 1 .0f , 1 .0f );
65
- glEnableClientState ( GL_VERTEX_ARRAY );
66
-
67
- #pragma region Load and use shaders
68
- GLuint m_shaderVert = CreateShader ( GL_VERTEX_SHADER, " res/shader.vertex.glsl" );
69
- GLuint m_shaderFrag = CreateShader ( GL_FRAGMENT_SHADER, " res/shader.fragment.glsl" );
70
- GLuint m_program = glCreateProgram ( );
71
- glAttachShader ( m_program, m_shaderVert );
72
- glAttachShader ( m_program, m_shaderFrag );
73
- glLinkProgram ( m_program );
74
- glUseProgram ( m_program );
75
- #pragma endregion
76
-
77
- m_uniformDistToCamera = glGetUniformLocation ( m_program, " distCameraToCenter" );
78
- glUniform1f ( m_uniformDistToCamera, m_cameraDistance );
79
- m_uniformSphereRadius = glGetUniformLocation ( m_program, " sphereRadius" );
80
- glUniform1f ( m_uniformSphereRadius, (GLfloat)m_info->sphereRadius );
81
-
82
- #pragma region Projection Matrix
83
- int height = glutGet ( GLUT_WINDOW_HEIGHT );
84
- int width = glutGet ( GLUT_WINDOW_WIDTH );
85
- glViewport ( 0 , 0 , width, height );
86
- glMatrixMode ( GL_PROJECTION );
87
- glLoadIdentity ( );
88
- gluPerspective ( 90.0 , (GLfloat) width / (GLfloat) height, 0.1 , 1000.0 );
89
- #pragma endregion
90
-
91
- // Host alokacija
92
- float *Coord = (float *) malloc ( sizeof (cl_float4) * m_info->n );
93
- float *V = (float *) calloc ( m_info->n , 4 * sizeof (float ) );
94
- m_info->generateFunc ( Coord, m_info );
95
-
96
- // Device alokacija in kopiranje podatkov
97
- glGenBuffers ( 2 , m_vboVertices );
98
- glBindBuffer ( GL_ARRAY_BUFFER, m_vboVertices[0 ] );
99
- glBufferData ( GL_ARRAY_BUFFER, sizeof (cl_float4) * m_info->n , Coord, GL_STATIC_DRAW ); // upload data to video card
100
- glBindBuffer ( GL_ARRAY_BUFFER, m_vboVertices[1 ] );
101
- glBufferData ( GL_ARRAY_BUFFER, sizeof (cl_float4) * m_info->n , Coord, GL_STATIC_DRAW ); // upload data to video card
102
-
103
- devCoord[0 ] = CL->CreateBufferFromGLBuffer ( CL_MEM_READ_WRITE, m_vboVertices[0 ] );
104
- devCoord[1 ] = CL->CreateBufferFromGLBuffer ( CL_MEM_READ_WRITE, m_vboVertices[1 ] );
105
- cl_mem devV = CL->CreateBuffer ( m_info->n *sizeof (cl_float4), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, V );
106
-
107
- free ( Coord );
108
- free ( V );
109
-
110
- // priprava šèepca
111
- CL->SetKernelArgument <cl_mem>( 2 , &devV );
112
- CL->SetKernelArgument <cl_int>( 3 , &(m_info->n ) );
113
- CL->SetKernelArgument <cl_float>( 4 , &(m_info->eps ) );
114
- CL->SetKernelArgument <cl_float>( 5 , &(m_info->kappa ) );
115
- CL->SetKernelArgument <cl_float>( 6 , &(m_info->dt ) );
54
+ glClearColor ( 0.0 , 0.0 , 0.0 , 1.0 );
55
+ glDisable ( GL_DEPTH_TEST );
56
+
57
+ glEnable ( GL_BLEND );
58
+ glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
59
+ glEnable ( GL_POINT_SMOOTH );
60
+ glPointSize ( 2 . );
61
+ glColor3f ( 1 .0f , 1 .0f , 1 .0f );
62
+ glEnableClientState ( GL_VERTEX_ARRAY );
63
+
64
+ // Load and use shaders
65
+ const GLuint m_shaderVert = CreateShader ( GL_VERTEX_SHADER, " res/shader.vertex.glsl" );
66
+ const GLuint m_shaderFrag = CreateShader ( GL_FRAGMENT_SHADER, " res/shader.fragment.glsl" );
67
+ const GLuint m_program = glCreateProgram ( );
68
+ glAttachShader ( m_program, m_shaderVert );
69
+ glAttachShader ( m_program, m_shaderFrag );
70
+ glLinkProgram ( m_program );
71
+ glUseProgram ( m_program );
72
+
73
+ m_uniformDistToCamera = glGetUniformLocation ( m_program, " distCameraToCenter" );
74
+ glUniform1f ( m_uniformDistToCamera, m_cameraDistance );
75
+ m_uniformSphereRadius = glGetUniformLocation ( m_program, " sphereRadius" );
76
+ glUniform1f ( m_uniformSphereRadius, (GLfloat)m_info->sphereRadius );
77
+
78
+ // Projection Matrix
79
+ const int height = glutGet ( GLUT_WINDOW_HEIGHT );
80
+ const int width = glutGet ( GLUT_WINDOW_WIDTH );
81
+ glViewport ( 0 , 0 , width, height );
82
+ glMatrixMode ( GL_PROJECTION );
83
+ glLoadIdentity ( );
84
+ gluPerspective ( 90.0 , (GLfloat) width / (GLfloat) height, 0.1 , 1000.0 );
85
+
86
+ // Host alokacija
87
+ float *Coord = (float *) malloc ( sizeof (cl_float4) * m_info->n );
88
+ float *V = (float *) calloc ( m_info->n , 4 * sizeof (float ) );
89
+ m_info->generateFunc ( Coord, m_info );
90
+
91
+ // Device alokacija in kopiranje podatkov
92
+ glGenBuffers ( 2 , m_vboVertices );
93
+ glBindBuffer ( GL_ARRAY_BUFFER, m_vboVertices[0 ] );
94
+ glBufferData ( GL_ARRAY_BUFFER, sizeof (cl_float4) * m_info->n , Coord, GL_STATIC_DRAW ); // upload data to video card
95
+ glBindBuffer ( GL_ARRAY_BUFFER, m_vboVertices[1 ] );
96
+ glBufferData ( GL_ARRAY_BUFFER, sizeof (cl_float4) * m_info->n , Coord, GL_STATIC_DRAW ); // upload data to video card
97
+
98
+ devCoord[0 ] = CL->CreateBufferFromGLBuffer ( CL_MEM_READ_WRITE, m_vboVertices[0 ] );
99
+ devCoord[1 ] = CL->CreateBufferFromGLBuffer ( CL_MEM_READ_WRITE, m_vboVertices[1 ] );
100
+ cl_mem devV = CL->CreateBuffer ( m_info->n *sizeof (cl_float4), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, V );
101
+
102
+ free ( Coord );
103
+ free ( V );
104
+
105
+ // priprava šèepca
106
+ CL->SetKernelArgument <cl_mem>( 2 , &devV );
107
+ CL->SetKernelArgument <cl_int>( 3 , &(m_info->n ) );
108
+ CL->SetKernelArgument <cl_float>( 4 , &(m_info->eps ) );
109
+ CL->SetKernelArgument <cl_float>( 5 , &(m_info->kappa ) );
110
+ CL->SetKernelArgument <cl_float>( 6 , &(m_info->dt ) );
116
111
}
117
112
118
- void GL::Start ( ) {
119
- Init ();
113
+ void GL::Start () {
114
+ Init ();
120
115
121
- glutMainLoop ( );
116
+ glutMainLoop ( );
122
117
}
123
118
124
- void GL::Display ( void ) {
125
- if ( m_paused ) return ;
126
- m_timer.TicSimple ();
119
+ void GL::Display () {
120
+ if ( m_paused ) return ;
121
+ m_timer.TicSimple ();
127
122
128
- glFinish ( );
129
- CL->AcquireObjectsFromGLAndFinish ( 2 , devCoord );
123
+ glFinish ( );
124
+ CL->AcquireObjectsFromGLAndFinish ( 2 , devCoord );
130
125
131
- // execute the kernel
132
- CL->SetKernelArgument <cl_mem>( 0 , (devCoord+idx) );
133
- CL->SetKernelArgument <cl_mem>( 1 , (devCoord+((idx+1 )%2 )) );
134
- CL->ExecuteKernel ( );
126
+ // execute the kernel
127
+ CL->SetKernelArgument <cl_mem>( 0 , (devCoord+idx) );
128
+ CL->SetKernelArgument <cl_mem>( 1 , (devCoord+((idx+1 )%2 )) );
129
+ CL->ExecuteKernel ( );
135
130
136
- CL->ReleaseObjectsToGLAndFinish ( 2 , devCoord );
131
+ CL->ReleaseObjectsToGLAndFinish ( 2 , devCoord );
137
132
138
- idx = ++idx % 2 ;
139
- Render ();
133
+ idx = ++idx % 2 ;
134
+ Render ();
140
135
141
- float time = (float ) m_timer.TocSimple ();
142
- sprintf_s ( title, 50 , " %2.0ffps %.3fs" , 1 /time, time );
143
- glutSetWindowTitle ( title );
136
+ const float time = (float ) m_timer.TocSimple ();
137
+ sprintf_s ( title, 50 , " %2.0ffps %.3fs" , 1 /time, time );
138
+ glutSetWindowTitle ( title );
144
139
}
145
140
146
- void GL::Render ( void ) {
147
- if ( m_paused ) return ;
141
+ void GL::Render () {
142
+ if ( m_paused ) return ;
148
143
149
- glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
150
- UpdateView ( );
144
+ glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
145
+ UpdateView ( );
151
146
152
- glBindBuffer ( GL_ARRAY_BUFFER, m_vboVertices[idx] );
153
- glVertexPointer ( 3 , GL_FLOAT, 4 *sizeof (float ), 0 );
154
- glDrawArrays ( GL_POINTS, 0 , m_info->n );
147
+ glBindBuffer ( GL_ARRAY_BUFFER, m_vboVertices[idx] );
148
+ glVertexPointer ( 3 , GL_FLOAT, 4 *sizeof (float ), nullptr );
149
+ glDrawArrays ( GL_POINTS, 0 , m_info->n );
155
150
156
- glutSwapBuffers ( );
151
+ glutSwapBuffers ( );
157
152
}
158
153
159
- void GL::Refresh ( int ms ) {
160
- // this makes sure the Display function is called every ms miliseconds
161
- glutTimerFunc ( ms, GL::Refresh, ms );
162
- glutPostRedisplay ( );
154
+ void GL::Refresh ( const int ms ) {
155
+ // this makes sure the Display function is called every ms miliseconds
156
+ glutTimerFunc ( ms, GL::Refresh, ms );
157
+ glutPostRedisplay ( );
163
158
}
164
- void GL::Keyboard ( unsigned char key, int x, int y ) {
165
- switch ( key ) {
166
- case 27 : // ESC
167
- exit ( 0 );
168
- break ;
169
- case ' ' :
170
- m_paused = !m_paused;
171
- break ;
172
- case ' +' :
173
- m_cameraDistance -= DELTA_CAMERA_DISTANCE;
174
- glUniform1f ( m_uniformDistToCamera, m_cameraDistance );
175
- break ;
176
- case ' -' :
177
- m_cameraDistance += DELTA_CAMERA_DISTANCE;
178
- glUniform1f ( m_uniformDistToCamera, m_cameraDistance );
179
- break ;
180
- case ' f' :
181
- glutFullScreenToggle ();
182
- break ;
183
- default :
184
- break ;
185
- }
159
+
160
+ void GL::Keyboard ( const unsigned char key, int x, int y ) {
161
+ switch ( key ) {
162
+ case 27 : // ESC
163
+ exit ( 0 ) ;
164
+ case ' ' :
165
+ m_paused = !m_paused;
166
+ break ;
167
+ case ' +' :
168
+ m_cameraDistance -= DELTA_CAMERA_DISTANCE;
169
+ glUniform1f ( m_uniformDistToCamera, m_cameraDistance );
170
+ break ;
171
+ case ' -' :
172
+ m_cameraDistance += DELTA_CAMERA_DISTANCE;
173
+ glUniform1f ( m_uniformDistToCamera, m_cameraDistance );
174
+ break ;
175
+ case ' f' :
176
+ glutFullScreenToggle ();
177
+ break ;
178
+ default :
179
+ break ;
180
+ }
186
181
}
187
- void GL::KeyboardSpecial ( int key, int x, int y ) {
188
- switch ( key ) {
189
- case GLUT_KEY_LEFT:
190
- m_angleY -= DELTA_ANGLE_Y;
191
- break ;
192
- case GLUT_KEY_RIGHT:
193
- m_angleY += DELTA_ANGLE_Y;
194
- break ;
195
- case GLUT_KEY_UP:
196
- m_angleX -= DELTA_ANGLE_X;
197
- break ;
198
- case GLUT_KEY_DOWN:
199
- m_angleX += DELTA_ANGLE_X;
200
- break ;
201
- default :
202
- break ;
203
- }
182
+ void GL::KeyboardSpecial (const int key, int x, int y ) {
183
+ switch ( key ) {
184
+ case GLUT_KEY_LEFT:
185
+ m_angleY -= DELTA_ANGLE_Y;
186
+ break ;
187
+ case GLUT_KEY_RIGHT:
188
+ m_angleY += DELTA_ANGLE_Y;
189
+ break ;
190
+ case GLUT_KEY_UP:
191
+ m_angleX -= DELTA_ANGLE_X;
192
+ break ;
193
+ case GLUT_KEY_DOWN:
194
+ m_angleX += DELTA_ANGLE_X;
195
+ break ;
196
+ default :
197
+ break ;
198
+ }
204
199
}
205
200
206
201
void GL::UpdateView () {
207
- glMatrixMode ( GL_MODELVIEW );
208
- glLoadIdentity ( );
209
- glTranslatef ( 0.0 , 0.0 , -m_cameraDistance );
210
- glRotatef ( m_angleX, 1 , 0 , 0 );
211
- glRotatef ( m_angleY, 0 , 1 , 0 );
202
+ glMatrixMode ( GL_MODELVIEW );
203
+ glLoadIdentity ( );
204
+ glTranslatef ( 0.0 , 0.0 , -m_cameraDistance );
205
+ glRotatef ( m_angleX, 1 , 0 , 0 );
206
+ glRotatef ( m_angleY, 0 , 1 , 0 );
212
207
}
213
208
214
- void GL::CheckShaderCompileStatus ( GLuint shader ) {
215
- int bufflen;
216
- glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &bufflen );
217
- if ( bufflen > 1 ) {
218
- GLchar* log_string = new char [bufflen + 1 ];
219
- glGetShaderInfoLog ( shader, bufflen, 0 , log_string );
220
- printf ( " Log:\n %s" , log_string );
221
- delete log_string;
222
- }
223
-
224
- int success;
225
- glGetShaderiv ( shader, GL_COMPILE_STATUS, &success );
226
- if ( success != GL_TRUE ) {
227
- printf ( " Failed to compile shader.\n " );
228
- exit ( -1001 );
229
- }
209
+ void GL::CheckShaderCompileStatus (const GLuint shader ) {
210
+ int bufflen;
211
+ glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &bufflen );
212
+ if ( bufflen > 1 ) {
213
+ GLchar* log_string = new char [bufflen + 1 ];
214
+ glGetShaderInfoLog ( shader, bufflen, nullptr , log_string );
215
+ printf ( " Log:\n %s" , log_string );
216
+ delete[] log_string;
217
+ }
218
+
219
+ int success;
220
+ glGetShaderiv ( shader, GL_COMPILE_STATUS, &success );
221
+ if ( success != GL_TRUE ) {
222
+ printf ( " Failed to compile shader.\n " );
223
+ exit ( -1001 );
224
+ }
230
225
}
231
- GLint GL::CreateShader ( GLenum shaderType, char *filename ) {
232
- GLint shader = glCreateShader ( shaderType );
233
- const GLchar *src = WOCL::ReadWholeFile ( filename, NULL );
234
- glShaderSource ( shader, 1 , &src, NULL );
235
- glCompileShader ( shader );
236
- CheckShaderCompileStatus ( shader );
237
- return shader;
226
+ GLint GL::CreateShader (const GLenum shaderType, char *filename ) {
227
+ const GLint shader = glCreateShader ( shaderType );
228
+ const GLchar *src = WOCL::ReadWholeFile ( filename, nullptr );
229
+ glShaderSource ( shader, 1 , &src, nullptr );
230
+ glCompileShader ( shader );
231
+ CheckShaderCompileStatus ( shader );
232
+ return shader;
238
233
}
0 commit comments