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