6 vector< pair<int,culooks::window*> > culooks::Windows;
7 pthread_t culooks::glThreadId;
9 int culooks::windowid = 0;
11 int culooks::cube::allid = 0;
15 void culooks::cube::draw()
17 glMatrixMode(GL_MODELVIEW);
20 //cout << zoom << endl;
22 glScalef(zoom, zoom, zoom);
23 glRotatef(az, 0, 1, 0);
24 glRotatef(alt, 1, 0, 0);
28 glVertex2f(-1, -1); glVertex2f(1, -1); glVertex2f(1, 1); glVertex2f(-1, 1);
38 void culooks::cube::drawAll()
40 glMatrixMode(GL_MODELVIEW);
43 glTranslatef(-1,-1,-1);
45 for (int iz=0; iz<l; iz++) {
46 for (int iy=0; iy<l; iy++) {
47 for (int ix=0; ix<l; ix++) {
49 glTranslatef(2.0*ix/l,2.0*iy/l,2.0*iz/l);
50 glScalef(2.0/l,2.0/l,2.0/l);
54 glColor4f(link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 0],
55 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 1],
56 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 2],
57 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 3]);
58 glVertex3f(0,0,0); glVertex3f(1,0,0);
59 glColor4f(link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 0],
60 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 1],
61 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 2],
62 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 3]);
63 glVertex3f(0,0,0); glVertex3f(0,1,0);
64 glColor4f(link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 0],
65 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 1],
66 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 2],
67 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 3]);
68 glVertex3f(0,0,0); glVertex3f(0,0,1);
74 glColor4f(plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 0],
75 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 1],
76 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 2],
77 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 3]);
78 glVertex3f(0, 0, 0); glVertex3f(1, 0, 0); glVertex3f(1, 1, 0); glVertex3f(0, 1, 0);
79 glColor4f(plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 0],
80 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 1],
81 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 2],
82 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 3]);
83 glVertex3f(0, 0, 0); glVertex3f(1, 0, 0); glVertex3f(1, 0, 1); glVertex3f(0, 0, 1);
84 glColor4f(plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 0],
85 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 1],
86 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 2],
87 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 3]);
88 glVertex3f(0, 0, 0); glVertex3f(0, 1, 0); glVertex3f(0, 1, 1); glVertex3f(0, 0, 1);
103 culooks::window* getWin()
105 for (int iwin=0; iwin<culooks::Windows.size(); iwin++)
106 if ( culooks::Windows.at(iwin).first == glutGetWindow() ) {
107 return culooks::Windows.at(iwin).second;
109 cerr << "Something terrible happened: Could not find window-id!" << endl;
113 static int getCubeFromPos(int x, int y)
115 culooks::window *Win = getWin();
117 int col = x / ((float)Win->w / Win->layout[0]);
118 int row = Win->layout[1] - y / ((float)Win->h / Win->layout[1]);
120 return col + row*Win->layout[0];
124 static void motionFunc(int x, int y)
127 culooks::window *Win = getWin();
129 Win->cubes.at(rotcube[0]).az += rotcube[1] - x;
130 Win->cubes.at(rotcube[0]).alt += rotcube[2] - y;
139 static void mouseFunc(int button, int state, int x, int y)
141 culooks::window *Win = getWin();
144 rotcube[0] = getCubeFromPos(x,y);
150 Win->cubes.at(getCubeFromPos(x,y)).zoom *= 1.1;
153 else if (button == 3) {
154 Win->cubes.at(getCubeFromPos(x,y)).zoom *= 0.9;
160 static void reshapeFunc(int w, int h)
162 culooks::window *Win = getWin();
169 neww = Win->aspect*h;
171 else if ( h == Win->h ) {
173 newh = w / Win->aspect;
176 neww = ( pow(Win->aspect,2)*w + Win->aspect*h ) / ( pow(Win->aspect,2) + 1 );
177 newh = ( Win->aspect*w + h ) / ( pow(Win->aspect,2) + 1 );
180 glutReshapeWindow(neww,newh);
181 glViewport(0,0,neww,newh);
187 static void displayFunc()
189 cout << ":" << glutGetWindow() << endl;
191 culooks::window *Win = getWin();
193 glClearColor(0,0,0,0);
194 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
196 glMatrixMode(GL_MODELVIEW);
199 glScalef(1.0/Win->layout[0],1.0/Win->layout[1],1.0);
200 glTranslatef(-Win->layout[0]+1, -Win->layout[1]+1, 0);
202 for (int icube=0; icube < Win->cubes.size(); icube++) {
203 //glScalef(1.0/Env->layout[0],1.0/Env->layout[1],1.0);
204 //glRotatef(2, 1, 0, 0);
205 Win->cubes.at(icube).draw();
206 if ((icube+1)%Win->layout[0] == 0) {
207 glTranslatef(-2*Win->layout[0]+2, 2, 0);
210 glTranslatef(2, 0, 0);
217 static void initWindow(int winid)
219 glutInitWindowSize(culooks::Windows[winid].second->w, culooks::Windows[winid].second->h);
220 glutInitWindowPosition(winid*100,winid*100);
221 glutCreateWindow( ("culooks / " + culooks::Windows[winid].second->name).c_str() );
223 glutDisplayFunc(&mygl::displayFunc);
224 glutReshapeFunc(&mygl::reshapeFunc);
225 glutMotionFunc(&mygl::motionFunc);
226 glutMouseFunc(&mygl::mouseFunc);
228 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
229 glEnable( GL_BLEND );
231 culooks::Windows[winid].first = glutGetWindow();
233 culooks::Windows[winid].second->initialized = true;
236 static void idleFunc_master()
238 for (int iwin=0; iwin < culooks::Windows.size(); iwin++)
239 if (!culooks::Windows[iwin].second->initialized)
243 static void* glutThread(void *_wincon)
245 culooks::wincontext *wincon = (culooks::wincontext *)_wincon;
246 glXMakeCurrent( wincon->gDisplay, wincon->gDrawable, wincon->gContext );
248 glutIdleFunc(&idleFunc_master);
254 culooks::culooks (const char* name, const int& xcubes, const int& ycubes, const int& l, int *argc, char **argv)
256 window *Win = new window;
261 glutInit(argc, argv);
262 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
265 Win->layout[0] = xcubes;
266 Win->layout[1] = ycubes;
267 Win->aspect = (double)xcubes/ycubes;
268 for (int icube=0; icube<xcubes*ycubes; icube++) {
270 Win->cubes.push_back(newCube);
273 if( Win->layout[0] >= Win->layout[1] ) {
275 winsize[1] = (640.0/Win->layout[0])*Win->layout[1];
279 winsize[0] = (640.0/Win->layout[1])*Win->layout[0];
287 Win->initialized = false;
289 Windows.push_back( pair<int,culooks::window*>(0, Win) );
292 wincon.gContext = glXGetCurrentContext();
293 wincon.gDisplay = glXGetCurrentDisplay();
294 wincon.gDrawable = glXGetCurrentDrawable();
296 glXMakeCurrent(0,0,0);
298 pthread_create(&glThreadId, 0, &mygl::glutThread, &wincon);
305 culooks::cube::cube(int _l)
308 link = new float[l*l*l*3 * 4];
309 plaq = new float[l*l*l*3 * 4];
316 for (int i=0; i<l*l*l*3; i++) {
317 link[i*4+0]=1; link[i*4+3]=0.6;
318 plaq[i*4+1]=1; plaq[i*4+3]=0.2;