fogcoord.c revision 32001f49
1/*
2 * Exercise GL_EXT_fog_coord
3 */
4
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <math.h>
9#include <GL/glew.h>
10#include "glut_wrap.h"
11
12static int Width = 600;
13static int Height = 200;
14static GLfloat Near = 5.0, Far = 25.0;
15
16
17static void Display( void )
18{
19   GLfloat t;
20
21   glClearColor(0.2, 0.2, 0.8, 0);
22   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
23
24   for (t = 0.0; t <= 1.0; t += 0.25) {
25      GLfloat f = Near + t * (Far - Near);
26      printf("glFogCoord(%4.1f)\n", f);
27      glFogCoordfEXT(f);
28
29      glPushMatrix();
30         glTranslatef(t * 10.0 - 5.0, 0, 0);
31         glBegin(GL_POLYGON);
32         glVertex2f(-1, -1);
33         glVertex2f( 1, -1);
34         glVertex2f( 1,  1);
35         glVertex2f(-1,  1);
36         glEnd();
37      glPopMatrix();
38   }
39   glutSwapBuffers();
40}
41
42
43static void Reshape( int width, int height )
44{
45   GLfloat ar = (float) width / (float) height;
46   Width = width;
47   Height = height;
48   glViewport( 0, 0, width, height );
49   glMatrixMode( GL_PROJECTION );
50   glLoadIdentity();
51   glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
52   glMatrixMode( GL_MODELVIEW );
53   glLoadIdentity();
54   glTranslatef( 0.0, 0.0, -15.0 );
55}
56
57
58static void Key( unsigned char key, int x, int y )
59{
60   (void) x;
61   (void) y;
62   switch (key) {
63      case 27:
64         exit(0);
65         break;
66   }
67   glutPostRedisplay();
68}
69
70
71static void Init( void )
72{
73   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
74   printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
75   /* setup lighting, etc */
76   if (!glutExtensionSupported("GL_EXT_fog_coord")) {
77      printf("Sorry, this program requires GL_EXT_fog_coord\n");
78      exit(1);
79   }
80   glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
81   glFogi(GL_FOG_MODE, GL_LINEAR);
82   glFogf(GL_FOG_START, Near);
83   glFogf(GL_FOG_END, Far);
84   glEnable(GL_FOG);
85   printf("Squares should be colored from white -> gray -> black.\n");
86}
87
88
89int main( int argc, char *argv[] )
90{
91   glutInit( &argc, argv );
92   glutInitWindowPosition( 0, 0 );
93   glutInitWindowSize( Width, Height );
94   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
95   glutCreateWindow(argv[0]);
96   glewInit();
97   glutReshapeFunc( Reshape );
98   glutKeyboardFunc( Key );
99   glutDisplayFunc( Display );
100   Init();
101   glutMainLoop();
102   return 0;
103}
104