]> git.treefish.org Git - phys/latlib.git/blob - culooks_drawing.cpp
Made c++11 standard dependency obsolete.
[phys/latlib.git] / culooks_drawing.cpp
1 #include "culooks.h"
2
3 #include <iostream>
4 #include <cmath>
5
6 using namespace std;
7
8 int culooks::drawing::rotcube[3];
9
10 culooks::window* culooks::drawing::getWin()
11 {
12   for (int iwin=0; iwin<culooks::Windows.size(); iwin++)
13     if ( culooks::Windows.at(iwin).first == glutGetWindow() ) {
14       return culooks::Windows.at(iwin).second;
15     }
16   cerr << "Something terrible happened: Could not find window-id!" << endl;
17   exit(1);
18 }
19
20 int culooks::drawing::getCubeFromPos(int x, int y)
21 {
22   culooks::window *Win = getWin();
23
24   int col = x / ((float)Win->w / Win->layout[0]);
25   int row = Win->layout[1] - y / ((float)Win->h / Win->layout[1]);
26
27   return col + row*Win->layout[0];
28 }
29
30
31 void culooks::drawing::motionFunc(int x, int y)
32 {   
33   culooks::window *Win = getWin();
34    
35   Win->cubes.at(rotcube[0]).az += rotcube[1] - x;
36   Win->cubes.at(rotcube[0]).alt += rotcube[2] - y;
37
38   rotcube[1] = x;
39   rotcube[2] = y;
40    
41
42   glutPostRedisplay();
43 }
44
45 void culooks::drawing::mouseFunc(int button, int state, int x, int y)
46 {
47   culooks::window *Win = getWin();
48
49   if (button == 0) {
50     rotcube[0] = getCubeFromPos(x,y);
51     rotcube[1] = x;
52     rotcube[2] = y;
53   }
54
55   if (button == 4) {
56     Win->cubes.at(getCubeFromPos(x,y)).zoom *= 1.1;
57     glutPostRedisplay();
58   }
59   else if (button == 3) {
60     Win->cubes.at(getCubeFromPos(x,y)).zoom *= 0.9;
61     glutPostRedisplay();
62   }
63 }
64
65
66 void culooks::drawing::reshapeFunc(int w, int h)
67 {
68   culooks::window *Win = getWin();
69     
70   int neww;
71   int newh;
72
73   if ( w == Win->w ) {
74     newh = h;
75     neww = Win->aspect*h;
76   }
77   else if ( h == Win->h ) {
78     neww = w;
79     newh = w / Win->aspect;
80   }
81   else {
82     neww = ( pow(Win->aspect,2)*w + Win->aspect*h ) / ( pow(Win->aspect,2) + 1 );
83     newh = ( Win->aspect*w + h ) / ( pow(Win->aspect,2) + 1 );
84   }
85
86   glutReshapeWindow(neww,newh);
87   glViewport(0,0,neww,newh);
88
89   Win->w = neww;
90   Win->h = newh;
91 }
92
93 void culooks::drawing::displayFunc()
94 {
95   culooks::window *Win = getWin();
96
97   glClearColor(Win->bgcolor[0], Win->bgcolor[1], Win->bgcolor[2], Win->bgcolor[3]);
98   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
99     
100   glMatrixMode(GL_MODELVIEW);
101   glLoadIdentity();
102     
103   glScalef(1.0/Win->layout[0],1.0/Win->layout[1],1.0);
104   glTranslatef(-Win->layout[0]+1, -Win->layout[1]+1, 0);
105     
106   for (int icube=0; icube < Win->cubes.size(); icube++) {
107     Win->cubes.at(icube).draw();
108     if ((icube+1)%Win->layout[0] == 0) {
109       glTranslatef(-2*Win->layout[0]+2, 2, 0);
110     }
111     else {
112       glTranslatef(2, 0, 0);
113     }
114   }
115     
116   glutSwapBuffers();
117 }
118  
119 void culooks::drawing::initWindow(int winid)
120 {
121   
122   glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
123   glutInitWindowSize(culooks::Windows[winid].second->w, culooks::Windows[winid].second->h);
124   glutInitWindowPosition(winid*100,winid*100);
125   glutCreateWindow( ("culooks / " + culooks::Windows[winid].second->name).c_str() );
126
127   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
128
129   glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);       
130   glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);      
131   glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
132
133   glutDisplayFunc(&drawing::displayFunc);
134   glutReshapeFunc(&drawing::reshapeFunc);
135   glutMotionFunc(&drawing::motionFunc);
136   glutMouseFunc(&drawing::mouseFunc);
137   glutIdleFunc(&drawing::idleFunc);
138
139   culooks::Windows[winid].first = glutGetWindow();
140   culooks::Windows[winid].second->initialized = true;
141   culooks::Windows[winid].second->redisplay = true;
142 }
143
144 void culooks::drawing::idleFunc()
145 {
146   int activeWindow = glutGetWindow();
147
148   for (int iwin=0; iwin < culooks::Windows.size(); iwin++) {
149     if (!culooks::Windows[iwin].second->initialized)
150       initWindow(iwin);
151     if (culooks::Windows[iwin].second->redisplay) {
152       glutSetWindow(culooks::Windows[iwin].first);
153       glutPostRedisplay();
154       culooks::Windows[iwin].second->redisplay = false;
155     }
156   }
157
158   glutSetWindow(activeWindow);
159 }
160
161 void* culooks::drawing::glutThread(void *_comArg)
162 {
163   comarg *comArg = (comarg*)_comArg;
164   glutInit(comArg->argc, comArg->argv);
165   initWindow(0);
166   glutMainLoop();
167 }