-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuistate.cpp
More file actions
executable file
·188 lines (170 loc) · 4.53 KB
/
uistate.cpp
File metadata and controls
executable file
·188 lines (170 loc) · 4.53 KB
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
// -*- Mode: c++ -*-
// $Id: uistate.cpp,v 1.3 2004/01/31 03:19:19 ps Exp $
// $Source: /cs/research/multires/cvsroot/src/frame/uistate.cpp,v $
// include files
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "uistate.h"
#include "util.h"
// code
UIState::UIState( void )
: p_mousex(-1), p_mousey(-1),
p_windowx(-1), p_windowy(-1),
p_button(0),
p_dstyle(HIDDENLINE),
p_radius( 1 ),
p_trans(0,0,0),
p_ctrans( 0, 0, -( 1 + 1 / sinf( Deg2Rad( 25 ) ) ) ),
p_coldtrans(0,0,0),
p_aspect( 1 ),
p_near( 1 / sinf( Deg2Rad( 25 ) ) ),
p_far( 1 / sinf( Deg2Rad( 25 ) ) + 2 ),
p_fov( 50 ),
p_ball()
{
}
void
UIState::SetupViewport( void )
{
glViewport( 0, 0, p_windowx, p_windowy );
}
void
UIState::SetupViewingFrustum( void )
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( p_fov / std::min( 1.f, p_aspect ), p_aspect, p_near, p_far );
glMatrixMode( GL_MODELVIEW );
}
void
UIState::ApplyViewingTransformation( void )
{
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( p_ctrans.x, p_ctrans.y, p_ctrans.z );
GLfloat M[4][4];
p_ball.Update();
p_ball.Value( M );
// apply model transform
glMultMatrixf( ( float *)M );
// move world to the origin
glTranslatef( -p_trans.x, -p_trans.y, -p_trans.z );
}
void
UIState::OnLButtonDown( const int x, const int y )
{
p_button |= LEFT_BUTTON;
p_ball.Mouse( ( p_aspect>1 ? p_aspect : 1 ) * ( 2.f*x / p_windowx - 1 ),
-( p_aspect>1 ? 1 : 1/p_aspect ) * ( 2.f*y / p_windowy - 1 ) );
switch( glutGetModifiers() ){
case GLUT_ACTIVE_SHIFT:
p_ball.UseSet( ArcBall::CameraAxes );
p_ball.Update();
glutPostRedisplay();
break;
case GLUT_ACTIVE_CTRL:
p_ball.UseSet( ArcBall::BodyAxes );
p_ball.Update();
glutPostRedisplay();
break;
case GLUT_ACTIVE_ALT:
break;
default:
;
}
p_ball.BeginDrag();
}
void
UIState::OnLButtonUp( const int, const int )
{
p_button &= ~LEFT_BUTTON;
p_ball.EndDrag();
p_ball.UseSet( ArcBall::NoAxes );
p_ball.Update();
glutPostRedisplay();
}
void
UIState::OnMButtonDown( const int x, const int y )
{
p_button |= MIDDLE_BUTTON;
p_ball.Mouse( ( p_aspect>1 ? p_aspect : 1 ) * ( 2.f*x / p_windowx - 1 ),
-( p_aspect>1 ? 1 : 1/p_aspect ) * ( 2.f*y / p_windowy - 1 ) );
p_ball.BeginTrans();
p_coldtrans = p_ctrans;
}
void
UIState::OnMButtonUp( const int, const int )
{
p_button &= ~MIDDLE_BUTTON;
glutPostRedisplay();
}
void
UIState::OnRButtonDown( const int x, const int y )
{
p_button |= RIGHT_BUTTON;
p_ball.Mouse( ( p_aspect>1 ? p_aspect : 1 ) * ( 2.f*x / p_windowx - 1 ),
-( p_aspect>1 ? 1 : 1/p_aspect ) * ( 2.f*y / p_windowy - 1 ) );
p_ball.BeginTrans();
p_coldtrans = p_ctrans;
}
void
UIState::OnRButtonUp( const int, const int )
{
p_button &= ~RIGHT_BUTTON;
glutPostRedisplay();
}
void
UIState::ResetModelTransform( void )
{
p_ctrans = glm::vec3( 0, 0, -p_radius * ( 1.f + 1 / sinf( Deg2Rad( p_fov/2 ) ) ) ),
p_ball.Reset();
}
void UIState::MouseFunction(int button, int state, int x, int y)
{
switch( button ){
case GLUT_LEFT_BUTTON:
if( state == GLUT_DOWN ) OnLButtonDown( x, y );
else if( state == GLUT_UP ) OnLButtonUp( x, y );
break;
case GLUT_MIDDLE_BUTTON:
if( state == GLUT_DOWN ) OnMButtonDown( x, y );
else if( state == GLUT_UP ) OnMButtonUp( x, y );
break;
case GLUT_RIGHT_BUTTON:
if( state == GLUT_DOWN ) {
OnRButtonDown( x, y );
p_mousex = x;
p_mousey = y;
}
else if( state == GLUT_UP ) OnRButtonUp( x, y );
break;
default:
break;
}
}
void
UIState::MotionFunction(const int x, const int y)
{
Ball().Mouse( ( Aspect() > 1 ? Aspect() : 1 )
* ( 2.f * x / WindowX() - 1 ),
-( Aspect() > 1 ? 1 : 1/Aspect() )
* ( 2.f * y / WindowY() - 1 ) );
if( ButtonM() ){
// translation case
glm::vec3 t = Ball().Trans();
CTrans().z = ( 1 - 2 * t.y ) * COldTrans().z;
}
if( ButtonR() ){
CTrans().x += .005 * (float)(x - p_mousex);
CTrans().y += .005 * (float)(p_mousey - y);
p_mousex = x;
p_mousey = y;
}
glutPostRedisplay();
}