4 * Based on glutskel.c by Brian Paul
5 * and NeHe's Volumetric fog tutorial!
18 static PFNGLFOGCOORDPOINTEREXTPROC glFogCoordPointer_ext
;
23 static GLboolean fogCoord
;
24 static GLfloat fogDensity
= 0.75;
25 static GLfloat fogStart
= 1.0, fogEnd
= DEPTH
;
26 static GLfloat fogColor
[4] = {0.6f
, 0.3f
, 0.0f
, 1.0f
};
27 static const char *ModeStr
= NULL
;
28 static GLboolean Arrays
= GL_FALSE
;
29 static GLboolean Texture
= GL_TRUE
;
46 glFogCoordf_ext (GLfloat f
)
54 PrintString(const char *s
)
57 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
71 sprintf(s
, "Mode(m): %s Start(s/S): %g End(e/E): %g Density(d/D): %g",
72 ModeStr
, fogStart
, fogEnd
, fogDensity
);
73 glWindowPos2iARB(5, 20);
76 sprintf(s
, "Arrays(a): %s glFogCoord(c): %s EyeZ(z/z): %g",
77 (Arrays
? "Yes" : "No"),
78 (fogCoord
? "Yes" : "No"),
80 glWindowPos2iARB(5, 5);
86 SetFogMode(GLint fogMode
)
95 ModeStr
= "GL_LINEAR";
97 glFogi(GL_FOG_MODE
, GL_LINEAR
);
98 glFogf(GL_FOG_START
, fogStart
);
99 glFogf(GL_FOG_END
, fogEnd
);
104 glFogi(GL_FOG_MODE
, GL_EXP
);
105 glFogf(GL_FOG_DENSITY
, fogDensity
);
110 glFogi(GL_FOG_MODE
, GL_EXP2
);
111 glFogf(GL_FOG_DENSITY
, fogDensity
);
119 SetFogCoord(GLboolean fogCoord
)
121 if (!GLEW_EXT_fog_coord
) {
126 glFogi(GL_FOG_COORDINATE_SOURCE_EXT
, GL_FOG_COORDINATE_EXT
);
129 glFogi(GL_FOG_COORDINATE_SOURCE_EXT
, GL_FRAGMENT_DEPTH_EXT
);
135 /* could reuse vertices */
136 static GLuint vertex_index
[] = {
153 static GLfloat vertex_pointer
[][3] = {
155 {-1.0f
,-1.0f
,-DEPTH
}, { 1.0f
,-1.0f
,-DEPTH
}, { 1.0f
, 1.0f
,-DEPTH
}, {-1.0f
, 1.0f
,-DEPTH
},
158 {-1.0f
,-1.0f
,-DEPTH
}, { 1.0f
,-1.0f
,-DEPTH
}, { 1.0f
,-1.0f
, 0.0}, {-1.0f
,-1.0f
, 0.0},
161 {-1.0f
, 1.0f
,-DEPTH
}, { 1.0f
, 1.0f
,-DEPTH
}, { 1.0f
, 1.0f
, 0.0}, {-1.0f
, 1.0f
, 0.0},
164 { 1.0f
,-1.0f
, 0.0}, { 1.0f
, 1.0f
, 0.0}, { 1.0f
, 1.0f
,-DEPTH
}, { 1.0f
,-1.0f
,-DEPTH
},
167 {-1.0f
,-1.0f
, 0.0}, {-1.0f
, 1.0f
, 0.0}, {-1.0f
, 1.0f
,-DEPTH
}, {-1.0f
,-1.0f
,-DEPTH
}
170 static GLfloat texcoord_pointer
[][2] = {
172 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
},
175 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, DEPTH
}, {0.0f
, DEPTH
},
178 {1.0f
, 0.0f
}, {0.0f
, 0.0f
}, {0.0f
, DEPTH
}, {1.0f
, DEPTH
},
181 {0.0f
, 1.0f
}, {0.0f
, 0.0f
}, {DEPTH
, 0.0f
}, {DEPTH
, 1.0f
},
184 {0.0f
, 0.0f
}, {0.0f
, 1.0f
}, {DEPTH
, 1.0f
}, {DEPTH
, 0.0f
}
187 static GLfloat fogcoord_pointer
[] = {
189 DEPTH
, DEPTH
, DEPTH
, DEPTH
,
192 DEPTH
, DEPTH
, 0.0, 0.0,
195 DEPTH
, DEPTH
, 0.0, 0.0,
198 0.0, 0.0, DEPTH
, DEPTH
,
201 0.0, 0.0, DEPTH
, DEPTH
208 glClear (GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
211 glTranslatef(0.0f
, 0.0f
, -camz
);
218 glEnable(GL_TEXTURE_2D
);
221 glEnableClientState(GL_VERTEX_ARRAY
);
222 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
223 glDrawElements(GL_QUADS
, sizeof(vertex_index
) / sizeof(vertex_index
[0]),
224 GL_UNSIGNED_INT
, vertex_index
);
225 glDisableClientState(GL_VERTEX_ARRAY
);
226 glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
231 glFogCoordf_ext(DEPTH
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f(-1.0f
,-1.0f
,-DEPTH
);
232 glFogCoordf_ext(DEPTH
); glTexCoord2f(1.0f
, 0.0f
); glVertex3f( 1.0f
,-1.0f
,-DEPTH
);
233 glFogCoordf_ext(DEPTH
); glTexCoord2f(1.0f
, 1.0f
); glVertex3f( 1.0f
, 1.0f
,-DEPTH
);
234 glFogCoordf_ext(DEPTH
); glTexCoord2f(0.0f
, 1.0f
); glVertex3f(-1.0f
, 1.0f
,-DEPTH
);
239 glFogCoordf_ext(DEPTH
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f(-1.0f
,-1.0f
,-DEPTH
);
240 glFogCoordf_ext(DEPTH
); glTexCoord2f(1.0f
, 0.0f
); glVertex3f( 1.0f
,-1.0f
,-DEPTH
);
241 glFogCoordf_ext(0.0f
); glTexCoord2f(1.0f
, DEPTH
); glVertex3f( 1.0f
,-1.0f
,0.0);
242 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, DEPTH
); glVertex3f(-1.0f
,-1.0f
,0.0);
247 glFogCoordf_ext(DEPTH
); glTexCoord2f(1.0f
, 0.0f
); glVertex3f(-1.0f
, 1.0f
,-DEPTH
);
248 glFogCoordf_ext(DEPTH
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f( 1.0f
, 1.0f
,-DEPTH
);
249 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, DEPTH
); glVertex3f( 1.0f
, 1.0f
,0.0);
250 glFogCoordf_ext(0.0f
); glTexCoord2f(1.0f
, DEPTH
); glVertex3f(-1.0f
, 1.0f
,0.0);
255 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, 1.0f
); glVertex3f( 1.0f
,-1.0f
,0.0);
256 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f( 1.0f
, 1.0f
,0.0);
257 glFogCoordf_ext(DEPTH
); glTexCoord2f(DEPTH
, 0.0f
); glVertex3f( 1.0f
, 1.0f
,-DEPTH
);
258 glFogCoordf_ext(DEPTH
); glTexCoord2f(DEPTH
, 1.0f
); glVertex3f( 1.0f
,-1.0f
,-DEPTH
);
263 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, 0.0f
); glVertex3f(-1.0f
,-1.0f
,0.0);
264 glFogCoordf_ext(0.0f
); glTexCoord2f(0.0f
, 1.0f
); glVertex3f(-1.0f
, 1.0f
,0.0);
265 glFogCoordf_ext(DEPTH
); glTexCoord2f(DEPTH
, 1.0f
); glVertex3f(-1.0f
, 1.0f
,-DEPTH
);
266 glFogCoordf_ext(DEPTH
); glTexCoord2f(DEPTH
, 0.0f
); glVertex3f(-1.0f
,-1.0f
,-DEPTH
);
270 glDisable(GL_TEXTURE_2D
);
279 Reshape( int width
, int height
)
281 glViewport(0, 0, width
, height
);
282 glMatrixMode(GL_PROJECTION
);
284 glFrustum(-1, 1, -1, 1, 1.0, 100);
285 glMatrixMode(GL_MODELVIEW
);
291 Key( unsigned char key
, int x
, int y
)
301 fogMode
= SetFogMode(fogMode
+ 1);
308 if (fogDensity
> 0.0) {
314 if (fogStart
> 0.0) {
320 if (fogStart
< 100.0) {
332 if (fogEnd
< 100.0) {
338 fogCoord
= SetFogCoord(fogCoord
^ GL_TRUE
);
363 static const GLubyte teximage
[2][2][4] = {
364 { { 255, 255, 255, 255}, { 128, 128, 128, 255} },
365 { { 128, 128, 128, 255}, { 255, 255, 255, 255} }
368 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
370 if (!GLEW_EXT_fog_coord
) {
371 printf("GL_EXT_fog_coord not supported!\n");
374 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, 2, 2, 0,
375 GL_RGBA
, GL_UNSIGNED_BYTE
, teximage
);
376 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
377 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
379 glClearColor(0.1f
, 0.1f
, 0.1f
, 0.0f
);
381 glDepthFunc(GL_LEQUAL
);
382 glEnable(GL_DEPTH_TEST
);
383 glShadeModel(GL_SMOOTH
);
384 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_NICEST
);
386 glFogfv(GL_FOG_COLOR
, fogColor
);
387 glHint(GL_FOG_HINT
, GL_NICEST
);
388 fogCoord
= SetFogCoord(GL_TRUE
); /* try to enable fog_coord */
389 fogMode
= SetFogMode(1);
391 glEnableClientState(GL_VERTEX_ARRAY
);
392 glVertexPointer(3, GL_FLOAT
, 0, vertex_pointer
);
394 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
395 glTexCoordPointer(2, GL_FLOAT
, 0, texcoord_pointer
);
397 if (GLEW_EXT_fog_coord
) {
398 glEnableClientState(GL_FOG_COORDINATE_ARRAY_EXT
);
399 glFogCoordPointerEXT(GL_FLOAT
, 0, fogcoord_pointer
);
407 main( int argc
, char *argv
[] )
409 glutInit( &argc
, argv
);
410 glutInitWindowSize( 600, 600 );
411 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
412 glutCreateWindow(argv
[0]);
414 glutReshapeFunc( Reshape
);
415 glutKeyboardFunc( Key
);
416 glutDisplayFunc( Display
);