Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This program is under the GNU GPL. |
| 3 | * Use at your own risk. |
| 4 | * |
| 5 | * written by David Bucciarelli (humanware@plus.it) |
| 6 | * Humanware s.r.l. |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
Brian | a5eb9e7 | 2007-12-12 08:05:12 -0700 | [diff] [blame] | 11 | #include <string.h> |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 12 | #include <math.h> |
| 13 | #include <time.h> |
| 14 | #include <GL/glut.h> |
Karl Schultz | 164ce12 | 2002-01-16 00:48:43 +0000 | [diff] [blame] | 15 | #ifndef M_PI |
| 16 | #define M_PI 3.14159265 |
| 17 | #endif |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 18 | |
| 19 | #include "particles.h" |
| 20 | extern "C" { |
Karl Schultz | 164ce12 | 2002-01-16 00:48:43 +0000 | [diff] [blame] | 21 | #include "readtex.h" |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Karl Schultz | 164ce12 | 2002-01-16 00:48:43 +0000 | [diff] [blame] | 24 | #ifdef _WIN32 |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 25 | #include <windows.h> |
| 26 | #include <mmsystem.h> |
| 27 | #endif |
| 28 | |
| 29 | #ifdef XMESA |
| 30 | #include "GL/xmesa.h" |
| 31 | static int fullscreen=1; |
| 32 | #endif |
| 33 | |
| 34 | static int WIDTH=640; |
| 35 | static int HEIGHT=480; |
| 36 | static int NUMPART=7500; |
| 37 | |
| 38 | #define FRAME 50 |
| 39 | |
| 40 | static float fogcolor[4]={1.0,1.0,1.0,1.0}; |
| 41 | |
| 42 | #define DIMP 40.0 |
| 43 | #define DIMTP 32.0 |
| 44 | |
| 45 | static float q[4][3]={ |
| 46 | {-DIMP,0.0,-DIMP}, |
| 47 | {DIMP,0.0,-DIMP}, |
| 48 | {DIMP,0.0,DIMP}, |
| 49 | {-DIMP,0.0,DIMP} |
| 50 | }; |
| 51 | |
| 52 | static float qt[4][2]={ |
| 53 | {-DIMTP,-DIMTP}, |
| 54 | {DIMTP,-DIMTP}, |
| 55 | {DIMTP,DIMTP}, |
| 56 | {-DIMTP,DIMTP} |
| 57 | }; |
| 58 | |
| 59 | static int win=0; |
| 60 | |
| 61 | static int fog=1; |
| 62 | static int help=1; |
| 63 | |
| 64 | static GLuint groundid; |
| 65 | |
| 66 | static float obs[3]={2.0,1.0,0.0}; |
| 67 | static float dir[3]; |
| 68 | static float v=0.0; |
| 69 | static float alpha=-90.0; |
| 70 | static float beta=90.0; |
| 71 | |
| 72 | static particleSystem *ps; |
| 73 | |
| 74 | static float gettime() |
| 75 | { |
| 76 | static clock_t told=0; |
| 77 | clock_t tnew,ris; |
| 78 | |
| 79 | tnew=clock(); |
| 80 | |
| 81 | ris=tnew-told; |
| 82 | |
| 83 | told=tnew; |
| 84 | |
| 85 | return(ris/(float)CLOCKS_PER_SEC); |
| 86 | } |
| 87 | |
| 88 | static float gettimerain() |
| 89 | { |
| 90 | static clock_t told=0; |
| 91 | clock_t tnew,ris; |
| 92 | |
| 93 | tnew=clock(); |
| 94 | |
| 95 | ris=tnew-told; |
| 96 | |
| 97 | told=tnew; |
| 98 | |
| 99 | return(ris/(float)CLOCKS_PER_SEC); |
| 100 | } |
| 101 | |
| 102 | static void calcposobs(void) |
| 103 | { |
| 104 | dir[0]=sin(alpha*M_PI/180.0); |
| 105 | dir[2]=cos(alpha*M_PI/180.0)*sin(beta*M_PI/180.0); |
| 106 | dir[1]=cos(beta*M_PI/180.0); |
| 107 | |
| 108 | obs[0]+=v*dir[0]; |
| 109 | obs[1]+=v*dir[1]; |
| 110 | obs[2]+=v*dir[2]; |
| 111 | |
| 112 | rainParticle::setRainingArea(obs[0]-7.0f,-0.2f,obs[2]-7.0f,obs[0]+7.0f,8.0f,obs[2]+7.0f); |
| 113 | } |
| 114 | |
Guillaume Melquiond | ee2c8d6 | 2008-09-13 14:21:50 -0600 | [diff] [blame] | 115 | static void printstring(void *font, const char *string) |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 116 | { |
| 117 | int len,i; |
| 118 | |
| 119 | len=(int)strlen(string); |
| 120 | for(i=0;i<len;i++) |
| 121 | glutBitmapCharacter(font,string[i]); |
| 122 | } |
| 123 | |
| 124 | static void reshape(int width, int height) |
| 125 | { |
| 126 | WIDTH=width; |
| 127 | HEIGHT=height; |
| 128 | glViewport(0,0,(GLint)width,(GLint)height); |
| 129 | glMatrixMode(GL_PROJECTION); |
| 130 | glLoadIdentity(); |
| 131 | gluPerspective(70.0,width/(float)height,0.1,30.0); |
| 132 | |
| 133 | glMatrixMode(GL_MODELVIEW); |
| 134 | } |
| 135 | |
| 136 | static void printhelp(void) |
| 137 | { |
| 138 | glEnable(GL_BLEND); |
| 139 | glColor4f(0.0,0.0,0.0,0.5); |
| 140 | glRecti(40,40,600,440); |
| 141 | glDisable(GL_BLEND); |
| 142 | |
| 143 | glColor3f(1.0,0.0,0.0); |
| 144 | glRasterPos2i(300,420); |
| 145 | printstring(GLUT_BITMAP_TIMES_ROMAN_24,"Help"); |
| 146 | |
| 147 | glRasterPos2i(60,390); |
Brian Paul | c39a4bc | 2005-04-04 20:06:40 +0000 | [diff] [blame] | 148 | printstring(GLUT_BITMAP_TIMES_ROMAN_24,"h - Toggle Help"); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 149 | |
| 150 | glRasterPos2i(60,360); |
Brian Paul | c39a4bc | 2005-04-04 20:06:40 +0000 | [diff] [blame] | 151 | printstring(GLUT_BITMAP_TIMES_ROMAN_24,"f - Toggle Fog"); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 152 | glRasterPos2i(60,330); |
| 153 | printstring(GLUT_BITMAP_TIMES_ROMAN_24,"Arrow Keys - Rotate"); |
| 154 | glRasterPos2i(60,300); |
| 155 | printstring(GLUT_BITMAP_TIMES_ROMAN_24,"a - Increase velocity"); |
| 156 | glRasterPos2i(60,270); |
| 157 | printstring(GLUT_BITMAP_TIMES_ROMAN_24,"z - Decrease velocity"); |
| 158 | glRasterPos2i(60,240); |
| 159 | printstring(GLUT_BITMAP_TIMES_ROMAN_24,"l - Increase rain length"); |
| 160 | glRasterPos2i(60,210); |
| 161 | printstring(GLUT_BITMAP_TIMES_ROMAN_24,"k - Increase rain length"); |
| 162 | } |
| 163 | |
| 164 | static void drawrain(void) |
| 165 | { |
| 166 | static int count=0; |
| 167 | static char frbuf[80]; |
| 168 | float fr; |
| 169 | |
| 170 | glEnable(GL_DEPTH_TEST); |
| 171 | |
| 172 | if(fog) |
| 173 | glEnable(GL_FOG); |
| 174 | else |
| 175 | glDisable(GL_FOG); |
| 176 | |
| 177 | glDepthMask(GL_TRUE); |
| 178 | glClearColor(1.0,1.0,1.0,1.0); |
| 179 | glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
| 180 | |
| 181 | glPushMatrix(); |
| 182 | calcposobs(); |
| 183 | gluLookAt(obs[0],obs[1],obs[2], |
| 184 | obs[0]+dir[0],obs[1]+dir[1],obs[2]+dir[2], |
| 185 | 0.0,1.0,0.0); |
| 186 | |
| 187 | glColor4f(1.0,1.0,1.0,1.0); |
| 188 | |
| 189 | glEnable(GL_TEXTURE_2D); |
| 190 | |
| 191 | glBindTexture(GL_TEXTURE_2D,groundid); |
| 192 | glBegin(GL_QUADS); |
| 193 | glTexCoord2fv(qt[0]); |
| 194 | glVertex3fv(q[0]); |
| 195 | glTexCoord2fv(qt[1]); |
| 196 | glVertex3fv(q[1]); |
| 197 | glTexCoord2fv(qt[2]); |
| 198 | glVertex3fv(q[2]); |
| 199 | glTexCoord2fv(qt[3]); |
| 200 | glVertex3fv(q[3]); |
| 201 | glEnd(); |
| 202 | |
| 203 | // Particle System |
| 204 | |
| 205 | glDisable(GL_TEXTURE_2D); |
| 206 | glShadeModel(GL_SMOOTH); |
| 207 | glEnable(GL_BLEND); |
| 208 | |
| 209 | ps->draw(); |
| 210 | ps->addTime(gettimerain()); |
| 211 | |
| 212 | glShadeModel(GL_FLAT); |
| 213 | |
| 214 | |
| 215 | if((count % FRAME)==0) { |
| 216 | fr=gettime(); |
| 217 | sprintf(frbuf,"Frame rate: %f",FRAME/fr); |
| 218 | } |
| 219 | |
| 220 | glDisable(GL_TEXTURE_2D); |
| 221 | glDisable(GL_DEPTH_TEST); |
| 222 | glDisable(GL_FOG); |
| 223 | |
| 224 | glMatrixMode(GL_PROJECTION); |
| 225 | glLoadIdentity(); |
| 226 | glOrtho(-0.5,639.5,-0.5,479.5,-1.0,1.0); |
| 227 | glMatrixMode(GL_MODELVIEW); |
| 228 | glLoadIdentity(); |
| 229 | |
| 230 | glColor3f(1.0,0.0,0.0); |
| 231 | glRasterPos2i(10,10); |
| 232 | printstring(GLUT_BITMAP_HELVETICA_18,frbuf); |
| 233 | glRasterPos2i(350,470); |
| 234 | printstring(GLUT_BITMAP_HELVETICA_10,"Rain V1.0 Written by David Bucciarelli (humanware@plus.it)"); |
| 235 | |
| 236 | if(help) |
| 237 | printhelp(); |
| 238 | |
| 239 | reshape(WIDTH,HEIGHT); |
| 240 | glPopMatrix(); |
| 241 | |
| 242 | glutSwapBuffers(); |
| 243 | |
| 244 | count++; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | static void special(int key, int x, int y) |
| 249 | { |
| 250 | switch (key) { |
| 251 | case GLUT_KEY_LEFT: |
| 252 | alpha+=2.0; |
| 253 | break; |
| 254 | case GLUT_KEY_RIGHT: |
| 255 | alpha-=2.0; |
| 256 | break; |
| 257 | case GLUT_KEY_DOWN: |
| 258 | beta-=2.0; |
| 259 | break; |
| 260 | case GLUT_KEY_UP: |
| 261 | beta+=2.0; |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | static void key(unsigned char key, int x, int y) |
| 267 | { |
| 268 | switch (key) { |
| 269 | case 27: |
| 270 | exit(0); |
| 271 | break; |
| 272 | |
| 273 | case 'a': |
| 274 | v+=0.01; |
| 275 | break; |
| 276 | case 'z': |
| 277 | v-=0.01; |
| 278 | break; |
| 279 | |
| 280 | case 'l': |
| 281 | rainParticle::setLength(rainParticle::getLength()+0.025f); |
| 282 | break; |
| 283 | case 'k': |
| 284 | rainParticle::setLength(rainParticle::getLength()-0.025f); |
| 285 | break; |
| 286 | |
| 287 | case 'h': |
| 288 | help=(!help); |
| 289 | break; |
| 290 | case 'f': |
| 291 | fog=(!fog); |
| 292 | break; |
| 293 | #ifdef XMESA |
| 294 | case ' ': |
| 295 | XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW); |
| 296 | fullscreen=(!fullscreen); |
| 297 | break; |
| 298 | #endif |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static void inittextures(void) |
| 303 | { |
Karl Schultz | 164ce12 | 2002-01-16 00:48:43 +0000 | [diff] [blame] | 304 | GLubyte *img; |
| 305 | GLint width,height; |
| 306 | GLenum format; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 307 | GLenum gluerr; |
| 308 | |
| 309 | glGenTextures(1,&groundid); |
| 310 | glBindTexture(GL_TEXTURE_2D,groundid); |
| 311 | |
Karl Schultz | 164ce12 | 2002-01-16 00:48:43 +0000 | [diff] [blame] | 312 | if(!(img=LoadRGBImage("../images/s128.rgb",&width,&height,&format))){ |
| 313 | fprintf(stderr,"Error reading a texture.\n"); |
| 314 | exit(-1); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 315 | } |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 316 | glPixelStorei(GL_UNPACK_ALIGNMENT,4); |
Karl Schultz | 164ce12 | 2002-01-16 00:48:43 +0000 | [diff] [blame] | 317 | if((gluerr=(GLenum)gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height,GL_RGB, |
| 318 | GL_UNSIGNED_BYTE, (GLvoid *)(img)))) { |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 319 | fprintf(stderr,"GLULib%s\n",gluErrorString(gluerr)); |
| 320 | exit(-1); |
| 321 | } |
| 322 | |
| 323 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); |
| 324 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); |
| 325 | |
| 326 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); |
| 327 | glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); |
| 328 | |
| 329 | glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL); |
| 330 | } |
| 331 | |
| 332 | static void initparticle(void) |
| 333 | { |
| 334 | ps=new particleSystem; |
| 335 | |
| 336 | rainParticle::setRainingArea(-7.0f,-0.2f,-7.0f,7.0f,8.0f,7.0f); |
| 337 | |
| 338 | for(int i=0;i<NUMPART;i++) { |
| 339 | rainParticle *p=new rainParticle; |
| 340 | p->randomHeight(); |
| 341 | |
| 342 | ps->addParticle((particle *)p); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | int main(int ac,char **av) |
| 347 | { |
| 348 | fprintf(stderr,"Rain V1.0\nWritten by David Bucciarelli (humanware@plus.it)\n"); |
| 349 | |
| 350 | /* Default settings */ |
| 351 | |
| 352 | WIDTH=640; |
| 353 | HEIGHT=480; |
| 354 | |
| 355 | glutInitWindowPosition(0,0); |
| 356 | glutInitWindowSize(WIDTH,HEIGHT); |
| 357 | glutInit(&ac,av); |
| 358 | |
| 359 | glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE); |
| 360 | |
| 361 | if(!(win=glutCreateWindow("Rain"))) { |
| 362 | fprintf(stderr,"Error opening a window.\n"); |
| 363 | exit(-1); |
| 364 | } |
| 365 | |
| 366 | reshape(WIDTH,HEIGHT); |
| 367 | |
| 368 | inittextures(); |
| 369 | |
| 370 | glShadeModel(GL_FLAT); |
| 371 | glEnable(GL_DEPTH_TEST); |
| 372 | |
| 373 | glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); |
| 374 | |
| 375 | glEnable(GL_FOG); |
| 376 | glFogi(GL_FOG_MODE,GL_EXP); |
| 377 | glFogfv(GL_FOG_COLOR,fogcolor); |
| 378 | glFogf(GL_FOG_DENSITY,0.1); |
| 379 | #ifdef FX |
| 380 | glHint(GL_FOG_HINT,GL_NICEST); |
| 381 | #endif |
| 382 | |
| 383 | initparticle(); |
| 384 | |
| 385 | glutKeyboardFunc(key); |
| 386 | glutSpecialFunc(special); |
| 387 | glutDisplayFunc(drawrain); |
| 388 | glutIdleFunc(drawrain); |
| 389 | glutReshapeFunc(reshape); |
| 390 | glutMainLoop(); |
| 391 | |
| 392 | return(0); |
| 393 | } |