// We are going to override (is that the right word?) the draw()
// method of ModelerView to draw out MyModel
void MyModel::draw()
{
	// This call takes care of a lot of the nasty projection 
	// matrix stuff.  Unless you want to fudge directly with the 
	// projection matrix, don't bother with this ...

	ModelerView::draw();


	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	ChooseCostume(VAL(COSTUME));

	
		LocateBody(VAL(XPOS), VAL(YPOS), VAL(ZPOS), VAL(ROTATE));
		RotateHead(0, VAL(NECK) + 180, 0);
		int SType = VAL(SLASH_TYPE);
		switch (SType){
		case VERTICAL:
			RotateExcalibur(0.0, 90.0, 2*LOWER_ARM_SLASH*VAL(SLASH) < 40.0 ? 2*LOWER_ARM_SLASH*VAL(SLASH) : 40.0, "yzx");
			RotateRightUpperArm(1.5*VAL(SLASH), 0.0, 0.0);
			RotateRightLowerArm(LOWER_ARM_SLASH*VAL(SLASH), 0.0, 2*LOWER_ARM_SLASH*VAL(SLASH) < 40.0 ? -2*LOWER_ARM_SLASH*VAL(SLASH) : -40.0, "zxy");
			RotateLeftUpperArm(1.5*VAL(SLASH), 0.0, 0.0);
			RotateLeftLowerArm(LOWER_ARM_SLASH*VAL(SLASH), 0.0, 2*LOWER_ARM_SLASH*VAL(SLASH) < 40.0 ? 2*LOWER_ARM_SLASH*VAL(SLASH) : 40.0, "zxy");
			break;
		case STAND:
			RotateExcalibur(VAL(SLASH)>75?-VAL(SLASH)*2+25:-VAL(SLASH)*125.0/75.0, 0.0,VAL(SLASH) < 40.0 ? VAL(SLASH) : 40.0 , "xyz");
			RotateRightUpperArm(1.1*VAL(SLASH), 0.0, 0.0);
			RotateRightLowerArm(0.6*VAL(SLASH), 0.0, VAL(SLASH) < 40.0 ? -VAL(SLASH) : -40.0, "zxy");
			RotateLeftUpperArm(1.1*VAL(SLASH), 0.0, 0.0);
			RotateLeftLowerArm(0.6*VAL(SLASH), 0.0, VAL(SLASH) < 40.0 ? VAL(SLASH) : 40.0, "zxy");
			break;
		case SKEW:
			if (VAL(SLASH) < 45.0){
				RotateExcalibur(0.0, 0.0, 70.0 * VAL(SLASH) / 45.0, "xyz");
				RotateRightUpperArm(90.0*VAL(SLASH) / 45.0, -30.0 * VAL(SLASH) / 45.0, 0.0, "xyz");
				RotateRightLowerArm(110.0*VAL(SLASH) / 45.0, 30.0*VAL(SLASH) / 45.0, 0.0, "xyz");
				RotateLeftUpperArm(90.0*VAL(SLASH) / 45.0, -45.0*VAL(SLASH) / 45.0, 0.0, "xyz");
				RotateLeftLowerArm(65.0*VAL(SLASH) / 45.0, -15.0*VAL(SLASH) / 45.0, 0.0, "xyz");
			}
			else{
				RotateExcalibur(0.0, 90.0*(VAL(SLASH) - 45.0) / 45.0, 70.0 *(1 - (VAL(SLASH) - 45.0) / 45.0), "xyz");
				RotateRightUpperArm(90.0 - 30.0*(VAL(SLASH) - 45.0) / 45.0, -30.0 + 55.0 * (VAL(SLASH) - 45.0) / 45.0, 0.0, "xyz");
				RotateRightLowerArm(110.0 - 110.0*(VAL(SLASH) - 45.0) / 45.0, 30.0 - 30.0*(VAL(SLASH) - 45.0) / 45.0, 0.0, "xyz");
				RotateLeftUpperArm(90.0 - 40.0*(VAL(SLASH) - 45.0) / 45.0, -45.0 + 30.0*(VAL(SLASH) - 45.0) / 45.0, 0.0, "xyz");
				RotateLeftLowerArm(65.0 - 50.0*(VAL(SLASH) - 45.0) / 45.0, -15.0 + 15.0*(VAL(SLASH) - 45.0) / 45.0, 10.0*(VAL(SLASH) - 45.0) / 45.0, "xyz");
				break;
			}
		case SINGLE:
			if (VAL(SLASH) < 45.0){
				RotateExcalibur(0.0, 0.0, 70.0 * VAL(SLASH) / 45.0, "xyz");
				RotateRightUpperArm(90.0*VAL(SLASH) / 45.0, -30.0 * VAL(SLASH) / 45.0, 0.0, "xyz");
				RotateRightLowerArm(110.0*VAL(SLASH) / 45.0, 30.0*VAL(SLASH) / 45.0, 0.0, "xyz");
			}
			else{
				RotateExcalibur(0.0, -90.0*(VAL(SLASH) - 45.0) / 45.0, 70.0 *(1 - (VAL(SLASH) - 45.0) / 45.0), "xyz");
				RotateRightUpperArm(90.0, -30.0 + 75.0 * (VAL(SLASH) - 45.0) / 45.0, 0.0, "xyz");
				RotateRightLowerArm(110.0 - 110.0*(VAL(SLASH) - 45.0) / 45.0, 30.0 + 60.0*(VAL(SLASH) - 45.0) / 45.0, 0.0, "xyz");
			}
				break;
		}
		
		
	treeRoot->RootRender();
	
	if (valid()){
		ParticleSystem *ps = ModelerApplication::Instance()->GetParticleSystem();
		if (ps != NULL) {
			ps->computeForcesAndUpdateParticles(t);
			ps->drawParticles(t);
		}
	}

	endDraw();
}
/********************************************************
 *
 *
 * Draw Robot
 *
 *
 ********************************************************/
void Virtual3dCharacterViewer::glDrawBody(Character3DBody& body)
{
    glPushMatrix();


    // getHipPos() ; // get Hip's Position from the IK calculator .
    LocateHip (body);
    body.HipPosInCamera = getCurTranslation () ;
    body.HipPosInScreen = getScreenPos(body.HipPosInCamera) ;
    RotateHip(body) ;
    glPushMatrix();     // save the Hip Matrix
    drawHip (body) ;

    glTranslatef( 0.0, body.Model.LenHipToChest, 0.0 ) ;
    RotateChest (body);
    drawChest (body) ;


    glTranslatef(0.0, body.Model.LenChestToNeck, 0.0);

    body.NeckPosInCamera = getCurTranslation () ;
    body.NeckPos = TransformVector( WorldMatInv, body.NeckPosInCamera ) ;
    body.NeckPosInScreen = getScreenPos(body.NeckPosInCamera) ;


    glPushMatrix();     // save the Neck Matrix

    RotateLeftShoulder(body);
    drawLeftShoulder(body);

    glTranslatef(body.Model.LenNeckToShoulder, 0.0, 0.0);
    RotateLeftUpperArm(body);
    drawLeftUpperArm(body);


    glTranslatef(body.Model.LenShoulderToElbow, 0.0, 0.0);
    RotateLeftLowerArm(body);
    drawLeftLowerArm(body);



    glTranslatef(body.Model.LenElbowToWrist, 0.0, 0.0);

    body.LeftWristPosInCamera = getCurTranslation () ;
    Vector3D bias(-1,0,0) ;
    body.LeftWristPos = TransformVector( WorldMatInv, body.LeftWristPosInCamera );
    body.LeftWristPosInScreen = getScreenPos(body.LeftWristPosInCamera) ;

    RotateLeftHand(body);
    drawLeftHand(body);


    glPopMatrix();      // reload the Neck Matrix
    glPushMatrix();     // restore the Neck Matrix again .


    RotateRightShoulder(body);
    drawRightShoulder(body);

    glTranslatef(-body.Model.LenNeckToShoulder, 0.0, 0.0);
    RotateRightUpperArm(body);
    drawRightUpperArm(body);


    glTranslatef(-body.Model.LenShoulderToElbow, 0.0, 0.0);
    RotateRightLowerArm(body);
    drawRightLowerArm(body);

    glTranslatef(-body.Model.LenElbowToWrist, 0.0, 0.0);

    body.RightWristPosInCamera = getCurTranslation () ;
    body.RightWristPos = TransformVector( WorldMatInv, body.RightWristPosInCamera ) ;
    body.RightWristPosInScreen = getScreenPos(body.RightWristPosInCamera) ;

    RotateRightHand(body);
    drawRightHand(body);


    glPopMatrix();      // reload the Neck Matrix again .

    RotateHead(body);
    drawHead (body);
    glTranslatef(0.0, body.Model.LenNeckToHead, 0.0);
    body.HeadPosInCamera = getCurTranslation () ;
    body.HeadPos = TransformVector( WorldMatInv, body.HeadPosInCamera ) ;
    body.HeadPosInScreen = getScreenPos(body.HeadPosInCamera) ;

    glPopMatrix();      // reload the Hip Matrix .
    glPushMatrix();     // restore the Hip Matrix again.

    RotateLeftThighRoot (body);
    drawLeftThighRoot(body);

    glTranslatef(body.Model.LenHipToThigh, 0.0, 0.0);

    RotateLeftThigh (body) ;
    drawLeftThigh(body);

    glTranslatef(0.0, -body.Model.LenThighToKnee, 0.0);

    RotateLeftShank(body);
    drawLeftShank(body);

    glTranslatef(0.0, -body.Model.LenKneeToAnkle, 0.0);
    body.LeftAnklePosInCamera = getCurTranslation();
    body.LeftAnklePos = TransformVector( WorldMatInv, body.LeftAnklePosInCamera ) ;
    body.LeftAnklePosInScreen = getScreenPos(body.LeftAnklePosInCamera) ;

    RotateLeftFoot (body);
    drawLeftFoot(body);


    glPopMatrix();      // reload the Hip Matrix again.


    RotateRightThighRoot (body);
    drawRightThighRoot(body);

    glTranslatef(-body.Model.LenHipToThigh, 0.0, 0.0);

    RotateRightThigh (body) ;
    drawRightThigh(body);

    glTranslatef(0.0, -body.Model.LenThighToKnee, 0.0);

    RotateRightShank(body);
    drawRightShank(body);

    glTranslatef(0.0, -body.Model.LenKneeToAnkle, 0.0);
    body.RightAnklePosInCamera = getCurTranslation();
    body.RightAnklePos = TransformVector( WorldMatInv, body.RightAnklePosInCamera ) ;
    body.RightAnklePosInScreen = getScreenPos(body.RightAnklePosInCamera) ;



    RotateRightFoot (body);
    drawRightFoot(body);


    glPopMatrix();
}