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

游戏引擎

开发平台:

Visual C++

  1. // SkyBox.cpp: implementation of the CSkyBox class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Texture.h"
  6. #include "SkyBox.h"
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. CSkyBox::CSkyBox()
  11. {
  12. }
  13. CSkyBox::~CSkyBox()
  14. {
  15. for(int i=0;i<5;i++)
  16.     glDeleteTextures(1,&texSky[i]);
  17. }
  18. bool CSkyBox::LoadSkyTexture()
  19. {
  20. CTexture Texture;
  21. char filename[64];
  22. for(int i=0;i<5;i++)
  23. {
  24. wsprintf(filename,"sky/skyb%d.bmp",i);
  25.         if(!Texture.MakeTextureBind(filename,&texSky[i],1,0))
  26.      return false;
  27.   }
  28. return true;
  29. }
  30. bool CSkyBox::InitSkyBox(int XLenght, int YLenght,int ZLenght,
  31.  int CenterX,int CenterY,int CenterZ)
  32. {
  33. m_XLength=XLenght;
  34. m_YLength=YLenght;
  35. m_ZLength=ZLenght;
  36. m_CenterX=CenterX;
  37. m_CenterY=CenterY;
  38. m_CenterZ=CenterZ;
  39. if(!LoadSkyTexture())return false;
  40. return true;
  41. }
  42. void CSkyBox::DrawSkyBox()
  43. {
  44. int lx,ly,lz,cx,cy,cz;
  45. lx=m_XLength;
  46. ly=m_YLength;
  47. lz=m_ZLength;
  48. cx=m_CenterX;
  49. cy=m_CenterY;
  50. cz=m_CenterZ;
  51. float btmy=0.0f;//This variable indicate how much texture should be cut from 0 to btmy
  52. //    glDisable(GL_FOG); 
  53. ////////front
  54.   glBindTexture(GL_TEXTURE_2D, texSky[0]);
  55.     glEnable(GL_TEXTURE_2D); 
  56.    glBegin(GL_QUADS);
  57.             glTexCoord2f(0,btmy);
  58.     glVertex3i(-lx+cx, cy,   -lz+cz);
  59.              glTexCoord2f(1,btmy);
  60.     glVertex3i(lx+cx, cy,   -lz+cz);
  61.             glTexCoord2f(1,1);
  62.     glVertex3i(lx+cx, ly+cy,   -lz+cz);
  63.             glTexCoord2f(0,1);
  64.     glVertex3i(-lx+cx, ly+cy,    -lz+cz);
  65.             glEnd();
  66. glDisable(GL_TEXTURE_2D); 
  67. ///     ////////////////sky1
  68. /////////////////right
  69. glBindTexture(GL_TEXTURE_2D, texSky[1]);
  70.     glEnable(GL_TEXTURE_2D); 
  71.   
  72. glBegin(GL_QUADS);
  73.             glTexCoord2f(0,btmy);
  74.     glVertex3i(lx+cx, cy,    -lz+cz);
  75.              glTexCoord2f(1,btmy);
  76.     glVertex3i(lx+cx, cy,    lz+cz);
  77.             glTexCoord2f(1,1);
  78.     glVertex3i(lx+cx, ly+cy,    lz+cz);
  79.             glTexCoord2f(0,1);
  80.     glVertex3i(lx+cx, ly+cy,    -lz+cz);
  81.             glEnd();
  82. glDisable(GL_TEXTURE_2D); 
  83. ///     ////////////////sky2
  84. //////////////back
  85. glBindTexture(GL_TEXTURE_2D, texSky[2]);
  86.     glEnable(GL_TEXTURE_2D); 
  87.  
  88. glBegin(GL_QUADS);
  89.             glTexCoord2f(0,btmy);
  90.     glVertex3i(lx+cx, cy,    lz+cz);
  91.              glTexCoord2f(1,btmy);
  92.     glVertex3i(-lx+cx, cy,    lz+cz);
  93.             glTexCoord2f(1,1);
  94.     glVertex3i(-lx+cx, ly+cy,    lz+cz);
  95.             glTexCoord2f(0,1);
  96.     glVertex3i(lx+cx, ly+cy,    lz+cz);
  97.             glEnd();
  98. glDisable(GL_TEXTURE_2D); 
  99. ///     ////////////////sky3
  100. ////////////////left
  101. glBindTexture(GL_TEXTURE_2D, texSky[3]);
  102.     glEnable(GL_TEXTURE_2D); 
  103.        glBegin(GL_QUADS);
  104.             glTexCoord2f(0,btmy);
  105.     glVertex3i(-lx+cx, cy,    lz+cz);
  106.              glTexCoord2f(1,btmy);
  107.     glVertex3i(-lx+cx, cy,    -lz+cz);
  108.             glTexCoord2f(1,1);
  109.     glVertex3i(-lx+cx, ly+cy,    -lz+cz);
  110.             glTexCoord2f(0,1);
  111.     glVertex3i(-lx+cx, ly+cy,    lz+cz);
  112.             glEnd();
  113. glDisable(GL_TEXTURE_2D); 
  114. ///     ////////////////sky4
  115. /////////////up
  116. glBindTexture(GL_TEXTURE_2D, texSky[4]);
  117.     glEnable(GL_TEXTURE_2D); 
  118. glBegin(GL_QUADS);
  119.             glTexCoord2f(0,0);
  120.     glVertex3i(-lx+cx, ly+cy,    -lz+cz);
  121.              glTexCoord2f(1,0);
  122.     glVertex3i(lx+cx,ly+cy,    -lz+cz);
  123.             glTexCoord2f(1,1);
  124.     glVertex3i(lx+cx, ly+cy,    lz+cz);
  125.             glTexCoord2f(0,1);
  126.     glVertex3i(-lx+cx, ly+cy,    lz+cz);
  127.             glEnd();
  128. glDisable(GL_TEXTURE_2D);
  129.   //  glEnable(GL_FOG); 
  130. }