]> git.treefish.org Git - phys/latlib.git/commitdiff
...
authorAlex Schmidt <alex@treefish.org>
Tue, 19 Feb 2013 09:59:43 +0000 (10:59 +0100)
committerAlex Schmidt <alex@treefish.org>
Tue, 19 Feb 2013 09:59:43 +0000 (10:59 +0100)
CMakeLists.txt
culooks.cpp
culooks.h
culooks_drawing.cpp [new file with mode: 0644]

index 96f472a7c085b2663df0b57ee2a2e990c8600266..05e07d9cf7c0cf6c74243e74cf476f9474421192 100644 (file)
@@ -14,7 +14,7 @@ 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_culooks culooks.cpp culooks_cube.cpp)
+add_library(lat_culooks culooks.cpp culooks_cube.cpp culooks_drawing.cpp)
 target_link_libraries(lat_culooks ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})
 
 add_executable(neigh_test neigh_test.cpp)
index cb5eda7ffde80f186a1600be00e50d32ba8aeac4..19e6077a05db2fc4ac129ff990bc164421936a37 100644 (file)
@@ -17,164 +17,10 @@ int culooks::windowid = 0;
 
 using namespace std;
 
-namespace mygl 
-{
-  int rotcube[3];
-
-  culooks::window* getWin()
-  {
-    for (int iwin=0; iwin<culooks::Windows.size(); iwin++)
-      if ( culooks::Windows.at(iwin).first == glutGetWindow() ) {
-       return culooks::Windows.at(iwin).second;
-      }
-    //cerr << "Something terrible happened: Could not find window-id!" << endl;
-    exit(1);
-  }
-
-  static int getCubeFromPos(int x, int y)
-  {
-    culooks::window *Win = getWin();
-
-    int col = x / ((float)Win->w / 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 *leer)
-  {
-    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);
@@ -209,7 +55,7 @@ culooks::culooks (const char* name, const int& xcubes, const int& ycubes, const
   
   if (windowid == 0) {
     glXMakeCurrent(0,0,0);
-    pthread_create(&glThreadId, 0, &mygl::glutThread, NULL);
+    pthread_create(&glThreadId, 0, &drawing::glutThread, NULL);
   }
 
   mywid = windowid;
index 1705ab57ddefbffb9f70233ba3aeb41b2d0fcc75..76f0919ce2dbdf3b8ddf29be7269dbbd05fc6fb4 100644 (file)
--- a/culooks.h
+++ b/culooks.h
@@ -14,7 +14,12 @@ using namespace std;
 
 class culooks
 {
+ public:
+  
+  culooks (const char* name, const int& xcubes, const int& ycubes, const int& l, int *argc, char **argv);
+
  private:
+  
   class cube {
   public:
     cube(int l);
@@ -30,8 +35,7 @@ class culooks
     float *link;
     int l;
   };
-
- public:
+  
   struct window {
     int layout[2];
     double aspect;
@@ -42,17 +46,23 @@ class culooks
     string name;
   };
 
-  culooks (const char* name, const int& xcubes, const int& ycubes, const int& l, int *argc, char **argv);
-
-  static vector< pair<int,window*> > Windows;
-
-  struct wincontext {
-    GLXDrawable gDrawable;
-    Display *gDisplay;
-    GLXContext gContext;
+  class drawing 
+  {
+  private:
+    static int rotcube[3];
+    static culooks::window* getWin();
+    static int getCubeFromPos(int x, int y);
+    static void motionFunc(int x, int y);
+    static void mouseFunc(int button, int state, int x, int y);
+    static void reshapeFunc(int w, int h);
+    static void displayFunc();
+    static void initWindow(int winid);
+    static void idleFunc_master();
+  public:
+    static void* glutThread(void *leer);
   };
-
- private:
+  
+  static vector< pair<int,window*> > Windows;
   static pthread_t glThreadId;
   static int windowid;
   int mywid;
diff --git a/culooks_drawing.cpp b/culooks_drawing.cpp
new file mode 100644 (file)
index 0000000..fb5773c
--- /dev/null
@@ -0,0 +1,150 @@
+#include "culooks.h"
+
+#include <iostream>
+#include <cmath>
+
+using namespace std;
+
+int culooks::drawing::rotcube[3];
+
+culooks::window* culooks::drawing::getWin()
+{
+  for (int iwin=0; iwin<culooks::Windows.size(); iwin++)
+    if ( culooks::Windows.at(iwin).first == glutGetWindow() ) {
+      return culooks::Windows.at(iwin).second;
+    }
+  cerr << "Something terrible happened: Could not find window-id!" << endl;
+  exit(1);
+}
+
+int culooks::drawing::getCubeFromPos(int x, int y)
+{
+  culooks::window *Win = getWin();
+
+  int col = x / ((float)Win->w / Win->layout[0]);
+  int row = Win->layout[1] - y / ((float)Win->h / Win->layout[1]);
+
+  return col + row*Win->layout[0];
+}
+
+
+void culooks::drawing::motionFunc(int x, int y)
+{   
+  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();
+}
+
+void culooks::drawing::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();
+  }
+}
+
+
+void culooks::drawing::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;
+}
+
+void culooks::drawing::displayFunc()
+{
+  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++) {
+    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();
+}
+void culooks::drawing::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(&drawing::displayFunc);
+  glutReshapeFunc(&drawing::reshapeFunc);
+  glutMotionFunc(&drawing::motionFunc);
+  glutMouseFunc(&drawing::mouseFunc);
+
+  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
+  glEnable( GL_BLEND );
+
+  culooks::Windows[winid].first = glutGetWindow();
+
+  culooks::Windows[winid].second->initialized = true;
+}
+
+void culooks::drawing::idleFunc_master()
+{
+  for (int iwin=0; iwin < culooks::Windows.size(); iwin++)
+    if (!culooks::Windows[iwin].second->initialized)
+      initWindow(iwin);
+}
+
+void* culooks::drawing::glutThread(void *leer)
+{
+  initWindow(0);
+  glutIdleFunc(&idleFunc_master);
+  glutMainLoop();
+}