5 vector< pair<int,culooks::window*> > culooks::Windows;
6 pthread_t culooks::glThreadId;
8 int culooks::windowid = 0;
10 int culooks::cube::allid = 0;
14 void culooks::cube::draw()
16 glMatrixMode(GL_MODELVIEW);
19 //cout << zoom << endl;
21 glScalef(zoom, zoom, zoom);
22 glRotatef(az, 0, 1, 0);
23 glRotatef(alt, 1, 0, 0);
27 glVertex2f(-1, -1); glVertex2f(1, -1); glVertex2f(1, 1); glVertex2f(-1, 1);
37 void culooks::cube::drawAll()
39 glMatrixMode(GL_MODELVIEW);
42 glTranslatef(-1,-1,-1);
44 for (int iz=0; iz<l; iz++) {
45 for (int iy=0; iy<l; iy++) {
46 for (int ix=0; ix<l; ix++) {
48 glTranslatef(2.0*ix/l,2.0*iy/l,2.0*iz/l);
49 glScalef(2.0/l,2.0/l,2.0/l);
53 glColor4f(link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 0],
54 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 1],
55 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 2],
56 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 3]);
57 glVertex3f(0,0,0); glVertex3f(1,0,0);
58 glColor4f(link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 0],
59 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 1],
60 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 2],
61 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 3]);
62 glVertex3f(0,0,0); glVertex3f(0,1,0);
63 glColor4f(link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 0],
64 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 1],
65 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 2],
66 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 3]);
67 glVertex3f(0,0,0); glVertex3f(0,0,1);
73 glColor4f(plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 0],
74 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 1],
75 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 2],
76 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 3]);
77 glVertex3f(0, 0, 0); glVertex3f(1, 0, 0); glVertex3f(1, 1, 0); glVertex3f(0, 1, 0);
78 glColor4f(plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 0],
79 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 1],
80 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 2],
81 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 3]);
82 glVertex3f(0, 0, 0); glVertex3f(1, 0, 0); glVertex3f(1, 0, 1); glVertex3f(0, 0, 1);
83 glColor4f(plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 0],
84 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 1],
85 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 2],
86 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 3]);
87 glVertex3f(0, 0, 0); glVertex3f(0, 1, 0); glVertex3f(0, 1, 1); glVertex3f(0, 0, 1);
102 culooks::window* getWin()
104 for (int iwin=0; iwin<culooks::Windows.size(); iwin++)
105 if ( culooks::Windows.at(iwin).first == glutGetWindow() ) {
106 return culooks::Windows.at(iwin).second;
108 //cerr << "Something terrible happened: Could not find window-id!" << endl;
112 static int getCubeFromPos(int x, int y)
114 culooks::window *Win = getWin();
116 int col = x / ((float)Win->w / Win->layout[0]);
117 int row = Win->layout[1] - y / ((float)Win->h / Win->layout[1]);
119 return col + row*Win->layout[0];
123 static void motionFunc(int x, int y)
126 culooks::window *Win = getWin();
128 Win->cubes.at(rotcube[0]).az += rotcube[1] - x;
129 Win->cubes.at(rotcube[0]).alt += rotcube[2] - y;
138 static void mouseFunc(int button, int state, int x, int y)
140 culooks::window *Win = getWin();
143 rotcube[0] = getCubeFromPos(x,y);
149 Win->cubes.at(getCubeFromPos(x,y)).zoom *= 1.1;
152 else if (button == 3) {
153 Win->cubes.at(getCubeFromPos(x,y)).zoom *= 0.9;
159 static void reshapeFunc(int w, int h)
161 culooks::window *Win = getWin();
168 neww = Win->aspect*h;
170 else if ( h == Win->h ) {
172 newh = w / Win->aspect;
175 neww = ( pow(Win->aspect,2)*w + Win->aspect*h ) / ( pow(Win->aspect,2) + 1 );
176 newh = ( Win->aspect*w + h ) / ( pow(Win->aspect,2) + 1 );
179 glutReshapeWindow(neww,newh);
180 glViewport(0,0,neww,newh);
186 static void displayFunc()
188 //cout << ":" << glutGetWindow() << endl;
190 culooks::window *Win = getWin();
192 glClearColor(0,0,0,0);
193 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
195 glMatrixMode(GL_MODELVIEW);
198 glScalef(1.0/Win->layout[0],1.0/Win->layout[1],1.0);
199 glTranslatef(-Win->layout[0]+1, -Win->layout[1]+1, 0);
201 for (int icube=0; icube < Win->cubes.size(); icube++) {
202 //glScalef(1.0/Env->layout[0],1.0/Env->layout[1],1.0);
203 //glRotatef(2, 1, 0, 0);
204 Win->cubes.at(icube).draw();
205 if ((icube+1)%Win->layout[0] == 0) {
206 glTranslatef(-2*Win->layout[0]+2, 2, 0);
209 glTranslatef(2, 0, 0);
216 static void initWindow(int winid)
218 glutInitWindowSize(culooks::Windows[winid].second->w, culooks::Windows[winid].second->h);
219 glutInitWindowPosition(winid*100,winid*100);
220 glutCreateWindow( ("culooks / " + culooks::Windows[winid].second->name).c_str() );
222 glutDisplayFunc(&mygl::displayFunc);
223 glutReshapeFunc(&mygl::reshapeFunc);
224 glutMotionFunc(&mygl::motionFunc);
225 glutMouseFunc(&mygl::mouseFunc);
227 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
228 glEnable( GL_BLEND );
230 culooks::Windows[winid].first = glutGetWindow();
232 culooks::Windows[winid].second->initialized = true;
235 static void idleFunc_master()
237 for (int iwin=0; iwin < culooks::Windows.size(); iwin++)
238 if (!culooks::Windows[iwin].second->initialized)
242 static void* glutThread(void *_wincon)
244 culooks::wincontext *wincon = (culooks::wincontext *)_wincon;
245 glXMakeCurrent( wincon->gDisplay, wincon->gDrawable, wincon->gContext );
247 glutIdleFunc(&idleFunc_master);
253 culooks::culooks (const char* name, const int& xcubes, const int& ycubes, const int& l, int *argc, char **argv)
255 window *Win = new window;
260 glutInit(argc, argv);
261 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
264 Win->layout[0] = xcubes;
265 Win->layout[1] = ycubes;
266 Win->aspect = (double)xcubes/ycubes;
267 for (int icube=0; icube<xcubes*ycubes; icube++) {
269 Win->cubes.push_back(newCube);
272 if( Win->layout[0] >= Win->layout[1] ) {
274 winsize[1] = (640.0/Win->layout[0])*Win->layout[1];
278 winsize[0] = (640.0/Win->layout[1])*Win->layout[0];
286 Win->initialized = false;
288 Windows.push_back( pair<int,culooks::window*>(0, Win) );
291 wincon.gContext = glXGetCurrentContext();
292 wincon.gDisplay = glXGetCurrentDisplay();
293 wincon.gDrawable = glXGetCurrentDrawable();
295 glXMakeCurrent(0,0,0);
297 pthread_create(&glThreadId, 0, &mygl::glutThread, &wincon);
304 culooks::cube::cube(int _l)
307 link = new float[l*l*l*3 * 4];
308 plaq = new float[l*l*l*3 * 4];
315 for (int i=0; i<l*l*l*3; i++) {
316 link[i*4+0]=1; link[i*4+3]=0.6;
317 plaq[i*4+1]=1; plaq[i*4+3]=0.2;