SkyBox.cpp
上传用户:dfjhuyju
上传日期:2013-03-13
资源大小:11035k
文件大小:5k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // SkyBox.cpp: implementation of the CSkyBox class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "SkyBox.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. CSkyBox::CSkyBox()
  10. {
  11. }
  12. CSkyBox::~CSkyBox()
  13. {
  14. }
  15. bool CSkyBox::InitSkyBox(int XLenght, int YLenght,int ZLenght,
  16.  int CenterX,int CenterY,int CenterZ)
  17. {
  18. m_XLength=XLenght;
  19. m_YLength=YLenght;
  20. m_ZLength=ZLenght;
  21. m_CenterX=CenterX;
  22. m_CenterY=CenterY;
  23. m_CenterZ=CenterZ;
  24. texSky[0] = CreateTexture("Sky\skyb0.bmp");
  25. texSky[1] = CreateTexture("Sky\skyb1.bmp");
  26. texSky[2] = CreateTexture("Sky\skyb2.bmp");
  27. texSky[3] = CreateTexture("Sky\skyb3.bmp");
  28. texSky[4] = CreateTexture("Sky\skyb4.bmp");
  29. texSky[5] = CreateTexture("Sky\ground.bmp");
  30. return true;
  31. }
  32. void CSkyBox::DrawSkyBox()
  33. {
  34. int lx,ly,lz,cx,cy,cz;
  35. lx=m_XLength;
  36. ly=m_YLength;
  37. lz=m_ZLength;
  38. cx=m_CenterX;
  39. cy=m_CenterY;
  40. cz=m_CenterZ;
  41. float btmy=0.0f;//This variable indicate how much texture should be cut from 0 to btmy
  42. //  glDisable(GL_FOG); 
  43. ////////front
  44.   glBindTexture(GL_TEXTURE_2D, texSky[0]);
  45.     glEnable(GL_TEXTURE_2D); 
  46.    glBegin(GL_QUADS);
  47.             glTexCoord2f(0,btmy);
  48.     glVertex3i(-lx+cx, cy,   -lz+cz);
  49.              glTexCoord2f(1,btmy);
  50.     glVertex3i(lx+cx, cy,   -lz+cz);
  51.             glTexCoord2f(1,1);
  52.     glVertex3i(lx+cx, ly+cy,   -lz+cz);
  53.             glTexCoord2f(0,1);
  54.     glVertex3i(-lx+cx, ly+cy,    -lz+cz);
  55.             glEnd();
  56. glDisable(GL_TEXTURE_2D); 
  57. ///     ////////////////sky1
  58. /////////////////right
  59. glBindTexture(GL_TEXTURE_2D, texSky[1]);
  60.     glEnable(GL_TEXTURE_2D); 
  61.   
  62. glBegin(GL_QUADS);
  63.             glTexCoord2f(0,btmy);
  64.     glVertex3i(lx+cx, cy,    -lz+cz);
  65.              glTexCoord2f(1,btmy);
  66.     glVertex3i(lx+cx, cy,    lz+cz);
  67.             glTexCoord2f(1,1);
  68.     glVertex3i(lx+cx, ly+cy,    lz+cz);
  69.             glTexCoord2f(0,1);
  70.     glVertex3i(lx+cx, ly+cy,    -lz+cz);
  71.             glEnd();
  72. glDisable(GL_TEXTURE_2D); 
  73. ///     ////////////////sky2
  74. //////////////back
  75. glBindTexture(GL_TEXTURE_2D, texSky[2]);
  76.     glEnable(GL_TEXTURE_2D); 
  77.  
  78. glBegin(GL_QUADS);
  79.             glTexCoord2f(0,btmy);
  80.     glVertex3i(lx+cx, cy,    lz+cz);
  81.              glTexCoord2f(1,btmy);
  82.     glVertex3i(-lx+cx, cy,    lz+cz);
  83.             glTexCoord2f(1,1);
  84.     glVertex3i(-lx+cx, ly+cy,    lz+cz);
  85.             glTexCoord2f(0,1);
  86.     glVertex3i(lx+cx, ly+cy,    lz+cz);
  87.             glEnd();
  88. glDisable(GL_TEXTURE_2D); 
  89. ///     ////////////////sky3
  90. ////////////////left
  91. glBindTexture(GL_TEXTURE_2D, texSky[3]);
  92.     glEnable(GL_TEXTURE_2D); 
  93.        glBegin(GL_QUADS);
  94.             glTexCoord2f(0,btmy);
  95.     glVertex3i(-lx+cx, cy,    lz+cz);
  96.              glTexCoord2f(1,btmy);
  97.     glVertex3i(-lx+cx, cy,    -lz+cz);
  98.             glTexCoord2f(1,1);
  99.     glVertex3i(-lx+cx, ly+cy,    -lz+cz);
  100.             glTexCoord2f(0,1);
  101.     glVertex3i(-lx+cx, ly+cy,    lz+cz);
  102.             glEnd();
  103. glDisable(GL_TEXTURE_2D); 
  104. ///     ////////////////sky4
  105. /////////////up
  106. glBindTexture(GL_TEXTURE_2D, texSky[4]);
  107.     glEnable(GL_TEXTURE_2D); 
  108. glBegin(GL_QUADS);
  109.             glTexCoord2f(0,0);
  110.     glVertex3i(-lx+cx, ly+cy,    -lz+cz);
  111.              glTexCoord2f(1,0);
  112.     glVertex3i(lx+cx,ly+cy,    -lz+cz);
  113.             glTexCoord2f(1,1);
  114.     glVertex3i(lx+cx, ly+cy,    lz+cz);
  115.             glTexCoord2f(0,1);
  116.     glVertex3i(-lx+cx, ly+cy,    lz+cz);
  117.             glEnd();
  118. glDisable(GL_TEXTURE_2D);
  119. ///////地面
  120. glBindTexture(GL_TEXTURE_2D, texSky[5]);
  121.     glEnable(GL_TEXTURE_2D); 
  122. glBegin(GL_QUADS);
  123.             glTexCoord2f(0,0);
  124.     glVertex3i(-lx+cx, cy,    -lz+cz);
  125.              glTexCoord2f(1,0);
  126.     glVertex3i(lx+cx,cy,    -lz+cz);
  127.             glTexCoord2f(1,1);
  128.     glVertex3i(lx+cx, cy,    lz+cz);
  129.             glTexCoord2f(0,1);
  130.     glVertex3i(-lx+cx, cy,    lz+cz);
  131.             glEnd();
  132. glDisable(GL_TEXTURE_2D);
  133. }
  134. GLuint CSkyBox::CreateTexture( CString filename )
  135. {
  136.     GLuint      texture;     
  137. _AUX_RGBImageRec *Image;
  138.     Image = auxDIBImageLoadA( (const char*) filename );
  139.     glGenTextures(1, &texture);
  140.     glBindTexture(GL_TEXTURE_2D, texture);
  141.    
  142.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_CLAMP);
  143. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_CLAMP);
  144. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_NEAREST);
  145. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
  146. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  147. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  148. gluBuild2DMipmaps(GL_TEXTURE_2D, 3, Image->sizeX,
  149.                               Image->sizeY, GL_RGB,
  150.                               GL_UNSIGNED_BYTE, Image->data);
  151.    
  152.    delete Image;
  153. return texture;
  154. }