132001f49Smrg/**
232001f49Smrg * Draw a series of quads, each with a different texture.
332001f49Smrg */
432001f49Smrg
532001f49Smrg#include <stdio.h>
632001f49Smrg#include <string.h>
732001f49Smrg#include <stdlib.h>
832001f49Smrg#include <GL/glew.h>
932001f49Smrg#include "glut_wrap.h"
1032001f49Smrg
1132001f49Smrg#define NUM_TEX 10
1232001f49Smrg
1332001f49Smrgstatic GLint Win = 0;
1432001f49Smrgstatic GLuint Tex[NUM_TEX];
1532001f49Smrg
1632001f49Smrg
1732001f49Smrgstatic void Init(void)
1832001f49Smrg{
1932001f49Smrg   int i;
2032001f49Smrg
2132001f49Smrg   fprintf(stderr, "GL_RENDERER   = %s\n", (char *) glGetString(GL_RENDERER));
2232001f49Smrg   fflush(stderr);
2332001f49Smrg
2432001f49Smrg   glGenTextures(NUM_TEX, Tex);
2532001f49Smrg
2632001f49Smrg   for (i = 0; i < NUM_TEX; i++) {
2732001f49Smrg      glBindTexture(GL_TEXTURE_2D, Tex[i]);
2832001f49Smrg
2932001f49Smrg      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3032001f49Smrg      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
3132001f49Smrg      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
3232001f49Smrg      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3332001f49Smrg      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3432001f49Smrg   }
3532001f49Smrg}
3632001f49Smrg
3732001f49Smrg
3832001f49Smrgstatic void Reshape(int width, int height)
3932001f49Smrg{
4032001f49Smrg   float ar = (float) width / height;
4132001f49Smrg   glViewport(0, 0, width, height);
4232001f49Smrg   glMatrixMode(GL_PROJECTION);
4332001f49Smrg   glLoadIdentity();
4432001f49Smrg   glOrtho(-ar, ar, -1.0, 1.0, -1.0, 1.0);
4532001f49Smrg   glMatrixMode(GL_MODELVIEW);
4632001f49Smrg}
4732001f49Smrg
4832001f49Smrg
4932001f49Smrgstatic void Key(unsigned char key, int x, int y)
5032001f49Smrg{
5132001f49Smrg   if (key == 27) {
5232001f49Smrg      glDeleteTextures(NUM_TEX, Tex);
5332001f49Smrg      glutDestroyWindow(Win);
5432001f49Smrg      exit(1);
5532001f49Smrg   }
5632001f49Smrg   glutPostRedisplay();
5732001f49Smrg}
5832001f49Smrg
5932001f49Smrg
6032001f49Smrgstatic void Draw(void)
6132001f49Smrg{
6232001f49Smrg   GLubyte tex[16][16][4];
6332001f49Smrg   int t, i, j;
6432001f49Smrg
6532001f49Smrg   for (t = 0; t < NUM_TEX; t++) {
6632001f49Smrg
6732001f49Smrg      for (i = 0; i < 16; i++) {
6832001f49Smrg         for (j = 0; j < 16; j++) {
6932001f49Smrg            if (i < t) {
7032001f49Smrg               /* red row */
7132001f49Smrg               tex[i][j][0] = 255;
7232001f49Smrg               tex[i][j][1] = 0;
7332001f49Smrg               tex[i][j][2] = 0;
7432001f49Smrg               tex[i][j][3] = 255;
7532001f49Smrg            }
7632001f49Smrg            else if ((i + j) & 1) {
7732001f49Smrg               tex[i][j][0] = 128;
7832001f49Smrg               tex[i][j][1] = 128;
7932001f49Smrg               tex[i][j][2] = 128;
8032001f49Smrg               tex[i][j][3] = 255;
8132001f49Smrg            }
8232001f49Smrg            else {
8332001f49Smrg               tex[i][j][0] = 255;
8432001f49Smrg               tex[i][j][1] = 255;
8532001f49Smrg               tex[i][j][2] = 255;
8632001f49Smrg               tex[i][j][3] = 255;
8732001f49Smrg            }
8832001f49Smrg         }
8932001f49Smrg      }
9032001f49Smrg
9132001f49Smrg      glBindTexture(GL_TEXTURE_2D, Tex[t]);
9232001f49Smrg      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0,
9332001f49Smrg                   GL_RGBA, GL_UNSIGNED_BYTE, tex);
9432001f49Smrg   }
9532001f49Smrg
9632001f49Smrg   glEnable(GL_TEXTURE_2D);
9732001f49Smrg
9832001f49Smrg   glClear(GL_COLOR_BUFFER_BIT);
9932001f49Smrg
10032001f49Smrg   for (i = 0; i < NUM_TEX; i++) {
10132001f49Smrg
10232001f49Smrg      glBindTexture(GL_TEXTURE_2D, Tex[i]);
10332001f49Smrg
10432001f49Smrg      glPushMatrix();
10532001f49Smrg      glTranslatef(-4.0 + i, 0, 0);
10632001f49Smrg      glScalef(0.5, 0.5, 1.0);
10732001f49Smrg
10832001f49Smrg      glBegin(GL_QUADS);
10932001f49Smrg      glTexCoord2f(1,0);
11032001f49Smrg      glVertex3f( 0.9, -0.9, 0.0);
11132001f49Smrg      glTexCoord2f(1,1);
11232001f49Smrg      glVertex3f( 0.9,  0.9, 0.0);
11332001f49Smrg      glTexCoord2f(0,1);
11432001f49Smrg      glVertex3f(-0.9,  0.9, 0.0);
11532001f49Smrg      glTexCoord2f(0,0);
11632001f49Smrg      glVertex3f(-0.9,  -0.9, 0.0);
11732001f49Smrg      glEnd();
11832001f49Smrg
11932001f49Smrg      glPopMatrix();
12032001f49Smrg   }
12132001f49Smrg
12232001f49Smrg
12332001f49Smrg   glutSwapBuffers();
12432001f49Smrg}
12532001f49Smrg
12632001f49Smrg
12732001f49Smrgint main(int argc, char **argv)
12832001f49Smrg{
12932001f49Smrg   glutInit(&argc, argv);
13032001f49Smrg   glutInitWindowSize(900, 200);
13132001f49Smrg   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
13232001f49Smrg   Win = glutCreateWindow(*argv);
13332001f49Smrg   if (!Win) {
13432001f49Smrg      exit(1);
13532001f49Smrg   }
13632001f49Smrg   glewInit();
13732001f49Smrg   Init();
13832001f49Smrg   glutReshapeFunc(Reshape);
13932001f49Smrg   glutKeyboardFunc(Key);
14032001f49Smrg   glutDisplayFunc(Draw);
14132001f49Smrg   glutMainLoop();
14232001f49Smrg   return 0;
14332001f49Smrg}
144