X-Git-Url: http://git.treefish.org/~alex/phys/latlib.git/blobdiff_plain/b8c3dcf5e075cc961b2693b1fcb8d1b8604e8f7c..2c7ea0058473eb98219ff87c64e28f31fac34b90:/culooks.cpp?ds=inline diff --git a/culooks.cpp b/culooks.cpp new file mode 100644 index 0000000..fe8fd00 --- /dev/null +++ b/culooks.cpp @@ -0,0 +1,320 @@ +#include "culooks.h" + +#include +#include + +vector< pair > culooks::Windows; +pthread_t culooks::glThreadId; + +int culooks::windowid = 0; + +int culooks::cube::allid = 0; + +using namespace std; + +void culooks::cube::draw() +{ + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + + //cout << zoom << endl; + + glScalef(zoom, zoom, zoom); + glRotatef(az, 0, 1, 0); + glRotatef(alt, 1, 0, 0); + + /* + glBegin(GL_QUADS); + glVertex2f(-1, -1); glVertex2f(1, -1); glVertex2f(1, 1); glVertex2f(-1, 1); + glEnd(); + */ + + //drawBox(); + drawAll(); + + glPopMatrix(); +} + +void culooks::cube::drawAll() +{ + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + + glTranslatef(-1,-1,-1); + + for (int iz=0; izw / Win->layout[0]); + int row = Win->layout[1] - y / ((float)Win->h / Win->layout[1]); + + return col + row*Win->layout[0]; + } + + + static void motionFunc(int x, int y) + { + cout << x << endl; + culooks::window *Win = getWin(); + + Win->cubes.at(rotcube[0]).az += rotcube[1] - x; + Win->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) + { + culooks::window *Win = getWin(); + + if (button == 0) { + rotcube[0] = getCubeFromPos(x,y); + rotcube[1] = x; + rotcube[2] = y; + } + + if (button == 4) { + Win->cubes.at(getCubeFromPos(x,y)).zoom *= 1.1; + glutPostRedisplay(); + } + else if (button == 3) { + Win->cubes.at(getCubeFromPos(x,y)).zoom *= 0.9; + glutPostRedisplay(); + } + } + + + static void reshapeFunc(int w, int h) + { + culooks::window *Win = getWin(); + + int neww; + int newh; + + if ( w == Win->w ) { + newh = h; + neww = Win->aspect*h; + } + else if ( h == Win->h ) { + neww = w; + newh = w / Win->aspect; + } + else { + neww = ( pow(Win->aspect,2)*w + Win->aspect*h ) / ( pow(Win->aspect,2) + 1 ); + newh = ( Win->aspect*w + h ) / ( pow(Win->aspect,2) + 1 ); + } + + glutReshapeWindow(neww,newh); + glViewport(0,0,neww,newh); + + Win->w = neww; + Win->h = newh; + } + + static void displayFunc() + { + cout << ":" << glutGetWindow() << endl; + + culooks::window *Win = getWin(); + + glClearColor(0,0,0,0); + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glScalef(1.0/Win->layout[0],1.0/Win->layout[1],1.0); + glTranslatef(-Win->layout[0]+1, -Win->layout[1]+1, 0); + + for (int icube=0; icube < Win->cubes.size(); icube++) { + //glScalef(1.0/Env->layout[0],1.0/Env->layout[1],1.0); + //glRotatef(2, 1, 0, 0); + Win->cubes.at(icube).draw(); + if ((icube+1)%Win->layout[0] == 0) { + glTranslatef(-2*Win->layout[0]+2, 2, 0); + } + else { + glTranslatef(2, 0, 0); + } + } + + glutSwapBuffers(); + } + + static void initWindow(int winid) + { + glutInitWindowSize(culooks::Windows[winid].second->w, culooks::Windows[winid].second->h); + glutInitWindowPosition(winid*100,winid*100); + glutCreateWindow( ("culooks / " + culooks::Windows[winid].second->name).c_str() ); + + glutDisplayFunc(&mygl::displayFunc); + glutReshapeFunc(&mygl::reshapeFunc); + glutMotionFunc(&mygl::motionFunc); + glutMouseFunc(&mygl::mouseFunc); + + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable( GL_BLEND ); + + culooks::Windows[winid].first = glutGetWindow(); + + culooks::Windows[winid].second->initialized = true; + } + + static void idleFunc_master() + { + for (int iwin=0; iwin < culooks::Windows.size(); iwin++) + if (!culooks::Windows[iwin].second->initialized) + initWindow(iwin); + } + + static void* glutThread(void *_wincon) + { + culooks::wincontext *wincon = (culooks::wincontext *)_wincon; + glXMakeCurrent( wincon->gDisplay, wincon->gDrawable, wincon->gContext ); + initWindow(0); + glutIdleFunc(&idleFunc_master); + glutMainLoop(); + } +}; + + +culooks::culooks (const char* name, const int& xcubes, const int& ycubes, const int& l, int *argc, char **argv) +{ + window *Win = new window; + int winsize[2]; + wincontext wincon; + + if (windowid == 0) { + glutInit(argc, argv); + glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); + } + + Win->layout[0] = xcubes; + Win->layout[1] = ycubes; + Win->aspect = (double)xcubes/ycubes; + for (int icube=0; icubecubes.push_back(newCube); + } + + if( Win->layout[0] >= Win->layout[1] ) { + winsize[0] = 640; + winsize[1] = (640.0/Win->layout[0])*Win->layout[1]; + } + else { + winsize[1] = 640; + winsize[0] = (640.0/Win->layout[1])*Win->layout[0]; + } + + Win->w = winsize[0]; + Win->h = winsize[1]; + + Win->name = name; + + Win->initialized = false; + + Windows.push_back( pair(0, Win) ); + + if (windowid == 0) { + wincon.gContext = glXGetCurrentContext(); + wincon.gDisplay = glXGetCurrentDisplay(); + wincon.gDrawable = glXGetCurrentDrawable(); + + glXMakeCurrent(0,0,0); + + pthread_create(&glThreadId, 0, &mygl::glutThread, &wincon); + } + + mywid = windowid; + windowid++; +} + +culooks::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