2 FLUIDS v.1 - SPH Fluid Simulator for CPU and GPU
3 Copyright (C) 2009. Rama Hoetzlein, http://www.rchoetzlein.com
6 This software is provided 'as-is', without any express or implied
7 warranty. In no event will the authors be held liable for any damages
8 arising from the use of this software.
10 Permission is granted to anyone to use this software for any purpose,
11 including commercial applications, and to alter it and redistribute it
12 freely, subject to the following restrictions:
14 1. The origin of this software must not be misrepresented; you must not
15 claim that you wrote the original software. If you use this software
16 in a product, an acknowledgment in the product documentation would be
17 appreciated but is not required.
18 2. Altered source versions must be plainly marked as such, and must not be
19 misrepresented as being the original software.
20 3. This notice may not be removed or altered from any source distribution.
23 #include "common_defs.h"
24 #include "gl_helper.h"
31 float light_x, light_y, light_z;
32 float light_tox, light_toy, light_toz;
36 void *font = GLUT_BITMAP_8_BY_13;
37 void *fonts[] = {GLUT_BITMAP_9_BY_15,
38 GLUT_BITMAP_TIMES_ROMAN_10,
39 GLUT_BITMAP_TIMES_ROMAN_24};
45 GLuint glSphere = 65535;
48 void setSphereRadius ( float r )
50 if ( glRadius == r ) return;
54 if ( glSphere != 65535 ) glDeleteLists ( glSphere, 1 );
55 glSphere = glGenLists ( 1 );
56 float x, y, z, x1, y1, z1;
57 glNewList ( glSphere, GL_COMPILE );
58 glBegin ( GL_TRIANGLE_STRIP );
59 for ( float tilt=-90; tilt <= 90; tilt += 10.0) {
60 for ( float ang=0; ang <= 360; ang += 30.0) {
61 x = sin ( ang*DEGtoRAD) * cos ( tilt*DEGtoRAD );
62 y = cos ( ang*DEGtoRAD) * cos ( tilt*DEGtoRAD );
63 z = sin ( tilt*DEGtoRAD ) ;
64 x1 = sin ( ang*DEGtoRAD) * cos ( (tilt+10.0)*DEGtoRAD ) ;
65 y1 = cos ( ang*DEGtoRAD) * cos ( (tilt+10.0)*DEGtoRAD ) ;
66 z1 = sin ( (tilt+10.0)*DEGtoRAD );
67 glNormal3f ( x, y, z ); glVertex3f ( x*r, y*r, z*r );
68 glNormal3f ( x1, y1, z1 ); glVertex3f ( x1*r, y1*r, z1*r );
77 if ( glRadius == 0.0 ) setSphereRadius ( 1.0 );
78 glCallList ( glSphere );
81 // Check if there have been any openGL problems
84 GLenum errCode = glGetError();
85 if (errCode != GL_NO_ERROR) {
86 const GLubyte* errString = gluErrorString(errCode);
87 fprintf( stderr, "OpenGL error: %s\n", errString );
91 void drawText ( int x, int y, char* msg)
95 len = (int) strlen(msg);
96 for (i = 0; i < len; i++)
97 glutBitmapCharacter(font, msg[i]);
102 glColor3f ( 0.3, 0.3, 0.3 );
103 glBegin ( GL_LINES );
104 for (float x=-40; x<=40.0; x+=10.0 ) {
105 glVertex3f ( x, -40.0, 0 );
106 glVertex3f ( x, 40.0, 0 );
108 for (float y=-40; y<=40.0; y+=10.0 ) {
109 glVertex3f ( -40.0, y, 0 );
110 glVertex3f ( 40.0, y, 0 );
119 if ( ++tm_cnt > 5 ) {
120 tm_elaps.SetSystemTime ( ACC_NSEC ); // get current sytem time - accurate to 1 ns
121 tm_elaps = tm_elaps - tm_last; // get elapsed time from 5 frames ago
122 tm_fps = 5.0 * 1000.0 / tm_elaps.GetMSec (); // compute fps
123 tm_cnt = 0; // reset frame counter
124 tm_last.SetSystemTime ( ACC_NSEC );
128 void checkFrameBuffers ()
131 status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
133 case GL_FRAMEBUFFER_COMPLETE_EXT: printf ( "FBO complete\n" ); break;
134 case GL_FRAMEBUFFER_UNSUPPORTED_EXT: printf ( "FBO format unsupported\n"); break;
135 default: printf ( "Unknown FBO error\n");
139 void disableShadows ()
141 glDisable ( GL_TEXTURE_2D );
143 glActiveTextureARB( GL_TEXTURE1_ARB );
144 glBindTexture ( GL_TEXTURE_2D, 0 );
145 glDisable ( GL_TEXTURE_GEN_S );
146 glDisable ( GL_TEXTURE_GEN_T );
147 glDisable ( GL_TEXTURE_GEN_R );
148 glDisable ( GL_TEXTURE_GEN_Q );
150 glActiveTextureARB( GL_TEXTURE2_ARB );
151 glBindTexture ( GL_TEXTURE_2D, 0 );
152 glDisable ( GL_TEXTURE_GEN_S );
153 glDisable ( GL_TEXTURE_GEN_T );
154 glDisable ( GL_TEXTURE_GEN_R );
155 glDisable ( GL_TEXTURE_GEN_Q );
159 // Materials & Textures
160 GLuint shadow1_id = 0; // display buffer shadows
161 GLuint shadow2_id = 0; // display buffer shadows
164 GLuint frameBufferObject = 0; // frame buffer shadows
166 void createFrameBuffer ()
168 //Generate the frame buffer object
169 glGenFramebuffersEXT (1, &frameBufferObject);
170 glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, frameBufferObject); // Turn on frame buffer object
171 glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, shadow1_id, 0);
172 glDrawBuffer (GL_NONE); // Set Draw & ReadBuffer to none since we're rendering depth only
173 glReadBuffer (GL_NONE);
174 checkFrameBuffers (); // Check completeness of frame buffer object (no need for stencil and depth attachement)
175 glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0); // Turn off frame buffer object
178 void createShadowTextures ()
180 // Create depth texture maps
181 glActiveTextureARB( GL_TEXTURE1_ARB );
182 glGenTextures( 1, &shadow1_id );
183 glBindTexture ( GL_TEXTURE_2D, shadow1_id );
184 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
185 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
187 //-- sets region outside shadow to 0
188 //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER_ARB );
189 //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER_ARB );
191 //-- sets region outside shadow to 1 (border edge color)
192 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
193 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
195 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
196 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
197 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
198 glTexImage2D ( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24_ARB, TEX_SIZE, TEX_SIZE, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0);
200 glActiveTextureARB( GL_TEXTURE2_ARB );
201 glGenTextures( 1, &shadow2_id );
202 glBindTexture ( GL_TEXTURE_2D, shadow2_id );
203 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
204 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
205 //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER_ARB );
206 //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER_ARB );
207 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
208 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
209 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
210 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
211 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
212 glTexImage2D ( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24_ARB, TEX_SIZE, TEX_SIZE, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0);
215 void computeLightMatrix ( int n, int tx, int ty )
218 // Construct projective texturing matrix
220 // S - light bias matrix
221 glMatrixMode ( GL_MODELVIEW );
223 glTranslatef ( 0.5, 0.5, 0.5 );
224 glScalef ( 0.5, 0.5, 0.5 );
225 // Plight - light projection matrix
226 gluPerspective ( light_mfov*2.0, float(tx) / ty, LIGHT_NEAR, LIGHT_FAR );
227 // L^-1 - light view inverse matrix
228 gluLookAt ( light_x, light_y, light_z, light_tox, light_toy, light_toz, 0, 0, 1);
230 glGetFloatv ( GL_MODELVIEW_MATRIX, light_proj );
235 void renderDepthMap_Clear ( float wx, float wy )
237 glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, frameBufferObject);
238 glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, shadow1_id, 0);
239 glActiveTextureARB( GL_TEXTURE1_ARB ); // TEXTURE1 = shadow map stage
240 glViewport (1, 1, TEX_SIZE-2, TEX_SIZE-2); // Note: Avoid artifact cause by drawing into border pixels
241 glClear ( GL_DEPTH_BUFFER_BIT );
242 glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
243 glViewport ( 0, 0, (GLsizei) wx, (GLsizei) wy );
246 void renderDepthMap_FrameBuffer ( int n, float wx, float wy )
250 computeLightMatrix ( n, TEX_SIZE, TEX_SIZE );
252 glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, frameBufferObject);
255 glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, shadow1_id, 0);
257 glFramebufferTexture2DEXT (GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, shadow2_id, 0);
260 if ( n == 0 ) glActiveTextureARB( GL_TEXTURE1_ARB ); // TEXTURE1 = shadow map stage
261 else glActiveTextureARB( GL_TEXTURE2_ARB ); // TEXTURE2 = shadow map stage
263 glViewport (1, 1, TEX_SIZE-2, TEX_SIZE-2); // Note: Avoid artifact cause by drawing into border pixels
264 glClear ( GL_DEPTH_BUFFER_BIT );
267 // Plight - projection matrix of light
268 glMatrixMode ( GL_PROJECTION ); // Setup projection for depth-map rendering
270 gluPerspective ( light_mfov*2.0, float(TEX_SIZE) / TEX_SIZE, LIGHT_NEAR, LIGHT_FAR );
272 // L^-1 - light view matrix (gluLookAt computes inverse)
273 glMatrixMode ( GL_MODELVIEW); // Setup view for depth-map rendering
275 gluLookAt ( light_x, light_y, light_z, light_tox, light_toy, light_toz, 0, 0, 1);
276 glPushMatrix (); // Save view matrix for later
277 glGetFloatv ( GL_MODELVIEW_MATRIX, vmat );
280 glDisable ( GL_LIGHTING );
281 glColor4f ( 1, 1, 1, 1 );
282 glShadeModel (GL_FLAT); // No shading (faster)
284 glEnable ( GL_CULL_FACE );
285 glCullFace ( GL_FRONT );
287 glEnable ( GL_POLYGON_OFFSET_FILL );
288 glPolygonOffset ( 50.0, 0.1 ); // Depth bias
290 drawScene ( &vmat[0], false ); // Draw scene.
292 glDisable ( GL_POLYGON_OFFSET_FILL );
293 glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0);
294 glViewport ( 0, 0, (GLsizei) wx, (GLsizei) wy );
296 //glCullFace (GL_BACK); // Restore render states
297 //glBindTexture ( GL_TEXTURE_2D, 0);
300 void renderShadowStage ( int n, float* vmat )
305 computeLightMatrix ( n, TEX_SIZE, TEX_SIZE );
307 glActiveTextureARB( GL_TEXTURE1_ARB ); // TEXTURE1 = shadow map stage #1
309 glActiveTextureARB( GL_TEXTURE2_ARB ); // TEXTURE2 = shadow map stage #2
311 glEnable ( GL_TEXTURE_2D );
312 if ( n == 0 ) glBindTexture ( GL_TEXTURE_2D, shadow1_id );
313 else glBindTexture ( GL_TEXTURE_2D, shadow2_id );
315 glMatrixMode( GL_MODELVIEW );
316 glLoadMatrixf ( vmat );
318 row[0] = light_proj[0]; row[1] = light_proj[4]; row[2] = light_proj[8]; row[3] = light_proj[12];
319 glTexGenfv(GL_S, GL_EYE_PLANE, &row[0] );
320 row[0] = light_proj[1]; row[1] = light_proj[5]; row[2] = light_proj[9]; row[3] = light_proj[13];
321 glTexGenfv(GL_T, GL_EYE_PLANE, &row[0] );
322 row[0] = light_proj[2]; row[1] = light_proj[6]; row[2] = light_proj[10]; row[3] = light_proj[14];
323 glTexGenfv(GL_R, GL_EYE_PLANE, &row[0] );
324 row[0] = light_proj[3]; row[1] = light_proj[7]; row[2] = light_proj[11]; row[3] = light_proj[15];
325 glTexGenfv(GL_Q, GL_EYE_PLANE, &row[0] );
326 glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
327 glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
328 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
329 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
330 glEnable(GL_TEXTURE_GEN_S);
331 glEnable(GL_TEXTURE_GEN_T);
332 glEnable(GL_TEXTURE_GEN_R);
333 glEnable(GL_TEXTURE_GEN_Q);
335 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
337 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE ) ;
338 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE ) ;
339 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS ) ;
340 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_CONSTANT ) ;
346 glTexEnvfv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, &pos[0] );
349 void renderShadows ( float* vmat )
353 renderShadowStage ( 0, vmat );
354 // renderShadowStage ( 1, vmat );
356 glActiveTextureARB( GL_TEXTURE0_ARB ); // Render Tex 0 - Base render
357 glDisable ( GL_TEXTURE_GEN_S );
358 glDisable ( GL_TEXTURE_GEN_T );
359 glDisable ( GL_TEXTURE_GEN_R );
360 glDisable ( GL_TEXTURE_GEN_Q );
361 glTexEnvi ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
362 glEnable ( GL_LIGHTING );
363 glLightModeli (GL_LIGHT_MODEL_COLOR_CONTROL_EXT, GL_SEPARATE_SPECULAR_COLOR_EXT);
365 glEnable ( GL_LIGHT0 );
366 pos[0] = light_x; pos[1] = light_y; pos[2] = light_z; pos[3] = 1.0;
367 glLightfv ( GL_LIGHT0, GL_POSITION, &pos[0] );
369 /* glEnable ( GL_LIGHT1 );
370 pos[0] = light[1].x; pos[1] = light[1].y; pos[2] = light[1].z; pos[3] = 1.0;
371 glLightfv ( GL_LIGHT1, GL_POSITION, &pos[0] );*/
376 void setShadowLight ( float fx, float fy, float fz, float tx, float ty, float tz, float fov )
387 void setShadowLightColor ( float dr, float dg, float db, float sr, float sg, float sb )
389 GLfloat amb[4] = {0.0,0.0,0.0,1};
392 GLfloat pos[4] = {0.0,0.0,0.0, 100.0};
395 dif[0] = dr; dif[1] = dg; dif[2] = db; dif[3] = 1;
396 spec[0] = sr; spec[1] = sg; spec[2] = sb; spec[3] = 1;
397 glLightfv(GL_LIGHT0, GL_AMBIENT, &amb[0] );
398 glLightfv(GL_LIGHT0, GL_DIFFUSE, &dif[0] );
399 glLightfv(GL_LIGHT0, GL_SPECULAR, &spec[0] );