-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelerView.cpp
executable file
·219 lines (177 loc) · 4.99 KB
/
ModelerView.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
#include "ModelerView.h"
#include "camera.h"
#include "modelerapp.h"
#include <FL/Fl.H>
#include <FL/Fl_Gl_Window.H>
#include <FL/gl.h>
#include <cstdio>
#ifdef __APPLE__
# include <OpenGL/glu.h>
#else
# include <GL/glu.h>
#endif
// Accessing the values of sliders is a very lengthy function call.
// We use a macro VAL() to shorten it.
#define VAL(x) ( static_cast< float >( ModelerApplication::Instance()->GetControlValue( x ) ) )
ModelerView::ModelerView(int x, int y, int w, int h,
const char *label):Fl_Gl_Window(x, y, w, h, label)
{
m_camera = new Camera();
m_camera->SetDimensions( w, h );
m_camera->SetDistance( 2 );
m_camera->SetCenter( Vector3f( 0.5, 0.5, 0.5 ) );
m_drawAxes = true;
m_drawSkeleton = true;
}
// If you want to load files, etc, do that here.
void ModelerView::loadModel(int argc, char* argv[])
{
glutInit( &argc, argv );
// Load the model based on the command-line arguments
string prefix = argv[ 1 ];
string skeletonFile = prefix + ".skel";
string meshFile = prefix + ".obj";
string attachmentsFile = prefix + ".attach";
model.load(skeletonFile.c_str(), meshFile.c_str(), attachmentsFile.c_str());
}
ModelerView::~ModelerView()
{
delete m_camera;
}
int ModelerView::handle( int event )
{
unsigned eventCoordX = Fl::event_x();
unsigned eventCoordY = Fl::event_y();
unsigned eventButton = Fl::event_button();
unsigned eventState = Fl::event_state();
switch( event )
{
case FL_PUSH:
{
switch (eventButton)
{
case FL_LEFT_MOUSE:
m_camera->MouseClick( Camera::LEFT, eventCoordX, eventCoordY );
break;
case FL_MIDDLE_MOUSE:
m_camera->MouseClick( Camera::MIDDLE, eventCoordX, eventCoordY );
break;
case FL_RIGHT_MOUSE:
m_camera->MouseClick( Camera::RIGHT, eventCoordX, eventCoordY );
break;
}
}
break;
case FL_DRAG:
{
m_camera->MouseDrag(eventCoordX, eventCoordY);
}
break;
case FL_RELEASE:
{
m_camera->MouseRelease(eventCoordX, eventCoordY);
}
break;
case FL_KEYUP:
{
unsigned key = Fl::event_key();
if( key == 'a' )
{
m_drawAxes = !m_drawAxes;
cout << "drawAxes is now: " << m_drawAxes << endl;
}
else if( key == 's' )
{
m_drawSkeleton = !m_drawSkeleton;
cout << "drawSkeleton is now: " << m_drawSkeleton << endl;
}
}
break;
default:
return 0;
}
redraw();
return 1;
}
void ModelerView::update()
{
// update the skeleton from sliders
updateJoints();
// Update the bone to world transforms for SSD.
model.updateCurrentJointToWorldTransforms();
// update the mesh given the new skeleton
model.updateMesh();
}
void ModelerView::updateJoints()
{
for(unsigned int jointNo = 0; jointNo < 18; jointNo++)
{
float rx = VAL( jointNo * 3 );
float ry = VAL( jointNo * 3 + 1 );
float rz = VAL( jointNo * 3 + 2 );
model.setJointTransform(jointNo, rx, ry, rz);
}
}
// Call the draw function of the parent. This sets up the
// viewport, projection, and camera position, as well as some
// default lighting parameters.
void ModelerView::draw()
{
// Window is !valid() upon resize
// FLTK convention has you initializing rendering here.
if( !valid() )
{
// Setup opengl
glShadeModel( GL_SMOOTH );
glEnable( GL_DEPTH_TEST );
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_NORMALIZE );
m_camera->SetDimensions(w(),h());
m_camera->SetViewport(0,0,w(),h());
m_camera->ApplyViewport();
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
m_camera->SetPerspective( 50.0f );
glLoadMatrixf( m_camera->projectionMatrix() );
}
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Note that the lighting is applied *before* applying the camera
// transform. This is so the light appeared fixed on the camera.
GLfloat Lt0diff[] = {1.0,1.0,1.0,1.0};
GLfloat Lt0pos[] = {3.0,3.0,5.0,1.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, Lt0diff);
glLightfv(GL_LIGHT0, GL_POSITION, Lt0pos);
// These are just some default material colors
GLfloat diffColor[] = {0.4f, 0.4f, 0.4f, 1.f};
GLfloat specColor[] = {0.6f, 0.6f, 0.6f, 1.f};
GLfloat shininess[] = {50.0f};
glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffColor );
glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, specColor );
glMaterialfv( GL_FRONT_AND_BACK, GL_SHININESS, shininess );
// Load the camera view matrix
glLoadMatrixf( m_camera->viewMatrix() );
if(m_drawAxes)
{
drawAxes();
}
model.draw( m_camera->viewMatrix(), m_drawSkeleton );
}
void ModelerView::drawAxes()
{
glDisable( GL_LIGHTING );
glBegin( GL_LINES );
glColor3f( 1, 0, 0 );
glVertex3f( 0, 0, 0 );
glVertex3f( 1, 0, 0 );
glColor3f( 0, 1, 0 );
glVertex3f( 0, 0, 0 );
glVertex3f( 0, 1, 0 );
glColor3f( 0, 0, 1 );
glVertex3f( 0, 0, 0 );
glVertex3f( 0, 0, 1 );
glEnd();
glEnable( GL_LIGHTING );
}