8 vector< pair <unsigned long int,cubelooks::env*> > Envs;
10 bool cubelooks::initDone = false;
11 GLXDrawable cubelooks::gDrawable;
12 Display *cubelooks::gDisplay;
14 int cubelooks::cube::allid = 0;
16 void cubelooks::cube::drawBox()
20 glVertex3f(-1.0f, 1.0f, -1.0f);
21 glVertex3f( 1.0f, 1.0f, -1.0f);
22 glVertex3f(-1.0f, -1.0f, -1.0f);
23 glVertex3f( 1.0f, -1.0f, -1.0f);
24 glVertex3f(-1.0f, 1.0f, 1.0f);
25 glVertex3f( 1.0f, 1.0f, 1.0f);
26 glVertex3f(-1.0f, -1.0f, 1.0f);
27 glVertex3f( 1.0f, -1.0f, 1.0f);
29 glVertex3f( 1.0f, 1.0f, -1.0f);
30 glVertex3f( 1.0f, -1.0f, -1.0f);
31 glVertex3f( -1.0f, 1.0f, -1.0f);
32 glVertex3f( -1.0f, -1.0f, -1.0f);
33 glVertex3f( 1.0f, 1.0f, 1.0f);
34 glVertex3f( 1.0f, -1.0f, 1.0f);
35 glVertex3f( -1.0f, 1.0f, 1.0f);
36 glVertex3f( -1.0f, -1.0f, 1.0f);
38 glVertex3f( -1.0f, -1.0f, 1.0f);
39 glVertex3f( -1.0f, -1.0f, -1.0f);
40 glVertex3f( -1.0f, 1.0f, 1.0f);
41 glVertex3f( -1.0f, 1.0f, -1.0f);
42 glVertex3f( 1.0f, -1.0f, 1.0f);
43 glVertex3f( 1.0f, -1.0f, -1.0f);
44 glVertex3f( 1.0f, 1.0f, 1.0f);
45 glVertex3f( 1.0f, 1.0f, -1.0f);
50 void cubelooks::cube::drawAll()
52 glMatrixMode(GL_MODELVIEW);
55 glTranslatef(-1,-1,-1);
57 for (int iz=0; iz<l; iz++) {
58 for (int iy=0; iy<l; iy++) {
59 for (int ix=0; ix<l; ix++) {
61 glTranslatef(2.0*ix/l,2.0*iy/l,2.0*iz/l);
62 glScalef(2.0/l,2.0/l,2.0/l);
66 glColor4f(link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 0],
67 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 1],
68 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 2],
69 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 3]);
70 glVertex3f(0,0,0); glVertex3f(1,0,0);
71 glColor4f(link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 0],
72 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 1],
73 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 2],
74 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 3]);
75 glVertex3f(0,0,0); glVertex3f(0,1,0);
76 glColor4f(link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 0],
77 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 1],
78 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 2],
79 link[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 3]);
80 glVertex3f(0,0,0); glVertex3f(0,0,1);
86 glColor4f(plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 0],
87 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 1],
88 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 2],
89 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 2*4 + 3]);
90 glVertex3f(0, 0, 0); glVertex3f(1, 0, 0); glVertex3f(1, 1, 0); glVertex3f(0, 1, 0);
91 glColor4f(plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 0],
92 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 1],
93 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 2],
94 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 1*4 + 3]);
95 glVertex3f(0, 0, 0); glVertex3f(1, 0, 0); glVertex3f(1, 0, 1); glVertex3f(0, 0, 1);
96 glColor4f(plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 0],
97 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 1],
98 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 2],
99 plaq[iz*l*l*3*4 + iy*l*3*4 + ix*3*4 + 0*4 + 3]);
100 glVertex3f(0, 0, 0); glVertex3f(0, 1, 0); glVertex3f(0, 1, 1); glVertex3f(0, 0, 1);
111 void cubelooks::cube::draw()
113 glMatrixMode(GL_MODELVIEW);
116 //cout << zoom << endl;
118 glScalef(zoom, zoom, zoom);
119 glRotatef(az, 0, 1, 0);
120 glRotatef(alt, 1, 0, 0);
124 glVertex2f(-1, -1); glVertex2f(1, -1); glVertex2f(1, 1); glVertex2f(-1, 1);
138 cubelooks::env* getEnv()
140 for (int ienv=0; ienv<Envs.size(); ienv++)
141 if ( Envs.at(ienv).first == pthread_self() ) {
142 return Envs.at(ienv).second;
144 cerr << "Something terrible happened: Could not find env-thread-id!" << endl;
148 static int getCubeFromPos(int x, int y)
150 cubelooks::env *Env = getEnv();
152 int col = x / ((float)Env->w / Env->layout[0]);
153 int row = Env->layout[1] - y / ((float)Env->h / Env->layout[1]);
155 return col + row*Env->layout[0];
158 static void idleFunc()
163 static void motionFunc(int x, int y)
165 cubelooks::env *Env = getEnv();
167 Env->cubes.at(rotcube[0]).az += rotcube[1] - x;
168 Env->cubes.at(rotcube[0]).alt += rotcube[2] - y;
176 static void mouseFunc(int button, int state, int x, int y)
178 cubelooks::env *Env = getEnv();
181 rotcube[0] = getCubeFromPos(x,y);
187 Env->cubes.at(getCubeFromPos(x,y)).zoom *= 1.1;
190 else if (button == 3) {
191 Env->cubes.at(getCubeFromPos(x,y)).zoom *= 0.9;
196 static void reshapeFunc(int w, int h)
198 cubelooks::env *Env = getEnv();
205 neww = Env->aspect*h;
207 else if ( h == Env->h ) {
209 newh = w / Env->aspect;
212 neww = ( pow(Env->aspect,2)*w + Env->aspect*h ) / ( pow(Env->aspect,2) + 1 );
213 newh = ( Env->aspect*w + h ) / ( pow(Env->aspect,2) + 1 );
216 glutReshapeWindow(neww,newh);
217 glViewport(0,0,neww,newh);
223 static void displayFunc()
225 cubelooks::env *Env = getEnv();
227 glClearColor(0,0,0,0);
228 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
230 glMatrixMode(GL_MODELVIEW);
233 glScalef(1.0/Env->layout[0],1.0/Env->layout[1],1.0);
234 glTranslatef(-Env->layout[0]+1, -Env->layout[1]+1, 0);
236 for (int icube=0; icube < Env->cubes.size(); icube++) {
237 //glScalef(1.0/Env->layout[0],1.0/Env->layout[1],1.0);
238 //glRotatef(2, 1, 0, 0);
239 Env->cubes.at(icube).draw();
240 if ((icube+1)%Env->layout[0] == 0) {
241 glTranslatef(-2*Env->layout[0]+2, 2, 0);
244 glTranslatef(2, 0, 0);
251 static void* glutThread(void *_Env)
253 cubelooks::env *Env = (cubelooks::env *)_Env;
255 //glXMakeCurrent(0, glutCreateWindow("cubelooks"));
259 //cout << Env->layout[1] << endl;
261 glXMakeCurrent( Env->gDisplay, Env->gDrawable, Env->gContext );
263 //cout << "da" << endl;
265 glutDisplayFunc(&displayFunc);
266 glutReshapeFunc(&reshapeFunc);
267 glutMotionFunc(&motionFunc);
268 glutMouseFunc(&mouseFunc);
269 //glutIdleFunc(&idleFunc);
271 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
272 glEnable( GL_BLEND );
274 Envs.push_back( pair<unsigned long int,cubelooks::env*>(pthread_self(), Env) );
280 cubelooks::cube::cube(int _l)
283 link = new float[l*l*l*3 * 4];
284 plaq = new float[l*l*l*3 * 4];
291 for (int i=0; i<l*l*l*3; i++) {
292 link[i*4+0]=1; link[i*4+3]=0.6;
293 plaq[i*4+1]=1; plaq[i*4+3]=0.2;
297 cubelooks::cubelooks(const int& xcubes, const int& ycubes, const int& l, int *argc, char **argv)
305 Env->layout[0] = xcubes;
306 Env->layout[1] = ycubes;
307 Env->aspect = (double)xcubes/ycubes;
308 for (int icube=0; icube<xcubes*ycubes; icube++) {
310 Env->cubes.push_back(newCube);
314 glutInit( Env->argc, Env->argv );
315 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
318 if( Env->layout[0] >= Env->layout[1] ) {
320 winsize[1] = (640.0/Env->layout[0])*Env->layout[1];
324 winsize[0] = (640.0/Env->layout[1])*Env->layout[0];
330 glutInitWindowSize(winsize[0], winsize[1]);
331 glutInitWindowPosition(0,0);
332 glutCreateWindow("cubelooks");
335 gDisplay = glXGetCurrentDisplay();
336 gDrawable = glXGetCurrentDrawable();
340 Env->gContext = glXGetCurrentContext();
341 Env->gDisplay = gDisplay;
342 Env->gDrawable = glXGetCurrentDrawable();
344 glXMakeCurrent(0,0,0);
346 pthread_create(&glThreadId, 0, &mygl::glutThread, Env);