13 vector< pair<int,culooks::window*> > culooks::Windows;
14 pthread_t culooks::glThreadId;
16 int culooks::windowid = 0;
24 culooks::window* getWin()
26 for (int iwin=0; iwin<culooks::Windows.size(); iwin++)
27 if ( culooks::Windows.at(iwin).first == glutGetWindow() ) {
28 return culooks::Windows.at(iwin).second;
30 //cerr << "Something terrible happened: Could not find window-id!" << endl;
34 static int getCubeFromPos(int x, int y)
36 culooks::window *Win = getWin();
38 int col = x / ((float)Win->w / Win->layout[0]);
39 int row = Win->layout[1] - y / ((float)Win->h / Win->layout[1]);
41 return col + row*Win->layout[0];
45 static void motionFunc(int x, int y)
48 culooks::window *Win = getWin();
50 Win->cubes.at(rotcube[0]).az += rotcube[1] - x;
51 Win->cubes.at(rotcube[0]).alt += rotcube[2] - y;
60 static void mouseFunc(int button, int state, int x, int y)
62 culooks::window *Win = getWin();
65 rotcube[0] = getCubeFromPos(x,y);
71 Win->cubes.at(getCubeFromPos(x,y)).zoom *= 1.1;
74 else if (button == 3) {
75 Win->cubes.at(getCubeFromPos(x,y)).zoom *= 0.9;
81 static void reshapeFunc(int w, int h)
83 culooks::window *Win = getWin();
92 else if ( h == Win->h ) {
94 newh = w / Win->aspect;
97 neww = ( pow(Win->aspect,2)*w + Win->aspect*h ) / ( pow(Win->aspect,2) + 1 );
98 newh = ( Win->aspect*w + h ) / ( pow(Win->aspect,2) + 1 );
101 glutReshapeWindow(neww,newh);
102 glViewport(0,0,neww,newh);
108 static void displayFunc()
110 //cout << ":" << glutGetWindow() << endl;
112 culooks::window *Win = getWin();
114 glClearColor(0,0,0,0);
115 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
117 glMatrixMode(GL_MODELVIEW);
120 glScalef(1.0/Win->layout[0],1.0/Win->layout[1],1.0);
121 glTranslatef(-Win->layout[0]+1, -Win->layout[1]+1, 0);
123 for (int icube=0; icube < Win->cubes.size(); icube++) {
124 //glScalef(1.0/Env->layout[0],1.0/Env->layout[1],1.0);
125 //glRotatef(2, 1, 0, 0);
126 Win->cubes.at(icube).draw();
127 if ((icube+1)%Win->layout[0] == 0) {
128 glTranslatef(-2*Win->layout[0]+2, 2, 0);
131 glTranslatef(2, 0, 0);
138 static void initWindow(int winid)
140 glutInitWindowSize(culooks::Windows[winid].second->w, culooks::Windows[winid].second->h);
141 glutInitWindowPosition(winid*100,winid*100);
142 glutCreateWindow( ("culooks / " + culooks::Windows[winid].second->name).c_str() );
144 glutDisplayFunc(&mygl::displayFunc);
145 glutReshapeFunc(&mygl::reshapeFunc);
146 glutMotionFunc(&mygl::motionFunc);
147 glutMouseFunc(&mygl::mouseFunc);
149 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
150 glEnable( GL_BLEND );
152 culooks::Windows[winid].first = glutGetWindow();
154 culooks::Windows[winid].second->initialized = true;
157 static void idleFunc_master()
159 for (int iwin=0; iwin < culooks::Windows.size(); iwin++)
160 if (!culooks::Windows[iwin].second->initialized)
164 static void* glutThread(void *leer)
167 glutIdleFunc(&idleFunc_master);
173 culooks::culooks (const char* name, const int& xcubes, const int& ycubes, const int& l, int *argc, char **argv)
175 window *Win = new window;
180 glutInit(argc, argv);
181 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
184 Win->layout[0] = xcubes;
185 Win->layout[1] = ycubes;
186 Win->aspect = (double)xcubes/ycubes;
187 for (int icube=0; icube<xcubes*ycubes; icube++) {
189 Win->cubes.push_back(newCube);
192 if( Win->layout[0] >= Win->layout[1] ) {
194 winsize[1] = (640.0/Win->layout[0])*Win->layout[1];
198 winsize[0] = (640.0/Win->layout[1])*Win->layout[0];
206 Win->initialized = false;
208 Windows.push_back( pair<int,culooks::window*>(0, Win) );
211 glXMakeCurrent(0,0,0);
212 pthread_create(&glThreadId, 0, &mygl::glutThread, NULL);