2 * This program is under the GNU GPL.
3 * Use at your own risk.
5 * written by David Bucciarelli (humanware@plus.it)
15 #define M_PI 3.14159265
18 #include "particles.h"
26 #include "particles.cxx"
32 static int fullscreen
=1;
36 static int HEIGHT
=480;
37 static int NUMPART
=7500;
41 static float fogcolor
[4]={1.0,1.0,1.0,1.0};
46 static float q
[4][3]={
53 static float qt
[4][2]={
65 static GLuint groundid
;
67 static float obs
[3]={2.0,1.0,0.0};
70 static float alpha
=-90.0;
71 static float beta
=90.0;
73 static particleSystem
*ps
;
75 static float gettime()
77 static clock_t told
=0;
86 return(ris
/(float)CLOCKS_PER_SEC
);
89 static float gettimerain()
91 static clock_t told
=0;
100 return(ris
/(float)CLOCKS_PER_SEC
);
103 static void calcposobs(void)
105 dir
[0]=sin(alpha
*M_PI
/180.0);
106 dir
[2]=cos(alpha
*M_PI
/180.0)*sin(beta
*M_PI
/180.0);
107 dir
[1]=cos(beta
*M_PI
/180.0);
113 rainParticle::setRainingArea(obs
[0]-7.0f
,-0.2f
,obs
[2]-7.0f
,obs
[0]+7.0f
,8.0f
,obs
[2]+7.0f
);
116 static void printstring(void *font
, char *string
)
120 len
=(int)strlen(string
);
122 glutBitmapCharacter(font
,string
[i
]);
125 static void reshape(int width
, int height
)
129 glViewport(0,0,(GLint
)width
,(GLint
)height
);
130 glMatrixMode(GL_PROJECTION
);
132 gluPerspective(70.0,width
/(float)height
,0.1,30.0);
134 glMatrixMode(GL_MODELVIEW
);
137 static void printhelp(void)
140 glColor4f(0.0,0.0,0.0,0.5);
141 glRecti(40,40,600,440);
144 glColor3f(1.0,0.0,0.0);
145 glRasterPos2i(300,420);
146 printstring(GLUT_BITMAP_TIMES_ROMAN_24
,"Help");
148 glRasterPos2i(60,390);
149 printstring(GLUT_BITMAP_TIMES_ROMAN_24
,"h - Toggle Help");
151 glRasterPos2i(60,360);
152 printstring(GLUT_BITMAP_TIMES_ROMAN_24
,"f - Toggle Fog");
153 glRasterPos2i(60,330);
154 printstring(GLUT_BITMAP_TIMES_ROMAN_24
,"Arrow Keys - Rotate");
155 glRasterPos2i(60,300);
156 printstring(GLUT_BITMAP_TIMES_ROMAN_24
,"a - Increase velocity");
157 glRasterPos2i(60,270);
158 printstring(GLUT_BITMAP_TIMES_ROMAN_24
,"z - Decrease velocity");
159 glRasterPos2i(60,240);
160 printstring(GLUT_BITMAP_TIMES_ROMAN_24
,"l - Increase rain length");
161 glRasterPos2i(60,210);
162 printstring(GLUT_BITMAP_TIMES_ROMAN_24
,"k - Increase rain length");
165 static void drawrain(void)
168 static char frbuf
[80];
171 glEnable(GL_DEPTH_TEST
);
178 glDepthMask(GL_TRUE
);
179 glClearColor(1.0,1.0,1.0,1.0);
180 glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
184 gluLookAt(obs
[0],obs
[1],obs
[2],
185 obs
[0]+dir
[0],obs
[1]+dir
[1],obs
[2]+dir
[2],
188 glColor4f(1.0,1.0,1.0,1.0);
190 glEnable(GL_TEXTURE_2D
);
192 glBindTexture(GL_TEXTURE_2D
,groundid
);
194 glTexCoord2fv(qt
[0]);
196 glTexCoord2fv(qt
[1]);
198 glTexCoord2fv(qt
[2]);
200 glTexCoord2fv(qt
[3]);
206 glDisable(GL_TEXTURE_2D
);
207 glShadeModel(GL_SMOOTH
);
211 ps
->addTime(gettimerain());
213 glShadeModel(GL_FLAT
);
216 if((count
% FRAME
)==0) {
218 sprintf(frbuf
,"Frame rate: %f",FRAME
/fr
);
221 glDisable(GL_TEXTURE_2D
);
222 glDisable(GL_DEPTH_TEST
);
225 glMatrixMode(GL_PROJECTION
);
227 glOrtho(-0.5,639.5,-0.5,479.5,-1.0,1.0);
228 glMatrixMode(GL_MODELVIEW
);
231 glColor3f(1.0,0.0,0.0);
232 glRasterPos2i(10,10);
233 printstring(GLUT_BITMAP_HELVETICA_18
,frbuf
);
234 glRasterPos2i(350,470);
235 printstring(GLUT_BITMAP_HELVETICA_10
,"Rain V1.0 Written by David Bucciarelli (humanware@plus.it)");
240 reshape(WIDTH
,HEIGHT
);
249 static void special(int key
, int x
, int y
)
267 static void key(unsigned char key
, int x
, int y
)
282 rainParticle::setLength(rainParticle::getLength()+0.025f
);
285 rainParticle::setLength(rainParticle::getLength()-0.025f
);
296 XMesaSetFXmode(fullscreen
? XMESA_FX_FULLSCREEN
: XMESA_FX_WINDOW
);
297 fullscreen
=(!fullscreen
);
303 static void inittextures(void)
310 glGenTextures(1,&groundid
);
311 glBindTexture(GL_TEXTURE_2D
,groundid
);
313 if(!(img
=LoadRGBImage("../images/s128.rgb",&width
,&height
,&format
))){
314 fprintf(stderr
,"Error reading a texture.\n");
317 glPixelStorei(GL_UNPACK_ALIGNMENT
,4);
318 if((gluerr
=(GLenum
)gluBuild2DMipmaps(GL_TEXTURE_2D
, 3, width
, height
,GL_RGB
,
319 GL_UNSIGNED_BYTE
, (GLvoid
*)(img
)))) {
320 fprintf(stderr
,"GLULib%s\n",gluErrorString(gluerr
));
324 glTexParameterf(GL_TEXTURE_2D
,GL_TEXTURE_WRAP_S
,GL_REPEAT
);
325 glTexParameterf(GL_TEXTURE_2D
,GL_TEXTURE_WRAP_T
,GL_REPEAT
);
327 glTexParameterf(GL_TEXTURE_2D
,GL_TEXTURE_MIN_FILTER
,GL_LINEAR_MIPMAP_LINEAR
);
328 glTexParameterf(GL_TEXTURE_2D
,GL_TEXTURE_MAG_FILTER
,GL_LINEAR
);
330 glTexEnvf(GL_TEXTURE_ENV
,GL_TEXTURE_ENV_MODE
,GL_DECAL
);
333 static void initparticle(void)
335 ps
=new particleSystem
;
337 rainParticle::setRainingArea(-7.0f
,-0.2f
,-7.0f
,7.0f
,8.0f
,7.0f
);
339 for(int i
=0;i
<NUMPART
;i
++) {
340 rainParticle
*p
=new rainParticle
;
343 ps
->addParticle((particle
*)p
);
347 int main(int ac
,char **av
)
349 fprintf(stderr
,"Rain V1.0\nWritten by David Bucciarelli (humanware@plus.it)\n");
351 /* Default settings */
356 glutInitWindowPosition(0,0);
357 glutInitWindowSize(WIDTH
,HEIGHT
);
360 glutInitDisplayMode(GLUT_RGB
|GLUT_DEPTH
|GLUT_DOUBLE
);
362 if(!(win
=glutCreateWindow("Rain"))) {
363 fprintf(stderr
,"Error opening a window.\n");
367 reshape(WIDTH
,HEIGHT
);
371 glShadeModel(GL_FLAT
);
372 glEnable(GL_DEPTH_TEST
);
374 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
377 glFogi(GL_FOG_MODE
,GL_EXP
);
378 glFogfv(GL_FOG_COLOR
,fogcolor
);
379 glFogf(GL_FOG_DENSITY
,0.1);
381 glHint(GL_FOG_HINT
,GL_NICEST
);
386 glutKeyboardFunc(key
);
387 glutSpecialFunc(special
);
388 glutDisplayFunc(drawrain
);
389 glutIdleFunc(drawrain
);
390 glutReshapeFunc(reshape
);