Merge remote branch 'main/master' into radeon-rewrite
[mesa.git] / progs / tests / minmag.c
1 /*
2 * Test minification vs. magnification filtering.
3 * Draw two quads with different filtering modes:
4 *
5 * +--------------------------+ +--------------------------+
6 * | MagFilter = GL_LINEAR | | MagFilter = GL_LINEAR |
7 * | MinFilter = GL_LINEAR | | MinFilter = GL_NEAREST |
8 * +--------------------------+ +--------------------------+
9 *
10 * They should look different when the quad is smaller than the level 0
11 * texture size (when minifying).
12 */
13
14 #include <assert.h>
15 #include <math.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <GL/glew.h>
21 #include <GL/glut.h>
22
23
24 static GLint Width = 1000, Height = 500;
25
26
27 static GLint TexWidth = 256, TexHeight = 256;
28 static GLfloat Zpos = 5;
29 static GLboolean MipMap = 0*GL_TRUE;
30 static GLboolean LinearFilter = GL_TRUE;
31
32
33 static void
34 redraw(void)
35 {
36 GLfloat w = 1.0;
37 GLfloat h = 1.0;
38
39 glClear( GL_COLOR_BUFFER_BIT );
40
41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
42
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
44
45 glPushMatrix();
46 glTranslatef(-1.5, 0, -Zpos);
47 glBegin(GL_POLYGON);
48 glTexCoord2f(0, 0); glVertex2f(-w, -h);
49 glTexCoord2f(1, 0); glVertex2f( w, -h);
50 glTexCoord2f(1, 1); glVertex2f( w, h);
51 glTexCoord2f(0, 1); glVertex2f(-w, h);
52 glEnd();
53 glPopMatrix();
54
55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
56
57 glPushMatrix();
58 glTranslatef(1.5, 0, -Zpos);
59 glBegin(GL_POLYGON);
60 glTexCoord2f(0, 0); glVertex2f(-w, -h);
61 glTexCoord2f(1, 0); glVertex2f( w, -h);
62 glTexCoord2f(1, 1); glVertex2f( w, h);
63 glTexCoord2f(0, 1); glVertex2f(-w, h);
64 glEnd();
65 glPopMatrix();
66
67 glutSwapBuffers();
68 }
69
70
71 static void
72 init(void)
73 {
74 GLubyte color[10][4] = {
75 { 0, 0, 0, 0 },
76 { 1, 0, 0, 0 },
77 { 0, 1, 0, 0 },
78 { 0, 0, 1, 0 },
79 { 0, 1, 1, 0 },
80 { 1, 0, 1, 0 },
81 { 1, 1, 0, 0 },
82 { 1, 0, 0, 0 },
83 { 0, 1, 0, 0 },
84 { 0, 0, 1, 0 }
85 };
86 GLubyte *texImage;
87
88 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
89 printf("Left quad should be linear filtered and right should be nearest filtered.\n");
90 printf("Press z/Z to change quad distance.\n");
91
92 texImage = (GLubyte*) malloc(4 * TexWidth * TexHeight * sizeof(GLubyte));
93 assert(texImage);
94
95 {
96 GLint level = 0;
97 GLint w = TexWidth, h = TexHeight;
98 while (1) {
99 int i, j;
100
101 for (i = 0; i < h; i++) {
102 for (j = 0;j < w; j++) {
103 if (w==1 || h==1 || (((i / 2) ^ (j / 2)) & 1)) {
104 /*if (j < i) {*/
105 texImage[(i*w+j) * 4 + 0] = 255;
106 texImage[(i*w+j) * 4 + 1] = 255;
107 texImage[(i*w+j) * 4 + 2] = 255;
108 texImage[(i*w+j) * 4 + 3] = 255;
109 }
110 else {
111 texImage[(i*w+j) * 4 + 0] = color[level][0] * 255;
112 texImage[(i*w+j) * 4 + 1] = color[level][1] * 255;
113 texImage[(i*w+j) * 4 + 2] = color[level][2] * 255;
114 texImage[(i*w+j) * 4 + 3] = color[level][3] * 255;
115 }
116 }
117 }
118
119 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, w, h, 0,
120 GL_RGBA, GL_UNSIGNED_BYTE, texImage);
121
122 printf("Texture level %d: %d x %d\n", level, w, h);
123 if (!MipMap)
124 break;
125
126 if (w == 1 && h == 1)
127 break;
128 if (w > 1)
129 w /= 2;
130 if (h > 1)
131 h /= 2;
132 level++;
133 }
134 }
135
136 free(texImage);
137
138 glClearColor(0.25, 0.25, 0.25, 1.0);
139 glEnable(GL_TEXTURE_2D);
140
141 glViewport(0, 0, Width, Height);
142 }
143
144
145
146 static void
147 Reshape(int width, int height)
148 {
149 float ar = (float) width /height;
150 Width = width;
151 Height = height;
152 glViewport(0, 0, width, height);
153 glMatrixMode(GL_PROJECTION);
154 glLoadIdentity();
155 glFrustum(-ar, ar, -1.0, 1.0, 5.0, 2500.0);
156 glMatrixMode(GL_MODELVIEW);
157 glLoadIdentity();
158 glTranslatef(0.0, 0.0, -15.0);
159 }
160
161
162 static void
163 Key(unsigned char key, int x, int y)
164 {
165 (void) x;
166 (void) y;
167 switch (key) {
168 case 'z':
169 Zpos--;
170 break;
171 case 'Z':
172 Zpos++;
173 break;
174 case 'f':
175 LinearFilter = !LinearFilter;
176 break;
177 case 27:
178 exit(0);
179 break;
180 }
181 glutPostRedisplay();
182 }
183
184
185 int
186 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(redraw);
197 init();
198 glutMainLoop();
199 return 0;
200 }