Use correct PV when clipping.
[mesa.git] / progs / demos / anisotropic.c
1 /* $Id: anisotropic.c,v 1.1 2001/03/22 15:24:15 gareth Exp $ */
2
3 /*
4 * GL_ARB_texture_filter_anisotropic demo
5 *
6 * Gareth Hughes
7 * March 2001
8 *
9 * Copyright (C) 2000 Brian Paul All Rights Reserved.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
25 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 */
28
29 /* This is a fairly early version. All it does is draw a couple of
30 * textured quads with different forms of texture filtering. Eventually,
31 * you'll be able to adjust the maximum anisotropy and so on.
32 */
33
34 #include <math.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <GL/glut.h>
38
39 #define TEXTURE_SIZE 256
40 static GLubyte image[TEXTURE_SIZE][TEXTURE_SIZE][3];
41
42 static GLfloat repeat = 1.0;
43
44 static GLfloat texcoords[] = {
45 0.0, 0.0,
46 0.0, 1.0,
47 1.0, 0.0,
48 1.0, 1.0
49 };
50
51 static GLfloat vertices[] = {
52 -400.0, -400.0, 0.0,
53 -400.0, 400.0, -7000.0,
54 400.0, -400.0, 0.0,
55 400.0, 400.0, -7000.0
56 };
57
58 static GLfloat maxAnisotropy;
59
60
61 static void init( void )
62 {
63 int i, j;
64
65 if ( !glutExtensionSupported( "GL_EXT_texture_filter_anisotropic" ) ) {
66 fprintf( stderr, "Sorry, this demo requires GL_EXT_texture_filter_anisotropic.\n" );
67 exit( 0 );
68 }
69
70 glClearColor( 0.0, 0.0, 0.0, 0.0 );
71 glShadeModel( GL_SMOOTH );
72
73 /* Init the vertex arrays.
74 */
75 glEnableClientState( GL_VERTEX_ARRAY );
76 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
77
78 glVertexPointer( 3, GL_FLOAT, 0, vertices );
79 glTexCoordPointer( 2, GL_FLOAT, 0, texcoords );
80
81 /* Init the texture environment.
82 */
83 glEnable( GL_TEXTURE_2D );
84 glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
85
86 glMatrixMode( GL_TEXTURE );
87 glScalef( repeat, repeat, 0 );
88
89 glMatrixMode( GL_MODELVIEW );
90
91 glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy );
92 printf( "Maximum supported anisotropy: %.2f\n", maxAnisotropy );
93
94 /* Make the texture image.
95 */
96 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
97
98 for ( i = 0 ; i < TEXTURE_SIZE ; i++ ) {
99 for ( j = 0 ; j < TEXTURE_SIZE ; j++ ) {
100 if ( (i/4 + j/4) & 1 ) {
101 image[i][j][0] = 0;
102 image[i][j][1] = 0;
103 image[i][j][2] = 0;
104 } else {
105 image[i][j][0] = 255;
106 image[i][j][1] = 255;
107 image[i][j][2] = 255;
108 }
109 }
110 }
111
112 gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGB, TEXTURE_SIZE, TEXTURE_SIZE,
113 GL_RGB, GL_UNSIGNED_BYTE, image );
114 }
115
116 static void display( void )
117 {
118 GLint vp[4], w, h;
119
120 glClear( GL_COLOR_BUFFER_BIT );
121
122 glGetIntegerv( GL_VIEWPORT, vp );
123 w = vp[2] / 2;
124 h = vp[3] / 2;
125
126 /* Upper left corner:
127 */
128 glViewport( 1, h + 1, w - 2, h - 2 );
129
130 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
131 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy );
132
133 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
134
135
136 /* Upper right corner:
137 */
138 glViewport( w + 1, h + 1, w - 2, h - 2 );
139
140 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
141 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy );
142
143 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
144
145
146 /* Lower left corner:
147 */
148 glViewport( 1, 1, w - 2, h - 2 );
149
150 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR );
151 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy );
152
153 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
154
155
156 /* Lower right corner:
157 */
158 glViewport( w + 1, 1, w - 2, h - 2 );
159
160 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
161 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy );
162
163 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
164
165
166 glViewport( vp[0], vp[1], vp[2], vp[3] );
167
168 glutSwapBuffers();
169 }
170
171 static void reshape( int w, int h )
172 {
173 glViewport( 0, 0, (GLsizei) w, (GLsizei) h );
174
175 glMatrixMode( GL_PROJECTION );
176 glLoadIdentity();
177 glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 10000.0 );
178
179 glMatrixMode( GL_MODELVIEW );
180 glLoadIdentity();
181 glTranslatef( 0.0, 0.0, -10.0 );
182 }
183
184 static void key( unsigned char key, int x, int y )
185 {
186 (void) x; (void) y;
187
188 switch ( key ) {
189 case 27:
190 exit( 0 );
191 }
192
193 glutPostRedisplay();
194 }
195
196 static void usage( void )
197 {
198 /* Nothing yet... */
199 }
200
201 int main( int argc, char **argv )
202 {
203 glutInit( &argc, argv );
204 glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
205 glutInitWindowSize( 600, 300 );
206 glutInitWindowPosition( 0, 0 );
207 glutCreateWindow( "Anisotropic Texture Filter Demo" );
208
209 init();
210
211 glutDisplayFunc( display );
212 glutReshapeFunc( reshape );
213 glutKeyboardFunc( key );
214
215 usage();
216
217 glutMainLoop();
218
219 return 0;
220 }