RLMManager.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:9k
- // RLMManager.cpp: implementation of the CRLMManager class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "RLMManager.h"
- #include "texture.h"
- #include <stdio.h>
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CRLMManager::CRLMManager()
- {
- m_bMultitexEnable=false;
- m_pLight=NULL;
- m_pQuads=NULL;
- m_pTexID=NULL;
- m_pVertPool=NULL;
- }
- CRLMManager::~CRLMManager()
- {
- if(m_pLight!=NULL)
- delete [] m_pLight;
- if(m_pQuads!=NULL)
- delete [] m_pQuads;
- if(m_pTexID!=NULL)
- delete [] m_pTexID;
- if(m_pVertPool!=NULL)
- delete [] m_pVertPool;
- }
- bool CRLMManager::ReadRLMFile()
- {
- FILE *fp=NULL;
- fp=fopen("lightmap2.rlm", "rb");
- if(fp==NULL)
- {
- MessageBox(0, "Can not open rlm file !", "Error", MB_OK | MB_ICONERROR);
- return false; //
- }
- /////////////////////// check the file
- char id[24],version;
- fread(id, sizeof(char),24,fp);
- fread(&version,sizeof(char),1 ,fp);
- if(strncmp( id, "Radiosity Lighting Model", 24 ) != 0 )
- {
- MessageBox(0, "Not a avalid rlm file !", "Error", MB_OK | MB_ICONERROR);
- fclose(fp);
- return false; // "Not a valid .skn file."
- }
- if ( version !=1 )
- {
- MessageBox(0, "Version error !", "Error", MB_OK | MB_ICONERROR);
- fclose(fp);
- return false; // "Not a valid .skn file."
- }
- ////////////////read light info
- fread(&m_diffuse, sizeof(unsigned char),1 ,fp);
- fread(&m_lightnum,sizeof(int), 1 ,fp);
- m_pLight=new LIGHT[m_lightnum];
- fread(m_pLight ,sizeof(LIGHT), m_lightnum ,fp);
- ////////////////read texture info
- fread(&m_texnum, sizeof(int),1 ,fp);
- m_pTexID=new unsigned int [m_texnum];
- char filename[64];
- CTexture cTexture;
- for(int i=0;i<m_texnum;i++)
- {
- fread(filename ,sizeof(char),64 ,fp);
- if(!cTexture.MakeTextureBind(filename,&m_pTexID[i]))
- {
- MessageBox(0, "Can not open texture file !", "Error", MB_OK | MB_ICONERROR);
- fclose(fp);
- return false; // "Not a valid .skn file."
- }
- }
- /////////////// read vertex data
- fread(&m_vertnum, sizeof(int),1 ,fp);
- m_pVertPool=new VERTEX [m_vertnum];
- fread(m_pVertPool, sizeof(VERTEX),m_vertnum ,fp);
- /////////////// read quads data
- fread(&m_quadnum, sizeof(int),1 ,fp);
- m_pQuads=new RLM_QUAD[m_quadnum];
- m_pRlmID=new unsigned int [m_quadnum];
- unsigned char temp[32*32*3];
- for( i=0;i<m_quadnum;i++)
- {
- fread(&m_pQuads[i], sizeof(RLM_QUAD), 1,fp);
- fread(temp,sizeof(unsigned char),32*32*3,fp);
- if(!cTexture.MakeRadiosityLightmapBind(temp,32,32,&m_pRlmID[i]))
- {
- MessageBox(0, "Can not make rlm texture !", "Error", MB_OK | MB_ICONERROR);
- fclose(fp);
- return false; // "Not a valid .skn file."
- }
- }
- ///////////// close file
- fclose(fp);
- return true;
- }
- ////////////////////////////////////////
- bool CRLMManager::InitRadiosityScene()
- {
- if(!ReadRLMFile())
- {
- MessageBox(0, "Read rlm file error !", "Error", MB_OK | MB_ICONERROR);
- return FALSE;
- }
- /* if(!m_cExtension.initMultitexture())
- {
- MessageBox(0, "cRadiosity error 2", "Error", MB_OK | MB_ICONERROR);
- return FALSE;
- }*/
- m_bMultitexEnable=true;
- return true;
- }
- void CRLMManager::DrawRadiosityScene(int renderModel)
- {
- // if(m_bMultitexEnable)MultitextureDrawScene();
- // else
- BlendDrawScene(renderModel);
- DrawLight();
- }
- void CRLMManager::BlendDrawScene(int renderModel)
- {
- int i,j;
- for(i=0;i<m_quadnum;i++)//m_quadnum
- {
- //////////////First layer
- glBindTexture(GL_TEXTURE_2D,m_pTexID[m_pQuads[i].texid]);
- glEnable(GL_TEXTURE_2D);
-
- glBegin(GL_QUADS);
- for(j=0;j<4;j++)
- {
- glTexCoord2f(m_pQuads[i].texcoord[j].u,m_pQuads[i].texcoord[j].v);
- glVertex3f(m_pVertPool[(m_pQuads[i].vertindex[j])].xpos,
- m_pVertPool[(m_pQuads[i].vertindex[j])].ypos,
- m_pVertPool[(m_pQuads[i].vertindex[j])].zpos);
- }
- glEnd();
-
- ////////////second layer
- if(renderModel==0)glBlendFunc(GL_DST_COLOR,GL_ONE);
- if(renderModel==1)glBlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
- if(renderModel==2)glBlendFunc(GL_ONE,GL_SRC_COLOR);
- if(renderModel==3)glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);
- if(renderModel==4)glBlendFunc(GL_SRC_COLOR,GL_ONE);
- if(renderModel==5)glBlendFunc(GL_ZERO,GL_ONE);
- if(renderModel<6)glEnable(GL_BLEND);
- glBindTexture(GL_TEXTURE_2D,m_pRlmID[i]);
- glEnable(GL_TEXTURE_2D);
- glBegin(GL_QUADS);
- glTexCoord2f(0,0);
- glVertex3f(m_pVertPool[(m_pQuads[i].vertindex[0])].xpos,
- m_pVertPool[(m_pQuads[i].vertindex[0])].ypos,
- m_pVertPool[(m_pQuads[i].vertindex[0])].zpos);
- glTexCoord2f(1,0);
- glVertex3f(m_pVertPool[(m_pQuads[i].vertindex[1])].xpos,
- m_pVertPool[(m_pQuads[i].vertindex[1])].ypos,
- m_pVertPool[(m_pQuads[i].vertindex[1])].zpos);
- glTexCoord2f(1,1);
- glVertex3f(m_pVertPool[(m_pQuads[i].vertindex[2])].xpos,
- m_pVertPool[(m_pQuads[i].vertindex[2])].ypos,
- m_pVertPool[(m_pQuads[i].vertindex[2])].zpos);
- glTexCoord2f(0,1);
- glVertex3f(m_pVertPool[(m_pQuads[i].vertindex[3])].xpos,
- m_pVertPool[(m_pQuads[i].vertindex[3])].ypos,
- m_pVertPool[(m_pQuads[i].vertindex[3])].zpos);
- glEnd();
- glDisable(GL_BLEND);
-
- }
- }
- void CRLMManager::MultitextureDrawScene()
- { /*
- m_cExtension.glActiveTextureARB(GL_TEXTURE1_ARB);
- glBindTexture(GL_TEXTURE_2D,m_texlight);
- glEnable(GL_TEXTURE_2D);
- m_cExtension.glActiveTextureARB(GL_TEXTURE0_ARB);
- glBindTexture(GL_TEXTURE_2D,m_tex0);
- glEnable(GL_TEXTURE_2D);
- glBegin(GL_QUADS);
- m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE0_ARB,0,0);
- m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE1_ARB,0,0);
- glVertex3f(0,0,0);
- m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE0_ARB,0,1);
- m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE1_ARB,0,1);
- glVertex3f(0,0,3.2f);
- m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE0_ARB,1,1);
- m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE1_ARB,1,1);
- glVertex3f(-3.2f,0,3.2f);
- m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE0_ARB,1,0);
- m_cExtension.glMultiTexCoord2fARB(GL_TEXTURE1_ARB,1,0);
- glVertex3f(-3.2f,0,0);
- glEnd();
- m_cExtension.glActiveTextureARB(GL_TEXTURE1_ARB);
- glDisable(GL_TEXTURE_2D);
- m_cExtension.glActiveTextureARB(GL_TEXTURE0_ARB);
- glDisable(GL_TEXTURE_2D);
- */
- }
- void CRLMManager::DrawLight()
- {
- glDisable(GL_TEXTURE_2D);
- glDisable(GL_CULL_FACE);
- for(int i=0;i<m_lightnum;i++)
- {
- glColor3f(m_pLight[i].color.r,m_pLight[i].color.g,m_pLight[i].color.b);
- float size=0.2f;
- glBegin(GL_QUADS);
- /////Y
- glVertex3f(m_pLight[i].position.xpos-size,
- m_pLight[i].position.ypos,
- m_pLight[i].position.zpos);
- glVertex3f(m_pLight[i].position.xpos,
- m_pLight[i].position.ypos,
- m_pLight[i].position.zpos+size);
- glVertex3f(m_pLight[i].position.xpos+size,
- m_pLight[i].position.ypos,
- m_pLight[i].position.zpos);
- glVertex3f(m_pLight[i].position.xpos,
- m_pLight[i].position.ypos,
- m_pLight[i].position.zpos-size);
- glEnd();
- glBegin(GL_QUADS);
- //////// X
- glVertex3f(m_pLight[i].position.xpos,
- m_pLight[i].position.ypos-size,
- m_pLight[i].position.zpos);
- glVertex3f(m_pLight[i].position.xpos,
- m_pLight[i].position.ypos,
- m_pLight[i].position.zpos+size);
- glVertex3f(m_pLight[i].position.xpos,
- m_pLight[i].position.ypos+size,
- m_pLight[i].position.zpos);
- glVertex3f(m_pLight[i].position.xpos,
- m_pLight[i].position.ypos,
- m_pLight[i].position.zpos-size);
- glEnd();
- glBegin(GL_QUADS);
- /////// Z
- glVertex3f(m_pLight[i].position.xpos-size,
- m_pLight[i].position.ypos,
- m_pLight[i].position.zpos);
- glVertex3f(m_pLight[i].position.xpos,
- m_pLight[i].position.ypos+size,
- m_pLight[i].position.zpos);
- glVertex3f(m_pLight[i].position.xpos+size,
- m_pLight[i].position.ypos,
- m_pLight[i].position.zpos);
- glVertex3f(m_pLight[i].position.xpos,
- m_pLight[i].position.ypos-size,
- m_pLight[i].position.zpos);
- glEnd();
- }
- glEnable(GL_CULL_FACE);
- glColor3f(1,1,1);
- }