FIGHTERTESTVIEW.CPP
Upload User: nthssl
Upload Date: 2022-04-05
Package Size: 25357k
Code Size: 19k
Category:

OpenCV

Development Platform:

Visual C++

  1. // FighterTestView.cpp : implementation of the CFighterTestView class
  2. //
  3. #include "stdafx.h"
  4. #include "FighterTest.h"
  5. #include "MilkshapeModel.h"
  6. #include "FighterTestDoc.h"
  7. #include "FighterTestView.h"
  8. #include "math.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. GLuint texture[4]; // Storage For 3 Textures
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CFighterTestView
  17. IMPLEMENT_DYNCREATE(CFighterTestView, CView)
  18. BEGIN_MESSAGE_MAP(CFighterTestView, CView)
  19. //{{AFX_MSG_MAP(CFighterTestView)
  20. ON_WM_CREATE()
  21. ON_WM_DESTROY()
  22. ON_WM_SIZE()
  23. ON_WM_TIMER()
  24. ON_WM_KEYDOWN()
  25. //}}AFX_MSG_MAP
  26. // Standard printing commands
  27. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  28. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  29. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  30. END_MESSAGE_MAP()
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CFighterTestView construction/destruction
  33. CFighterTestView::CFighterTestView()
  34. {
  35. // TODO: add construction code here
  36. pModel = NULL;
  37. active=TRUE;
  38. Ioan=0;
  39. sky=true;
  40. timebullet=0;
  41. cameratype=1;
  42. nb,smoke=false;
  43. }
  44. CFighterTestView::~CFighterTestView()
  45. {
  46. }
  47. BOOL CFighterTestView::PreCreateWindow(CREATESTRUCT& cs)
  48. {
  49. // TODO: Modify the Window class or styles here by modifying
  50. //  the CREATESTRUCT cs
  51. ////////////////////////////////////////////////////////////////
  52. //设置窗口类型
  53. cs.style |=WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  54. ////////////////////////////////////////////////////////////////
  55. return CView::PreCreateWindow(cs);
  56. }
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CFighterTestView drawing
  59. void CFighterTestView::OnDraw(CDC* pDC)
  60. {
  61. CFighterTestDoc* pDoc = GetDocument();
  62. ASSERT_VALID(pDoc);
  63. // TODO: add draw code for native data here
  64. //////////////////////////////////////////////////////////////////
  65. RenderScene(); //渲染场景
  66. //////////////////////////////////////////////////////////////////
  67. }
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CFighterTestView printing
  70. BOOL CFighterTestView::OnPreparePrinting(CPrintInfo* pInfo)
  71. {
  72. // default preparation
  73. return DoPreparePrinting(pInfo);
  74. }
  75. void CFighterTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  76. {
  77. // TODO: add extra initialization before printing
  78. }
  79. void CFighterTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  80. {
  81. // TODO: add cleanup after printing
  82. }
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CFighterTestView diagnostics
  85. #ifdef _DEBUG
  86. void CFighterTestView::AssertValid() const
  87. {
  88. CView::AssertValid();
  89. }
  90. void CFighterTestView::Dump(CDumpContext& dc) const
  91. {
  92. CView::Dump(dc);
  93. }
  94. CFighterTestDoc* CFighterTestView::GetDocument() // non-debug version is inline
  95. {
  96. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFighterTestDoc)));
  97. return (CFighterTestDoc*)m_pDocument;
  98. }
  99. #endif //_DEBUG
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CFighterTestView message handlers
  102. int CFighterTestView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  103. {
  104. if (CView::OnCreate(lpCreateStruct) == -1)
  105. return -1;
  106. // TODO: Add your specialized creation code here
  107. //////////////////////////////////////////////////////////////////
  108. //初始化OpenGL和设置定时器
  109. m_pDC = new CClientDC(this);
  110. SetTimer(1, 20, NULL);
  111. InitializeOpenGL(m_pDC);
  112. //////////////////////////////////////////////////////////////////
  113. pModel = new MilkshapeModel();
  114. if ( pModel->loadModelData( "Aereo.ms3d" ) == false )
  115. {
  116. MessageBox("Couldn't load the model Aereo.ms3d", "Error", MB_OK | MB_ICONERROR );
  117. return 0;
  118. }
  119. InitGL();
  120. return 0;
  121. }
  122. void CFighterTestView::OnDestroy() 
  123. {
  124. CView::OnDestroy();
  125. // TODO: Add your message handler code here
  126. /////////////////////////////////////////////////////////////////
  127. //删除调色板和渲染上下文、定时器
  128. ::wglMakeCurrent(0,0);
  129. ::wglDeleteContext( m_hRC);
  130. if (m_hPalette)
  131.     DeleteObject(m_hPalette);
  132. if ( m_pDC )
  133. {
  134. delete m_pDC;
  135. }
  136. KillTimer(1);
  137. /////////////////////////////////////////////////////////////////
  138. }
  139. void CFighterTestView::OnSize(UINT nType, int cx, int cy) 
  140. {
  141. CView::OnSize(nType, cx, cy);
  142. // TODO: Add your message handler code here
  143. if (cy==0)
  144. {
  145. cy=1;
  146. }
  147. /////////////////////////////////////////////////////////////////
  148. //添加窗口缩放时的图形变换函数
  149. glViewport(0,0,cx,cy);
  150. /////////////////////////////////////////////////////////////////
  151. glViewport(0,0,cx,cy);
  152. glMatrixMode(GL_PROJECTION);
  153. glLoadIdentity();
  154. gluPerspective(45.0f,(GLfloat)cx/(GLfloat)cy,0.1f,2000.0f);
  155. glMatrixMode(GL_MODELVIEW);
  156. glLoadIdentity();
  157. }
  158. void CFighterTestView::OnTimer(UINT nIDEvent) 
  159. {
  160. // TODO: Add your message handler code here and/or call default
  161. /////////////////////////////////////////////////////////////////
  162. //添加定时器响应函数和场景更新函数
  163. Invalidate(FALSE);
  164. /////////////////////////////////////////////////////////////////
  165. CView::OnTimer(nIDEvent);
  166. }
  167. /////////////////////////////////////////////////////////////////////
  168. //                   设置逻辑调色板
  169. //////////////////////////////////////////////////////////////////////
  170. void CFighterTestView::SetLogicalPalette(void)
  171. {
  172.     struct
  173.     {
  174.         WORD Version;
  175.         WORD NumberOfEntries;
  176.         PALETTEENTRY aEntries[256];
  177.     } logicalPalette = { 0x300, 256 };
  178. BYTE reds[] = {0, 36, 72, 109, 145, 182, 218, 255};
  179. BYTE greens[] = {0, 36, 72, 109, 145, 182, 218, 255};
  180. BYTE blues[] = {0, 85, 170, 255};
  181.     for (int colorNum=0; colorNum<256; ++colorNum)
  182.     {
  183.         logicalPalette.aEntries[colorNum].peRed =
  184.             reds[colorNum & 0x07];
  185.         logicalPalette.aEntries[colorNum].peGreen =
  186.             greens[(colorNum >> 0x03) & 0x07];
  187.         logicalPalette.aEntries[colorNum].peBlue =
  188.             blues[(colorNum >> 0x06) & 0x03];
  189.         logicalPalette.aEntries[colorNum].peFlags = 0;
  190.     }
  191.     m_hPalette = CreatePalette ((LOGPALETTE*)&logicalPalette);
  192. }
  193. //////////////////////////////////////////////////////////
  194. // 初始化openGL场景
  195. //////////////////////////////////////////////////////////
  196. BOOL CFighterTestView::InitializeOpenGL(CDC* pDC)
  197. {
  198. m_pDC = pDC;
  199. SetupPixelFormat();
  200. //生成绘制描述表
  201. m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
  202. //置当前绘制描述表
  203. ::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
  204. return TRUE;
  205. }
  206. //////////////////////////////////////////////////////////
  207. // 设置像素格式
  208. //////////////////////////////////////////////////////////
  209. BOOL CFighterTestView::SetupPixelFormat()
  210. {
  211. PIXELFORMATDESCRIPTOR pfd = { 
  212.     sizeof(PIXELFORMATDESCRIPTOR),    // pfd结构的大小 
  213.     1,                                // 版本号 
  214.     PFD_DRAW_TO_WINDOW |              // 支持在窗口中绘图 
  215.     PFD_SUPPORT_OPENGL |              // 支持 OpenGL 
  216.     PFD_DOUBLEBUFFER,                 // 双缓存模式 
  217.     PFD_TYPE_RGBA,                    // RGBA 颜色模式 
  218.     24,                               // 24 位颜色深度 
  219.     0, 0, 0, 0, 0, 0,                 // 忽略颜色位 
  220.     0,                                // 没有非透明度缓存 
  221.     0,                                // 忽略移位位 
  222.     0,                                // 无累加缓存 
  223.     0, 0, 0, 0,                       // 忽略累加位 
  224.     32,                               // 32 位深度缓存     
  225.     0,                                // 无模板缓存 
  226.     0,                                // 无辅助缓存 
  227.     PFD_MAIN_PLANE,                   // 主层 
  228.     0,                                // 保留 
  229.     0, 0, 0                           // 忽略层,可见性和损毁掩模 
  230. }; 
  231. int pixelformat;
  232. pixelformat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);//选择像素格式
  233. ::SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd); //设置像素格式
  234. if(pfd.dwFlags & PFD_NEED_PALETTE)
  235. SetLogicalPalette(); //设置逻辑调色板
  236. return TRUE;
  237. }
  238. //////////////////////////////////////////////////////////
  239. // 场景绘制与渲染
  240. //////////////////////////////////////////////////////////
  241. BOOL CFighterTestView::RenderScene() 
  242. {
  243. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  244. glLoadIdentity();
  245. Camera();
  246. // 绘制蓝天白云背景
  247. if(sky==true)
  248. {
  249. glEnable(GL_TEXTURE_2D);
  250. glPushMatrix();
  251. glBindTexture(GL_TEXTURE_2D, texture[0]);
  252. glTranslatef(Io.x,0,Io.z);
  253. glRotatef(90,1,0,0);
  254. glColor4f(1,1,1,1);
  255. gluSphere(qobj,1000,32,32);
  256. glPopMatrix();
  257. glDisable(GL_TEXTURE_2D);
  258. }
  259. if(rand()%100==3)
  260. timebullet=10+rand()%100; // 随机产生子弹
  261. if(timebullet>0)
  262. {
  263. timebullet--;
  264. if( timebullet%4==0 )
  265. ActiveBullet(); // 激活子弹
  266. }
  267. DrawTerrain(); // 绘制地形
  268. MoveAereo(); // 实现飞机运动
  269. DrawAereo(); // 绘制飞行
  270. if(smoke==true)
  271. {
  272. InitSmoke02(); // 初始化尾部烟雾
  273. DrawSmoke(); // 绘制尾部烟雾
  274. }
  275. MoveDrawBullet(); // 实现子弹运动
  276. ::SwapBuffers(m_pDC->GetSafeHdc()); //交互缓冲区
  277. return TRUE;
  278. }
  279. int CFighterTestView::InitGL(GLvoid) // All Setup For OpenGL Goes Here
  280. {
  281. GLfloat LightPosition[]={ 1.0f, 140.0f, 1.0f, 1.0f };
  282. if(!m_texture.LoadTextures())
  283. return FALSE;
  284. InitBullet(); // 初始化子弹
  285. InitTerrain(); // 初始化地形
  286. InitAereo(); // 初始化飞机
  287. InitSmoke01(); // 初始化尾部烟雾
  288. pModel->reloadTextures(); // 装入模型纹理
  289. glShadeModel(GL_SMOOTH);
  290. glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
  291. glClearDepth(1.0f);
  292. glEnable(GL_DEPTH_TEST);
  293. glDepthFunc(GL_LEQUAL);
  294. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  295. glLightfv(GL_LIGHT0, GL_POSITION,LightPosition);
  296. glEnable(GL_LIGHT0);
  297. qobj = gluNewQuadric();
  298. gluQuadricNormals(qobj,GLU_SMOOTH);
  299. gluQuadricTexture(qobj,GL_TRUE);
  300. return TRUE;
  301. }
  302. double CFighterTestView::VectorLength( Vector v )
  303. {
  304. return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
  305. }
  306. Vector CFighterTestView::VectorNormalize( Vector v )
  307. {
  308. Vector vresult;
  309. double l = VectorLength( v );
  310. vresult.x = v.x/l;
  311. vresult.y = v.y/l;
  312. vresult.z = v.z/l;
  313. return vresult;
  314. }
  315. Vector CFighterTestView::VectorMultiply( Vector v1, Vector v2 )
  316. {
  317. Vector vresult;
  318. vresult.x = v1.y * v2.z - v1.z * v2.y;
  319. vresult.y = v1.z * v2.x - v1.x * v2.z;
  320. vresult.z = v1.x * v2.y - v1.y * v2.x;
  321. return vresult;
  322. }
  323. Vector CFighterTestView::VectorScalarMultiply( Vector v, double s )
  324. {
  325. Vector vresult;
  326. vresult.x = v.x * s;
  327. vresult.y = v.y * s;
  328. vresult.z = v.z * s;
  329. return vresult;
  330. }
  331. Vector CFighterTestView::VectorDiff( Vector v1, Vector v2 )
  332. {
  333. Vector vresult;
  334. vresult.x = v1.x - v2.x;
  335. vresult.y = v1.y - v2.y;
  336. vresult.z = v1.z - v2.z;
  337. return vresult;
  338. }
  339. void CFighterTestView::Luce(double x, double y, double z,double dimensione,float r,float g,float b,float iox,float ioy,float ioz,int Tex)
  340. {
  341. Vector Position;
  342. Vector MyPosition;
  343. Position.x = x;
  344. Position.y = y;
  345. Position.z = z;
  346. MyPosition.x=iox;
  347. MyPosition.y=ioy;
  348. MyPosition.z=ioz;
  349. Vector sight = VectorDiff(MyPosition, Position);
  350. Vector cz;
  351. cz.x = 0;
  352. cz.y = 0;
  353. cz.z = 1;
  354. Vector cross1 = VectorMultiply( sight, cz );
  355. Vector cross2 = VectorMultiply( sight, cross1 );
  356. cross1 = VectorNormalize(cross1);
  357. cross2 = VectorNormalize(cross2);
  358. cross1 = VectorScalarMultiply(cross1, dimensione);
  359. cross2 = VectorScalarMultiply(cross2, dimensione);
  360. glColor3f(r,g,b);
  361. glEnable(GL_TEXTURE_2D);
  362. glEnable (GL_BLEND);
  363. glBlendFunc( (1,1,1,1), (1,1,1,1));
  364. glDepthMask (GL_FALSE);
  365. glBindTexture( GL_TEXTURE_2D, texture[Tex] );
  366. glBegin(GL_QUADS);
  367. glTexCoord2d( 0.0, 0.0 );
  368. glVertex3d( Position.x + cross1.x, Position.y + cross1.y, Position.z + cross1.z);
  369. glTexCoord2d( 1.0, 0.0 );
  370. glVertex3d( Position.x - cross2.x, Position.y - cross2.y, Position.z - cross2.z);
  371. glTexCoord2d( 1.0, 1.0 );
  372. glVertex3d( Position.x - cross1.x, Position.y - cross1.y, Position.z - cross1.z);
  373. glTexCoord2d( 0.0, 1.0 );
  374. glVertex3d( Position.x + cross2.x, Position.y + cross2.y, Position.z + cross2.z);
  375. glEnd();
  376. glDisable(GL_TEXTURE_2D);
  377. glDisable (GL_BLEND);
  378. glDepthMask (GL_TRUE);
  379. }
  380. void CFighterTestView::InitBullet(void)
  381. {
  382. int t;
  383. for(t=0;t<MAXBULLET;t++)
  384. {
  385. bul[t].active=false; // 所有子弹未激活
  386. bul[t].explosion=false; // 所有子弹未爆炸
  387. }
  388. }
  389. void CFighterTestView::InitTerrain(void)
  390. {
  391. int x,z;
  392. double xi,zi;
  393. for(z=0;z<DIMZ;z++)
  394. {
  395. zi=(double)z-((double)DIMZ/2);
  396. zi=zi*SIZE;
  397. for(x=0;x<DIMX;x++)
  398. {
  399. xi=(double)x-((double)DIMX/2);
  400. xi=xi*SIZE;
  401. Terrain[x][z].x=xi;
  402. Terrain[x][z].y=0; 
  403. Terrain[x][z].z=zi;
  404. }
  405. }
  406. }
  407. void CFighterTestView::InitAereo(void)
  408. {
  409. // 初始化飞机的位置
  410. aereo.x=150+(float)(rand()%1000)/10;
  411. aereo.z=20+(float)(rand()%10000)/20;
  412. aereo.y=10;
  413. // 初始化飞机的角度
  414. aereo.an=(float)90+(float)(rand()%45);
  415. aereo.anend=0;
  416. aereo.dan=0;
  417. // 初始化飞机的速度
  418. aereo.vel=4+(float)(rand()%100)/20;
  419. aereo.fvirata=0;
  420. aereo.virata=false;
  421. aereo.attivo=true;
  422. aereo.tempovirata=0;
  423. aereo.cntscia=5;
  424. // 视点位置
  425. Io.x=aereo.x;
  426. Io.y=aereo.y+4;
  427. Io.z=aereo.z;
  428. Ioan=20;
  429. }
  430. void CFighterTestView::InitSmoke01(void)
  431. {
  432. int t;
  433. for(t=0;t<MAXPUF;t++)
  434. Smoke[t].time=-1; //所有烟雾未激活
  435. }
  436. void CFighterTestView::ActiveBullet(void)
  437. {
  438. int t;
  439. int r;
  440. for(t=0;t<MAXBULLET;t++)
  441. {
  442. if(bul[t].active==false)
  443. {
  444. r=rand()%4-rand()%4;
  445. bul[t].explosion=false; // 是否爆炸
  446. bul[t].active=true; // 是否激活
  447. // 子弹的位置
  448. bul[t].x=aereo.x;
  449. bul[t].y=aereo.y;
  450. bul[t].z=aereo.z;
  451. // 子弹的位移速度
  452. bul[t].dx=(aereo.vel*2)*cos((aereo.an+90+r)*3.141/180);
  453. bul[t].dy=0;
  454. bul[t].dz=(aereo.vel+2)*sin((aereo.an+90+r)*3.141/180);
  455. break;
  456. }
  457. }
  458. }
  459. void CFighterTestView::InitSmoke02(void)
  460. {
  461. register int t;
  462. int flag=1;
  463. float dx,dy,dz;
  464. for(t=0;t<MAXPUF;t++)
  465. {
  466. if(Smoke[t].time<0 && flag>=0)
  467. {
  468. if(flag!=0)
  469. {
  470. dx=cos(aereo.an*3.141/180);
  471. dy=0;
  472. dz=sin(aereo.an*3.141/180);
  473. }
  474. else
  475. {
  476. dx=1+cos(-aereo.an*3.141/180);
  477. dy=2*sin(aereo.dan*aereo.fvirata*3.141/180);
  478. dz=1+sin(-aereo.an*3.141/180);
  479. }
  480. // 烟雾的寿命
  481. Smoke[t].time=TIMEPUF;
  482. // 烟雾的位置
  483. Smoke[t].x=aereo.x+dx;
  484. Smoke[t].y=aereo.y-dy-1.2;
  485. Smoke[t].z=aereo.z+dz;
  486. // 烟雾的尺寸
  487. Smoke[t].dim=0;
  488. // 烟雾的颜色
  489. Smoke[t].r=1;
  490. Smoke[t].g=1;
  491. Smoke[t].b=1;
  492. flag--;
  493. if(flag<0)
  494. break;
  495. }
  496. }
  497. }
  498. void CFighterTestView::MoveDrawBullet(void)
  499. {
  500. int t;
  501. for(t=0;t<MAXBULLET;t++)
  502. {
  503. if(bul[t].explosion==false)
  504. {
  505. if(bul[t].active==true)
  506. {
  507. bul[t].x+=bul[t].dx;
  508. bul[t].y-=bul[t].dy;
  509. bul[t].z+=bul[t].dz;
  510. bul[t].dy+=(float)GRAVITY;
  511. if(bul[t].y<0)
  512. {
  513. bul[t].active=false;
  514. bul[t].explosion=true;
  515. bul[t].dim=0;
  516. }
  517. glColor3f(1,1,1);
  518. glBegin(GL_POINTS);
  519. glVertex3f(bul[t].x,bul[t].y,bul[t].z);
  520. glEnd();
  521. }
  522. }
  523. else
  524. {
  525. bul[t].dim+=0.1;
  526. if(bul[t].dim>1)
  527. bul[t].explosion=false;
  528. Luce(bul[t].x,bul[t].y,bul[t].z,4*bul[t].dim,1-bul[t].dim,0,0,Io.x,Io.y,Io.z,3);
  529. }
  530. }
  531. glColor3f(1,1,1);
  532. }
  533. void CFighterTestView::MoveAereo(void)
  534. {
  535. register float dx,dz;
  536. dx=aereo.vel*cos((90+aereo.an)*3.141/180);
  537. dz=aereo.vel*sin((90+aereo.an)*3.141/180);
  538. aereo.x+=dx;
  539. aereo.z+=dz;
  540. if(aereo.x<-4000)
  541. aereo.x=4000;
  542. if(aereo.x>4000)
  543. aereo.x=-4000;
  544. if(aereo.z<-4000)
  545. aereo.z=4000;
  546. if(aereo.z>4000)
  547. aereo.z=-4000;
  548. // Virata
  549. if(aereo.virata==false )
  550. {
  551. if(rand()%100==0)
  552. {
  553. float dan;
  554. dan=(float)(rand()%160-rand()%160);
  555. aereo.anend=aereo.an+dan;
  556. aereo.dan=dan/TEMPOVIRATA;
  557. aereo.virata=true;
  558. aereo.tempovirata=0;
  559. aereo.fvirata=0;
  560. }
  561. }
  562. else
  563. {
  564. aereo.an+=aereo.dan;
  565. if(aereo.an<(aereo.anend+1) && aereo.an>(aereo.anend-1))
  566. aereo.virata=false;
  567. }
  568. }
  569. void CFighterTestView::DrawTerrain(void)
  570. {
  571. register int x,z;
  572. register float fk=(float)1/100;
  573. glEnable(GL_TEXTURE_2D);
  574. glBindTexture( GL_TEXTURE_2D, texture[1] );
  575. for(z=0;z<DIMZ-1;z++)
  576. {
  577. for(x=0;x<DIMX-1;x++)
  578. {
  579. glBegin(GL_QUADS);
  580. glTexCoord2d( 0.0, 0.0 );
  581. glVertex3f(Terrain[x][z].x,Terrain[x][z].y,Terrain[x][z].z);
  582. glTexCoord2d( 0.0, 1.0 );
  583. glVertex3f(Terrain[x][z+1].x,Terrain[x][z+1].y,Terrain[x][z+1].z);
  584. glTexCoord2d( 1.0, 1.0 );
  585. glVertex3f(Terrain[x+1][z+1].x,Terrain[x+1][z+1].y,Terrain[x+1][z+1].z);
  586. glTexCoord2d( 1.0, 0.0 );
  587. glVertex3f(Terrain[x+1][z].x,Terrain[x+1][z].y,Terrain[x+1][z].z);
  588. glEnd();
  589. }
  590. }
  591. glDisable(GL_TEXTURE_2D);
  592. }
  593. void CFighterTestView::DrawAereo(void)
  594. {
  595. glPushMatrix();
  596. glEnable(GL_LIGHTING);
  597. glTranslatef(aereo.x,aereo.y,aereo.z);
  598. glRotatef(-aereo.an,0,1,0);
  599. if(aereo.virata==true)
  600. {
  601. glRotatef(aereo.dan*aereo.fvirata,0,0,1);
  602. if(aereo.tempovirata<TEMPOVIRATA/2)
  603. aereo.fvirata+=0.5;
  604. else
  605. aereo.fvirata-=0.5;
  606. aereo.tempovirata++;
  607. }
  608. pModel->draw();
  609. glDisable(GL_LIGHTING);
  610. glPopMatrix();
  611. }
  612. void CFighterTestView::DrawSmoke(void)
  613. {
  614. int t;
  615. float fk=(float)2/TIMEPUF;
  616. for(t=0;t<MAXPUF;t++)
  617. {
  618. if(Smoke[t].time>0)
  619. {
  620. Luce(Smoke[t].x,Smoke[t].y,Smoke[t].z,Smoke[t].dim,Smoke[t].r,Smoke[t].g,Smoke[t].b,Io.x,Io.y,Io.z,2);
  621. Smoke[t].dim+=0.05; // 烟雾的尺寸变大
  622. // 烟雾的颜色变化
  623. Smoke[t].r-=fk;
  624. Smoke[t].g-=fk;
  625. Smoke[t].b-=fk;
  626. // 烟雾的寿命缩短
  627. Smoke[t].time--;
  628. if(Smoke[t].time<=0)
  629. Smoke[t].time=-1; // 烟雾消息
  630. }
  631. }
  632. glColor3f(1,1,1);
  633. }
  634. void CFighterTestView::Camera(void)
  635. {
  636. float dist;
  637. float dx,dy,dz;
  638. if(cameratype==0)
  639. {
  640. dist=sqrt( (aereo.x-Io.x)*(aereo.x-Io.x)+(aereo.z-Io.z)*(aereo.z-Io.z) );
  641. if(dist>400)
  642. {
  643. Io.x=aereo.x+150*cos((aereo.an+rand()%180)*3.141/180);
  644. Io.y=(float)(1+rand()%35);
  645. Io.z=aereo.z+150*sin((aereo.an+rand()%180)*3.141/180);
  646. aereo.vel=1+(float)(rand()%100)/50;
  647. }
  648. }
  649. if(cameratype==1)
  650. {
  651. dx=30*cos((aereo.an-90+Ioan)*3.141/180);
  652. dy=6;
  653. dz=30*sin((aereo.an-90+Ioan)*3.141/180);
  654. Io.x=aereo.x+dx;
  655. Io.y=aereo.y+dy;
  656. Io.z=aereo.z+dz;
  657. }
  658. if(cameratype==2)
  659. {
  660. if(bul[nb].active==true)
  661. {
  662. Io.x=bul[nb].x;
  663. Io.y=bul[nb].y;
  664. Io.z=bul[nb].z;
  665. }
  666. else
  667. cameratype=0;
  668. }
  669. gluLookAt(Io.x,Io.y,Io.z,aereo.x,aereo.y,aereo.z,0.0f,1.0f,0.0f);
  670. }
  671. void CFighterTestView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  672. {
  673. // TODO: Add your message handler code here and/or call default
  674. // Camera position
  675. if(nChar==VK_LEFT)
  676. Ioan++;
  677. if(nChar==VK_RIGHT)
  678. Ioan--;
  679. if(nChar==VK_UP)
  680. Io.y+=0.1;
  681. if(nChar==VK_DOWN)
  682. Io.y-=0.1;
  683. if(nChar==65) // 按下'A'键
  684. {
  685. timebullet=rand()%100;
  686. }
  687. if(nChar==83) // 按下'S'键
  688. {
  689. if(sky==false)
  690. sky=true;
  691. else
  692. sky=false;
  693. }
  694. if(nChar==77) // 按下'M'键
  695. {
  696. if(smoke==false)
  697. smoke=true;
  698. else
  699. smoke=false;
  700. }
  701. // 视点模式
  702. if(nChar==49) // 按下'1'键
  703. {
  704. cameratype=0;
  705. }
  706. if(nChar==50) // 按下'2'键
  707. {
  708. cameratype=1;
  709. }
  710. if(nChar==51) // 按下'3'键
  711. {
  712. int t;
  713. cameratype=2;
  714. nb=-1;
  715. for(t=0;t<MAXBULLET;t++)
  716. {
  717. if(bul[t].active==true)
  718. {
  719. nb=t;
  720. break;
  721. }
  722. }
  723. if(nb==-1)
  724. cameratype=0;
  725. }
  726. CView::OnKeyDown(nChar, nRepCnt, nFlags);
  727. }