]> git.treefish.org Git - phys/latlib.git/blob - culooks_drawing.cpp
implemented linewidth.
[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   glLineWidth(Win->linewidth);
98
99   glClearColor(Win->bgcolor[0], Win->bgcolor[1], Win->bgcolor[2], Win->bgcolor[3]);
100   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
101     
102   glMatrixMode(GL_MODELVIEW);
103   glLoadIdentity();
104     
105   glScalef(1.0/Win->layout[0],1.0/Win->layout[1],1.0);
106   glTranslatef(-Win->layout[0]+1, -Win->layout[1]+1, 0);
107     
108   for (int icube=0; icube < Win->cubes.size(); icube++) {
109     Win->cubes.at(icube).draw();
110     if ((icube+1)%Win->layout[0] == 0) {
111       glTranslatef(-2*Win->layout[0]+2, 2, 0);
112     }
113     else {
114       glTranslatef(2, 0, 0);
115     }
116   }
117     
118   glutSwapBuffers();
119 }
120  
121 void culooks::drawing::initWindow(int winid)
122 {
123   
124   glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
125   glutInitWindowSize(culooks::Windows[winid].second->w, culooks::Windows[winid].second->h);
126   glutInitWindowPosition(winid*100,winid*100);
127   glutCreateWindow( ("culooks / " + culooks::Windows[winid].second->name).c_str() );
128
129   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
130
131   glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);       
132   glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);      
133   glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
134
135   glutDisplayFunc(&drawing::displayFunc);
136   glutReshapeFunc(&drawing::reshapeFunc);
137   glutMotionFunc(&drawing::motionFunc);
138   glutMouseFunc(&drawing::mouseFunc);
139   glutIdleFunc(&drawing::idleFunc);
140
141   culooks::Windows[winid].first = glutGetWindow();
142   culooks::Windows[winid].second->initialized = true;
143   culooks::Windows[winid].second->redisplay = true;
144 }
145
146 void culooks::drawing::idleFunc()
147 {
148   int activeWindow = glutGetWindow();
149
150   for (int iwin=0; iwin < culooks::Windows.size(); iwin++) {
151     if (!culooks::Windows[iwin].second->initialized)
152       initWindow(iwin);
153     if (culooks::Windows[iwin].second->redisplay) {
154       glutSetWindow(culooks::Windows[iwin].first);
155       glutPostRedisplay();
156       culooks::Windows[iwin].second->redisplay = false;
157     }
158   }
159
160   glutSetWindow(activeWindow);
161 }
162
163 void* culooks::drawing::glutThread(void *_comArg)
164 {
165   comarg *comArg = (comarg*)_comArg;
166   glutInit(comArg->argc, comArg->argv);
167   initWindow(0);
168   glutMainLoop();
169 }