+#include "cubelooks.h"
+#include <GL/glut.h>
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+/* GLOBAL */
+vector< pair <unsigned long int,cubelooks::env*> > Envs;
+
+int cubelooks::cube::allid = 0;
+
+void cubelooks::cube::draw()
+{
+ glBegin(GL_QUADS);
+ glVertex2f(-1, -1); glVertex2f(1, -1); glVertex2f(1, 1); glVertex2f(-1, 1);
+ glEnd();
+}
+
+namespace mygl
+{
+ cubelooks::env* getEnv()
+ {
+ for (int ienv=0; ienv<Envs.size(); ienv++)
+ if ( Envs.at(ienv).first == pthread_self() ) {
+ return Envs.at(ienv).second;
+ }
+ cerr << "Something terrible happened: Could not find env-thread-id!" << endl;
+ exit(1);
+ }
+
+ static void idleFunc()
+ {
+ glutPostRedisplay();
+ }
+
+ static void reshapeFunc(int w, int h)
+ {
+ glutReshapeWindow(10,10);
+ glViewport(0,0,w,h);
+ cout << w << ":" << h << endl;
+ }
+
+ static void displayFunc()
+ {
+ cubelooks::env *Env = getEnv();
+
+ glClearColor(0,0,0,0);
+ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
+
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+
+ for (int icube=0; icube < Env->cubes.size(); icube++) {
+ glScalef(1.0/Env->layout[0],1.0/Env->layout[1],1.0);
+ //glRotatef(2, 1, 0, 0);
+ Env->cubes.at(icube).draw();
+ }
+
+ glutSwapBuffers();
+ }
+
+ static void* glutThread(void *_Env)
+ {
+ int winsize[2];
+
+ cubelooks::env* Env = (cubelooks::env*)_Env;
+
+ glutInit( ((cubelooks::env*)Env)->argc, ((cubelooks::env*)Env)->argv );
+ glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
+
+ if( Env->layout[0] >= Env->layout[1] ) {
+ winsize[0] = 640;
+ winsize[1] = (640.0/Env->layout[0])*Env->layout[1];
+ }
+ else {
+ winsize[1] = 640;
+ winsize[0] = (640.0/Env->layout[1])*Env->layout[0];
+ }
+
+ glutInitWindowSize(winsize[0], winsize[1]);
+
+ glutInitWindowPosition(0,0);
+
+ glutCreateWindow("cubelooks");
+
+ glutDisplayFunc(&displayFunc);
+ glutReshapeFunc(&reshapeFunc);
+ //glutIdleFunc(&idleFunc);
+
+ Envs.push_back( pair<unsigned long int,cubelooks::env*>(pthread_self(), (cubelooks::env*)Env) );
+
+ glutMainLoop();
+ }
+};
+
+cubelooks::cube::cube()
+{
+ id = allid;
+ allid++;
+}
+
+cubelooks::cubelooks(const int& xcubes, const int& ycubes, int *argc, char **argv)
+{
+ Env.argc = argc;
+ Env.argv = argv;
+ Env.layout[0] = xcubes;
+ Env.layout[1] = ycubes;
+ for (int icube=0; icube<xcubes*ycubes; icube++) {
+ cube newCube;
+ Env.cubes.push_back(newCube);
+ }
+
+ pthread_create(&glThreadId, 0, &mygl::glutThread, &Env);
+}