Use glad instead of GLEW.
[mesa-demos.git] / src / trivial / tri-multitex-vbo.c
blobfbd8d743c9a57f0bda57966cedd2aa134796b8df
1 /*
2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
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.
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.
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.
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <glad/glad.h>
29 #include "glut_wrap.h"
31 #define NR_VERTS 4
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 = {
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 } },
45 { { 0x00, 0x00, 0xff, 0x00 },
46 { 0x00, 0xff, 0x00, 0x00 },
47 { 0xff, 0x00, 0x00, 0x00 },
48 { 0xff, 0xff, 0xff, 0x00 }
51 { { 1, -1 },
52 { 1, 1 },
53 { -1, 1 },
54 { -1, -1 } },
56 { { 3, 0 },
57 { 0, 3 },
58 { -3, 0 },
59 { 0, -3} },
63 GLuint indices[] = { 0, 1, 2, 3 };
65 GLuint arrayObj, elementObj;
68 GLenum doubleBuffer;
71 #define Offset(ptr, member) (void *)((const char *)&((ptr)->member) - (const char *)(ptr))
73 static void Init(void)
75 GLuint texObj[2];
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);
82 glClearColor(0.0, 0.0, 1.0, 0.0);
84 glGenTextures(2, texObj);
86 #define SIZE 32
88 GLubyte tex2d[SIZE][SIZE][3];
89 GLint s, t;
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;
99 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
100 glActiveTextureARB(GL_TEXTURE0_ARB);
101 glBindTexture(GL_TEXTURE_2D, texObj[0]);
104 glTexImage2D(GL_TEXTURE_2D, 0, 3, SIZE, SIZE, 0,
105 GL_RGB, GL_UNSIGNED_BYTE, tex2d);
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);
113 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
117 GLubyte tex2d[SIZE][SIZE][3];
118 GLint s, t;
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;
129 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
130 glActiveTextureARB(GL_TEXTURE1_ARB);
131 glBindTexture(GL_TEXTURE_2D, texObj[1]);
133 glTexImage2D(GL_TEXTURE_2D, 0, 3, SIZE, SIZE, 0,
134 GL_RGB, GL_UNSIGNED_BYTE, tex2d);
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);
142 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
145 glActiveTextureARB( GL_TEXTURE0_ARB );
150 glGenBuffersARB(1, &arrayObj);
151 glGenBuffersARB(1, &elementObj);
153 glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrayObj);
154 glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, elementObj);
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);
159 glEnableClientState( GL_VERTEX_ARRAY );
160 glVertexPointer( 4, GL_FLOAT, 0, Offset(&verts, position) );
162 glEnableClientState( GL_COLOR_ARRAY );
163 glColorPointer( 4, GL_UNSIGNED_BYTE, 0, Offset(&verts, color) );
165 glClientActiveTextureARB( GL_TEXTURE0_ARB );
166 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
167 glTexCoordPointer( 2, GL_FLOAT, 0, Offset(&verts, tex0) );
169 glClientActiveTextureARB( GL_TEXTURE1_ARB );
170 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
171 glTexCoordPointer( 2, GL_FLOAT, 0, Offset(&verts, tex1) );
173 glClientActiveTextureARB( GL_TEXTURE0_ARB );
177 static void Reshape(int width, int height)
180 glViewport(0, 0, (GLint)width, (GLint)height);
182 glMatrixMode(GL_PROJECTION);
183 glLoadIdentity();
184 /* glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0); */
185 glMatrixMode(GL_MODELVIEW);
188 static void Key(unsigned char key, int x, int y)
191 switch (key) {
192 case 27:
193 exit(1);
194 default:
195 break;
198 glutPostRedisplay();
201 static void Draw(void)
203 glClear(GL_COLOR_BUFFER_BIT);
205 glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, NULL );
207 glFlush();
209 if (doubleBuffer) {
210 glutSwapBuffers();
214 static GLenum Args(int argc, char **argv)
216 GLint i;
218 doubleBuffer = GL_FALSE;
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;
230 return GL_TRUE;
233 int main(int argc, char **argv)
235 GLenum type;
237 glutInit(&argc, argv);
239 if (Args(argc, argv) == GL_FALSE) {
240 exit(1);
243 glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
245 type = GLUT_RGB | GLUT_ALPHA;
246 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
247 glutInitDisplayMode(type);
249 if (glutCreateWindow(*argv) == GL_FALSE) {
250 exit(1);
253 gladLoadGL();
254 Init();
256 glutReshapeFunc(Reshape);
257 glutKeyboardFunc(Key);
258 glutDisplayFunc(Draw);
259 glutMainLoop();
260 return 0;