-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvxScene.cpp
236 lines (187 loc) · 5.29 KB
/
vxScene.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
*
* file vxScene.cpp
*
* This source file is a part of VoxelBrain software.
*
* (c) Nanyang Technological University
*
* Author: Konstantin Levinski
*
*
*/
#include <map>
#include <stdio.h>
#include <stdlib.h> //For getenv().
#include "vxOpenGlTools.h"
#include "vxLighting.h"
#include "vxDrawPlane.h"
#include "vxProjection.h"
#include "vxAction.h"
#include "vxMotion.h"
#include "vxScene.h"
using namespace std;
/*
Action bindings.
*/
///
///KEY-action relationship
int current_key = 0;
Action * current_action = NULL;
/*
Uniform treatment of actions.
*/
void InvokeAction(int id);
void RepeatAction(int x, int y);
void FinishAction(int id);
void GLFWCALL GLFWWindowSizeCb(int,int){ };
int GLFWCALL GLFWWindowCloseCb(void){ return 1; };
void GLFWCALL GLFWWindowRefreshCb(void){ };
void GLFWCALL GLFWMouseButtonCb(int,int){ };
void GLFWCALL GLFWMouseWheelCb(int pos){ };
void GLFWCALL GLFWCharCb(int,int){ };
void GLFWCALL GLFWMousePosCb(int x,int y){RepeatAction(x,y);};
void GLFWCALL GLFWKeyCb(int key, int state){
if(state == GLFW_PRESS){
InvokeAction(key); // Start action associated with the key.
}else{
FinishAction(key); // Stop action (if the current key is correct).
};
};
void setupCallbacks(){
glfwSetWindowSizeCallback( GLFWWindowSizeCb );
glfwSetWindowCloseCallback( GLFWWindowCloseCb );
glfwSetWindowRefreshCallback( GLFWWindowRefreshCb );
glfwSetKeyCallback( GLFWKeyCb );
glfwSetCharCallback( GLFWMousePosCb );
glfwSetMouseButtonCallback( GLFWMouseButtonCb );
glfwSetMousePosCallback( GLFWMousePosCb );
glfwSetMouseWheelCallback( GLFWMouseWheelCb );
};
//Actions:
struct HomingAction: Action {
int interactive_change;
HomingAction(): interactive_change(0){};
void Reset(){
//only change every 30 clicks.
GetProjection()->Reset ( interactive_change / 30 );
}
void Start() { Reset(); };
void Do() {
interactive_change += GetMotion()->dx + GetMotion()->dy;
Reset();
};
} homing_action;
struct ZoomingAction: Action {
int start_x, start_y;
void Start(){
start_x = GetMotion()->x; start_y = GetMotion()->y;
};
void Do(){
Motion * m = GetMotion();
float dx2 = (start_x- m->x)*(start_x - m->x);
float dy2 = (start_y- m->y)*(start_y - m->y);
float new_zoom = 100.0/(100.0+sqrt(dx2+dy2));
GetProjection()->Zoom( start_x, start_y, new_zoom);
};
} zooming_action;
struct RotationAction: Action {
void Do(){
GetProjection()->Rotate( GetMotion()->dy*0.01, GetMotion()->dx*0.01);
};
} rotation_action;
//check if fullscreen wanted
bool Fullscreen(){
return NULL == getenv("VXX_NO_FULLSCREEN");
};
Scene * Scene::run(Action * to_draw){
glfwInit();
//Finding resolution
const int maxmodes=100;
GLFWvidmode modes[maxmodes];
int pixels = 0;
int better_mode = 0;
int total_modes = glfwGetVideoModes(modes, maxmodes);
for(int i = 0; i < total_modes; i++){
int cur_pixels = modes[i].Width*modes[i].Height;
if(cur_pixels > pixels){
better_mode = i;
pixels = cur_pixels;
};
};
//Setting the mode.
float FS=Fullscreen()?1:0.25; //use all or half a screen;
if( !glfwOpenWindow( modes[better_mode].Width*FS,
modes[better_mode].Height*FS,
0,0,0,0, 16,0,
Fullscreen()?GLFW_FULLSCREEN:GLFW_WINDOW ) )
{
glfwTerminate();
}
glfwEnable(GLFW_MOUSE_CURSOR);
glfwSetWindowTitle( "3D" );
glfwEnable( GLFW_STICKY_KEYS );
glfwEnable( GLFW_MOUSE_CURSOR );
glfwDisable( GLFW_AUTO_POLL_EVENTS );
setupCallbacks();
rotation_action.bind( GLFW_KEY_LCTRL );
homing_action.bind( GLFW_KEY_TAB );
zooming_action.bind( GLFW_KEY_LSHIFT );
do //Main Loop.
{
GetProjection()->Draw();
glDisable (GL_BLEND);
glDisable(GL_LIGHTING);
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT |
GL_STENCIL_BUFFER_BIT);
//reference plane
DrawPlane(V3f(0,0,0), V3f(20,0,0), V3f(0,20,0), 5);
DrawLighting();
to_draw->Draw();
glfwSwapBuffers();
if(current_action){
glfwPollEvents(); // Action is most probably changing something.
}else{ // No action, so image is probably static.
glfwWaitEvents(); // Lock and wait for a new event.
};
}
while(!glfwGetKey( GLFW_KEY_ESC ) &&
glfwGetWindowParam( GLFW_OPENED ));
return this;
};
//Scene:
Scene the_scene;
Scene * GetScene(){
return &the_scene;
};
//Action implementation:
void InvokeAction(int id){
if(current_key != 0) return; // Another action is in progress.
Action * in = GetAction(id);
if(!in) return; //Don't know what to do with this action.
current_key = id; //remeber what key we are processing.
current_action = in;
int x, y; glfwGetMousePos( &x, &y);
GetMotion()->UpdateRay()->Init(x, y);
in->Start();
in->Do();
};
void RepeatAction(int x, int y){
if(!current_key)return;
Action * in = GetAction(current_key); if(!in) return; //Don't know what to do with this action.
glfwGetMousePos( &x, &y);
GetMotion()->UpdateRay()->Move(x, y);
in->Do();
};
void FinishAction(int id){
if(current_key != id) return; // Nothing to finish
Action * in = GetAction(id); if(!in) return;
int x, y; glfwGetMousePos( &x, &y);
GetMotion()->UpdateRay()->Move( x, y);
in->Do();
GetMotion()->Move( x, y);
in->End();
current_key = 0;
};
// End of vxScene.cpp