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

Last change on this file since 3898 was 2762, checked in by Sam Hocevar, 15 years ago
  • Fix the transparency in the 3D palette viewer.
File size: 4.2 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    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
30    glDisable(GL_CULL_FACE);
31
32    glShadeModel(GL_SMOOTH);
33    glEnable(GL_BLEND);
34    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
35    glEnable(GL_DEPTH_TEST);
36
37    setcamera();
38
39    tbInit(GLUT_LEFT_BUTTON);
40    tbAnimate(GL_FALSE);
41}
42
43static void parsekey(unsigned char key, int x, int y)
44{
45    switch (key)
46    {
47    case 27:
48        exit(0);
49        break;
50    }
51}
52
53static void motion(int x, int y)
54{
55    tbMotion(x, y);
56}
57
58static void mouse(int button, int state, int x, int y)
59{
60    tbMouse(button, state, x, y);
61}
62
63static void reshape(int w, int h)
64{
65    glMatrixMode (GL_MODELVIEW);
66    glViewport (0, 0, w, h);
67    glLoadIdentity();
68
69    w_win = w;
70    h_win = h;
71    setcamera();
72
73    tbReshape(w_win, h_win);
74}
75
76#define F(x) ((x)*1.01 - 0.005)
77#define CP       glColor4f(x0,y0,z0,0.9); glVertex3f(x0,y0,z0)
78#define BP       glColor3f(0.0,0.0,0.0); glVertex3f(F(x0),F(y0),F(z0))
79#define BLACK    x0 = y0 = z0 = 0.0
80#define RED      x0 = 1.0; y0 = z0 = 0.0
81//#define XRED     x0 = 0.8; y0 = z0 = 0.0
82#define XRED RED
83#define GREEN    y0 = 1.0; x0 = z0 = 0.0
84//#define XGREEN   y0 = 0.7; x0 = z0 = 0.0
85#define XGREEN GREEN
86#define BLUE     z0 = 1.0; x0 = y0 = 0.0
87//#define XBLUE    x0 = 0.0; y0 = 0.0; z0 = 0.5
88#define XBLUE BLUE
89#define YELLOW   x0 = y0 = 1.0; z0 = 0.0
90#define CYAN     y0 = z0 = 1.0; x0 = 0.0
91#define MAGENTA  x0 = z0 = 1.0; y0 = 0.0
92#define WHITE    x0 = y0 = z0 = 1.0
93
94static void display(void)
95{
96    float x0,y0,z0;
97
98    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
99
100    setcamera();
101
102    tbMatrix();
103
104    glPushMatrix();
105        glTranslatef(-.5, -.5, -.5);
106        // Bounding box
107        glBegin(GL_LINE_LOOP);
108            BLACK; CP; RED; CP; MAGENTA; CP; BLUE; CP;
109            CYAN; CP; WHITE; CP; YELLOW; CP; GREEN; CP;
110        glEnd();
111        glBegin(GL_LINES);
112            BLACK; CP; BLUE; CP;
113            GREEN; CP; CYAN; CP;
114            RED; CP; YELLOW; CP;
115            MAGENTA; CP; WHITE; CP;
116        glEnd();
117        // Our colour space
118        glBegin(GL_TRIANGLES); XRED; CP; XGREEN; CP; XBLUE; CP; glEnd();
119        glBegin(GL_TRIANGLES); XRED; CP; XGREEN; CP; YELLOW; CP; glEnd();
120        glBegin(GL_TRIANGLES); XRED; CP; XBLUE; CP; WHITE; CP; glEnd();
121        glBegin(GL_TRIANGLES); XRED; CP; YELLOW; CP; WHITE; CP; glEnd();
122        glBegin(GL_TRIANGLES); XBLUE; CP; XGREEN; CP; WHITE; CP; glEnd();
123        glBegin(GL_TRIANGLES); YELLOW; CP; XGREEN; CP; WHITE; CP; glEnd();
124        // Better edges
125        glBegin(GL_LINES);
126            XBLUE; BP; XRED; BP; YELLOW; BP; XRED; BP;
127            XBLUE; BP; XGREEN; BP; YELLOW; BP; XGREEN; BP;
128            XBLUE; BP; WHITE; BP; YELLOW; BP; WHITE; BP;
129            XRED; BP; XGREEN; BP;
130            XGREEN; BP; WHITE; BP;
131            WHITE; BP; XRED; BP;
132        glEnd();
133        glTranslatef(.5, .5, .4);
134        glColor4f(.2, .2, .2, .5);
135        glBegin(GL_QUADS);
136            glVertex3f(0.891063,-0.45388,0);
137            glVertex3f(0.0773943,0.151941,-0.985355);
138            glVertex3f(-0.891063,0.45388,0);
139            glVertex3f(-0.0773943,-0.151941,0.985355);
140        glEnd();
141    glPopMatrix();
142
143    glutSwapBuffers();
144}
145
146int main(int argc, char *argv[])
147{
148    glutInit(&argc, argv);
149    glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE);
150    glutInitWindowPosition(50, 50);
151    glutInitWindowSize(w_win, h_win);
152    glutCreateWindow("FTGL TEST");
153    glutDisplayFunc(display);
154    glutKeyboardFunc(parsekey);
155    glutMouseFunc(mouse);
156    glutMotionFunc(motion);
157    glutReshapeFunc(reshape);
158    glutIdleFunc(display);
159
160    myinit();
161    glutMainLoop();
162
163    return 0;
164}
165
Note: See TracBrowser for help on using the repository browser.