progs/tests: also test stencil INCR_WRAP mode if supported
[mesa.git] / progs / tests / fog.c
1 /*
2 * Copyright 2005 Eric Anholt
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Authors:
25 * Eric Anholt <anholt@FreeBSD.org>
26 * Brian Paul (fogcoord.c used as a skeleton)
27 */
28
29 /*
30 * Test to exercise fog modes and for comparison with GL_EXT_fog_coord.
31 */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <math.h>
36 #include <GL/glew.h>
37 #include <GL/glut.h>
38
39 static int Width = 600;
40 static int Height = 600;
41 static GLfloat Near = 0.0, Far = 1.0;
42 GLboolean has_fogcoord;
43
44 static void drawString( const char *string )
45 {
46 glRasterPos2f(0, .5);
47 while ( *string ) {
48 glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_10, *string );
49 string++;
50 }
51 }
52
53 static void Display( void )
54 {
55 GLint i, depthi;
56 GLfloat fogcolor[4] = {1, 1, 1, 1};
57
58 glEnable(GL_FOG);
59 glFogfv(GL_FOG_COLOR, fogcolor);
60
61 glClearColor(0.2, 0.2, 0.8, 0);
62 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
63
64 glPushMatrix();
65 for (i = 0; i < 6; i++) {
66 if (i >= 3 && !has_fogcoord)
67 break;
68
69 glPushMatrix();
70 for (depthi = 0; depthi < 5; depthi++) {
71 GLfloat depth = Near + (Far - Near) * depthi / 4;
72
73 switch (i % 3) {
74 case 0:
75 glFogi(GL_FOG_MODE, GL_LINEAR);
76 glFogf(GL_FOG_START, Near);
77 glFogf(GL_FOG_END, Far);
78 break;
79 case 1:
80 glFogi(GL_FOG_MODE, GL_EXP);
81 glFogf(GL_FOG_DENSITY, 2);
82 break;
83 case 2:
84 glFogi(GL_FOG_MODE, GL_EXP2);
85 glFogf(GL_FOG_DENSITY, 2);
86 break;
87 }
88
89 glColor4f(0, 0, 0, 0);
90 if (i < 3) {
91 if (has_fogcoord)
92 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
93
94 glBegin(GL_POLYGON);
95 glVertex3f(0, 0, depth);
96 glVertex3f(1, 0, depth);
97 glVertex3f(1, 1, depth);
98 glVertex3f(0, 1, depth);
99 glEnd();
100 } else {
101 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
102 glFogCoordfEXT(depth);
103
104 glBegin(GL_POLYGON);
105 glVertex3f(0, 0, (Near + Far) / 2);
106 glVertex3f(1, 0, (Near + Far) / 2);
107 glVertex3f(1, 1, (Near + Far) / 2);
108 glVertex3f(0, 1, (Near + Far) / 2);
109 glEnd();
110 }
111 glTranslatef(1.5, 0, 0);
112 }
113
114 glTranslatef(.1, 0, 0);
115 switch (i) {
116 case 0:
117 drawString("GL_LINEAR");
118 break;
119 case 1:
120 drawString("GL_EXP");
121 break;
122 case 2:
123 drawString("GL_EXP2");
124 break;
125 case 3:
126 drawString("GL_FOGCOORD GL_LINEAR");
127 break;
128 case 4:
129 drawString("GL_FOGCOORD GL_EXP");
130 break;
131 case 5:
132 drawString("GL_FOGCOORD GL_EXP2");
133 break;
134 }
135
136 glPopMatrix();
137 glTranslatef(0, 1.5, 0);
138 }
139 glPopMatrix();
140
141 glutSwapBuffers();
142 }
143
144
145 static void Reshape( int width, int height )
146 {
147 Width = width;
148 Height = height;
149 glViewport( 0, 0, width, height );
150
151 glMatrixMode( GL_PROJECTION );
152 glLoadIdentity();
153 glOrtho( 0, 11, 9, 0, -Near, -Far );
154
155 glMatrixMode( GL_MODELVIEW );
156 glLoadIdentity();
157 glTranslatef(.25, .25, 0);
158 }
159
160
161 static void Key( unsigned char key, int x, int y )
162 {
163 (void) x;
164 (void) y;
165 switch (key) {
166 case 27:
167 exit(0);
168 break;
169 }
170 glutPostRedisplay();
171 }
172
173
174 static void Init( void )
175 {
176 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
177 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
178 /* setup lighting, etc */
179 has_fogcoord = glutExtensionSupported("GL_EXT_fog_coord");
180 if (!has_fogcoord) {
181 printf("Some output of this program requires GL_EXT_fog_coord\n");
182 }
183 }
184
185
186 int main( int argc, char *argv[] )
187 {
188 glutInit( &argc, argv );
189 glutInitWindowPosition( 0, 0 );
190 glutInitWindowSize( Width, Height );
191 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
192 glutCreateWindow(argv[0]);
193 glewInit();
194 glutReshapeFunc( Reshape );
195 glutKeyboardFunc( Key );
196 glutDisplayFunc( Display );
197 Init();
198 glutMainLoop();
199 return 0;
200 }