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

Last change on this file since 2677 was 2677, checked in by Sam Hocevar, 12 years ago
  • Test stuff for the Rubik's cube colour reduction.
File size: 3.7 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(y0,x0,z0)
74#define BP       glColor3f(0.0,0.0,0.0); glVertex3f(F(y0),F(x0),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    glPopMatrix();
130
131    glutSwapBuffers();
132}
133
134int main(int argc, char *argv[])
135{
136    glutInit(&argc, argv);
137    glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE);
138    glutInitWindowPosition(50, 50);
139    glutInitWindowSize(w_win, h_win);
140    glutCreateWindow("FTGL TEST");
141    glutDisplayFunc(display);
142    glutKeyboardFunc(parsekey);
143    glutMouseFunc(mouse);
144    glutMotionFunc(motion);
145    glutReshapeFunc(reshape);
146    glutIdleFunc(display);
147
148    myinit();
149    glutMainLoop();
150
151    return 0;
152}
153
Note: See TracBrowser for help on using the repository browser.