]> git.treefish.org Git - phys/latlib.git/blob - culooks_drawing.cpp
...
[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(0,0,0,0);
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   glutInitWindowSize(culooks::Windows[winid].second->w, culooks::Windows[winid].second->h);
122   glutInitWindowPosition(winid*100,winid*100);
123   glutCreateWindow( ("culooks / " + culooks::Windows[winid].second->name).c_str() );
124     
125   glutDisplayFunc(&drawing::displayFunc);
126   glutReshapeFunc(&drawing::reshapeFunc);
127   glutMotionFunc(&drawing::motionFunc);
128   glutMouseFunc(&drawing::mouseFunc);
129
130   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
131   glEnable( GL_BLEND );
132
133   culooks::Windows[winid].first = glutGetWindow();
134
135   culooks::Windows[winid].second->initialized = true;
136 }
137
138 void culooks::drawing::idleFunc_master()
139 {
140   for (int iwin=0; iwin < culooks::Windows.size(); iwin++)
141     if (!culooks::Windows[iwin].second->initialized)
142       initWindow(iwin);
143 }
144
145 void* culooks::drawing::glutThread(void *leer)
146 {
147   initWindow(0);
148   glutIdleFunc(&idleFunc_master);
149   glutMainLoop();
150 }