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