4 * Based on glutskel.c by Brian Paul
5 * and NeHe's Volumetric fog tutorial!
21 static GLboolean fogCoord
;
22 static GLfloat fogDensity
= 0.75;
23 static GLfloat fogStart
= 1.0, fogEnd
= DEPTH
;
24 static GLfloat fogColor
[4] = {0.6f
, 0.3f
, 0.0f
, 1.0f
};
25 static const char *ModeStr
= NULL
;
26 static GLboolean Arrays
= GL_FALSE
;
27 static GLboolean Texture
= GL_TRUE
;
44 glFogCoordf_ext (GLfloat f
)
52 PrintString(const char *s
)
55 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
69 sprintf(s
, "Mode(m): %s Start(s/S): %g End(e/E): %g Density(d/D): %g",
70 ModeStr
, fogStart
, fogEnd
, fogDensity
);
71 glWindowPos2iARB(5, 20);
74 sprintf(s
, "Arrays(a): %s glFogCoord(c): %s EyeZ(z/z): %g",
75 (Arrays
? "Yes" : "No"),
76 (fogCoord
? "Yes" : "No"),
78 glWindowPos2iARB(5, 5);
84 SetFogMode(GLint fogMode
)
93 ModeStr
= "GL_LINEAR";
95 glFogi(GL_FOG_MODE
, GL_LINEAR
);
96 glFogf(GL_FOG_START
, fogStart
);
97 glFogf(GL_FOG_END
, fogEnd
);
102 glFogi(GL_FOG_MODE
, GL_EXP
);
103 glFogf(GL_FOG_DENSITY
, fogDensity
);
108 glFogi(GL_FOG_MODE
, GL_EXP2
);
109 glFogf(GL_FOG_DENSITY
, fogDensity
);
117 SetFogCoord(GLboolean fogCoord
)
119 if (!GLEW_EXT_fog_coord
) {
124 glFogi(GL_FOG_COORDINATE_SOURCE_EXT
, GL_FOG_COORDINATE_EXT
);
127 glFogi(GL_FOG_COORDINATE_SOURCE_EXT
, GL_FRAGMENT_DEPTH_EXT
);
133 /* could reuse vertices */
134 static GLuint vertex_index
[] = {
151 static GLfloat vertex_pointer
[][3] = {
153 {-1.0f
,-1.0f
,-DEPTH
}, { 1.0f
,-1.0f
,-DEPTH
}, { 1.0f
, 1.0f
,-DEPTH
}, {-1.0f
, 1.0f
,-DEPTH
},
156 {-1.0f
,-1.0f
,-DEPTH
}, { 1.0f
,-1.0f
,-DEPTH
}, { 1.0f
,-1.0f
, 0.0}, {-1.0f
,-1.0f
, 0.0},
159 {-1.0f
, 1.0f
,-DEPTH
}, { 1.0f
, 1.0f
,-DEPTH
}, { 1.0f
, 1.0f
, 0.0}, {-1.0f
, 1.0f
, 0.0},
162 { 1.0f
,-1.0f
, 0.0}, { 1.0f
, 1.0f
, 0.0}, { 1.0f
, 1.0f
,-DEPTH
}, { 1.0f
,-1.0f
,-DEPTH
},
165 {-1.0f
,-1.0f
, 0.0}, {-1.0f
, 1.0f
, 0.0}, {-1.0f
, 1.0f
,-DEPTH
}, {-1.0f
,-1.0f
,-DEPTH
}
168 static GLfloat texcoord_pointer
[][2] = {
170 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
},
173 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, DEPTH
}, {0.0f
, DEPTH
},
176 {1.0f
, 0.0f
}, {0.0f
, 0.0f
}, {0.0f
, DEPTH
}, {1.0f
, DEPTH
},
179 {0.0f
, 1.0f
}, {0.0f
, 0.0f
}, {DEPTH
, 0.0f
}, {DEPTH
, 1.0f
},
182 {0.0f
, 0.0f
}, {0.0f
, 1.0f
}, {DEPTH
, 1.0f
}, {DEPTH
, 0.0f
}
185 static GLfloat fogcoord_pointer
[] = {
187 DEPTH
, DEPTH
, DEPTH
, DEPTH
,
190 DEPTH
, DEPTH
, 0.0, 0.0,
193 DEPTH
, DEPTH
, 0.0, 0.0,
196 0.0, 0.0, DEPTH
, DEPTH
,
199 0.0, 0.0, DEPTH
, DEPTH
206 glClear (GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
209 glTranslatef(0.0f
, 0.0f
, -camz
);
216 glEnable(GL_TEXTURE_2D
);
219 glEnableClientState(GL_VERTEX_ARRAY
);
220 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
221 glDrawElements(GL_QUADS
, sizeof(vertex_index
) / sizeof(vertex_index
[0]),
222 GL_UNSIGNED_INT
, vertex_index
);
223 glDisableClientState(GL_VERTEX_ARRAY
);
224 glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
229 glFogCoordf_ext(DEPTH
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f(-1.0f
,-1.0f
,-DEPTH
);
230 glFogCoordf_ext(DEPTH
); glTexCoord2f(1.0f
, 0.0f
); glVertex3f( 1.0f
,-1.0f
,-DEPTH
);
231 glFogCoordf_ext(DEPTH
); glTexCoord2f(1.0f
, 1.0f
); glVertex3f( 1.0f
, 1.0f
,-DEPTH
);
232 glFogCoordf_ext(DEPTH
); glTexCoord2f(0.0f
, 1.0f
); glVertex3f(-1.0f
, 1.0f
,-DEPTH
);
237 glFogCoordf_ext(DEPTH
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f(-1.0f
,-1.0f
,-DEPTH
);
238 glFogCoordf_ext(DEPTH
); glTexCoord2f(1.0f
, 0.0f
); glVertex3f( 1.0f
,-1.0f
,-DEPTH
);
239 glFogCoordf_ext(0.0f
); glTexCoord2f(1.0f
, DEPTH
); glVertex3f( 1.0f
,-1.0f
,0.0);
240 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, DEPTH
); glVertex3f(-1.0f
,-1.0f
,0.0);
245 glFogCoordf_ext(DEPTH
); glTexCoord2f(1.0f
, 0.0f
); glVertex3f(-1.0f
, 1.0f
,-DEPTH
);
246 glFogCoordf_ext(DEPTH
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f( 1.0f
, 1.0f
,-DEPTH
);
247 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, DEPTH
); glVertex3f( 1.0f
, 1.0f
,0.0);
248 glFogCoordf_ext(0.0f
); glTexCoord2f(1.0f
, DEPTH
); glVertex3f(-1.0f
, 1.0f
,0.0);
253 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, 1.0f
); glVertex3f( 1.0f
,-1.0f
,0.0);
254 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f( 1.0f
, 1.0f
,0.0);
255 glFogCoordf_ext(DEPTH
); glTexCoord2f(DEPTH
, 0.0f
); glVertex3f( 1.0f
, 1.0f
,-DEPTH
);
256 glFogCoordf_ext(DEPTH
); glTexCoord2f(DEPTH
, 1.0f
); glVertex3f( 1.0f
,-1.0f
,-DEPTH
);
261 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f(-1.0f
,-1.0f
,0.0);
262 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, 1.0f
); glVertex3f(-1.0f
, 1.0f
,0.0);
263 glFogCoordf_ext(DEPTH
); glTexCoord2f(DEPTH
, 1.0f
); glVertex3f(-1.0f
, 1.0f
,-DEPTH
);
264 glFogCoordf_ext(DEPTH
); glTexCoord2f(DEPTH
, 0.0f
); glVertex3f(-1.0f
,-1.0f
,-DEPTH
);
268 glDisable(GL_TEXTURE_2D
);
277 Reshape( int width
, int height
)
279 glViewport(0, 0, width
, height
);
280 glMatrixMode(GL_PROJECTION
);
282 glFrustum(-1, 1, -1, 1, 1.0, 100);
283 glMatrixMode(GL_MODELVIEW
);
289 Key( unsigned char key
, int x
, int y
)
299 fogMode
= SetFogMode(fogMode
+ 1);
306 if (fogDensity
> 0.0) {
312 if (fogStart
> 0.0) {
318 if (fogStart
< 100.0) {
330 if (fogEnd
< 100.0) {
336 fogCoord
= SetFogCoord(fogCoord
^ GL_TRUE
);
361 static const GLubyte teximage
[2][2][4] = {
362 { { 255, 255, 255, 255}, { 128, 128, 128, 255} },
363 { { 128, 128, 128, 255}, { 255, 255, 255, 255} }
366 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
368 if (!GLEW_EXT_fog_coord
) {
369 printf("GL_EXT_fog_coord not supported!\n");
372 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 2, 2, 0,
373 GL_RGBA
, GL_UNSIGNED_BYTE
, teximage
);
374 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
375 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
377 glClearColor(0.1f
, 0.1f
, 0.1f
, 0.0f
);
379 glDepthFunc(GL_LEQUAL
);
380 glEnable(GL_DEPTH_TEST
);
381 glShadeModel(GL_SMOOTH
);
382 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_NICEST
);
384 glFogfv(GL_FOG_COLOR
, fogColor
);
385 glHint(GL_FOG_HINT
, GL_NICEST
);
386 fogCoord
= SetFogCoord(GL_TRUE
); /* try to enable fog_coord */
387 fogMode
= SetFogMode(1);
389 glEnableClientState(GL_VERTEX_ARRAY
);
390 glVertexPointer(3, GL_FLOAT
, 0, vertex_pointer
);
392 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
393 glTexCoordPointer(2, GL_FLOAT
, 0, texcoord_pointer
);
395 if (GLEW_EXT_fog_coord
) {
396 glEnableClientState(GL_FOG_COORDINATE_ARRAY_EXT
);
397 glFogCoordPointerEXT(GL_FLOAT
, 0, fogcoord_pointer
);
405 main( int argc
, char *argv
[] )
407 glutInitWindowSize( 600, 600 );
408 glutInit( &argc
, argv
);
409 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
410 glutCreateWindow(argv
[0]);
412 glutReshapeFunc( Reshape
);
413 glutKeyboardFunc( Key
);
414 glutDisplayFunc( Display
);