1 /*
2  * Copyright (c) 2011 David Airlie
3  *
4  * Based on tri.c which is:
5  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
6  *
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.
14  *
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.
19  *
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.
26  */
27 
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <GL/glew.h>
32 #include "glut_wrap.h"
33 
34 GLenum doubleBuffer = 1;
35 int win;
36 
Init(void)37 static void Init(void)
38 {
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);
43 
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
48 
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);
52    }
53 
54    glClearColor(0.3, 0.1, 0.3, 0.0);
55 }
56 
Reshape(int width,int height)57 static void Reshape(int width, int height)
58 {
59 
60    glViewport(0, 0, (GLint)width, (GLint)height);
61 
62    glMatrixMode(GL_PROJECTION);
63    glLoadIdentity();
64    glOrtho(-100.0, 100.0, -100.0, 100.0, -50.0, 1000.0);
65    glMatrixMode(GL_MODELVIEW);
66 }
67 
Key(unsigned char key,int x,int y)68 static void Key(unsigned char key, int x, int y)
69 {
70    switch (key) {
71       case 27:
72          glutDestroyWindow(win);
73          exit(0);
74       default:
75          glutPostRedisplay();
76          return;
77    }
78 }
79 
80 #define i32to10(x) ((x) >= 0 ? (x & 0x1ff) : 1024-(abs((x))& 0x1ff))
81 #define i32to2(x) ((x) >= 0 ? (x & 0x1) : 1-abs((x)))
82 
iconv(int x,int y,int z,int w)83 static unsigned iconv(int x, int y, int z, int w)
84 {
85 	unsigned val;
86 
87 	val = i32to10(x);
88 	val |= i32to10(y) << 10;
89 	val |= i32to10(z) << 20;
90 	val |= i32to2(w) << 30;
91 	return val;
92 }
93 #define conv(x,y,z,w) (((x) & 0x3ff) | ((y) & 0x3ff) << 10 | ((z) & 0x3ff)<< 20 | ((w) & 0x3) << 30)
94 
Draw(void)95 static void Draw(void)
96 {
97    glClear(GL_COLOR_BUFFER_BIT);
98 
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 */
109 
110    glFlush();
111 
112    if (doubleBuffer) {
113       glutSwapBuffers();
114    }
115 }
116 
Args(int argc,char ** argv)117 static GLenum Args(int argc, char **argv)
118 {
119    GLint i;
120 
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;
129       }
130    }
131    return GL_TRUE;
132 }
133 
main(int argc,char ** argv)134 int main(int argc, char **argv)
135 {
136    GLenum type;
137 
138    glutInit(&argc, argv);
139 
140    if (Args(argc, argv) == GL_FALSE) {
141       exit(1);
142    }
143 
144    glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
145 
146    type = GLUT_RGB | GLUT_ALPHA;
147    type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
148    glutInitDisplayMode(type);
149 
150    win = glutCreateWindow(*argv);
151    if (!win) {
152       exit(1);
153    }
154 
155    glewInit();
156    Init();
157 
158    glutReshapeFunc(Reshape);
159    glutKeyboardFunc(Key);
160    glutDisplayFunc(Draw);
161    glutMainLoop();
162    return 0;
163 }
164