Merge branch '7.8'
[mesa.git] / progs / redbook / multitex.c
1 /*
2 * Copyright (c) 1993-2003, Silicon Graphics, Inc.
3 * All Rights Reserved
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose and without fee is hereby granted, provided that the above
7 * copyright notice appear in all copies and that both the copyright
8 * notice and this permission notice appear in supporting documentation,
9 * and that the name of Silicon Graphics, Inc. not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission.
12 *
13 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND
14 * WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
16 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
17 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
18 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, LOSS OF
20 * PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD
21 * PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN ADVISED OF
22 * THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE
24 * OR PERFORMANCE OF THIS SOFTWARE.
25 *
26 * US Government Users Restricted Rights
27 * Use, duplication, or disclosure by the Government is subject to
28 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
29 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
30 * clause at DFARS 252.227-7013 and/or in similar or successor clauses
31 * in the FAR or the DOD or NASA FAR Supplement. Unpublished - rights
32 * reserved under the copyright laws of the United States.
33 *
34 * Contractor/manufacturer is:
35 * Silicon Graphics, Inc.
36 * 1500 Crittenden Lane
37 * Mountain View, CA 94043
38 * United State of America
39 *
40 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
41 */
42
43 /* multitex.c
44 */
45 #include <GL/glew.h>
46 #include <GL/glut.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49
50
51 static GLubyte texels0[32][32][4];
52 static GLubyte texels1[16][16][4];
53
54 static void makeCheckImages(void)
55 {
56 int i, j;
57
58 for (i = 0; i < 32; i++) {
59 for (j = 0; j < 32; j++) {
60 texels0[i][j][0] = (GLubyte) (255 * i / 31);
61 texels0[i][j][1] = (GLubyte) (255 * j / 31);
62 texels0[i][j][2] = (GLubyte) (i*j)/255;
63 texels0[i][j][3] = (GLubyte) 255;
64 }
65 }
66
67 for (i = 0; i < 16; i++) {
68 for (j = 0; j < 16; j++) {
69 texels1[i][j][0] = (GLubyte) 255;
70 texels1[i][j][1] = (GLubyte) (255 * i / 15);
71 texels1[i][j][2] = (GLubyte) (255 * j / 15);
72 texels1[i][j][3] = (GLubyte) 255;
73 }
74 }
75 }
76
77 static void init(void)
78 {
79 GLuint texNames[2];
80
81 glClearColor (0.0, 0.0, 0.0, 0.0);
82 glShadeModel(GL_FLAT);
83 glEnable(GL_DEPTH_TEST);
84
85 makeCheckImages();
86 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
87
88 glGenTextures(2, texNames);
89 glBindTexture(GL_TEXTURE_2D, texNames[0]);
90 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0, GL_RGBA,
91 GL_UNSIGNED_BYTE, texels0);
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
93 GL_NEAREST);
94 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
95 GL_NEAREST);
96 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
97 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
98
99 glBindTexture(GL_TEXTURE_2D, texNames[1]);
100 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA,
101 GL_UNSIGNED_BYTE, texels1);
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
104 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
105 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
106 /* Use the two texture objects to define two texture units
107 * for use in multitexturing */
108 glActiveTextureARB (GL_TEXTURE0_ARB);
109 glEnable(GL_TEXTURE_2D);
110 glBindTexture(GL_TEXTURE_2D, texNames[0]);
111 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
112 glMatrixMode (GL_TEXTURE);
113 glLoadIdentity();
114 glTranslatef(0.5f, 0.5f, 0.0f);
115 glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
116 glTranslatef(-0.5f, -0.5f, 0.0f);
117 glMatrixMode (GL_MODELVIEW);
118 glActiveTextureARB (GL_TEXTURE1_ARB);
119 glEnable(GL_TEXTURE_2D);
120 glBindTexture(GL_TEXTURE_2D, texNames[1]);
121 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
122 }
123
124 static void display(void)
125 {
126 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
127 glBegin(GL_TRIANGLES);
128 glMultiTexCoord2fARB (GL_TEXTURE0_ARB, 0.0, 0.0);
129 glMultiTexCoord2fARB (GL_TEXTURE1_ARB, 1.0, 0.0);
130 glVertex2f(0.0, 0.0);
131 glMultiTexCoord2fARB (GL_TEXTURE0_ARB, 0.5, 1.0);
132 glMultiTexCoord2fARB (GL_TEXTURE1_ARB, 0.5, 0.0);
133 glVertex2f(50.0, 100.0);
134 glMultiTexCoord2fARB (GL_TEXTURE0_ARB, 1.0, 0.0);
135 glMultiTexCoord2fARB (GL_TEXTURE1_ARB, 1.0, 1.0);
136 glVertex2f(100.0, 0.0);
137 glEnd();
138 glFlush();
139 }
140
141 static void reshape(int w, int h)
142 {
143 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
144 glMatrixMode(GL_PROJECTION);
145 glLoadIdentity();
146 if (w <= h)
147 gluOrtho2D(0.0, 100.0, 0.0, 100.0 * (GLdouble)h/(GLdouble)w);
148 else
149 gluOrtho2D(0.0, 100.0 * (GLdouble)w/(GLdouble)h, 0.0, 100.0);
150 glMatrixMode(GL_MODELVIEW);
151 glLoadIdentity();
152 }
153
154 static void keyboard(unsigned char key, int x, int y)
155 {
156 switch (key) {
157 case 27:
158 exit(0);
159 break;
160 }
161 }
162
163 int main(int argc, char** argv)
164 {
165 glutInit(&argc, argv);
166 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
167 glutInitWindowSize(250, 250);
168 glutInitWindowPosition(100, 100);
169 glutCreateWindow(argv[0]);
170 glewInit();
171 init();
172 glutReshapeFunc(reshape);
173 glutDisplayFunc(display);
174 glutKeyboardFunc (keyboard);
175 glutMainLoop();
176 return 0;
177 }