2 * Copyright (c) 1993-2003, Silicon Graphics, Inc.
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.
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.
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.
34 * Contractor/manufacturer is:
35 * Silicon Graphics, Inc.
36 * 1500 Crittenden Lane
37 * Mountain View, CA 94043
38 * United State of America
40 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
44 * This program renders a variety of quads showing different
45 * effects of texture combiner functions.
47 * The first row renders an untextured polygon (so you can
48 * compare the fragment colors) and then the 2 textures.
49 * The second row shows several different combiner functions
50 * on a single texture: replace, modulate, add, add-signed,
52 * The third row shows the interpolate combiner function
53 * on a single texture with a constant color/alpha value,
54 * varying the amount of interpolation.
55 * The fourth row uses multitexturing with two textures
56 * and different combiner functions.
57 * The fifth row are some combiner experiments: using the
58 * scaling factor and reversing the order of subtraction
59 * for a combination function.
61 #include <glad/glad.h>
62 #include "glut_wrap.h"
68 /* arrays for two textures */
69 static GLubyte image0
[imageHeight
][imageWidth
][4];
70 static GLubyte image1
[imageHeight
][imageWidth
][4];
72 static GLuint texName
[4];
74 static void makeImages(void)
77 for (i
= 0; i
< imageHeight
; i
++) {
78 for (j
= 0; j
< imageWidth
; j
++) {
79 c
= ((i
&2)==0)*255; /* horiz b & w stripes */
80 image0
[i
][j
][0] = (GLubyte
) c
;
81 image0
[i
][j
][1] = (GLubyte
) c
;
82 image0
[i
][j
][2] = (GLubyte
) c
;
83 image0
[i
][j
][3] = (GLubyte
) 255;
84 c
= ((j
&4)!=0)*128; /* wider vertical 50% cyan and black stripes */
85 image1
[i
][j
][0] = (GLubyte
) 0;
86 image1
[i
][j
][1] = (GLubyte
) c
;
87 image1
[i
][j
][2] = (GLubyte
) c
;
88 image1
[i
][j
][3] = (GLubyte
) 255;
93 static void init(void)
95 glClearColor (0.0, 0.0, 0.0, 0.0);
96 glShadeModel(GL_SMOOTH
);
99 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
101 glGenTextures(4, texName
);
103 glBindTexture(GL_TEXTURE_2D
, texName
[0]);
104 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
105 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
106 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
107 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
108 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, imageWidth
, imageHeight
,
109 0, GL_RGBA
, GL_UNSIGNED_BYTE
, image0
);
111 glBindTexture(GL_TEXTURE_2D
, texName
[1]);
112 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
113 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
114 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
115 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
116 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, imageWidth
, imageHeight
,
117 0, GL_RGBA
, GL_UNSIGNED_BYTE
, image1
);
119 /* smooth-shaded polygon with multiple texture coordinates */
120 glNewList (1, GL_COMPILE
);
122 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 0.0, 0.0);
123 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 0.0, 0.0);
124 glColor3f (0.5, 1.0, 0.25);
125 glVertex3f(0.0, 0.0, 0.0);
126 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 0.0, 2.0);
127 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 0.0, 2.0);
128 glColor3f (1.0, 1.0, 1.0);
129 glVertex3f(0.0, 1.0, 0.0);
130 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 2.0, 2.0);
131 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 2.0, 2.0);
132 glColor3f (1.0, 1.0, 1.0);
133 glVertex3f(1.0, 1.0, 0.0);
134 glMultiTexCoord2fARB(GL_TEXTURE0_ARB
, 2.0, 0.0);
135 glMultiTexCoord2fARB(GL_TEXTURE1_ARB
, 2.0, 0.0);
136 glColor3f (1.0, 0.5, 0.25);
137 glVertex3f(1.0, 0.0, 0.0);
142 static void display(void)
144 static GLfloat constColor
[4] = {0.0, 0.0, 0.0, 0.0}; /* for use as constant texture color */
146 glClear(GL_COLOR_BUFFER_BIT
);
148 glDisable(GL_TEXTURE_2D
); /* untextured polygon--see the "fragment" colors */
150 glTranslatef(0.0, 5.0, 0.0);
154 glEnable(GL_TEXTURE_2D
);
155 /* draw ordinary textured polys; 1 texture unit; combine mode disabled */
156 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
158 glBindTexture(GL_TEXTURE_2D
, texName
[0]);
159 glTranslatef(1.0, 5.0, 0.0);
164 glBindTexture(GL_TEXTURE_2D
, texName
[1]);
165 glTranslatef(2.0, 5.0, 0.0);
169 /* different combine modes enabled; 1 texture unit
171 * glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
172 * glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
173 * glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);
174 * glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
176 glBindTexture(GL_TEXTURE_2D
, texName
[0]);
177 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_ARB
);
178 glTexEnvf(GL_TEXTURE_ENV
, GL_COMBINE_RGB_ARB
, GL_REPLACE
);
179 glTexEnvf(GL_TEXTURE_ENV
, GL_SOURCE0_RGB_ARB
, GL_TEXTURE
);
180 glTexEnvf(GL_TEXTURE_ENV
, GL_OPERAND0_RGB_ARB
, GL_SRC_COLOR
);
182 glTranslatef(1.0, 4.0, 0.0);
186 glTexEnvf(GL_TEXTURE_ENV
, GL_COMBINE_RGB_ARB
, GL_MODULATE
);
187 glTexEnvf(GL_TEXTURE_ENV
, GL_SOURCE1_RGB_ARB
, GL_PREVIOUS_ARB
);
188 glTexEnvf(GL_TEXTURE_ENV
, GL_OPERAND1_RGB_ARB
, GL_SRC_COLOR
);
190 glTranslatef(2.0, 4.0, 0.0);
194 glTexEnvf(GL_TEXTURE_ENV
, GL_COMBINE_RGB_ARB
, GL_ADD
);
196 glTranslatef(3.0, 4.0, 0.0);
200 glTexEnvf(GL_TEXTURE_ENV
, GL_COMBINE_RGB_ARB
, GL_ADD_SIGNED_ARB
);
202 glTranslatef(4.0, 4.0, 0.0);
206 glTexEnvf(GL_TEXTURE_ENV
, GL_COMBINE_RGB_ARB
, GL_SUBTRACT_ARB
);
208 glTranslatef(5.0, 4.0, 0.0);
212 /* interpolate combine with constant color; 1 texture unit
213 * use different alpha values for constant color
215 * glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
216 * glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
217 * glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);
218 * glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
219 * glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);
220 * glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);
223 glTexEnvfv(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_COLOR
, constColor
);
224 glBindTexture(GL_TEXTURE_2D
, texName
[0]);
225 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_ARB
);
226 glTexEnvf(GL_TEXTURE_ENV
, GL_COMBINE_RGB_ARB
, GL_INTERPOLATE_ARB
);
227 glTexEnvf(GL_TEXTURE_ENV
, GL_SOURCE0_RGB_ARB
, GL_TEXTURE
);
228 glTexEnvf(GL_TEXTURE_ENV
, GL_OPERAND0_RGB_ARB
, GL_SRC_COLOR
);
229 glTexEnvf(GL_TEXTURE_ENV
, GL_SOURCE1_RGB_ARB
, GL_PREVIOUS_ARB
);
230 glTexEnvf(GL_TEXTURE_ENV
, GL_OPERAND1_RGB_ARB
, GL_SRC_COLOR
);
231 glTexEnvf(GL_TEXTURE_ENV
, GL_SOURCE2_RGB_ARB
, GL_CONSTANT_ARB
);
232 glTexEnvf(GL_TEXTURE_ENV
, GL_OPERAND2_RGB_ARB
, GL_SRC_ALPHA
);
234 glTranslatef(1.0, 3.0, 0.0);
239 glTexEnvfv(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_COLOR
, constColor
);
241 glTranslatef(2.0, 3.0, 0.0);
246 glTexEnvfv(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_COLOR
, constColor
);
248 glTranslatef(3.0, 3.0, 0.0);
253 glTexEnvfv(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_COLOR
, constColor
);
255 glTranslatef(4.0, 3.0, 0.0);
259 /* combine textures 0 & 1
261 * glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
262 * glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
263 * glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);
264 * glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
267 glActiveTextureARB (GL_TEXTURE0_ARB
);
268 glEnable (GL_TEXTURE_2D
);
269 glBindTexture(GL_TEXTURE_2D
, texName
[0]);
270 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
272 glActiveTextureARB (GL_TEXTURE1_ARB
);
273 glEnable (GL_TEXTURE_2D
);
274 glBindTexture(GL_TEXTURE_2D
, texName
[1]);
275 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_ARB
);
276 glTexEnvf(GL_TEXTURE_ENV
, GL_COMBINE_RGB_ARB
, GL_REPLACE
);
278 glTranslatef(1.0, 2.0, 0.0);
282 /* try different combiner modes of texture unit 1 */
283 glTexEnvf(GL_TEXTURE_ENV
, GL_COMBINE_RGB_ARB
, GL_MODULATE
);
285 glTranslatef(2.0, 2.0, 0.0);
289 glTexEnvf(GL_TEXTURE_ENV
, GL_COMBINE_RGB_ARB
, GL_ADD
);
291 glTranslatef(3.0, 2.0, 0.0);
295 glTexEnvf(GL_TEXTURE_ENV
, GL_COMBINE_RGB_ARB
, GL_ADD_SIGNED_ARB
);
297 glTranslatef(4.0, 2.0, 0.0);
301 glTexEnvf(GL_TEXTURE_ENV
, GL_COMBINE_RGB_ARB
, GL_SUBTRACT_ARB
);
303 glTranslatef(5.0, 2.0, 0.0);
307 /* some experiments */
309 /* see the effect of RGB_SCALE */
310 glTexEnvf(GL_TEXTURE_ENV
, GL_RGB_SCALE_ARB
, 2.0);
311 glTexEnvf(GL_TEXTURE_ENV
, GL_COMBINE_RGB_ARB
, GL_REPLACE
);
313 glTranslatef(1.0, 1.0, 0.0);
317 glTexEnvf(GL_TEXTURE_ENV
, GL_COMBINE_RGB_ARB
, GL_MODULATE
);
319 glTranslatef(2.0, 1.0, 0.0);
322 glTexEnvf(GL_TEXTURE_ENV
, GL_RGB_SCALE_ARB
, 1.0);
324 /* using SOURCE0 and SOURCE1, reverse the order of subtraction Arg1-Arg0 */
326 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_ARB
);
327 glTexEnvf(GL_TEXTURE_ENV
, GL_COMBINE_RGB_ARB
, GL_SUBTRACT_ARB
);
328 glTexEnvf(GL_TEXTURE_ENV
, GL_SOURCE0_RGB_ARB
, GL_PREVIOUS_ARB
);
329 glTexEnvf(GL_TEXTURE_ENV
, GL_OPERAND0_RGB_ARB
, GL_SRC_COLOR
);
330 glTexEnvf(GL_TEXTURE_ENV
, GL_SOURCE1_RGB_ARB
, GL_TEXTURE
);
331 glTexEnvf(GL_TEXTURE_ENV
, GL_OPERAND1_RGB_ARB
, GL_SRC_COLOR
);
333 glTranslatef(5.0, 1.0, 0.0);
337 glActiveTextureARB (GL_TEXTURE1_ARB
); /* deactivate multitexturing */
338 glDisable (GL_TEXTURE_2D
);
339 glActiveTextureARB (GL_TEXTURE0_ARB
); /* activate single texture unit */
344 static void reshape(int w
, int h
)
346 glViewport(0, 0, (GLsizei
) w
, (GLsizei
) h
);
347 glMatrixMode(GL_PROJECTION
);
349 gluOrtho2D(0.0, 7.0, 0.0, 7.0);
350 glMatrixMode(GL_MODELVIEW
);
354 static void keyboard (unsigned char key
, int x
, int y
)
365 int main(int argc
, char** argv
)
367 glutInit(&argc
, argv
);
368 glutInitDisplayMode(GLUT_SINGLE
| GLUT_RGB
);
369 glutInitWindowSize(400, 400);
370 glutInitWindowPosition(100, 100);
371 glutCreateWindow(argv
[0]);
374 glutDisplayFunc(display
);
375 glutReshapeFunc(reshape
);
376 glutKeyboardFunc(keyboard
);