ARB prog parser: fix parameter binding type
[mesa.git] / progs / trivial / tri-multitex-vbo.c
1 /*
2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that (i) the above copyright notices and this permission notice appear in
7 * all copies of the software and related documentation, and (ii) the name of
8 * Silicon Graphics may not be used in any advertising or
9 * publicity relating to the software without the specific, prior written
10 * permission of Silicon Graphics.
11 *
12 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
13 * ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <GL/glew.h>
29 #include <GL/glut.h>
30
31 #define NR_VERTS 4
32
33 struct {
34 GLfloat position[NR_VERTS][4];
35 GLubyte color[NR_VERTS][4];
36 GLfloat tex0[NR_VERTS][2];
37 GLfloat tex1[NR_VERTS][2];
38 } verts = {
39
40 { { 0.9, -0.9, 0.0, 1.0 },
41 { 0.9, 0.9, 0.0, 1.0 },
42 { -0.9, 0.9, 0.0, 1.0 },
43 { -0.9, -0.9, 0.0, 1.0 } },
44
45 { { 0x00, 0x00, 0xff, 0x00 },
46 { 0x00, 0xff, 0x00, 0x00 },
47 { 0xff, 0x00, 0x00, 0x00 },
48 { 0xff, 0xff, 0xff, 0x00 }
49 },
50
51 { { 1, -1 },
52 { 1, 1 },
53 { -1, 1 },
54 { -1, -1 } },
55
56 { { 3, 0 },
57 { 0, 3 },
58 { -3, 0 },
59 { 0, -3} },
60
61 };
62
63 GLuint indices[] = { 0, 1, 2, 3 };
64
65 GLuint arrayObj, elementObj;
66
67
68 GLenum doubleBuffer;
69
70
71 #define Offset(ptr, member) (void *)((const char *)&((ptr)->member) - (const char *)(ptr))
72
73 static void Init(void)
74 {
75 GLuint texObj[2];
76
77 fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
78 fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
79 fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
80 fflush(stderr);
81
82 glClearColor(0.0, 0.0, 1.0, 0.0);
83
84 glGenTextures(2, texObj);
85
86 #define SIZE 32
87 {
88 GLubyte tex2d[SIZE][SIZE][3];
89 GLint s, t;
90
91 for (s = 0; s < SIZE; s++) {
92 for (t = 0; t < SIZE; t++) {
93 tex2d[t][s][0] = s*255/(SIZE-1);
94 tex2d[t][s][1] = t*255/(SIZE-1);
95 tex2d[t][s][2] = 0;
96 }
97 }
98
99 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
100 glActiveTextureARB(GL_TEXTURE0_ARB);
101 glBindTexture(GL_TEXTURE_2D, texObj[0]);
102
103
104 glTexImage2D(GL_TEXTURE_2D, 0, 3, SIZE, SIZE, 0,
105 GL_RGB, GL_UNSIGNED_BYTE, tex2d);
106
107 glEnable(GL_TEXTURE_2D);
108 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
109 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
110 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
111 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
112
113 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
114 }
115
116 {
117 GLubyte tex2d[SIZE][SIZE][3];
118 GLint s, t;
119
120 for (s = 0; s < SIZE; s++) {
121 for (t = 0; t < SIZE; t++) {
122 GLboolean on = ((s/4) ^ (t/4)) & 1;
123 tex2d[t][s][0] = on ? 128 : 0;
124 tex2d[t][s][1] = on ? 128 : 0;
125 tex2d[t][s][2] = on ? 128 : 0;
126 }
127 }
128
129 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
130 glActiveTextureARB(GL_TEXTURE1_ARB);
131 glBindTexture(GL_TEXTURE_2D, texObj[1]);
132
133 glTexImage2D(GL_TEXTURE_2D, 0, 3, SIZE, SIZE, 0,
134 GL_RGB, GL_UNSIGNED_BYTE, tex2d);
135
136 glEnable(GL_TEXTURE_2D);
137 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
138 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
139 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
140 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
141
142 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
143 }
144
145 glActiveTextureARB( GL_TEXTURE0_ARB );
146
147
148 {
149
150 glGenBuffersARB(1, &arrayObj);
151 glGenBuffersARB(1, &elementObj);
152
153 glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrayObj);
154 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, elementObj);
155
156 glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), &verts, GL_STATIC_DRAW_ARB);
157 glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(indices), indices, GL_STATIC_DRAW_ARB);
158
159 glEnableClientState( GL_VERTEX_ARRAY );
160 glVertexPointer( 4, GL_FLOAT, 0, Offset(&verts, position) );
161
162 glEnableClientState( GL_COLOR_ARRAY );
163 glColorPointer( 4, GL_UNSIGNED_BYTE, 0, Offset(&verts, color) );
164
165 glClientActiveTextureARB( GL_TEXTURE0_ARB );
166 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
167 glTexCoordPointer( 2, GL_FLOAT, 0, Offset(&verts, tex0) );
168
169 glClientActiveTextureARB( GL_TEXTURE1_ARB );
170 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
171 glTexCoordPointer( 2, GL_FLOAT, 0, Offset(&verts, tex1) );
172
173 glClientActiveTextureARB( GL_TEXTURE0_ARB );
174 }
175 }
176
177 static void Reshape(int width, int height)
178 {
179
180 glViewport(0, 0, (GLint)width, (GLint)height);
181
182 glMatrixMode(GL_PROJECTION);
183 glLoadIdentity();
184 /* glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); */
185 glMatrixMode(GL_MODELVIEW);
186 }
187
188 static void Key(unsigned char key, int x, int y)
189 {
190
191 switch (key) {
192 case 27:
193 exit(1);
194 default:
195 break;
196 }
197
198 glutPostRedisplay();
199 }
200
201 static void Draw(void)
202 {
203 glClear(GL_COLOR_BUFFER_BIT);
204
205 glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, NULL );
206
207 glFlush();
208
209 if (doubleBuffer) {
210 glutSwapBuffers();
211 }
212 }
213
214 static GLenum Args(int argc, char **argv)
215 {
216 GLint i;
217
218 doubleBuffer = GL_FALSE;
219
220 for (i = 1; i < argc; i++) {
221 if (strcmp(argv[i], "-sb") == 0) {
222 doubleBuffer = GL_FALSE;
223 } else if (strcmp(argv[i], "-db") == 0) {
224 doubleBuffer = GL_TRUE;
225 } else {
226 fprintf(stderr, "%s (Bad option).\n", argv[i]);
227 return GL_FALSE;
228 }
229 }
230 return GL_TRUE;
231 }
232
233 int main(int argc, char **argv)
234 {
235 GLenum type;
236
237 glutInit(&argc, argv);
238
239 if (Args(argc, argv) == GL_FALSE) {
240 exit(1);
241 }
242
243 glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
244
245 type = GLUT_RGB | GLUT_ALPHA;
246 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
247 glutInitDisplayMode(type);
248
249 if (glutCreateWindow(*argv) == GL_FALSE) {
250 exit(1);
251 }
252
253 glewInit();
254 Init();
255
256 glutReshapeFunc(Reshape);
257 glutKeyboardFunc(Key);
258 glutDisplayFunc(Draw);
259 glutMainLoop();
260 return 0;
261 }