vbo-drawarrays-2101010: add bgra command line option
[mesa-demos.git] / src / trivial / tri-2101010.c
blob1945b29fbc9864ac2d88c88626960f6b3cae7998
1 /*
2 * Copyright (c) 2011 David Airlie
4 * Based on tri.c which is:
5 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the name of
11 * Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
16 * ANY KIND,
17 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
20 * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
21 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
22 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
23 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
24 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25 * OF THIS SOFTWARE.
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <GL/glew.h>
32 #include "glut_wrap.h"
34 GLenum doubleBuffer = 1;
35 int win;
37 static void Init(void)
39 fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
40 fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
41 fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
42 fflush(stderr);
44 #ifndef GL_ARB_vertex_type_2_10_10_10_rev
45 fprintf(stderr,"built without ARB_vertex_type_2_10_10_10_rev\n");
46 exit(1);
47 #endif
49 if (!glutExtensionSupported("GL_ARB_vertex_type_2_10_10_10_rev")){
50 fprintf(stderr,"requires ARB_vertex_type_2_10_10_10_rev\n");
51 exit(1);
54 glClearColor(0.3, 0.1, 0.3, 0.0);
57 static void Reshape(int width, int height)
60 glViewport(0, 0, (GLint)width, (GLint)height);
62 glMatrixMode(GL_PROJECTION);
63 glLoadIdentity();
64 glOrtho(-100.0, 100.0, -100.0, 100.0, -50.0, 1000.0);
65 glMatrixMode(GL_MODELVIEW);
68 static void Key(unsigned char key, int x, int y)
70 switch (key) {
71 case 27:
72 glutDestroyWindow(win);
73 exit(0);
74 default:
75 glutPostRedisplay();
76 return;
80 #define i32to10(x) ((x) >= 0 ? (x & 0x1ff) : 1024-(abs((x))& 0x1ff))
81 #define i32to2(x) ((x) >= 0 ? (x & 0x1) : 1-abs((x)))
83 static unsigned iconv(int x, int y, int z, int w)
85 unsigned val;
87 val = i32to10(x);
88 val |= i32to10(y) << 10;
89 val |= i32to10(z) << 20;
90 val |= i32to2(w) << 30;
91 return val;
93 #define conv(x,y,z,w) (((x) & 0x3ff) | ((y) & 0x3ff) << 10 | ((z) & 0x3ff)<< 20 | ((w) & 0x3) << 30)
95 static void Draw(void)
97 glClear(GL_COLOR_BUFFER_BIT);
99 #ifdef GL_ARB_vertex_type_2_10_10_10_rev
100 glBegin(GL_TRIANGLES);
101 glColorP3ui(GL_UNSIGNED_INT_2_10_10_10_REV, conv(820, 0, 0, 0));
102 glVertexP3ui(GL_INT_2_10_10_10_REV, iconv(-90, -90, -30, 0));
103 glColorP3ui(GL_UNSIGNED_INT_2_10_10_10_REV, conv(0, 921, 0, 0));
104 glVertexP3ui(GL_INT_2_10_10_10_REV, iconv(90, -90, -30, 0));
105 glColorP3ui(GL_UNSIGNED_INT_2_10_10_10_REV, conv(0, 0, 716, 0));
106 glVertexP3ui(GL_INT_2_10_10_10_REV, iconv(0, 90, -30, 0));
107 glEnd();
108 #endif /* GL_ARB_vertex_type_2_10_10_10_rev */
110 glFlush();
112 if (doubleBuffer) {
113 glutSwapBuffers();
117 static GLenum Args(int argc, char **argv)
119 GLint i;
121 for (i = 1; i < argc; i++) {
122 if (strcmp(argv[i], "-sb") == 0) {
123 doubleBuffer = GL_FALSE;
124 } else if (strcmp(argv[i], "-db") == 0) {
125 doubleBuffer = GL_TRUE;
126 } else {
127 fprintf(stderr, "%s (Bad option).\n", argv[i]);
128 return GL_FALSE;
131 return GL_TRUE;
134 int main(int argc, char **argv)
136 GLenum type;
138 glutInit(&argc, argv);
140 if (Args(argc, argv) == GL_FALSE) {
141 exit(1);
144 glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
146 type = GLUT_RGB | GLUT_ALPHA;
147 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
148 glutInitDisplayMode(type);
150 win = glutCreateWindow(*argv);
151 if (!win) {
152 exit(1);
155 glewInit();
156 Init();
158 glutReshapeFunc(Reshape);
159 glutKeyboardFunc(Key);
160 glutDisplayFunc(Draw);
161 glutMainLoop();
162 return 0;