4 * Based on glutskel.c by Brian Paul
5 * and NeHe's Volumetric fog tutorial!
10 #define GL_GLEXT_PROTOTYPES
18 static PFNGLFOGCOORDFEXTPROC glFogCoordf_ext
;
19 static PFNGLFOGCOORDPOINTEREXTPROC glFogCoordPointer_ext
;
21 static GLboolean have_fog_coord
;
26 static GLboolean fogCoord
;
27 static GLfloat fogDensity
= 0.75;
28 static GLfloat fogStart
= 1.0, fogEnd
= DEPTH
;
29 static GLfloat fogColor
[4] = {0.6f
, 0.3f
, 0.0f
, 1.0f
};
30 static const char *ModeStr
= NULL
;
31 static GLboolean Arrays
= GL_FALSE
;
32 static GLboolean Texture
= GL_TRUE
;
49 glFogCoordf_nop (GLfloat f
)
56 PrintString(const char *s
)
59 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
73 sprintf(s
, "Mode(m): %s Start(s/S): %g End(e/E): %g Density(d/D): %g",
74 ModeStr
, fogStart
, fogEnd
, fogDensity
);
75 glWindowPos2iARB(5, 20);
78 sprintf(s
, "Arrays(a): %s glFogCoord(c): %s EyeZ(z/z): %g",
79 (Arrays
? "Yes" : "No"),
80 (fogCoord
? "Yes" : "No"),
82 glWindowPos2iARB(5, 5);
88 SetFogMode(GLint fogMode
)
97 ModeStr
= "GL_LINEAR";
99 glFogi(GL_FOG_MODE
, GL_LINEAR
);
100 glFogf(GL_FOG_START
, fogStart
);
101 glFogf(GL_FOG_END
, fogEnd
);
106 glFogi(GL_FOG_MODE
, GL_EXP
);
107 glFogf(GL_FOG_DENSITY
, fogDensity
);
112 glFogi(GL_FOG_MODE
, GL_EXP2
);
113 glFogf(GL_FOG_DENSITY
, fogDensity
);
121 SetFogCoord(GLboolean fogCoord
)
123 glFogCoordf_ext
= glFogCoordf_nop
;
125 if (!have_fog_coord
) {
130 glFogCoordf_ext
= (PFNGLFOGCOORDFEXTPROC
)glutGetProcAddress("glFogCoordfEXT");
131 glFogi(GL_FOG_COORDINATE_SOURCE_EXT
, GL_FOG_COORDINATE_EXT
);
134 glFogi(GL_FOG_COORDINATE_SOURCE_EXT
, GL_FRAGMENT_DEPTH_EXT
);
140 /* could reuse vertices */
141 static GLuint vertex_index
[] = {
158 static GLfloat vertex_pointer
[][3] = {
160 {-1.0f
,-1.0f
,-DEPTH
}, { 1.0f
,-1.0f
,-DEPTH
}, { 1.0f
, 1.0f
,-DEPTH
}, {-1.0f
, 1.0f
,-DEPTH
},
163 {-1.0f
,-1.0f
,-DEPTH
}, { 1.0f
,-1.0f
,-DEPTH
}, { 1.0f
,-1.0f
, 0.0}, {-1.0f
,-1.0f
, 0.0},
166 {-1.0f
, 1.0f
,-DEPTH
}, { 1.0f
, 1.0f
,-DEPTH
}, { 1.0f
, 1.0f
, 0.0}, {-1.0f
, 1.0f
, 0.0},
169 { 1.0f
,-1.0f
, 0.0}, { 1.0f
, 1.0f
, 0.0}, { 1.0f
, 1.0f
,-DEPTH
}, { 1.0f
,-1.0f
,-DEPTH
},
172 {-1.0f
,-1.0f
, 0.0}, {-1.0f
, 1.0f
, 0.0}, {-1.0f
, 1.0f
,-DEPTH
}, {-1.0f
,-1.0f
,-DEPTH
}
175 static GLfloat texcoord_pointer
[][2] = {
177 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
},
180 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, DEPTH
}, {0.0f
, DEPTH
},
183 {1.0f
, 0.0f
}, {0.0f
, 0.0f
}, {0.0f
, DEPTH
}, {1.0f
, DEPTH
},
186 {0.0f
, 1.0f
}, {0.0f
, 0.0f
}, {DEPTH
, 0.0f
}, {DEPTH
, 1.0f
},
189 {0.0f
, 0.0f
}, {0.0f
, 1.0f
}, {DEPTH
, 1.0f
}, {DEPTH
, 0.0f
}
192 static GLfloat fogcoord_pointer
[] = {
194 DEPTH
, DEPTH
, DEPTH
, DEPTH
,
197 DEPTH
, DEPTH
, 0.0, 0.0,
200 DEPTH
, DEPTH
, 0.0, 0.0,
203 0.0, 0.0, DEPTH
, DEPTH
,
206 0.0, 0.0, DEPTH
, DEPTH
213 glClear (GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
216 glTranslatef(0.0f
, 0.0f
, -camz
);
223 glEnable(GL_TEXTURE_2D
);
226 glEnableClientState(GL_VERTEX_ARRAY
);
227 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
228 glDrawElements(GL_QUADS
, sizeof(vertex_index
) / sizeof(vertex_index
[0]),
229 GL_UNSIGNED_INT
, vertex_index
);
230 glDisableClientState(GL_VERTEX_ARRAY
);
231 glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
236 glFogCoordf_ext(DEPTH
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f(-1.0f
,-1.0f
,-DEPTH
);
237 glFogCoordf_ext(DEPTH
); glTexCoord2f(1.0f
, 0.0f
); glVertex3f( 1.0f
,-1.0f
,-DEPTH
);
238 glFogCoordf_ext(DEPTH
); glTexCoord2f(1.0f
, 1.0f
); glVertex3f( 1.0f
, 1.0f
,-DEPTH
);
239 glFogCoordf_ext(DEPTH
); glTexCoord2f(0.0f
, 1.0f
); glVertex3f(-1.0f
, 1.0f
,-DEPTH
);
244 glFogCoordf_ext(DEPTH
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f(-1.0f
,-1.0f
,-DEPTH
);
245 glFogCoordf_ext(DEPTH
); glTexCoord2f(1.0f
, 0.0f
); glVertex3f( 1.0f
,-1.0f
,-DEPTH
);
246 glFogCoordf_ext(0.0f
); glTexCoord2f(1.0f
, DEPTH
); glVertex3f( 1.0f
,-1.0f
,0.0);
247 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, DEPTH
); glVertex3f(-1.0f
,-1.0f
,0.0);
252 glFogCoordf_ext(DEPTH
); glTexCoord2f(1.0f
, 0.0f
); glVertex3f(-1.0f
, 1.0f
,-DEPTH
);
253 glFogCoordf_ext(DEPTH
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f( 1.0f
, 1.0f
,-DEPTH
);
254 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, DEPTH
); glVertex3f( 1.0f
, 1.0f
,0.0);
255 glFogCoordf_ext(0.0f
); glTexCoord2f(1.0f
, DEPTH
); glVertex3f(-1.0f
, 1.0f
,0.0);
260 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, 1.0f
); glVertex3f( 1.0f
,-1.0f
,0.0);
261 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f( 1.0f
, 1.0f
,0.0);
262 glFogCoordf_ext(DEPTH
); glTexCoord2f(DEPTH
, 0.0f
); glVertex3f( 1.0f
, 1.0f
,-DEPTH
);
263 glFogCoordf_ext(DEPTH
); glTexCoord2f(DEPTH
, 1.0f
); glVertex3f( 1.0f
,-1.0f
,-DEPTH
);
268 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f(-1.0f
,-1.0f
,0.0);
269 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, 1.0f
); glVertex3f(-1.0f
, 1.0f
,0.0);
270 glFogCoordf_ext(DEPTH
); glTexCoord2f(DEPTH
, 1.0f
); glVertex3f(-1.0f
, 1.0f
,-DEPTH
);
271 glFogCoordf_ext(DEPTH
); glTexCoord2f(DEPTH
, 0.0f
); glVertex3f(-1.0f
,-1.0f
,-DEPTH
);
275 glDisable(GL_TEXTURE_2D
);
284 Reshape( int width
, int height
)
286 glViewport(0, 0, width
, height
);
287 glMatrixMode(GL_PROJECTION
);
289 glFrustum(-1, 1, -1, 1, 1.0, 100);
290 glMatrixMode(GL_MODELVIEW
);
296 Key( unsigned char key
, int x
, int y
)
306 fogMode
= SetFogMode(fogMode
+ 1);
313 if (fogDensity
> 0.0) {
319 if (fogStart
> 0.0) {
325 if (fogStart
< 100.0) {
337 if (fogEnd
< 100.0) {
343 fogCoord
= SetFogCoord(fogCoord
^ GL_TRUE
);
368 static const GLubyte teximage
[2][2][4] = {
369 { { 255, 255, 255, 255}, { 128, 128, 128, 255} },
370 { { 128, 128, 128, 255}, { 255, 255, 255, 255} }
373 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
375 have_fog_coord
= glutExtensionSupported("GL_EXT_fog_coord");
376 if (!have_fog_coord
) {
377 printf("GL_EXT_fog_coord not supported!\n");
380 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 2, 2, 0,
381 GL_RGBA
, GL_UNSIGNED_BYTE
, teximage
);
382 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
383 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
385 glClearColor(0.1f
, 0.1f
, 0.1f
, 0.0f
);
387 glDepthFunc(GL_LEQUAL
);
388 glEnable(GL_DEPTH_TEST
);
389 glShadeModel(GL_SMOOTH
);
390 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_NICEST
);
392 glFogfv(GL_FOG_COLOR
, fogColor
);
393 glHint(GL_FOG_HINT
, GL_NICEST
);
394 fogCoord
= SetFogCoord(GL_TRUE
); /* try to enable fog_coord */
395 fogMode
= SetFogMode(1);
397 glEnableClientState(GL_VERTEX_ARRAY
);
398 glVertexPointer(3, GL_FLOAT
, 0, vertex_pointer
);
400 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
401 glTexCoordPointer(2, GL_FLOAT
, 0, texcoord_pointer
);
403 if (have_fog_coord
) {
404 glFogCoordPointer_ext
= (PFNGLFOGCOORDPOINTEREXTPROC
)glutGetProcAddress("glFogCoordPointerEXT");
405 glEnableClientState(GL_FOG_COORDINATE_ARRAY_EXT
);
406 glFogCoordPointer_ext(GL_FLOAT
, 0, fogcoord_pointer
);
414 main( int argc
, char *argv
[] )
416 glutInit( &argc
, argv
);
417 glutInitWindowSize( 600, 600 );
418 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
419 glutCreateWindow(argv
[0]);
420 glutReshapeFunc( Reshape
);
421 glutKeyboardFunc( Key
);
422 glutDisplayFunc( Display
);