X-Git-Url: http://git.treefish.org/~alex/phys/latlib.git/blobdiff_plain/0e276c2770688b3cc7676ee84c8e35246621f6df..79dada2bcf873731ed4e78cfed64d8274fb5c587:/cubelooks.cpp?ds=inline diff --git a/cubelooks.cpp b/cubelooks.cpp index ca675bd..6584383 100644 --- a/cubelooks.cpp +++ b/cubelooks.cpp @@ -2,31 +2,142 @@ #include #include #include +#include using namespace std; -/* GLOBAL */ -vector< pair > Envs; +cubelooks::env *Env; int cubelooks::cube::allid = 0; +void cubelooks::cube::drawBox() +{ + glBegin(GL_LINES); + + glVertex3f(-1.0f, 1.0f, -1.0f); + glVertex3f( 1.0f, 1.0f, -1.0f); + glVertex3f(-1.0f, -1.0f, -1.0f); + glVertex3f( 1.0f, -1.0f, -1.0f); + glVertex3f(-1.0f, 1.0f, 1.0f); + glVertex3f( 1.0f, 1.0f, 1.0f); + glVertex3f(-1.0f, -1.0f, 1.0f); + glVertex3f( 1.0f, -1.0f, 1.0f); + + glVertex3f( 1.0f, 1.0f, -1.0f); + glVertex3f( 1.0f, -1.0f, -1.0f); + glVertex3f( -1.0f, 1.0f, -1.0f); + glVertex3f( -1.0f, -1.0f, -1.0f); + glVertex3f( 1.0f, 1.0f, 1.0f); + glVertex3f( 1.0f, -1.0f, 1.0f); + glVertex3f( -1.0f, 1.0f, 1.0f); + glVertex3f( -1.0f, -1.0f, 1.0f); + + glVertex3f( -1.0f, -1.0f, 1.0f); + glVertex3f( -1.0f, -1.0f, -1.0f); + glVertex3f( -1.0f, 1.0f, 1.0f); + glVertex3f( -1.0f, 1.0f, -1.0f); + glVertex3f( 1.0f, -1.0f, 1.0f); + glVertex3f( 1.0f, -1.0f, -1.0f); + glVertex3f( 1.0f, 1.0f, 1.0f); + glVertex3f( 1.0f, 1.0f, -1.0f); + + glEnd(); +} + +void cubelooks::cube::drawAll() +{ + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + + glTranslatef(-1,-1,-1); + + for (int iz=0; izw / Env->layout[0]); + int row = Env->layout[1] - y / ((float)Env->h / Env->layout[1]); + + return col + row*Env->layout[0]; } static void idleFunc() @@ -34,27 +145,81 @@ namespace mygl glutPostRedisplay(); } + static void motionFunc(int x, int y) + { + Env->cubes.at(rotcube[0]).az += rotcube[1] - x; + Env->cubes.at(rotcube[0]).alt += rotcube[2] - y; + + rotcube[1] = x; + rotcube[2] = y; + + glutPostRedisplay(); + } + + static void mouseFunc(int button, int state, int x, int y) + { + if (button == 0) { + rotcube[0] = getCubeFromPos(x,y); + rotcube[1] = x; + rotcube[2] = y; + } + + if (button == 4) { + Env->cubes.at(getCubeFromPos(x,y)).zoom *= 1.1; + glutPostRedisplay(); + } + else if (button == 3) { + Env->cubes.at(getCubeFromPos(x,y)).zoom *= 0.9; + glutPostRedisplay(); + } + } + static void reshapeFunc(int w, int h) { - glutReshapeWindow(10,10); - glViewport(0,0,w,h); - cout << w << ":" << h << endl; + int neww; + int newh; + + if ( w == Env->w ) { + newh = h; + neww = Env->aspect*h; + } + else if ( h == Env->h ) { + neww = w; + newh = w / Env->aspect; + } + else { + neww = ( pow(Env->aspect,2)*w + Env->aspect*h ) / ( pow(Env->aspect,2) + 1 ); + newh = ( Env->aspect*w + h ) / ( pow(Env->aspect,2) + 1 ); + } + + glutReshapeWindow(neww,newh); + glViewport(0,0,neww,newh); + + Env->w = neww; + Env->h = newh; } 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(); + glScalef(1.0/Env->layout[0],1.0/Env->layout[1],1.0); + glTranslatef(-Env->layout[0]+1, -Env->layout[1]+1, 0); + for (int icube=0; icube < Env->cubes.size(); icube++) { - glScalef(1.0/Env->layout[0],1.0/Env->layout[1],1.0); + //glScalef(1.0/Env->layout[0],1.0/Env->layout[1],1.0); //glRotatef(2, 1, 0, 0); Env->cubes.at(icube).draw(); + if ((icube+1)%Env->layout[0] == 0) { + glTranslatef(-2*Env->layout[0]+2, 2, 0); + } + else { + glTranslatef(2, 0, 0); + } } glutSwapBuffers(); @@ -64,8 +229,6 @@ namespace mygl { 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); @@ -78,6 +241,9 @@ namespace mygl winsize[0] = (640.0/Env->layout[1])*Env->layout[0]; } + Env->w = winsize[0]; + Env->h = winsize[1]; + glutInitWindowSize(winsize[0], winsize[1]); glutInitWindowPosition(0,0); @@ -86,30 +252,49 @@ namespace mygl glutDisplayFunc(&displayFunc); glutReshapeFunc(&reshapeFunc); + glutMotionFunc(&motionFunc); + glutMouseFunc(&mouseFunc); //glutIdleFunc(&idleFunc); - Envs.push_back( pair(pthread_self(), (cubelooks::env*)Env) ); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable( GL_BLEND ); glutMainLoop(); } }; -cubelooks::cube::cube() +cubelooks::cube::cube(int _l) { + l = _l; + link = new float[l*l*l*3 * 4]; + plaq = new float[l*l*l*3 * 4]; + az = 30; + alt = -20; + zoom = 0.6; id = allid; allid++; + + for (int i=0; i