2 * This program is under the GNU GPL.
3 * Use at your own risk.
5 * written by David Bucciarelli (humanware@plus.it)
15 #include "particles.h"
27 static int fullscreen
=1;
31 static int HEIGHT
=480;
32 static int NUMPART
=7500;
36 static float fogcolor
[4]={1.0,1.0,1.0,1.0};
41 static float q
[4][3]={
48 static float qt
[4][2]={
60 static GLuint groundid
;
62 static float obs
[3]={2.0,1.0,0.0};
65 static float alpha
=-90.0;
66 static float beta
=90.0;
68 static particleSystem
*ps
;
70 static float gettime()
72 static clock_t told
=0;
81 return(ris
/(float)CLOCKS_PER_SEC
);
84 static float gettimerain()
86 static clock_t told
=0;
95 return(ris
/(float)CLOCKS_PER_SEC
);
98 static void calcposobs(void)
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);
108 rainParticle::setRainingArea(obs
[0]-7.0f
,-0.2f
,obs
[2]-7.0f
,obs
[0]+7.0f
,8.0f
,obs
[2]+7.0f
);
111 static void printstring(void *font
, char *string
)
115 len
=(int)strlen(string
);
117 glutBitmapCharacter(font
,string
[i
]);
120 static void reshape(int width
, int height
)
124 glViewport(0,0,(GLint
)width
,(GLint
)height
);
125 glMatrixMode(GL_PROJECTION
);
127 gluPerspective(70.0,width
/(float)height
,0.1,30.0);
129 glMatrixMode(GL_MODELVIEW
);
132 static void printhelp(void)
135 glColor4f(0.0,0.0,0.0,0.5);
136 glRecti(40,40,600,440);
139 glColor3f(1.0,0.0,0.0);
140 glRasterPos2i(300,420);
141 printstring(GLUT_BITMAP_TIMES_ROMAN_24
,"Help");
143 glRasterPos2i(60,390);
144 printstring(GLUT_BITMAP_TIMES_ROMAN_24
,"h - Togle Help");
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");
160 static void drawrain(void)
163 static char frbuf
[80];
166 glEnable(GL_DEPTH_TEST
);
173 glDepthMask(GL_TRUE
);
174 glClearColor(1.0,1.0,1.0,1.0);
175 glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
179 gluLookAt(obs
[0],obs
[1],obs
[2],
180 obs
[0]+dir
[0],obs
[1]+dir
[1],obs
[2]+dir
[2],
183 glColor4f(1.0,1.0,1.0,1.0);
185 glEnable(GL_TEXTURE_2D
);
187 glBindTexture(GL_TEXTURE_2D
,groundid
);
189 glTexCoord2fv(qt
[0]);
191 glTexCoord2fv(qt
[1]);
193 glTexCoord2fv(qt
[2]);
195 glTexCoord2fv(qt
[3]);
201 glDisable(GL_TEXTURE_2D
);
202 glShadeModel(GL_SMOOTH
);
206 ps
->addTime(gettimerain());
208 glShadeModel(GL_FLAT
);
211 if((count
% FRAME
)==0) {
213 sprintf(frbuf
,"Frame rate: %f",FRAME
/fr
);
216 glDisable(GL_TEXTURE_2D
);
217 glDisable(GL_DEPTH_TEST
);
220 glMatrixMode(GL_PROJECTION
);
222 glOrtho(-0.5,639.5,-0.5,479.5,-1.0,1.0);
223 glMatrixMode(GL_MODELVIEW
);
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)");
235 reshape(WIDTH
,HEIGHT
);
244 static void special(int key
, int x
, int y
)
262 static void key(unsigned char key
, int x
, int y
)
277 rainParticle::setLength(rainParticle::getLength()+0.025f
);
280 rainParticle::setLength(rainParticle::getLength()-0.025f
);
291 XMesaSetFXmode(fullscreen
? XMESA_FX_FULLSCREEN
: XMESA_FX_WINDOW
);
292 fullscreen
=(!fullscreen
);
298 static void inittextures(void)
303 glGenTextures(1,&groundid
);
304 glBindTexture(GL_TEXTURE_2D
,groundid
);
306 if(!(img
=ImageLoad("s128.rgb"))) {
307 fprintf(stderr
,"Error reading a texture.\n");
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
));
318 glTexParameterf(GL_TEXTURE_2D
,GL_TEXTURE_WRAP_S
,GL_REPEAT
);
319 glTexParameterf(GL_TEXTURE_2D
,GL_TEXTURE_WRAP_T
,GL_REPEAT
);
321 glTexParameterf(GL_TEXTURE_2D
,GL_TEXTURE_MIN_FILTER
,GL_LINEAR_MIPMAP_LINEAR
);
322 glTexParameterf(GL_TEXTURE_2D
,GL_TEXTURE_MAG_FILTER
,GL_LINEAR
);
324 glTexEnvf(GL_TEXTURE_ENV
,GL_TEXTURE_ENV_MODE
,GL_DECAL
);
327 static void initparticle(void)
329 ps
=new particleSystem
;
331 rainParticle::setRainingArea(-7.0f
,-0.2f
,-7.0f
,7.0f
,8.0f
,7.0f
);
333 for(int i
=0;i
<NUMPART
;i
++) {
334 rainParticle
*p
=new rainParticle
;
337 ps
->addParticle((particle
*)p
);
341 int main(int ac
,char **av
)
343 fprintf(stderr
,"Rain V1.0\nWritten by David Bucciarelli (humanware@plus.it)\n");
345 /* Default settings */
350 glutInitWindowPosition(0,0);
351 glutInitWindowSize(WIDTH
,HEIGHT
);
354 glutInitDisplayMode(GLUT_RGB
|GLUT_DEPTH
|GLUT_DOUBLE
);
356 if(!(win
=glutCreateWindow("Rain"))) {
357 fprintf(stderr
,"Error opening a window.\n");
361 reshape(WIDTH
,HEIGHT
);
365 glShadeModel(GL_FLAT
);
366 glEnable(GL_DEPTH_TEST
);
368 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
371 glFogi(GL_FOG_MODE
,GL_EXP
);
372 glFogfv(GL_FOG_COLOR
,fogcolor
);
373 glFogf(GL_FOG_DENSITY
,0.1);
375 glHint(GL_FOG_HINT
,GL_NICEST
);
380 glutKeyboardFunc(key
);
381 glutSpecialFunc(special
);
382 glutDisplayFunc(drawrain
);
383 glutIdleFunc(drawrain
);
384 glutReshapeFunc(reshape
);