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
34GLenum doubleBuffer = 1;
35int win;
36GLint list;
37
38
39#define i32to10(x) ((x) >= 0 ? (x & 0x1ff) : 1024-(abs((x))& 0x1ff))
40#define i32to2(x) ((x) >= 0 ? (x & 0x1) : 1-abs((x)))
41
42static unsigned iconv(int x, int y, int z, int w)
43{
44	unsigned val;
45
46	val = i32to10(x);
47	val |= i32to10(y) << 10;
48	val |= i32to10(z) << 20;
49	val |= i32to2(w) << 30;
50	return val;
51}
52#define conv(x,y,z,w) (((x) & 0x3ff) | ((y) & 0x3ff) << 10 | ((z) & 0x3ff)<< 20 | ((w) & 0x3) << 30)
53
54static void Init(void)
55{
56   fprintf(stderr, "GL_RENDERER   = %s\n", (char *) glGetString(GL_RENDERER));
57   fprintf(stderr, "GL_VERSION    = %s\n", (char *) glGetString(GL_VERSION));
58   fprintf(stderr, "GL_VENDOR     = %s\n", (char *) glGetString(GL_VENDOR));
59   fflush(stderr);
60
61#ifndef GL_ARB_vertex_type_2_10_10_10_rev
62   fprintf(stderr,"built without ARB_vertex_type_2_10_10_10_rev\n");
63   exit(1);
64#endif
65
66   if (!glutExtensionSupported("GL_ARB_vertex_type_2_10_10_10_rev")){
67     fprintf(stderr,"requires ARB_vertex_type_2_10_10_10_rev\n");
68     exit(1);
69   }
70
71
72   list = glGenLists(1);
73   glNewList(list, GL_COMPILE);
74   glBegin(GL_TRIANGLES);
75#ifdef GL_ARB_vertex_type_2_10_10_10_rev
76   glColorP3ui(GL_UNSIGNED_INT_2_10_10_10_REV, conv(820, 0, 0, 0));
77   glVertexP3ui(GL_INT_2_10_10_10_REV, iconv(-90, -90, -30, 0));
78   glColorP3ui(GL_UNSIGNED_INT_2_10_10_10_REV, conv(0, 921, 0, 0));
79   glVertexP3ui(GL_INT_2_10_10_10_REV, iconv(90, -90, -30, 0));
80   glColorP3ui(GL_UNSIGNED_INT_2_10_10_10_REV, conv(0, 0, 716, 0));
81   glVertexP3ui(GL_INT_2_10_10_10_REV, iconv(0, 90, -30, 0));
82#endif /* GL_ARB_vertex_type_2_10_10_10_rev */
83   glEnd();
84   glEndList();
85
86   glClearColor(0.3, 0.1, 0.3, 0.0);
87
88}
89
90static void Reshape(int width, int height)
91{
92
93   glViewport(0, 0, (GLint)width, (GLint)height);
94
95   glMatrixMode(GL_PROJECTION);
96   glLoadIdentity();
97   glOrtho(-100.0, 100.0, -100.0, 100.0, -50.0, 1000.0);
98   glMatrixMode(GL_MODELVIEW);
99}
100
101static void Key(unsigned char key, int x, int y)
102{
103   switch (key) {
104      case 27:
105         glutDestroyWindow(win);
106         exit(0);
107      default:
108         glutPostRedisplay();
109         return;
110   }
111}
112
113
114static void Draw(void)
115{
116   glClear(GL_COLOR_BUFFER_BIT);
117
118   glRotatef(45,0,0,1);
119   glCallList(list);
120
121   glFlush();
122
123   if (doubleBuffer) {
124      glutSwapBuffers();
125   }
126}
127
128static GLenum Args(int argc, char **argv)
129{
130   GLint i;
131
132   for (i = 1; i < argc; i++) {
133      if (strcmp(argv[i], "-sb") == 0) {
134         doubleBuffer = GL_FALSE;
135      } else if (strcmp(argv[i], "-db") == 0) {
136         doubleBuffer = GL_TRUE;
137      } else {
138         fprintf(stderr, "%s (Bad option).\n", argv[i]);
139         return GL_FALSE;
140      }
141   }
142   return GL_TRUE;
143}
144
145int main(int argc, char **argv)
146{
147   GLenum type;
148
149   glutInit(&argc, argv);
150
151   if (Args(argc, argv) == GL_FALSE) {
152      exit(1);
153   }
154
155   glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
156
157   type = GLUT_RGB | GLUT_ALPHA;
158   type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
159   glutInitDisplayMode(type);
160
161   win = glutCreateWindow(*argv);
162   if (!win) {
163      exit(1);
164   }
165
166   glewInit();
167   Init();
168
169   glutReshapeFunc(Reshape);
170   glutKeyboardFunc(Key);
171   glutDisplayFunc(Draw);
172   glutMainLoop();
173   return 0;
174}
175