2 * This program is under the GNU GPL.
3 * Use at your own risk.
5 * written by David Bucciarelli (humanware@plus.it)
16 #define M_PI 3.14159265
19 #include "particles.h"
27 #include "particles.cxx"
33 static int fullscreen
=1;
37 static int HEIGHT
=480;
38 static int NUMPART
=7500;
42 static float fogcolor
[4]={1.0,1.0,1.0,1.0};
47 static float q
[4][3]={
54 static float qt
[4][2]={
66 static GLuint groundid
;
68 static float obs
[3]={2.0,1.0,0.0};
71 static float alpha
=-90.0;
72 static float beta
=90.0;
74 static particleSystem
*ps
;
76 static float gettime()
78 static clock_t told
=0;
87 return(ris
/(float)CLOCKS_PER_SEC
);
90 static float gettimerain()
92 static clock_t told
=0;
101 return(ris
/(float)CLOCKS_PER_SEC
);
104 static void calcposobs(void)
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);
114 rainParticle::setRainingArea(obs
[0]-7.0f
,-0.2f
,obs
[2]-7.0f
,obs
[0]+7.0f
,8.0f
,obs
[2]+7.0f
);
117 static void printstring(void *font
, const char *string
)
121 len
=(int)strlen(string
);
123 glutBitmapCharacter(font
,string
[i
]);
126 static void reshape(int width
, int height
)
130 glViewport(0,0,(GLint
)width
,(GLint
)height
);
131 glMatrixMode(GL_PROJECTION
);
133 gluPerspective(70.0,width
/(float)height
,0.1,30.0);
135 glMatrixMode(GL_MODELVIEW
);
138 static void printhelp(void)
141 glColor4f(0.0,0.0,0.0,0.5);
142 glRecti(40,40,600,440);
145 glColor3f(1.0,0.0,0.0);
146 glRasterPos2i(300,420);
147 printstring(GLUT_BITMAP_TIMES_ROMAN_24
,"Help");
149 glRasterPos2i(60,390);
150 printstring(GLUT_BITMAP_TIMES_ROMAN_24
,"h - Toggle Help");
152 glRasterPos2i(60,360);
153 printstring(GLUT_BITMAP_TIMES_ROMAN_24
,"f - Toggle Fog");
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");
166 static void drawrain(void)
169 static char frbuf
[80];
172 glEnable(GL_DEPTH_TEST
);
179 glDepthMask(GL_TRUE
);
180 glClearColor(1.0,1.0,1.0,1.0);
181 glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
185 gluLookAt(obs
[0],obs
[1],obs
[2],
186 obs
[0]+dir
[0],obs
[1]+dir
[1],obs
[2]+dir
[2],
189 glColor4f(1.0,1.0,1.0,1.0);
191 glEnable(GL_TEXTURE_2D
);
193 glBindTexture(GL_TEXTURE_2D
,groundid
);
195 glTexCoord2fv(qt
[0]);
197 glTexCoord2fv(qt
[1]);
199 glTexCoord2fv(qt
[2]);
201 glTexCoord2fv(qt
[3]);
207 glDisable(GL_TEXTURE_2D
);
208 glShadeModel(GL_SMOOTH
);
212 ps
->addTime(gettimerain());
214 glShadeModel(GL_FLAT
);
217 if((count
% FRAME
)==0) {
219 sprintf(frbuf
,"Frame rate: %f",FRAME
/fr
);
222 glDisable(GL_TEXTURE_2D
);
223 glDisable(GL_DEPTH_TEST
);
226 glMatrixMode(GL_PROJECTION
);
228 glOrtho(-0.5,639.5,-0.5,479.5,-1.0,1.0);
229 glMatrixMode(GL_MODELVIEW
);
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)");
241 reshape(WIDTH
,HEIGHT
);
250 static void special(int key
, int x
, int y
)
268 static void key(unsigned char key
, int x
, int y
)
283 rainParticle::setLength(rainParticle::getLength()+0.025f
);
286 rainParticle::setLength(rainParticle::getLength()-0.025f
);
297 XMesaSetFXmode(fullscreen
? XMESA_FX_FULLSCREEN
: XMESA_FX_WINDOW
);
298 fullscreen
=(!fullscreen
);
304 static void inittextures(void)
311 glGenTextures(1,&groundid
);
312 glBindTexture(GL_TEXTURE_2D
,groundid
);
314 if(!(img
=LoadRGBImage("../images/s128.rgb",&width
,&height
,&format
))){
315 fprintf(stderr
,"Error reading a texture.\n");
318 glPixelStorei(GL_UNPACK_ALIGNMENT
,4);
319 if((gluerr
=(GLenum
)gluBuild2DMipmaps(GL_TEXTURE_2D
, 3, width
, height
,GL_RGB
,
320 GL_UNSIGNED_BYTE
, (GLvoid
*)(img
)))) {
321 fprintf(stderr
,"GLULib%s\n",gluErrorString(gluerr
));
325 glTexParameterf(GL_TEXTURE_2D
,GL_TEXTURE_WRAP_S
,GL_REPEAT
);
326 glTexParameterf(GL_TEXTURE_2D
,GL_TEXTURE_WRAP_T
,GL_REPEAT
);
328 glTexParameterf(GL_TEXTURE_2D
,GL_TEXTURE_MIN_FILTER
,GL_LINEAR_MIPMAP_LINEAR
);
329 glTexParameterf(GL_TEXTURE_2D
,GL_TEXTURE_MAG_FILTER
,GL_LINEAR
);
331 glTexEnvf(GL_TEXTURE_ENV
,GL_TEXTURE_ENV_MODE
,GL_DECAL
);
334 static void initparticle(void)
336 ps
=new particleSystem
;
338 rainParticle::setRainingArea(-7.0f
,-0.2f
,-7.0f
,7.0f
,8.0f
,7.0f
);
340 for(int i
=0;i
<NUMPART
;i
++) {
341 rainParticle
*p
=new rainParticle
;
344 ps
->addParticle((particle
*)p
);
348 int main(int ac
,char **av
)
350 fprintf(stderr
,"Rain V1.0\nWritten by David Bucciarelli (humanware@plus.it)\n");
352 /* Default settings */
357 glutInitWindowPosition(0,0);
358 glutInitWindowSize(WIDTH
,HEIGHT
);
361 glutInitDisplayMode(GLUT_RGB
|GLUT_DEPTH
|GLUT_DOUBLE
);
363 if(!(win
=glutCreateWindow("Rain"))) {
364 fprintf(stderr
,"Error opening a window.\n");
368 reshape(WIDTH
,HEIGHT
);
372 glShadeModel(GL_FLAT
);
373 glEnable(GL_DEPTH_TEST
);
375 glBlendFunc(GL_SRC_ALPHA
,GL_ONE_MINUS_SRC_ALPHA
);
378 glFogi(GL_FOG_MODE
,GL_EXP
);
379 glFogfv(GL_FOG_COLOR
,fogcolor
);
380 glFogf(GL_FOG_DENSITY
,0.1);
382 glHint(GL_FOG_HINT
,GL_NICEST
);
387 glutKeyboardFunc(key
);
388 glutSpecialFunc(special
);
389 glutDisplayFunc(drawrain
);
390 glutIdleFunc(drawrain
);
391 glutReshapeFunc(reshape
);