www.pudn.com > 林海血原源代码.zip > 3dE.cpp


// 3dE.cpp: implementation of the C3dE class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "3dE.h" 
#include "heightmap.h" 
#include "math.h" 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
C3dE::C3dE() 
{ 
	m_bControlled=true; 
	m_bAttacked=false; 
    m_bAttacking=false; 
	m_Health=100; 
 
	m_Height=8; 
	m_baseHeight=255; 
    m_eyePos=VERTEX(0,0,0); 
//    m_boundary; 
 
	m_RotX=0; 
	m_RotY=180; 
	m_RotZ=0; 
 
	m_RunVector=NORMAL(0,0,0); 
	m_JumpSpeed=0.4f; 
	m_Acceleration=0.1f; 
	m_MaxRunSpeed=2.1f; 
    m_Decrease=0.04f; 
    m_AirDecrease=0.0001f; 
	m_Gravity=0.02f; 
	m_bFlying=false; 
	m_stepHeight=2; 
 
    m_YBiasAngle=0; 
	m_YBias=0; 
    m_ZBiasAngle=0; 
	m_bBiasLeft=true; 
 
	m_gunYBias=0; 
	m_gunZBias=0; 
	m_gunState=0; 
 
	m_VisualAngle=30; //half ,degree 
} 
 
C3dE::~C3dE() 
{ 
 
} 
bool C3dE::Init3dExplorer(VERTEX eyePos,float rotY, bool bControlled) 
{ 
	m_bControlled =bControlled; 
     
	if(!m_Weapon.InitMd2Loader("model/myGun.md2","model/myGun.BMP",0.1f)) 
	{ 
		MessageBox(0, "cMD2 error", "Error", MB_OK | MB_ICONERROR); 
		return FALSE; 
	} 
	if(!m_cAmmoManager.InitAmmoManager()) 
	{ 
		MessageBox(0, "ammo error", "Error", MB_OK | MB_ICONERROR); 
		return FALSE; 
	} 
    m_eyePos=eyePos; 
	m_baseHeight=m_eyePos.ypos-m_Height; 
    m_RotY=rotY; 
	m_gunState=0; 
 
	return true; 
} 
 
void C3dE::Update3dEAmmo() 
{ 
//  m_cAmmoManager.UpdateAmmoManager();   
	////////// launch bullet 
	if(m_bAttacking) 
	{ 
			float cosin=cosf(m_RotY*0.0174533f); 
			float sinn =sinf(m_RotY*0.0174533f); 
			float rightBias=-0.02f; 
			float forward=-5.0f;//float(rand()%100)/100-2; 
    	    VERTEX vert=VERTEX(m_eyePos.xpos+cosin*rightBias-sinn*forward, 
			               m_eyePos.ypos-0.1f, 
						   m_eyePos.zpos-sinn*rightBias-cosin*forward); 
 //   	    m_cAmmoManager.LaunchRocket(vert,m_RotX,m_RotY);  
			m_cAmmoManager.LaunchRocket(vert,m_RotX,m_RotY); 
	} 
} 
 
void C3dE::ProcessInput(INPUT *pInput) 
{ 
	static bool model; 
	static bool lighting; 
    if(pInput->keys['F']) 
	{ 
        pInput->keys['F']=false; 
        model=!model; 
	} 
	if(model) 
	    glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); 
	 
    else 
	    glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); 
    if(pInput->keys['L']) 
	{ 
        pInput->keys['L']=false; 
        lighting=!lighting; 
	} 
	if(!lighting) 
	{ 
 
	    glEnable(GL_LIGHTING); 
	    glEnable(GL_LIGHT0); 
	} 
    else 
	{ 
	 
	    glDisable(GL_LIGHTING); 
	    glDisable(GL_LIGHT0); 
	} 
	///////////////////////////////// 
    glPushMatrix(); 
	glTranslatef(0.4f, 
		         sinf(m_gunYBias*0.0174533f )*0.1f-0.7f, 
				 -cosf(m_gunZBias*0.0174533f)*0.2f-0.0f); 
	glRotatef(20,1.0f,0.0f,0.0f); 
	glRotatef(-80,0.0f,1.0f,0.0f); 
 
    m_Weapon.RenderOneFrame(20); 
	glPopMatrix();  
    //////////// backup my position 
	VERTEX oldPos=m_eyePos; 
    //////////// Process mouse and keyboard Input 
    if(pInput->mouseButtons[0]) 
	{ 
		m_bAttacking=true; 
        pInput->mouseButtons[0]=false; 
		m_gunState=1; 
        m_gunZBias+=60; 
	} 
	else  
	{ 
		m_bAttacking=false; 
		m_gunYBias+=5; 
	} 
   	if (m_gunYBias >= 359.0f)			 
	    	m_gunYBias -= 359;		 
   	if(m_gunState==1) 
	{ 
		m_gunZBias+=60; 
		if (m_gunZBias >= 359.0f)			 
		{ 
			m_gunZBias=0; 
            m_gunState=0; 
 
		} 
	} 
    m_RotY+=(400-pInput->mousePos.x)*0.1f;// /mouse Speed 
    m_RotX-=(300-pInput->mousePos.y)*0.1f; 
	// Center mouse 
	SetCursorPos(400,300); 
	// Keyboard input 
	if (pInput->keys[VK_UP] && (!m_bFlying)) 
	{ 
        m_RunVector.nz-=m_Acceleration; 
		if(m_RunVector.nz<-m_MaxRunSpeed)m_RunVector.nz=-m_MaxRunSpeed; 
		////////////////Y bias 
    	if (m_YBiasAngle >= 359)		 
	    	m_YBiasAngle = 0;		 
    	else						 
	    	 m_YBiasAngle+= 8; 
		m_YBias=sinf(m_YBiasAngle*0.0174533f )/4.0f; 
        ////////////// Z bias 
/*        if(m_bBiasLeft)m_ZBiasAngle+=0.1f; 
		else m_ZBiasAngle-=0.1f; 
		if(m_ZBiasAngle>1)m_bBiasLeft=false; 
		if(m_ZBiasAngle<-1)m_bBiasLeft=true;*/ 
	} 
	else 
	{ 
		if(! pInput->keys[VK_DOWN] && m_RunVector.nz<0 ) 
		{ 
			if(m_bFlying) m_RunVector.nz+=m_AirDecrease; 
			else  m_RunVector.nz+=m_Decrease; 
		    if(m_RunVector.nz>0)m_RunVector.nz=0; 
     
		} 
	} 
 
	if (pInput->keys[VK_DOWN] && (!m_bFlying)) 
	{ 
        m_RunVector.nz+=m_Acceleration; 
		if(m_RunVector.nz>m_MaxRunSpeed)m_RunVector.nz=m_MaxRunSpeed; 
	} 
	else 
	{ 
	    if (! pInput->keys[VK_UP] && m_RunVector.nz>0) 
		{ 
			if(m_bFlying) m_RunVector.nz-=m_AirDecrease; 
			else  m_RunVector.nz-=m_Decrease; 
		    if(m_RunVector.nz<0)m_RunVector.nz=0; 
		} 
	} 
 
	if (pInput->keys[VK_LEFT]&& (!m_bFlying)) 
	{ 
 
        m_RunVector.nx-=m_Acceleration; 
		if(m_RunVector.nx<-m_MaxRunSpeed)m_RunVector.nx=-m_MaxRunSpeed; 
	} 
	else 
	{ 
    	if (!pInput->keys[VK_RIGHT] && m_RunVector.nx<0) 
		{ 
            m_RunVector.nx+=m_Decrease; 
	    	if(m_RunVector.nx>0)m_RunVector.nx=0; 
		} 
	} 
	if (pInput->keys[VK_RIGHT]&& (!m_bFlying) ) 
	{ 
 
        m_RunVector.nx+=m_Acceleration; 
		if(m_RunVector.nx>m_MaxRunSpeed)m_RunVector.nx=m_MaxRunSpeed; 
	} 
	else 
	{ 
	    if (! pInput->keys[VK_LEFT] && m_RunVector.nx>0) 
		{ 
            m_RunVector.nx-=m_Decrease; 
	    	if(m_RunVector.nx<0)m_RunVector.nx=0; 
		} 
	} 
 
	if (pInput->keys[VK_SPACE] && (!m_bFlying)) 
	{ 
        m_RunVector.ny=m_JumpSpeed; 
        m_bFlying=true; 
	} 
	if (pInput->mouseButtons[1] && (!m_bFlying)) 
	{ 
	} 
 
	if (pInput->keys[VK_NEXT]) 
	{ 
		m_Height+=4; 
	} 
 
	if (pInput->keys[VK_PRIOR]) 
	{ 
		m_Height-=4; 
	} 
	/////////////////////////////////////////// 
	/////// Now , Update eyePos 
	/////////////////////////////////////////// 
    m_eyePos.xpos +=m_RunVector.nz*sinf(m_RotY*0.0174533f)+m_RunVector.nx*cosf(m_RotY*0.0174533f); 
	m_eyePos.ypos +=m_RunVector.ny + m_YBias; 
	m_eyePos.zpos +=m_RunVector.nz*cosf(m_RotY*0.0174533f)-m_RunVector.nx*sinf(m_RotY*0.0174533f); 
    //////// check new position 
 
	m_baseHeight=m_eyePos.ypos-m_Height; 
 
    float  newHeight=m_Heightmap.GetHeight(&m_eyePos); 
	if(m_bFlying) 
	{ 
        m_RunVector.ny-=m_Gravity; 
		if(m_baseHeightm_stepHeight) /// collided 
		m_eyePos=oldPos; 
	else 		 
		if((newHeight-m_baseHeight)>0)//up 
	{ 
		float percent=1-(newHeight-m_baseHeight)/m_stepHeight; 
		//if(percent<0)percent=-percent; 
		m_baseHeight=m_baseHeight+(newHeight-m_baseHeight)*percent; 
 
        m_eyePos.ypos=m_baseHeight+m_Height; 
        m_eyePos.xpos=oldPos.xpos+(m_eyePos.xpos-oldPos.xpos)*percent; 
        m_eyePos.zpos=oldPos.zpos+(m_eyePos.zpos-oldPos.zpos)*percent; 
	} 
	else//down 
	{ 
		m_baseHeight=newHeight; 
        m_eyePos.ypos=m_baseHeight+m_Height; 
	} 
*/ 
	UpdateHeightmap(); 
} 
void C3dE::TransformWorld(int step) 
{ 
	////////////////////////////////////////// 
	if(step==0) 
	{ 
		glRotatef(float(m_RotX), 1.0f, 0.0f, 0.0f);	  
   	    glRotatef(float(m_ZBiasAngle), 0.0f, 0.0f, 1.0f);	 
        glRotatef(360.0f - m_RotY, 0.0f, 1.0f, 0.0f); 
		glTranslated(0,  0, 0); 
	} 
	else 
	{ 
     		glTranslated(-m_eyePos.xpos, -m_eyePos.ypos, -m_eyePos.zpos); 
 
	} 
 
} 
void C3dE::UpdateHeightmap() 
{ 
	m_Heightmap.m_pViewPos->xpos=m_eyePos.xpos; 
	m_Heightmap.m_pViewPos->ypos=m_eyePos.ypos; 
	m_Heightmap.m_pViewPos->zpos=m_eyePos.zpos; 
	if(m_RotX>=360)m_RotX-=360; 
	if(m_RotY>=360)m_RotY-=360; 
	if(m_RotX<0)m_RotX+=360; 
	if(m_RotY<0)m_RotY+=360; 
	*m_Heightmap.m_pViewRotX    =m_RotX; 
	*m_Heightmap.m_pViewRotY    =m_RotY; 
}