www.pudn.com > rayman.zip > Player.cpp
// Player.cpp: implementation of the Player class.
//
//////////////////////////////////////////////////////////////////////
#include "Player.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Player::Player()
{
md3Scale=0.025f;
md3YOffset=0.6f;
md3HeadingOffset=90.0f;
}
Player::~Player()
{
}
Player::Init()
{
//ÈËÎïµ÷ÓÃ
md3.SetModelPath("player");
md3.SetModelName("rayman");
md3.SetGunName("railgun");
md3.SetPos(0, 0, pos.z);
md3.Load();
md3.ActionIdle();
//md3.ActionWalk();
}
//Player::Update()
//{
Player::Render()
{
static int preAction=ACTION_IDLE;
if (preAction!=Action)
{
switch(Action)
{
case ACTION_WALK:
md3.ActionWalk();
break;
case ACTION_RUN:
md3.ActionRun();
break;
case ACTION_STAND:
md3.ActionIdle();
break;
case ACTION_ATTACK:
md3.ActionAttack();
break;
case ACTION_JUMP:
md3.ActionJump();
break;
case ACTION_DEATH1:
md3.ActionDeath1();
PlaySound("data/death1.wav", NULL, SND_ASYNC);
break;
default:
md3.ActionIdle();
break;
}
}
preAction=Action;
ShowShadow();
}
Player::Draw()
{
glPushMatrix();
glTranslatef(pos.x, md3YOffset, pos.z);
glRotatef(rot.y+md3HeadingOffset, 0.0, 1.0, 0.0);
glScalef(md3Scale, md3Scale, md3Scale);
md3.Render();
glPopMatrix();
glDisable(GL_CULL_FACE);
}
Player::ShowShadow()
{
glDisable(GL_LIGHTING);
glColor3f(1.0,1.0,0.0);
glPushMatrix();
glColor3f(1.0,1.0,1.0);
md3.bTextureEnable=true;
Draw();
glPopMatrix();
//glDisable(GL_CULL_FACE);
// Now we draw the shadow
glPushMatrix();
glShadowProjection(); //(l,e,n);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
//glColor3f(0.4,0.4,0.4);
glColor3f(0.1,0.1,0.1);
//glTranslatef(0.2f, 0.2f, 0.2f);
glDisable(GL_TEXTURE_2D);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset (-8.0, 1);
md3.bTextureEnable=false;
Draw();
glDisable(GL_POLYGON_OFFSET_FILL);
glPopMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
}
/*
* This is where the "magic" is done:
*
* Multiply the current ModelView-Matrix with a shadow-projetion
* matrix.
*
* l is the position of the light source
* e is a point on within the plane on which the shadow is to be
* projected.
* n is the normal vector of the plane.
*
* Everything that is drawn after this call is "squashed" down
* to the plane. Hint: Gray or black color and no lighting
* looks good for shadows *g*
*/
Player::glShadowProjection() //(float * l, float * e, float * n)
{
static float l[] = { 30.0, 80.0, 0.0 }; // Coordinates of the light source
static float n[] = { 0.0, -1.0, 0.0 }; // Normal vector for the plane
static float e[] = { 0.0, 0.0, 0.0 }; // Point of the plane
float d, c;
float mat[16];
// These are c and d (corresponding to the tutorial)
//l[0]++;
//if (l[0]>300.0f) l[0]=-300;
d = n[0]*l[0] + n[1]*l[1] + n[2]*l[2];
c = e[0]*n[0] + e[1]*n[1] + e[2]*n[2] - d;
// Create the matrix. OpenGL uses column by column
// ordering
mat[0] = l[0]*n[0]+c;
mat[4] = n[1]*l[0];
mat[8] = n[2]*l[0];
mat[12] = -l[0]*c-l[0]*d;
mat[1] = n[0]*l[1];
mat[5] = l[1]*n[1]+c;
mat[9] = n[2]*l[1];
mat[13] = -l[1]*c-l[1]*d;
mat[2] = n[0]*l[2];
mat[6] = n[1]*l[2];
mat[10] = l[2]*n[2]+c;
mat[14] = -l[2]*c-l[2]*d;
mat[3] = n[0];
mat[7] = n[1];
mat[11] = n[2];
mat[15] = -d;
// Finally multiply the matrices together *plonk*
glMultMatrixf(mat);
}