summaryrefslogtreecommitdiff
path: root/src/tests/mipmap_tunnel.c
blob: f06d3243eaec219ed1a24555f5fd164e8f7712c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
 * Display trilinear mipmap filtering quality.
 * We look down a long tunnel shape which has a mipmapped texture
 * applied to it.  Ideally, the transition from one mipmap level to
 * another should be nice and regular/circular.
 * This sort of test is frequently seen in online articles about GPU
 * texture filtering.
 *
 * Brian Paul
 * 13 Oct 2010
 */


#include <stdlib.h>
#include <stdio.h>
#include "glad/gl.h"
#include "glut_wrap.h"


static GLfloat LodBias = 0.0;
static GLboolean NearestFilter = GL_FALSE;
static GLfloat Zpos = -10.0, Zrot = 0.0;
static GLuint TexObj;
static GLboolean HaveAniso;
static GLfloat AnisoMax = 1.0, MaxAnisoMax = 8.0;

#define TEX_SIZE 1024


/** Make a solid-colored texture image */
static void
MakeImage(int level, int width, int height, const GLubyte color[4])
{
   const int makeStripes = 0;
   GLubyte img[TEX_SIZE * TEX_SIZE * 3];
   int i, j;
   for (i = 0; i < height; i++) {
      for (j = 0; j < width; j++) {
         int k = (i * width + j) * 3;
         int p = (i / 8) & makeStripes;
         if (p == 0) {
            img[k + 0] = color[0];
            img[k + 1] = color[1];
            img[k + 2] = color[2];
         }
         else {
            img[k + 0] = 0;
            img[k + 1] = 0;
            img[k + 2] = 0;
         }
      }
   }

   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   glTexImage2D(GL_TEXTURE_2D, level, GL_RGB, width, height, 0,
                GL_RGB, GL_UNSIGNED_BYTE, img);
}


/** Make a mipmap in which each level is a different, solid color */
static void
MakeMipmap(void)
{
   static const GLubyte colors[12][3] = {
      {255, 0, 0},
      {0, 255, 0},
      {0, 0, 255},
      {0, 255, 255},
      {255, 0, 255},
      {255, 255, 0},
      {255, 0, 0},
      {0, 255, 0},
      {0, 0, 255},
      {0, 255, 255},
      {255, 0, 255},
      {255, 255, 0},
   };
   int i, sz = TEX_SIZE;

   for (i = 0; sz > 0; i++) {
      MakeImage(i, sz, sz, colors[i]);
      printf("Level %d size: %d x %d\n", i, sz, sz);
      sz /= 2;
   }
}


static void
Init(void)
{
   glClearColor(.5, .5, .5, .5);

   glGenTextures(1, &TexObj);
   glBindTexture(GL_TEXTURE_2D, TexObj);
   MakeMipmap();

   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
   printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));

   HaveAniso = glutExtensionSupported("GL_EXT_texture_filter_anisotropic");
   if (HaveAniso) {
      glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &MaxAnisoMax);
      printf("GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT = %f\n", MaxAnisoMax);
   }
}


static void
DrawTunnel(void)
{
   const float radius = 10.0, height = 500.0;
   const int slices = 24, stacks = 52;
   const float bias = 0.995;
   GLUquadric *q = gluNewQuadric();

   glPushMatrix();
      glRotatef(180, 1, 0, 0);
      glEnable(GL_TEXTURE_2D);
      gluQuadricTexture(q, GL_TRUE);
      gluCylinder(q, radius, radius, height, slices, stacks);

      glDisable(GL_TEXTURE_2D);
      glColor3f(0, 0, 0);
      gluQuadricDrawStyle(q, GLU_LINE);
      gluCylinder(q, bias*radius, bias*radius, height/4, slices, stacks/4);
   glPopMatrix();

   gluDeleteQuadric(q);
}


static void
PrintString(const char *s)
{
   while (*s) {
      glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
      s++;
   }
}


static void
Display(void)
{
   char str[100];

   glBindTexture(GL_TEXTURE_2D, TexObj);

   if (NearestFilter) {
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                      GL_NEAREST_MIPMAP_NEAREST);
   }
   else {
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                      GL_LINEAR_MIPMAP_LINEAR);
   }

   if (HaveAniso) {
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, AnisoMax);
   }

   glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, LodBias);

   glClear(GL_COLOR_BUFFER_BIT);

   glPushMatrix();
      glTranslatef(0.0, 0.0, Zpos);
      glRotatef(Zrot, 0, 0, 1);
      DrawTunnel();
   glPopMatrix();

   glColor3f(1, 1, 1);
   glWindowPos2i(10, 10);
   if (HaveAniso)
      sprintf(str, "LOD bias (b/B): %.3f  MaxAnisotropy: %.3f", LodBias, AnisoMax);
   else
      sprintf(str, "LOD bias (b/B): %.3f", LodBias);
   PrintString(str);

   glutSwapBuffers();
}


static void
Reshape(int w, int h)
{
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(80.0, 1.0 * (GLfloat) w / (GLfloat) h, 1.0, 3000.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}


static void
Key(unsigned char k, int x, int y)
{
   (void) x;
   (void) y;
   switch (k) {
   case 'a':
      AnisoMax -= 0.25;
      if (AnisoMax <= 1.0)
         AnisoMax = 1.0;
      break;
   case 'A':
      AnisoMax += 0.25;
      if (AnisoMax > MaxAnisoMax)
         AnisoMax = MaxAnisoMax;
      break;
   case 'b':
      LodBias -= 0.125;
      break;
   case 'B':
      LodBias += 0.125;
      break;
   case 'f':
      NearestFilter = !NearestFilter;
      break;
   case 'r':
      Zrot--;
      break;
   case 'R':
      Zrot++;
      break;
   case 'z':
      Zpos--;
      break;
   case 'Z':
      Zpos++;
      break;
   case 27:
      exit(0);
      break;
   default:
      return;
   }
   glutPostRedisplay();
}


static void
Usage(void)
{
   printf("Keys:\n");
   printf("  b/B    decrease/increase GL_TEXTURE_LOD_BIAS\n");
   printf("  f      toggle nearest/linear filtering\n");
   printf("  r/R    rotate tunnel\n");
}


int
main(int argc, char **argv)
{
   glutInitWindowSize(600, 600);
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
   glutCreateWindow(argv[0]);
   gladLoaderLoadGL();
   glutReshapeFunc(Reshape);
   glutDisplayFunc(Display);
   glutKeyboardFunc(Key);
   Init();
   Usage();
   glutMainLoop();
   gladLoaderUnloadGL();
   return 0;
}