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