11#include < windows.h>
22#include < gl/gl.h>
33#include < gl/glut.h>
4+ #include < algorithm>
45#include " math.h"
56#include " Character.h"
67
7- #define KEY_FRAME_NUM 5
8- #define JOINT_NUM 8
8+ #define KEY_FRAME_NUM 4
99
10- Character::Character ()
11- : Entity(glutGet(GLUT_WINDOW_WIDTH) / 10, glutGet(GLUT_WINDOW_HEIGHT) / 4, 30, 50, 0 ) {
10+ Character::Character (Model* models[KEY_FRAME_NUM - 1 ] )
11+ : Entity(glutGet(GLUT_WINDOW_WIDTH) / 10, glutGet(GLUT_WINDOW_HEIGHT) / 4, 0, 0, NULL ) {
1212 jumpSpeed = 14 ;
1313 lowjumpSpeed = 10 ;
1414 jumpState = false ;
15-
16- upArmAngleL = 0 .0f ;
17- upArmAngleR = 0 .0f ;
18- lowArmAngleL = 0 .0f ;
19- lowArmAngleR = 0 .0f ;
20- upLegAngleL = 0 .0f ;
21- upLegAngleR = 0 .0f ;
22- lowLegAngleL = 0 .0f ;
23- lowLegAngleR = 0 .0f ;
2415
16+ if (models != nullptr ) {
17+ model = models[0 ];
18+ copy (models, models + KEY_FRAME_NUM - 1 , this ->models );
19+ width = 20 ; // can't read model's width height now..
20+ height = 60 ;
21+ }
2522 time = 0 ;
2623 currentKeyFrame = 0 ;
2724}
2825
26+ Character::~Character () {
27+ for (int i = 0 ; i < KEY_FRAME_NUM - 1 ; i++) {
28+ delete models[i];
29+ models[i] = nullptr ;
30+ }
31+ }
32+
2933void Character::draw () {
30- glPushMatrix ();
31- // to world coordinate
32- glTranslatef (x, y, 0 );
33- glScalef (7 , 3 , 7 );
34- // to make character stand up at model coordinate origin
35- glTranslatef (2 , 0 , 0 );
36- glRotatef (90 , 0 , 1 , 0 );
37- glTranslatef (0 , 8 , 0 );
38-
39- drawHead ();
40- drawTorso ();
41- drawLeftArm ();
42- drawRightArm ();
43- drawPelvis ();
44- glPopMatrix ();
34+ glColor3f (0 .9f , 0 .9f , 0 .8f );
35+ Entity::draw ();
4536}
4637
4738void Character::jump () {
@@ -68,15 +59,17 @@ void Character::setfall() {
6859 jumpState = true ;
6960}
7061
71- void Character::stop (Entity* ground ) {
62+ void Character::stop (float groundHeight ) {
7263 speed = 0 ;
7364 jumpState = false ;
74- y = ground-> getHeight () ;
65+ y = groundHeight ;
7566}
7667
7768void Character::stepMush () {
7869 speed = 7 ;
70+ jumpState = true ;
7971}
72+
8073void Character::sink () {
8174 y -= 10 ;
8275}
@@ -93,216 +86,22 @@ float Character::getPositionY() {
9386 return y;
9487}
9588
96- float * Character::interpolateKeyframes ( double & time )
89+ void Character::animation ( void (*t)( int ) )
9790{
98- float * angles = new float [JOINT_NUM];
91+ time += 0.1 ;
92+ time = fmod (time, keyFrameTimes[KEY_FRAME_NUM - 1 ]);
9993 int currentKeyFrame = 0 ;
94+
10095 while (time > keyFrameTimes[currentKeyFrame + 1 ])
10196 {
10297 currentKeyFrame++;
98+ if (currentKeyFrame == KEY_FRAME_NUM) {
99+ currentKeyFrame--;
100+ break ;
101+ }
103102 }
104- int nextKeyFrame = (currentKeyFrame + 1 ) % KEY_FRAME_NUM;
105- float t = (time - keyFrameTimes[currentKeyFrame]) / fabs (keyFrameTimes[nextKeyFrame] - keyFrameTimes[currentKeyFrame]);
106-
107- for (int i = 0 ; i < JOINT_NUM; i++) {
108- angles[i] = keyFrameAngles[currentKeyFrame][i] + t * (keyFrameAngles[nextKeyFrame][i] - keyFrameAngles[currentKeyFrame][i]);
109- }
110- return angles;
111- }
112-
113- void Character::animation (void (*t)(int ))
114- {
115- time += 0.1 ;
116- time = fmod (time, keyFrameTimes[KEY_FRAME_NUM - 1 ]);
117- float * angles = interpolateKeyframes (time);
118-
119- upArmAngleL = angles[0 ];
120- upArmAngleR = angles[1 ];
121- lowArmAngleL = angles[2 ];
122- lowArmAngleR = angles[3 ];
123- upLegAngleL = angles[4 ];
124- upLegAngleR = angles[5 ];
125- lowLegAngleL = angles[6 ];
126- lowLegAngleR = angles[7 ];
127-
128- glutPostRedisplay ();
129- }
130-
131- void Character::drawHead ()
132- {
133- glPushMatrix ();
134- // neck
135- glColor3f (1 , 0 , 0 );
136- glTranslatef (0 , 4.5 , 0 );
137- glutSolidCube (1 );
138- // head
139- glPushMatrix ();
140- glColor3f (0 , 0 , 1 );
141- glTranslatef (0 , 1.5 , 0 );
142- glScalef (2 , 1.5 , 1 );
143- glutSolidSphere (1 , 18 , 18 );
144- glPopMatrix ();
145- glPopMatrix () ;
146- }
147-
148- void Character::drawTorso ()
149- {
150- glPushMatrix ();
151- glColor3f (0 , 0 , 1 );
152- glTranslatef (0 , 2 , 0 );
153- glScalef (3 , 4 , 2 );
154- glutSolidCube (1 );
155- glPopMatrix ();
156- }
157-
158- void Character::drawLeftArm ()
159- {
160- glPushMatrix ();
161- glColor3f (0 , 1 , 0 );
162-
163- // Transform
164- glTranslatef (0.0 , 4.0 , 0.0 );
165- glRotatef (upArmAngleL, 1 , 0 , 0 ); // arm animation
166-
167- // Modeling
168- glTranslatef (-2 , -1.5 , 0 );
169-
170- // upper arm
171- glPushMatrix ();
172- glScalef (1 , 3 , 1 );
173- glutSolidCube (1.0 );
174- glPopMatrix ();
175- // lower arm
176- glPushMatrix ();
177- glTranslatef (0.0 , -1.5 , 0.0 );
178- glRotatef (lowArmAngleL, 1 , 0 , 0 ); // arm animation
179- glTranslatef (0.0 , -1.5 , 0.0 );
180- // hand
181- glPushMatrix ();
182- glColor3f (0 , 0 , 0 );
183- glTranslatef (0.0 , -2 , 0.0 );
184- glutSolidSphere (0.5 , 18 , 18 );
185- glPopMatrix ();
186- glColor3f (0 , 1 , 0 );
187- glScalef (1 , 3 , 1 );
188- glutSolidCube (1.0 );
189- glPopMatrix ();
190-
191- glPopMatrix ();
192- }
193-
194- void Character::drawRightArm ()
195- {
196- glPushMatrix ();
197- glColor3f (0 , 1 , 0 );
198-
199- // Transform
200- glTranslatef (0.0 , 4.0 , 0.0 );
201- glRotatef (upArmAngleR, 1 , 0 , 0 ); // arm animation
202-
203- // Modeling
204- glTranslatef (2 , -1.5 , 0 );
205-
206- // upper arm
207- glPushMatrix ();
208- glScalef (1 , 3 , 1 );
209- glutSolidCube (1.0 );
210- glPopMatrix ();
211- // lower arm
212- glPushMatrix ();
213- glTranslatef (0.0 , -1.5 , 0.0 );
214- glRotatef (lowArmAngleR, 1 , 0 , 0 ); // arm animation
215- glTranslatef (0.0 , -1.5 , 0.0 );
216- // hand
217- glPushMatrix ();
218- glColor3f (0 , 0 , 0 );
219- glTranslatef (0.0 , -2 , 0.0 );
220- glutSolidSphere (0.5 , 18 , 18 );
221- glPopMatrix ();
222- glColor3f (0 , 1 , 0 );
223- glScalef (1 , 3 , 1 );
224- glutSolidCube (1.0 );
225- glPopMatrix ();
226-
227- glPopMatrix ();
103+ if (!jumpState)
104+ Entity::model = models[currentKeyFrame];
105+ else
106+ Entity::model = models[0 ];
228107}
229-
230- void Character::drawPelvis ()
231- {
232- glPushMatrix ();
233- glColor3f (0 , 0 , 1 );
234- glTranslatef (0 , -0.5 , 0 );
235- glutSolidCube (1 );
236-
237- drawLeftLeg ();
238- drawRightLeg ();
239- glPopMatrix ();
240- }
241-
242- void Character::drawLeftLeg ()
243- {
244- glPushMatrix ();
245- glColor3f (0 , 1 , 1 );
246-
247- // Transform
248- glRotatef (upLegAngleL, 1 , 0 , 0 ); // leg animation
249-
250- // Modeling
251- // upper leg
252- glTranslatef (-1 , -2.0 , 0 );
253- glPushMatrix ();
254- glScalef (1 , 4 , 1 );
255- glutSolidCube (1 );
256- glPopMatrix ();
257- // lower leg
258- glPushMatrix ();
259- glTranslatef (0 , -2.0 , 0 );
260- glRotatef (lowLegAngleL, 1 , 0 , 0 ); // leg animation
261- glTranslatef (0 , -1.5 , 0 );
262- // foot
263- glPushMatrix ();
264- glTranslatef (0.0 , -2.0 , 0.5 );
265- glScalef (1 , 1 , 2.0 );
266- glColor3f (0 , 0 , 0 );
267- glutSolidCube (1 );
268- glPopMatrix ();
269- glColor3f (0 , 1 , 1 );
270- glScalef (1 , 3 , 1 );
271- glutSolidCube (1 );
272- glPopMatrix ();
273- glPopMatrix ();
274- }
275-
276- void Character::drawRightLeg ()
277- {
278- glPushMatrix ();
279- glColor3f (0 , 1 , 1 );
280-
281- // Transform
282- glRotatef (upLegAngleR, 1 , 0 , 0 ); // leg animation
283-
284- // Modeling
285- // upper leg
286- glTranslatef (1 , -2.0 , 0 );
287- glPushMatrix ();
288- glScalef (1 , 4 , 1 );
289- glutSolidCube (1 );
290- glPopMatrix ();
291- // lower leg
292- glPushMatrix ();
293- glTranslatef (0 , -2.0 , 0 );
294- glRotatef (lowLegAngleR, 1 , 0 , 0 ); // leg animation
295- glTranslatef (0 , -1.5 , 0 );
296- // foot
297- glPushMatrix ();
298- glTranslatef (0.0 , -2.0 , 0.5 );
299- glScalef (1 , 1 , 2.0 );
300- glColor3f (0 , 0 , 0 );
301- glutSolidCube (1 );
302- glPopMatrix ();
303- glColor3f (0 , 1 , 1 );
304- glScalef (1 , 3 , 1 );
305- glutSolidCube (1 );
306- glPopMatrix ();
307- glPopMatrix ();
308- }
0 commit comments