132001f49Smrg/*
232001f49Smrg * Copyright (c) 2011 David Airlie
332001f49Smrg *
432001f49Smrg * Based on tri.c which is:
532001f49Smrg * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
632001f49Smrg *
732001f49Smrg * Permission to use, copy, modify, distribute, and sell this software and
832001f49Smrg * its documentation for any purpose is hereby granted without fee, provided
932001f49Smrg * that (i) the above copyright notices and this permission notice appear in
1032001f49Smrg * all copies of the software and related documentation, and (ii) the name of
1132001f49Smrg * Silicon Graphics may not be used in any advertising or
1232001f49Smrg * publicity relating to the software without the specific, prior written
1332001f49Smrg * permission of Silicon Graphics.
1432001f49Smrg *
1532001f49Smrg * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
1632001f49Smrg * ANY KIND,
1732001f49Smrg * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
1832001f49Smrg * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
1932001f49Smrg *
2032001f49Smrg * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
2132001f49Smrg * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
2232001f49Smrg * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
2332001f49Smrg * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
2432001f49Smrg * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
2532001f49Smrg * OF THIS SOFTWARE.
2632001f49Smrg */
2732001f49Smrg
2832001f49Smrg#include <stdio.h>
2932001f49Smrg#include <string.h>
3032001f49Smrg#include <stdlib.h>
3132001f49Smrg#include <GL/glew.h>
3232001f49Smrg#include "glut_wrap.h"
3332001f49Smrg
3432001f49SmrgGLenum doubleBuffer = 1;
3532001f49Smrgint win;
3632001f49Smrg
3732001f49Smrgstatic void Init(void)
3832001f49Smrg{
3932001f49Smrg   fprintf(stderr, "GL_RENDERER   = %s\n", (char *) glGetString(GL_RENDERER));
4032001f49Smrg   fprintf(stderr, "GL_VERSION    = %s\n", (char *) glGetString(GL_VERSION));
4132001f49Smrg   fprintf(stderr, "GL_VENDOR     = %s\n", (char *) glGetString(GL_VENDOR));
4232001f49Smrg   fflush(stderr);
4332001f49Smrg
4432001f49Smrg#ifndef GL_ARB_vertex_type_2_10_10_10_rev
4532001f49Smrg   fprintf(stderr,"built without ARB_vertex_type_2_10_10_10_rev\n");
4632001f49Smrg   exit(1);
4732001f49Smrg#endif
4832001f49Smrg
4932001f49Smrg   if (!glutExtensionSupported("GL_ARB_vertex_type_2_10_10_10_rev")){
5032001f49Smrg     fprintf(stderr,"requires ARB_vertex_type_2_10_10_10_rev\n");
5132001f49Smrg     exit(1);
5232001f49Smrg   }
5332001f49Smrg
5432001f49Smrg   glClearColor(0.3, 0.1, 0.3, 0.0);
5532001f49Smrg}
5632001f49Smrg
5732001f49Smrgstatic void Reshape(int width, int height)
5832001f49Smrg{
5932001f49Smrg
6032001f49Smrg   glViewport(0, 0, (GLint)width, (GLint)height);
6132001f49Smrg
6232001f49Smrg   glMatrixMode(GL_PROJECTION);
6332001f49Smrg   glLoadIdentity();
6432001f49Smrg   glOrtho(-100.0, 100.0, -100.0, 100.0, -50.0, 1000.0);
6532001f49Smrg   glMatrixMode(GL_MODELVIEW);
6632001f49Smrg}
6732001f49Smrg
6832001f49Smrgstatic void Key(unsigned char key, int x, int y)
6932001f49Smrg{
7032001f49Smrg   switch (key) {
7132001f49Smrg      case 27:
7232001f49Smrg         glutDestroyWindow(win);
7332001f49Smrg         exit(0);
7432001f49Smrg      default:
7532001f49Smrg         glutPostRedisplay();
7632001f49Smrg         return;
7732001f49Smrg   }
7832001f49Smrg}
7932001f49Smrg
8032001f49Smrg#define i32to10(x) ((x) >= 0 ? (x & 0x1ff) : 1024-(abs((x))& 0x1ff))
8132001f49Smrg#define i32to2(x) ((x) >= 0 ? (x & 0x1) : 1-abs((x)))
8232001f49Smrg
8332001f49Smrgstatic unsigned iconv(int x, int y, int z, int w)
8432001f49Smrg{
8532001f49Smrg	unsigned val;
8632001f49Smrg
8732001f49Smrg	val = i32to10(x);
8832001f49Smrg	val |= i32to10(y) << 10;
8932001f49Smrg	val |= i32to10(z) << 20;
9032001f49Smrg	val |= i32to2(w) << 30;
9132001f49Smrg	return val;
9232001f49Smrg}
9332001f49Smrg#define conv(x,y,z,w) (((x) & 0x3ff) | ((y) & 0x3ff) << 10 | ((z) & 0x3ff)<< 20 | ((w) & 0x3) << 30)
9432001f49Smrg
9532001f49Smrgstatic void Draw(void)
9632001f49Smrg{
9732001f49Smrg   glClear(GL_COLOR_BUFFER_BIT);
9832001f49Smrg
9932001f49Smrg#ifdef GL_ARB_vertex_type_2_10_10_10_rev
10032001f49Smrg   glBegin(GL_TRIANGLES);
10132001f49Smrg   glColorP3ui(GL_UNSIGNED_INT_2_10_10_10_REV, conv(820, 0, 0, 0));
10232001f49Smrg   glVertexP3ui(GL_INT_2_10_10_10_REV, iconv(-90, -90, -30, 0));
10332001f49Smrg   glColorP3ui(GL_UNSIGNED_INT_2_10_10_10_REV, conv(0, 921, 0, 0));
10432001f49Smrg   glVertexP3ui(GL_INT_2_10_10_10_REV, iconv(90, -90, -30, 0));
10532001f49Smrg   glColorP3ui(GL_UNSIGNED_INT_2_10_10_10_REV, conv(0, 0, 716, 0));
10632001f49Smrg   glVertexP3ui(GL_INT_2_10_10_10_REV, iconv(0, 90, -30, 0));
10732001f49Smrg   glEnd();
10832001f49Smrg#endif /* GL_ARB_vertex_type_2_10_10_10_rev */
10932001f49Smrg
11032001f49Smrg   glFlush();
11132001f49Smrg
11232001f49Smrg   if (doubleBuffer) {
11332001f49Smrg      glutSwapBuffers();
11432001f49Smrg   }
11532001f49Smrg}
11632001f49Smrg
11732001f49Smrgstatic GLenum Args(int argc, char **argv)
11832001f49Smrg{
11932001f49Smrg   GLint i;
12032001f49Smrg
12132001f49Smrg   for (i = 1; i < argc; i++) {
12232001f49Smrg      if (strcmp(argv[i], "-sb") == 0) {
12332001f49Smrg         doubleBuffer = GL_FALSE;
12432001f49Smrg      } else if (strcmp(argv[i], "-db") == 0) {
12532001f49Smrg         doubleBuffer = GL_TRUE;
12632001f49Smrg      } else {
12732001f49Smrg         fprintf(stderr, "%s (Bad option).\n", argv[i]);
12832001f49Smrg         return GL_FALSE;
12932001f49Smrg      }
13032001f49Smrg   }
13132001f49Smrg   return GL_TRUE;
13232001f49Smrg}
13332001f49Smrg
13432001f49Smrgint main(int argc, char **argv)
13532001f49Smrg{
13632001f49Smrg   GLenum type;
13732001f49Smrg
13832001f49Smrg   glutInit(&argc, argv);
13932001f49Smrg
14032001f49Smrg   if (Args(argc, argv) == GL_FALSE) {
14132001f49Smrg      exit(1);
14232001f49Smrg   }
14332001f49Smrg
14432001f49Smrg   glutInitWindowPosition(0, 0); glutInitWindowSize( 250, 250);
14532001f49Smrg
14632001f49Smrg   type = GLUT_RGB | GLUT_ALPHA;
14732001f49Smrg   type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
14832001f49Smrg   glutInitDisplayMode(type);
14932001f49Smrg
15032001f49Smrg   win = glutCreateWindow(*argv);
15132001f49Smrg   if (!win) {
15232001f49Smrg      exit(1);
15332001f49Smrg   }
15432001f49Smrg
15532001f49Smrg   glewInit();
15632001f49Smrg   Init();
15732001f49Smrg
15832001f49Smrg   glutReshapeFunc(Reshape);
15932001f49Smrg   glutKeyboardFunc(Key);
16032001f49Smrg   glutDisplayFunc(Draw);
16132001f49Smrg   glutMainLoop();
16232001f49Smrg   return 0;
16332001f49Smrg}
164