#include <GL/glut.h>
#include "readtex.h"
+#define TEST_CLAMP 0
+#define TEST_MIPMAPS 0
+
#define MAX_TEXTURES 8
static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
static GLboolean Anim = GL_TRUE;
static GLboolean Blend = GL_FALSE;
-static GLboolean MipMap = 1+GL_FALSE;
+static GLuint Filter = 0;
+static GLboolean Clamp = GL_FALSE;
static GLuint NumTextures;
static GLuint Textures[MAX_TEXTURES];
};
+#define NUM_FILTERS 5
+static
+struct filter {
+ GLenum min, mag;
+ const char *name;
+} FilterModes[NUM_FILTERS] = {
+ { GL_NEAREST, GL_NEAREST, "Nearest,Nearest" },
+ { GL_LINEAR, GL_LINEAR, "Linear,Linear" },
+ { GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST, "NearestMipmapNearest,Nearest" },
+ { GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR, "LinearMipmapNearest,Linear" },
+ { GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, "LinearMipmapLinear,Linear" }
+};
+
+
+
+
static void
Idle(void)
{
glBindTexture(GL_TEXTURE_2D, Textures[i]);
glBegin(GL_POLYGON);
+#if TEST_CLAMP
+ glTexCoord2f( -0.5, -0.5 ); glVertex2f( -ar, -1.0 );
+ glTexCoord2f( 1.5, -0.5 ); glVertex2f( ar, -1.0 );
+ glTexCoord2f( 1.5, 1.5 ); glVertex2f( ar, 1.0 );
+ glTexCoord2f( -0.5, 1.5 ); glVertex2f( -ar, 1.0 );
+#else
glTexCoord2f( 0.0, 0.0 ); glVertex2f( -ar, -1.0 );
glTexCoord2f( 1.0, 0.0 ); glVertex2f( ar, -1.0 );
glTexCoord2f( 1.0, 1.0 ); glVertex2f( ar, 1.0 );
glTexCoord2f( 0.0, 1.0 ); glVertex2f( -ar, 1.0 );
+#endif
glEnd();
glPopMatrix();
static void
-SetTexFilters(void)
+SetTexParams(void)
{
GLuint i;
for (i = 0; i < NumTextures; i++) {
glBindTexture(GL_TEXTURE_2D, Textures[i]);
- if (MipMap) {
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
- GL_LINEAR_MIPMAP_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
+ FilterModes[Filter].min);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
+ FilterModes[Filter].mag);
+
+ if (Clamp) {
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
else {
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
}
}
(void) y;
switch (key) {
case 'a':
+ case ' ':
Anim = !Anim;
if (Anim)
glutIdleFunc(Idle);
case 'b':
Blend = !Blend;
break;
- case 'm':
- MipMap = !MipMap;
- SetTexFilters();
+ case 'f':
+ Filter = (Filter + 1) % NUM_FILTERS;
+ SetTexParams();
break;
case 'r':
Randomize();
+ break;
+#if TEST_CLAMP
+ case 'c':
+ Clamp = !Clamp;
+ SetTexParams();
+ break;
+#endif
case 'z':
Zrot -= step;
break;
break;
}
- printf("Blend=%s MipMap=%s\n",
+ printf("Blend=%s Filter=%s\n",
Blend ? "Y" : "n",
- MipMap ? "Y" : "n");
+ FilterModes[Filter].name);
glutPostRedisplay();
}
glGenTextures(n, Textures);
+ SetTexParams();
+
for (i = 0; i < n; i++) {
GLint w, h;
glBindTexture(GL_TEXTURE_2D, Textures[i]);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+#if TEST_MIPMAPS
+ {
+ static const GLubyte color[9][4] = {
+ {255, 0, 0},
+ {0, 255, 0},
+ {0, 0, 255},
+ {0, 255, 255},
+ {255, 0, 255},
+ {255, 255, 0},
+ {255, 128, 255},
+ {128, 128, 128},
+ {64, 64, 64}
+ };
+
+ GLubyte image[256*256*4];
+ int i, level;
+ w = h = 256;
+ for (level = 0; level <= 8; level++) {
+ for (i = 0; i < w * h; i++) {
+ image[i*4+0] = color[level][0];
+ image[i*4+1] = color[level][1];
+ image[i*4+2] = color[level][2];
+ image[i*4+3] = color[level][3];
+ }
+ printf("Load level %d: %d x %d\n", level, w>>level, h>>level);
+ glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, w>>level, h>>level, 0,
+ GL_RGBA, GL_UNSIGNED_BYTE, image);
+ }
+ }
+#else
if (!LoadRGBMipmaps2(files[i], GL_TEXTURE_2D, GL_RGB, &w, &h)) {
printf("Error: couldn't load %s\n", files[i]);
exit(1);
}
+#endif
TexAspect[i] = (float) w / (float) h;
printf("Loaded %s\n", files[i]);
}
printf("Keys:\n");
printf(" a - toggle animation\n");
printf(" b - toggle blending\n");
- printf(" m - toggle mipmapping\n");
+ printf(" f - change texture filter mode\n");
printf(" r - randomize\n");
printf(" ESC - exit\n");
}
{
glutInit(&argc, argv);
glutInitWindowPosition(0, 0);
- glutInitWindowSize(400, 400);
+ glutInitWindowSize(700, 700);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
Win = glutCreateWindow(argv[0]);
glutReshapeFunc(Reshape);