Initial revision
[lsystem3d.git] / src / callbacks.cpp
1 // Copyright (C) 2006 Erik Dahlberg
2 //
3 // This file is part of LSystem3d.
4 //
5 // LSystem3D is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // LSystem3D is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with LSystem3D; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
20
21
22 #include <unistd.h>
23
24 #include <GL/freeglut.h>
25
26 #include "callbacks.h"
27 #include "openglwindow.h"
28
29 extern OpenGLWindow *openglWindow;
30
31
32
33 // GLUT callback functions
34 // -----------------------
35
36 /**
37 * Key press
38 * @param key the key
39 * @param x mouse x-coordinate
40 * @param y mouse y-coordinate
41 */
42 void glutKeyboardCB(unsigned char key, int x, int y)
43 {
44 switch (key)
45 {
46 // quit
47 case 27: // escape key
48 glutLeaveMainLoop();
49 break;
50
51 // zoom in
52 case '+':
53 openglWindow->zoomIn();
54 break;
55
56 // zoom out
57 case '-':
58 openglWindow->zoomOut();
59 break;
60
61 // rotate around the y-axis
62 case 'y':
63 case 'Y':
64 openglWindow->rotateY();
65
66 default:
67 break;
68 }
69 }
70
71
72
73 /**
74 * Special key press
75 * @param key the key
76 * @param x mouse x-coordinate
77 * @param y mouse y-coordinate
78 */
79 void glutSpecialCB(int key, int x, int y)
80 {
81 switch (key)
82 {
83 // move camera left
84 case GLUT_KEY_LEFT:
85 openglWindow->left();
86 break;
87
88 // move camera right
89 case GLUT_KEY_RIGHT:
90 openglWindow->right();
91 break;
92
93 // move camera up
94 case GLUT_KEY_UP:
95 openglWindow->up();
96 break;
97
98 // move camera down
99 case GLUT_KEY_DOWN:
100 openglWindow->down();
101 break;
102
103 // move camera forth
104 case GLUT_KEY_PAGE_UP:
105 openglWindow->forth();
106 break;
107
108 // move camera back
109 case GLUT_KEY_PAGE_DOWN:
110 openglWindow->back();
111 break;
112
113 default:
114 break;
115 }
116 }
117
118
119
120 /**
121 * Redraw of screen
122 */
123 void glutDisplayCB()
124 {
125 openglWindow->draw();
126 }
127
128
129
130 /**
131 * Idling
132 */
133 void glutIdleCB()
134 {
135 // spare some cpu cycles
136 usleep(1);
137 }
138
139
140
141 // GLUI callback functions
142 // -----------------------
143
144 /**
145 * Angle changes
146 * @param id not used
147 */
148 void gluiSpinnerAngleCB(int id)
149 {
150 // regenerate and draw l-system only if "live mode" is enabled
151
152 if (openglWindow->liveActivated())
153 {
154 openglWindow->generate();
155 glutPostRedisplay();
156 }
157 }
158
159
160
161 /**
162 * Reset button pressed
163 * @param id not used
164 */
165 void gluiButtonResetCB(int id)
166 {
167 // place camera at initial position
168 openglWindow->defaultView();
169 }
170
171
172
173 /**
174 * Generate button pressed
175 * @param id not used
176 */
177 void gluiButtonGenerateCB(int id)
178 {
179 openglWindow->generate();
180 }