3dE.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:8k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. // 3dE.cpp: implementation of the C3dE class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "3dE.h"
  6. #include "heightmap.h"
  7. #include "math.h"
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. C3dE::C3dE()
  12. {
  13. m_bControlled=true;
  14. m_bAttacked=false;
  15.     m_bAttacking=false;
  16. m_Health=100;
  17. m_Height=8;
  18. m_baseHeight=255;
  19.     m_eyePos=VERTEX(0,0,0);
  20. //    m_boundary;
  21. m_RotX=0;
  22. m_RotY=180;
  23. m_RotZ=0;
  24. m_RunVector=NORMAL(0,0,0);
  25. m_JumpSpeed=0.4f;
  26. m_Acceleration=0.1f;
  27. m_MaxRunSpeed=2.1f;
  28.     m_Decrease=0.04f;
  29.     m_AirDecrease=0.0001f;
  30. m_Gravity=0.02f;
  31. m_bFlying=false;
  32. m_stepHeight=2;
  33.     m_YBiasAngle=0;
  34. m_YBias=0;
  35.     m_ZBiasAngle=0;
  36. m_bBiasLeft=true;
  37. m_gunYBias=0;
  38. m_gunZBias=0;
  39. m_gunState=0;
  40. m_VisualAngle=30; //half ,degree
  41. }
  42. C3dE::~C3dE()
  43. {
  44. }
  45. bool C3dE::Init3dExplorer(VERTEX eyePos,float rotY, bool bControlled)
  46. {
  47. m_bControlled =bControlled;
  48.     
  49. if(!m_Weapon.InitMd2Loader("model/myGun.md2","model/myGun.BMP",0.1f))
  50. {
  51. MessageBox(0, "cMD2 error", "Error", MB_OK | MB_ICONERROR);
  52. return FALSE;
  53. }
  54. if(!m_cAmmoManager.InitAmmoManager())
  55. {
  56. MessageBox(0, "ammo error", "Error", MB_OK | MB_ICONERROR);
  57. return FALSE;
  58. }
  59.     m_eyePos=eyePos;
  60. m_baseHeight=m_eyePos.ypos-m_Height;
  61.     m_RotY=rotY;
  62. m_gunState=0;
  63. return true;
  64. }
  65. void C3dE::Update3dEAmmo()
  66. {
  67. //  m_cAmmoManager.UpdateAmmoManager();  
  68. ////////// launch bullet
  69. if(m_bAttacking)
  70. {
  71. float cosin=cosf(m_RotY*0.0174533f);
  72. float sinn =sinf(m_RotY*0.0174533f);
  73. float rightBias=-0.02f;
  74. float forward=-5.0f;//float(rand()%100)/100-2;
  75.          VERTEX vert=VERTEX(m_eyePos.xpos+cosin*rightBias-sinn*forward,
  76.                m_eyePos.ypos-0.1f,
  77.    m_eyePos.zpos-sinn*rightBias-cosin*forward);
  78.  //        m_cAmmoManager.LaunchRocket(vert,m_RotX,m_RotY); 
  79. m_cAmmoManager.LaunchRocket(vert,m_RotX,m_RotY);
  80. }
  81. }
  82. void C3dE::ProcessInput(INPUT *pInput)
  83. {
  84. static bool model;
  85. static bool lighting;
  86.     if(pInput->keys['F'])
  87. {
  88.         pInput->keys['F']=false;
  89.         model=!model;
  90. }
  91. if(model)
  92.     glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  93.     else
  94.     glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  95.     if(pInput->keys['L'])
  96. {
  97.         pInput->keys['L']=false;
  98.         lighting=!lighting;
  99. }
  100. if(!lighting)
  101. {
  102.     glEnable(GL_LIGHTING);
  103.     glEnable(GL_LIGHT0);
  104. }
  105.     else
  106. {
  107.     glDisable(GL_LIGHTING);
  108.     glDisable(GL_LIGHT0);
  109. }
  110. /////////////////////////////////
  111.     glPushMatrix();
  112. glTranslatef(0.4f,
  113.          sinf(m_gunYBias*0.0174533f )*0.1f-0.7f,
  114.  -cosf(m_gunZBias*0.0174533f)*0.2f-0.0f);
  115. glRotatef(20,1.0f,0.0f,0.0f);
  116. glRotatef(-80,0.0f,1.0f,0.0f);
  117.     m_Weapon.RenderOneFrame(20);
  118. glPopMatrix(); 
  119.     //////////// backup my position
  120. VERTEX oldPos=m_eyePos;
  121.     //////////// Process mouse and keyboard Input
  122.     if(pInput->mouseButtons[0])
  123. {
  124. m_bAttacking=true;
  125.         pInput->mouseButtons[0]=false;
  126. m_gunState=1;
  127.         m_gunZBias+=60;
  128. }
  129. else 
  130. {
  131. m_bAttacking=false;
  132. m_gunYBias+=5;
  133. }
  134.     if (m_gunYBias >= 359.0f)
  135.      m_gunYBias -= 359;
  136.     if(m_gunState==1)
  137. {
  138. m_gunZBias+=60;
  139. if (m_gunZBias >= 359.0f)
  140. {
  141. m_gunZBias=0;
  142.             m_gunState=0;
  143. }
  144. }
  145.     m_RotY+=(400-pInput->mousePos.x)*0.1f;// /mouse Speed
  146.     m_RotX-=(300-pInput->mousePos.y)*0.1f;
  147. // Center mouse
  148. SetCursorPos(400,300);
  149. // Keyboard input
  150. if (pInput->keys[VK_UP] && (!m_bFlying))
  151. {
  152.         m_RunVector.nz-=m_Acceleration;
  153. if(m_RunVector.nz<-m_MaxRunSpeed)m_RunVector.nz=-m_MaxRunSpeed;
  154. ////////////////Y bias
  155.      if (m_YBiasAngle >= 359)
  156.      m_YBiasAngle = 0;
  157.      else
  158.       m_YBiasAngle+= 8;
  159. m_YBias=sinf(m_YBiasAngle*0.0174533f )/4.0f;
  160.         ////////////// Z bias
  161. /*        if(m_bBiasLeft)m_ZBiasAngle+=0.1f;
  162. else m_ZBiasAngle-=0.1f;
  163. if(m_ZBiasAngle>1)m_bBiasLeft=false;
  164. if(m_ZBiasAngle<-1)m_bBiasLeft=true;*/
  165. }
  166. else
  167. {
  168. if(! pInput->keys[VK_DOWN] && m_RunVector.nz<0 )
  169. {
  170. if(m_bFlying) m_RunVector.nz+=m_AirDecrease;
  171. else  m_RunVector.nz+=m_Decrease;
  172.     if(m_RunVector.nz>0)m_RunVector.nz=0;
  173.     
  174. }
  175. }
  176. if (pInput->keys[VK_DOWN] && (!m_bFlying))
  177. {
  178.         m_RunVector.nz+=m_Acceleration;
  179. if(m_RunVector.nz>m_MaxRunSpeed)m_RunVector.nz=m_MaxRunSpeed;
  180. }
  181. else
  182. {
  183.     if (! pInput->keys[VK_UP] && m_RunVector.nz>0)
  184. {
  185. if(m_bFlying) m_RunVector.nz-=m_AirDecrease;
  186. else  m_RunVector.nz-=m_Decrease;
  187.     if(m_RunVector.nz<0)m_RunVector.nz=0;
  188. }
  189. }
  190. if (pInput->keys[VK_LEFT]&& (!m_bFlying))
  191. {
  192.         m_RunVector.nx-=m_Acceleration;
  193. if(m_RunVector.nx<-m_MaxRunSpeed)m_RunVector.nx=-m_MaxRunSpeed;
  194. }
  195. else
  196. {
  197.      if (!pInput->keys[VK_RIGHT] && m_RunVector.nx<0)
  198. {
  199.             m_RunVector.nx+=m_Decrease;
  200.      if(m_RunVector.nx>0)m_RunVector.nx=0;
  201. }
  202. }
  203. if (pInput->keys[VK_RIGHT]&& (!m_bFlying) )
  204. {
  205.         m_RunVector.nx+=m_Acceleration;
  206. if(m_RunVector.nx>m_MaxRunSpeed)m_RunVector.nx=m_MaxRunSpeed;
  207. }
  208. else
  209. {
  210.     if (! pInput->keys[VK_LEFT] && m_RunVector.nx>0)
  211. {
  212.             m_RunVector.nx-=m_Decrease;
  213.      if(m_RunVector.nx<0)m_RunVector.nx=0;
  214. }
  215. }
  216. if (pInput->keys[VK_SPACE] && (!m_bFlying))
  217. {
  218.         m_RunVector.ny=m_JumpSpeed;
  219.         m_bFlying=true;
  220. }
  221. if (pInput->mouseButtons[1] && (!m_bFlying))
  222. {
  223. }
  224. if (pInput->keys[VK_NEXT])
  225. {
  226. m_Height+=4;
  227. }
  228. if (pInput->keys[VK_PRIOR])
  229. {
  230. m_Height-=4;
  231. }
  232. ///////////////////////////////////////////
  233. /////// Now , Update eyePos
  234. ///////////////////////////////////////////
  235.     m_eyePos.xpos +=m_RunVector.nz*sinf(m_RotY*0.0174533f)+m_RunVector.nx*cosf(m_RotY*0.0174533f);
  236. m_eyePos.ypos +=m_RunVector.ny + m_YBias;
  237. m_eyePos.zpos +=m_RunVector.nz*cosf(m_RotY*0.0174533f)-m_RunVector.nx*sinf(m_RotY*0.0174533f);
  238.     //////// check new position
  239. m_baseHeight=m_eyePos.ypos-m_Height;
  240.     float  newHeight=m_Heightmap.GetHeight(&m_eyePos);
  241. if(m_bFlying)
  242. {
  243.         m_RunVector.ny-=m_Gravity;
  244. if(m_baseHeight<newHeight)
  245. {
  246.             m_baseHeight=newHeight;
  247. m_eyePos.ypos=newHeight+m_Height;
  248. m_RunVector.ny=0;
  249.             m_bFlying=false;
  250. }
  251. return;
  252. }
  253. m_baseHeight=newHeight;
  254.         m_eyePos.ypos=m_baseHeight+m_Height;
  255. /*
  256. if((newHeight-m_baseHeight)>m_stepHeight) /// collided
  257. m_eyePos=oldPos;
  258. else 
  259. if((newHeight-m_baseHeight)>0)//up
  260. {
  261. float percent=1-(newHeight-m_baseHeight)/m_stepHeight;
  262. //if(percent<0)percent=-percent;
  263. m_baseHeight=m_baseHeight+(newHeight-m_baseHeight)*percent;
  264.         m_eyePos.ypos=m_baseHeight+m_Height;
  265.         m_eyePos.xpos=oldPos.xpos+(m_eyePos.xpos-oldPos.xpos)*percent;
  266.         m_eyePos.zpos=oldPos.zpos+(m_eyePos.zpos-oldPos.zpos)*percent;
  267. }
  268. else//down
  269. {
  270. m_baseHeight=newHeight;
  271.         m_eyePos.ypos=m_baseHeight+m_Height;
  272. }
  273. */
  274. UpdateHeightmap();
  275. }
  276. void C3dE::TransformWorld(int step)
  277. {
  278. //////////////////////////////////////////
  279. if(step==0)
  280. {
  281. glRotatef(float(m_RotX), 1.0f, 0.0f, 0.0f);  
  282.         glRotatef(float(m_ZBiasAngle), 0.0f, 0.0f, 1.0f);
  283.         glRotatef(360.0f - m_RotY, 0.0f, 1.0f, 0.0f);
  284. glTranslated(0,  0, 0);
  285. }
  286. else
  287. {
  288.       glTranslated(-m_eyePos.xpos, -m_eyePos.ypos, -m_eyePos.zpos);
  289. }
  290. }
  291. void C3dE::UpdateHeightmap()
  292. {
  293. m_Heightmap.m_pViewPos->xpos=m_eyePos.xpos;
  294. m_Heightmap.m_pViewPos->ypos=m_eyePos.ypos;
  295. m_Heightmap.m_pViewPos->zpos=m_eyePos.zpos;
  296. if(m_RotX>=360)m_RotX-=360;
  297. if(m_RotY>=360)m_RotY-=360;
  298. if(m_RotX<0)m_RotX+=360;
  299. if(m_RotY<0)m_RotY+=360;
  300. *m_Heightmap.m_pViewRotX    =m_RotX;
  301. *m_Heightmap.m_pViewRotY    =m_RotY;
  302. }