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

游戏引擎

开发平台:

Visual C++

  1. // RLMManager.cpp: implementation of the CRLMManager class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "RLMManager.h"
  6. #include "texture.h"
  7. #include <stdio.h>
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. CRLMManager::CRLMManager()
  12. {
  13.     m_bMultitexEnable=false;
  14. m_pLight=NULL;
  15. m_pQuads=NULL;
  16. m_pTexID=NULL;
  17. m_pVertPool=NULL;
  18. }
  19. CRLMManager::~CRLMManager()
  20. {
  21. if(m_pLight!=NULL)
  22. delete [] m_pLight;
  23. if(m_pQuads!=NULL)
  24. delete [] m_pQuads;
  25. if(m_pTexID!=NULL)
  26. delete [] m_pTexID;
  27. if(m_pVertPool!=NULL)
  28. delete [] m_pVertPool;
  29. }
  30. bool CRLMManager::ReadRLMFile()
  31. {
  32. FILE *fp=NULL;
  33.     fp=fopen("lightmap2.rlm", "rb");
  34. if(fp==NULL)
  35. {
  36. MessageBox(0, "Can not open rlm file !", "Error", MB_OK | MB_ICONERROR);
  37.     return false; //
  38. }
  39. /////////////////////// check the file
  40. char id[24],version;
  41. fread(id,      sizeof(char),24,fp);
  42. fread(&version,sizeof(char),1 ,fp);
  43. if(strncmp( id, "Radiosity Lighting Model", 24 ) != 0 )
  44. {
  45. MessageBox(0, "Not a avalid rlm file !", "Error", MB_OK | MB_ICONERROR);
  46.     fclose(fp);
  47.     return false; // "Not a valid .skn file."
  48. }
  49. if ( version !=1 )
  50. {
  51.     MessageBox(0, "Version error !", "Error", MB_OK | MB_ICONERROR);
  52.     fclose(fp);
  53.     return false; // "Not a valid .skn file."
  54. }
  55. ////////////////read light info
  56. fread(&m_diffuse, sizeof(unsigned char),1 ,fp);
  57. fread(&m_lightnum,sizeof(int),          1 ,fp);
  58.     m_pLight=new LIGHT[m_lightnum];
  59.     fread(m_pLight ,sizeof(LIGHT), m_lightnum ,fp);
  60. ////////////////read texture info
  61. fread(&m_texnum, sizeof(int),1 ,fp);
  62. m_pTexID=new unsigned int [m_texnum];
  63. char filename[64];
  64. CTexture  cTexture;
  65. for(int i=0;i<m_texnum;i++)
  66. {
  67.         fread(filename ,sizeof(char),64 ,fp);
  68.         if(!cTexture.MakeTextureBind(filename,&m_pTexID[i]))
  69. {
  70.         MessageBox(0, "Can not open texture file  !", "Error", MB_OK | MB_ICONERROR);
  71.         fclose(fp);
  72.         return false; // "Not a valid .skn file."
  73. }
  74. }
  75. /////////////// read vertex data
  76. fread(&m_vertnum, sizeof(int),1 ,fp);
  77. m_pVertPool=new VERTEX [m_vertnum];
  78. fread(m_pVertPool, sizeof(VERTEX),m_vertnum ,fp);
  79.     /////////////// read quads data
  80. fread(&m_quadnum, sizeof(int),1 ,fp);
  81.     m_pQuads=new RLM_QUAD[m_quadnum];
  82. m_pRlmID=new unsigned int [m_quadnum];
  83.     unsigned char temp[32*32*3];
  84. for( i=0;i<m_quadnum;i++)
  85. {
  86.      fread(&m_pQuads[i], sizeof(RLM_QUAD), 1,fp);
  87.         fread(temp,sizeof(unsigned char),32*32*3,fp);
  88. if(!cTexture.MakeRadiosityLightmapBind(temp,32,32,&m_pRlmID[i]))
  89. {
  90.         MessageBox(0, "Can not make rlm texture !", "Error", MB_OK | MB_ICONERROR);
  91.         fclose(fp);
  92.         return false; // "Not a valid .skn file."
  93. }
  94. }
  95. ///////////// close file
  96. fclose(fp);
  97. return true;
  98. }
  99. ////////////////////////////////////////
  100. bool CRLMManager::InitRadiosityScene()
  101. {
  102. if(!ReadRLMFile())
  103. {
  104. MessageBox(0, "Read rlm file error !", "Error", MB_OK | MB_ICONERROR);
  105. return FALSE;
  106. }
  107. /* if(!m_cExtension.initMultitexture())
  108. {
  109. MessageBox(0, "cRadiosity error 2", "Error", MB_OK | MB_ICONERROR);
  110. return FALSE;
  111. }*/
  112.     m_bMultitexEnable=true;
  113. return true;
  114. }
  115. void CRLMManager::DrawRadiosityScene(int renderModel)
  116. {
  117. // if(m_bMultitexEnable)MultitextureDrawScene();
  118. // else 
  119. BlendDrawScene(renderModel);
  120.         DrawLight();
  121. }
  122. void CRLMManager::BlendDrawScene(int renderModel)
  123. {
  124. int i,j;
  125. for(i=0;i<m_quadnum;i++)//m_quadnum
  126. {
  127. //////////////First layer
  128.         glBindTexture(GL_TEXTURE_2D,m_pTexID[m_pQuads[i].texid]);
  129.         glEnable(GL_TEXTURE_2D);
  130.     glBegin(GL_QUADS);
  131.     for(j=0;j<4;j++)
  132. {
  133.             glTexCoord2f(m_pQuads[i].texcoord[j].u,m_pQuads[i].texcoord[j].v);
  134. glVertex3f(m_pVertPool[(m_pQuads[i].vertindex[j])].xpos,
  135.        m_pVertPool[(m_pQuads[i].vertindex[j])].ypos,
  136.    m_pVertPool[(m_pQuads[i].vertindex[j])].zpos);
  137. }
  138.      glEnd();
  139.         ////////////second layer
  140.         if(renderModel==0)glBlendFunc(GL_DST_COLOR,GL_ONE);
  141.         if(renderModel==1)glBlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
  142.         if(renderModel==2)glBlendFunc(GL_ONE,GL_SRC_COLOR);
  143.         if(renderModel==3)glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);
  144.         if(renderModel==4)glBlendFunc(GL_SRC_COLOR,GL_ONE);
  145.         if(renderModel==5)glBlendFunc(GL_ZERO,GL_ONE);
  146. if(renderModel<6)glEnable(GL_BLEND);
  147.         glBindTexture(GL_TEXTURE_2D,m_pRlmID[i]);
  148.         glEnable(GL_TEXTURE_2D);
  149.      glBegin(GL_QUADS);
  150.             glTexCoord2f(0,0);
  151. glVertex3f(m_pVertPool[(m_pQuads[i].vertindex[0])].xpos,
  152.        m_pVertPool[(m_pQuads[i].vertindex[0])].ypos,
  153.    m_pVertPool[(m_pQuads[i].vertindex[0])].zpos);
  154.             glTexCoord2f(1,0);
  155. glVertex3f(m_pVertPool[(m_pQuads[i].vertindex[1])].xpos,
  156.        m_pVertPool[(m_pQuads[i].vertindex[1])].ypos,
  157.    m_pVertPool[(m_pQuads[i].vertindex[1])].zpos);
  158.             glTexCoord2f(1,1);
  159. glVertex3f(m_pVertPool[(m_pQuads[i].vertindex[2])].xpos,
  160.        m_pVertPool[(m_pQuads[i].vertindex[2])].ypos,
  161.    m_pVertPool[(m_pQuads[i].vertindex[2])].zpos);
  162.             glTexCoord2f(0,1);
  163. glVertex3f(m_pVertPool[(m_pQuads[i].vertindex[3])].xpos,
  164.        m_pVertPool[(m_pQuads[i].vertindex[3])].ypos,
  165.    m_pVertPool[(m_pQuads[i].vertindex[3])].zpos);
  166.     glEnd();
  167.         glDisable(GL_BLEND);
  168. }
  169. }
  170. void CRLMManager::MultitextureDrawScene()
  171. {  /*
  172. m_cExtension.glActiveTextureARB(GL_TEXTURE1_ARB);
  173. glBindTexture(GL_TEXTURE_2D,m_texlight);
  174. glEnable(GL_TEXTURE_2D);
  175.     m_cExtension.glActiveTextureARB(GL_TEXTURE0_ARB);
  176. glBindTexture(GL_TEXTURE_2D,m_tex0);
  177. glEnable(GL_TEXTURE_2D);
  178. glBegin(GL_QUADS);
  179. m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE0_ARB,0,0); 
  180. m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE1_ARB,0,0); 
  181. glVertex3f(0,0,0);
  182. m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE0_ARB,0,1); 
  183. m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE1_ARB,0,1); 
  184. glVertex3f(0,0,3.2f);
  185. m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE0_ARB,1,1); 
  186. m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE1_ARB,1,1); 
  187. glVertex3f(-3.2f,0,3.2f);
  188. m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE0_ARB,1,0); 
  189. m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE1_ARB,1,0); 
  190. glVertex3f(-3.2f,0,0);
  191. glEnd();
  192. m_cExtension.glActiveTextureARB(GL_TEXTURE1_ARB);
  193. glDisable(GL_TEXTURE_2D);
  194. m_cExtension.glActiveTextureARB(GL_TEXTURE0_ARB);
  195. glDisable(GL_TEXTURE_2D);
  196. */
  197. }
  198. void CRLMManager::DrawLight()
  199. {
  200. glDisable(GL_TEXTURE_2D);
  201. glDisable(GL_CULL_FACE);
  202. for(int i=0;i<m_lightnum;i++)
  203. {
  204. glColor3f(m_pLight[i].color.r,m_pLight[i].color.g,m_pLight[i].color.b);
  205.         float size=0.2f;
  206. glBegin(GL_QUADS);
  207.     /////Y
  208.             glVertex3f(m_pLight[i].position.xpos-size,
  209.                        m_pLight[i].position.ypos,
  210.    m_pLight[i].position.zpos);
  211.             glVertex3f(m_pLight[i].position.xpos,
  212.                        m_pLight[i].position.ypos,
  213.    m_pLight[i].position.zpos+size);
  214.             glVertex3f(m_pLight[i].position.xpos+size,
  215.                        m_pLight[i].position.ypos,
  216.    m_pLight[i].position.zpos);
  217.             glVertex3f(m_pLight[i].position.xpos,
  218.                        m_pLight[i].position.ypos,
  219.    m_pLight[i].position.zpos-size);
  220. glEnd();
  221. glBegin(GL_QUADS);
  222.             //////// X
  223.             glVertex3f(m_pLight[i].position.xpos,
  224.                        m_pLight[i].position.ypos-size,
  225.    m_pLight[i].position.zpos);
  226.             glVertex3f(m_pLight[i].position.xpos,
  227.                        m_pLight[i].position.ypos,
  228.    m_pLight[i].position.zpos+size);
  229.             glVertex3f(m_pLight[i].position.xpos,
  230.                        m_pLight[i].position.ypos+size,
  231.    m_pLight[i].position.zpos);
  232.             glVertex3f(m_pLight[i].position.xpos,
  233.                        m_pLight[i].position.ypos,
  234.    m_pLight[i].position.zpos-size);
  235. glEnd();
  236. glBegin(GL_QUADS);
  237.             /////// Z
  238.             glVertex3f(m_pLight[i].position.xpos-size,
  239.                        m_pLight[i].position.ypos,
  240.    m_pLight[i].position.zpos);
  241.             glVertex3f(m_pLight[i].position.xpos,
  242.                        m_pLight[i].position.ypos+size,
  243.    m_pLight[i].position.zpos);
  244.             glVertex3f(m_pLight[i].position.xpos+size,
  245.                        m_pLight[i].position.ypos,
  246.    m_pLight[i].position.zpos);
  247.             glVertex3f(m_pLight[i].position.xpos,
  248.                        m_pLight[i].position.ypos-size,
  249.    m_pLight[i].position.zpos);
  250. glEnd();
  251. }
  252.     glEnable(GL_CULL_FACE);
  253. glColor3f(1,1,1);
  254. }