source: research/2008-rubik/colorcube/visu.c @ 2723

Last change on this file since 2723 was 2723, checked in by Sam Hocevar, 12 years ago
  • Fix OpenGL orientation (it's supposed to be right-handed).
  • Add a grayscale plane to illustrate non-orthogonality.
File size: 4.0 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4
5#include <GL/gl.h>
6#include <GL/glut.h>
7
8#include "tb.h"
9
10int w_win = 640;
11int h_win = 480;
12
13static void setcamera(void)
14{
15    glMatrixMode (GL_PROJECTION);
16    glLoadIdentity ();
17    gluPerspective(40, (float)w_win / (float)h_win, 1, 1000);
18    glMatrixMode(GL_MODELVIEW);
19    glLoadIdentity();
20    gluLookAt(0.0, 0.0, (float)h_win / 200.0f, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
21}
22
23static void myinit(void)
24{
25    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
26    glClearColor(0.5, 0.5, 0.7, 0.0);
27    glColor3f(1.0, 1.0, 1.0);
28
29    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
30    glEnable(GL_DEPTH_TEST);
31    glShadeModel(GL_SMOOTH);
32
33    setcamera();
34
35    tbInit(GLUT_LEFT_BUTTON);
36    tbAnimate(GL_FALSE);
37}
38
39static void parsekey(unsigned char key, int x, int y)
40{
41    switch (key)
42    {
43    case 27:
44        exit(0);
45        break;
46    }
47}
48
49static void motion(int x, int y)
50{
51    tbMotion(x, y);
52}
53
54static void mouse(int button, int state, int x, int y)
55{
56    tbMouse(button, state, x, y);
57}
58
59static void reshape(int w, int h)
60{
61    glMatrixMode (GL_MODELVIEW);
62    glViewport (0, 0, w, h);
63    glLoadIdentity();
64
65    w_win = w;
66    h_win = h;
67    setcamera();
68
69    tbReshape(w_win, h_win);
70}
71
72#define F(x) ((x)*1.01 - 0.005)
73#define CP       glColor4f(x0,y0,z0,0.5); glVertex3f(x0,y0,z0)
74#define BP       glColor3f(0.0,0.0,0.0); glVertex3f(F(x0),F(y0),F(z0))
75#define BLACK    x0 = y0 = z0 = 0.0
76#define RED      x0 = 1.0; y0 = z0 = 0.0
77//#define XRED     x0 = 0.8; y0 = z0 = 0.0
78#define XRED RED
79#define GREEN    y0 = 1.0; x0 = z0 = 0.0
80//#define XGREEN   y0 = 0.7; x0 = z0 = 0.0
81#define XGREEN GREEN
82#define BLUE     z0 = 1.0; x0 = y0 = 0.0
83//#define XBLUE    x0 = 0.0; y0 = 0.0; z0 = 0.5
84#define XBLUE BLUE
85#define YELLOW   x0 = y0 = 1.0; z0 = 0.0
86#define CYAN     y0 = z0 = 1.0; x0 = 0.0
87#define MAGENTA  x0 = z0 = 1.0; y0 = 0.0
88#define WHITE    x0 = y0 = z0 = 1.0
89
90static void display(void)
91{
92    float x0,y0,z0;
93
94    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
95
96    setcamera();
97
98    tbMatrix();
99
100    glPushMatrix();
101        glTranslatef(-.5, -.5, -.5);
102        // Bounding box
103        glBegin(GL_LINE_LOOP);
104            BLACK; CP; RED; CP; MAGENTA; CP; BLUE; CP;
105            CYAN; CP; WHITE; CP; YELLOW; CP; GREEN; CP;
106        glEnd();
107        glBegin(GL_LINES);
108            BLACK; CP; BLUE; CP;
109            GREEN; CP; CYAN; CP;
110            RED; CP; YELLOW; CP;
111            MAGENTA; CP; WHITE; CP;
112        glEnd();
113        // Our colour space
114        glBegin(GL_TRIANGLES); XRED; CP; XGREEN; CP; XBLUE; CP; glEnd();
115        glBegin(GL_TRIANGLES); XRED; CP; XGREEN; CP; YELLOW; CP; glEnd();
116        glBegin(GL_TRIANGLES); XRED; CP; XBLUE; CP; WHITE; CP; glEnd();
117        glBegin(GL_TRIANGLES); XRED; CP; YELLOW; CP; WHITE; CP; glEnd();
118        glBegin(GL_TRIANGLES); XBLUE; CP; XGREEN; CP; WHITE; CP; glEnd();
119        glBegin(GL_TRIANGLES); YELLOW; CP; XGREEN; CP; WHITE; CP; glEnd();
120        // Better edges
121        glBegin(GL_LINES);
122            XBLUE; BP; XRED; BP; YELLOW; BP; XRED; BP;
123            XBLUE; BP; XGREEN; BP; YELLOW; BP; XGREEN; BP;
124            XBLUE; BP; WHITE; BP; YELLOW; BP; WHITE; BP;
125            XRED; BP; XGREEN; BP;
126            XGREEN; BP; WHITE; BP;
127            WHITE; BP; XRED; BP;
128        glEnd();
129        glTranslatef(.5, .5, .4);
130        glColor4f(.5, .5, .5, .5);
131        glBegin(GL_QUADS);
132            glVertex3f(0.891063,-0.45388,0);
133            glVertex3f(0.0773943,0.151941,-0.985355);
134            glVertex3f(-0.891063,0.45388,0);
135            glVertex3f(-0.0773943,-0.151941,0.985355);
136        glEnd();
137    glPopMatrix();
138
139    glutSwapBuffers();
140}
141
142int main(int argc, char *argv[])
143{
144    glutInit(&argc, argv);
145    glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE);
146    glutInitWindowPosition(50, 50);
147    glutInitWindowSize(w_win, h_win);
148    glutCreateWindow("FTGL TEST");
149    glutDisplayFunc(display);
150    glutKeyboardFunc(parsekey);
151    glutMouseFunc(mouse);
152    glutMotionFunc(motion);
153    glutReshapeFunc(reshape);
154    glutIdleFunc(display);
155
156    myinit();
157    glutMainLoop();
158
159    return 0;
160}
161
Note: See TracBrowser for help on using the repository browser.