Merge branch '7.8'
[mesa.git] / progs / trivial / tex-quads.c
1 /**
2 * Draw a series of quads, each with a different texture.
3 */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <GL/glew.h>
9 #include <GL/glut.h>
10
11 #define NUM_TEX 10
12
13 static GLint Win = 0;
14 static GLuint Tex[NUM_TEX];
15
16
17 static void Init(void)
18 {
19 int i;
20
21 fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
22 fflush(stderr);
23
24 glGenTextures(NUM_TEX, Tex);
25
26 for (i = 0; i < NUM_TEX; i++) {
27 glBindTexture(GL_TEXTURE_2D, Tex[i]);
28
29 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
30 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
31 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
32 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
33 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
34 }
35 }
36
37
38 static void Reshape(int width, int height)
39 {
40 float ar = (float) width / height;
41 glViewport(0, 0, width, height);
42 glMatrixMode(GL_PROJECTION);
43 glLoadIdentity();
44 glOrtho(-ar, ar, -1.0, 1.0, -1.0, 1.0);
45 glMatrixMode(GL_MODELVIEW);
46 }
47
48
49 static void Key(unsigned char key, int x, int y)
50 {
51 if (key == 27) {
52 glDeleteTextures(NUM_TEX, Tex);
53 glutDestroyWindow(Win);
54 exit(1);
55 }
56 glutPostRedisplay();
57 }
58
59
60 static void Draw(void)
61 {
62 GLubyte tex[16][16][4];
63 int t, i, j;
64
65 for (t = 0; t < NUM_TEX; t++) {
66
67 for (i = 0; i < 16; i++) {
68 for (j = 0; j < 16; j++) {
69 if (i < t) {
70 /* red row */
71 tex[i][j][0] = 255;
72 tex[i][j][1] = 0;
73 tex[i][j][2] = 0;
74 tex[i][j][3] = 255;
75 }
76 else if ((i + j) & 1) {
77 tex[i][j][0] = 128;
78 tex[i][j][1] = 128;
79 tex[i][j][2] = 128;
80 tex[i][j][3] = 255;
81 }
82 else {
83 tex[i][j][0] = 255;
84 tex[i][j][1] = 255;
85 tex[i][j][2] = 255;
86 tex[i][j][3] = 255;
87 }
88 }
89 }
90
91 glBindTexture(GL_TEXTURE_2D, Tex[t]);
92 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0,
93 GL_RGBA, GL_UNSIGNED_BYTE, tex);
94 }
95
96 glEnable(GL_TEXTURE_2D);
97
98 glClear(GL_COLOR_BUFFER_BIT);
99
100 for (i = 0; i < NUM_TEX; i++) {
101
102 glBindTexture(GL_TEXTURE_2D, Tex[i]);
103
104 glPushMatrix();
105 glTranslatef(-4.0 + i, 0, 0);
106 glScalef(0.5, 0.5, 1.0);
107
108 glBegin(GL_QUADS);
109 glTexCoord2f(1,0);
110 glVertex3f( 0.9, -0.9, 0.0);
111 glTexCoord2f(1,1);
112 glVertex3f( 0.9, 0.9, 0.0);
113 glTexCoord2f(0,1);
114 glVertex3f(-0.9, 0.9, 0.0);
115 glTexCoord2f(0,0);
116 glVertex3f(-0.9, -0.9, 0.0);
117 glEnd();
118
119 glPopMatrix();
120 }
121
122
123 glutSwapBuffers();
124 }
125
126
127 int main(int argc, char **argv)
128 {
129 glutInit(&argc, argv);
130 glutInitWindowSize(900, 200);
131 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
132 Win = glutCreateWindow(*argv);
133 if (!Win) {
134 exit(1);
135 }
136 glewInit();
137 Init();
138 glutReshapeFunc(Reshape);
139 glutKeyboardFunc(Key);
140 glutDisplayFunc(Draw);
141 glutMainLoop();
142 return 0;
143 }