From 2c7ea0058473eb98219ff87c64e28f31fac34b90 Mon Sep 17 00:00:00 2001 From: Alex Schmidt Date: Mon, 18 Feb 2013 16:17:37 +0100 Subject: [PATCH] ... --- .gitignore | 1 + CMakeLists.txt | 8 +- cubelooks.cpp => culooks.cpp | 313 +++++++++++-------------- cubelooks.h => culooks.h | 34 +-- cubelooks_test.cpp => culooks_test.cpp | 6 +- 5 files changed, 172 insertions(+), 190 deletions(-) rename cubelooks.cpp => culooks.cpp (50%) rename cubelooks.h => culooks.h (59%) rename cubelooks_test.cpp => culooks_test.cpp (54%) diff --git a/.gitignore b/.gitignore index 393c6b3..ec81062 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ Makefile *~ neigh_test cubelooks_test +culooks_test diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c8d546..5cbe4e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,11 +14,11 @@ add_library(lat_progress progress.cpp) find_package(OpenGL REQUIRED) find_package(GLUT REQUIRED) include_directories( ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} ../ ) -add_library(lat_cubelooks cubelooks.cpp) -target_link_libraries(lat_cubelooks ${OPENGL_LIBRARIES} ${GLUT_LIBRARY}) +add_library(lat_culooks culooks.cpp) +target_link_libraries(lat_culooks ${OPENGL_LIBRARIES} ${GLUT_LIBRARY}) add_executable(neigh_test neigh_test.cpp) target_link_libraries(neigh_test lat_neigh) -add_executable(cubelooks_test cubelooks_test.cpp) -target_link_libraries(cubelooks_test lat_cubelooks) \ No newline at end of file +add_executable(culooks_test culooks_test.cpp) +target_link_libraries(culooks_test lat_culooks) \ No newline at end of file diff --git a/cubelooks.cpp b/culooks.cpp similarity index 50% rename from cubelooks.cpp rename to culooks.cpp index f6b02ef..fe8fd00 100644 --- a/cubelooks.cpp +++ b/culooks.cpp @@ -1,53 +1,41 @@ -#include "cubelooks.h" +#include "culooks.h" + #include -#include #include -using namespace std; +vector< pair > culooks::Windows; +pthread_t culooks::glThreadId; -vector< pair > Envs; +int culooks::windowid = 0; -bool cubelooks::initDone = false; -GLXDrawable cubelooks::gDrawable; -Display *cubelooks::gDisplay; +int culooks::cube::allid = 0; -int cubelooks::cube::allid = 0; +using namespace std; -void cubelooks::cube::drawBox() +void culooks::cube::draw() { - 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); + 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 cubelooks::cube::drawAll() +void culooks::cube::drawAll() { glMatrixMode(GL_MODELVIEW); glPushMatrix(); @@ -108,74 +96,49 @@ void cubelooks::cube::drawAll() glPopMatrix(); } -void cubelooks::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(); -} - namespace mygl { int rotcube[3]; - cubelooks::env* getEnv() + culooks::window* getWin() { - for (int ienv=0; ienvw / Env->layout[0]); - int row = Env->layout[1] - y / ((float)Env->h / Env->layout[1]); + int col = x / ((float)Win->w / Win->layout[0]); + int row = Win->layout[1] - y / ((float)Win->h / Win->layout[1]); - return col + row*Env->layout[0]; - } - - static void idleFunc() - { - glutPostRedisplay(); + return col + row*Win->layout[0]; } + static void motionFunc(int x, int y) { - cubelooks::env *Env = getEnv(); - - Env->cubes.at(rotcube[0]).az += rotcube[1] - x; - Env->cubes.at(rotcube[0]).alt += rotcube[2] - 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) { - cubelooks::env *Env = getEnv(); + culooks::window *Win = getWin(); if (button == 0) { rotcube[0] = getCubeFromPos(x,y); @@ -184,164 +147,174 @@ namespace mygl } if (button == 4) { - Env->cubes.at(getCubeFromPos(x,y)).zoom *= 1.1; + Win->cubes.at(getCubeFromPos(x,y)).zoom *= 1.1; glutPostRedisplay(); } else if (button == 3) { - Env->cubes.at(getCubeFromPos(x,y)).zoom *= 0.9; + Win->cubes.at(getCubeFromPos(x,y)).zoom *= 0.9; glutPostRedisplay(); } } + static void reshapeFunc(int w, int h) { - cubelooks::env *Env = getEnv(); - + culooks::window *Win = getWin(); + int neww; int newh; - if ( w == Env->w ) { + if ( w == Win->w ) { newh = h; - neww = Env->aspect*h; + neww = Win->aspect*h; } - else if ( h == Env->h ) { + else if ( h == Win->h ) { neww = w; - newh = w / Env->aspect; + newh = w / Win->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 ); + 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); - Env->w = neww; - Env->h = newh; + Win->w = neww; + Win->h = newh; } static void displayFunc() { - cubelooks::env *Env = getEnv(); + 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/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/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); - Env->cubes.at(icube).draw(); - if ((icube+1)%Env->layout[0] == 0) { - glTranslatef(-2*Env->layout[0]+2, 2, 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* glutThread(void *_Env) + + static void initWindow(int winid) { - cubelooks::env *Env = (cubelooks::env *)_Env; - - //glXMakeCurrent(0, glutCreateWindow("cubelooks")); - - //glXCrateContext(0, - - //cout << Env->layout[1] << endl; - - glXMakeCurrent( Env->gDisplay, Env->gDrawable, Env->gContext ); - - //cout << "da" << endl; - - glutDisplayFunc(&displayFunc); - glutReshapeFunc(&reshapeFunc); - glutMotionFunc(&motionFunc); - glutMouseFunc(&mouseFunc); - //glutIdleFunc(&idleFunc); + 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 ); - Envs.push_back( pair(pthread_self(), Env) ); + 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(); } }; -cubelooks::cube::cube(int _l) + +culooks::culooks (const char* name, const int& xcubes, const int& ycubes, const int& l, int *argc, char **argv) { - 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++; + window *Win = new window; + int winsize[2]; + wincontext wincon; - for (int i=0; iargc = argc; - Env->argv = argv; - Env->layout[0] = xcubes; - Env->layout[1] = ycubes; - Env->aspect = (double)xcubes/ycubes; + Win->layout[0] = xcubes; + Win->layout[1] = ycubes; + Win->aspect = (double)xcubes/ycubes; for (int icube=0; icubecubes.push_back(newCube); + Win->cubes.push_back(newCube); } - if ( !initDone ) { - glutInit( Env->argc, Env->argv ); - glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); - } - - if( Env->layout[0] >= Env->layout[1] ) { + if( Win->layout[0] >= Win->layout[1] ) { winsize[0] = 640; - winsize[1] = (640.0/Env->layout[0])*Env->layout[1]; + winsize[1] = (640.0/Win->layout[0])*Win->layout[1]; } else { winsize[1] = 640; - winsize[0] = (640.0/Env->layout[1])*Env->layout[0]; + winsize[0] = (640.0/Win->layout[1])*Win->layout[0]; } - Env->w = winsize[0]; - Env->h = winsize[1]; + 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(); - glutInitWindowSize(winsize[0], winsize[1]); - glutInitWindowPosition(0,0); - glutCreateWindow("cubelooks"); + glXMakeCurrent(0,0,0); - if ( !initDone ) { - gDisplay = glXGetCurrentDisplay(); - gDrawable = glXGetCurrentDrawable(); - initDone = true; + pthread_create(&glThreadId, 0, &mygl::glutThread, &wincon); } - Env->gContext = glXGetCurrentContext(); - Env->gDisplay = gDisplay; - Env->gDrawable = glXGetCurrentDrawable(); + mywid = windowid; + windowid++; +} - glXMakeCurrent(0,0,0); +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++; - pthread_create(&glThreadId, 0, &mygl::glutThread, Env); + for (int i=0; i #include @@ -8,11 +8,11 @@ #include #include #include - +#include using namespace std; -class cubelooks +class culooks { public: class cube { @@ -30,26 +30,32 @@ class cubelooks float *link; int l; }; - - struct env { - int *argc; - char **argv; + + struct window { int layout[2]; double aspect; vector cubes; int w, h; + int gwinid; + bool initialized; + string name; + }; + + culooks (const char* name, const int& xcubes, const int& ycubes, const int& l, int *argc, char **argv); + + static vector< pair > Windows; + + struct wincontext { GLXDrawable gDrawable; Display *gDisplay; GLXContext gContext; }; - cubelooks(const int& xcubes, const int& ycubes, const int& l, int *argc, char **argv); - private: - pthread_t glThreadId; - static bool initDone; - static GLXDrawable gDrawable; - static Display *gDisplay; + static pthread_t glThreadId; + static int windowid; + int mywid; + }; #endif diff --git a/cubelooks_test.cpp b/culooks_test.cpp similarity index 54% rename from cubelooks_test.cpp rename to culooks_test.cpp index fc6e3f8..b1ff0bf 100644 --- a/cubelooks_test.cpp +++ b/culooks_test.cpp @@ -1,4 +1,4 @@ -#include "cubelooks.h" +#include "culooks.h" #include "unistd.h" #include @@ -7,7 +7,9 @@ using namespace std; int main(int argc, char **argv) { - cubelooks clooks(2,1, 6, &argc, argv); + culooks clooks("fenster1", 2,1, 6, &argc, argv); + culooks clooks2("fenster2", 4,2, 6, &argc, argv); + culooks clooks3("fenster3", 2,1, 6, &argc, argv); //sleep(2); -- 2.39.5