www.pudn.com > QuadTreeLOD4cs.rar > SkyBox.cpp, change:2003-08-14,size:6618b


// SkyBox.cpp: implementation of the CSkyBox class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "SkyBox.h" 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
#define BACK_ID		0		// The texture ID for the back side of the cube 
#define FRONT_ID	1		// The texture ID for the front side of the cube 
#define BOTTOM_ID	2		// The texture ID for the bottom side of the cube 
#define TOP_ID		3		// The texture ID for the top side of the cube 
#define LEFT_ID		4		// The texture ID for the left side of the cube 
#define RIGHT_ID	5		// The texture ID for the right side of the cube 
#define GL_CLAMP_TO_EDGE	0x812F						// This is for our skybox textures 
 
CSkyBox::CSkyBox() 
{ 
 
} 
 
CSkyBox::~CSkyBox() 
{ 
	for(int i=0;i<5;i++) 
	{ 
		unsigned int id = m_Txt[i].GetTxtID(); 
	    glDeleteTextures(1,&id); 
	} 
} 
void CSkyBox::Draw(const CFrustum &frustum) 
{ 
	 
//	glDisable(GL_TEXTURE_2D); 
	glEnable(GL_TEXTURE_2D); 
		// This centers the sky box around (m_fX, m_fY, m_fZ) 
//	m_fX = m_fX - m_fWidth  / 2; 
//	m_fY = m_fY - m_fHeight / 2; 
//	m_fZ = m_fZ - m_fLen / 2; 
 
	// Bind the BACK texture of the sky map to the BACK side of the cube 
	glBindTexture(GL_TEXTURE_2D, m_Txt[BACK_ID].GetTxtID()); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
 
 
	// Start drawing the side as a QUAD 
	glBegin(GL_QUADS);		 
		 
		// Assign the texture coordinates and vertices for the BACK Side 
		glTexCoord2f(1.0f, 0.0f); glVertex3f(m_fX + m_fWidth, m_fY,			m_fZ); 
		glTexCoord2f(1.0f, 1.0f); glVertex3f(m_fX + m_fWidth, m_fY + m_fHeight, m_fZ);  
		glTexCoord2f(0.0f, 1.0f); glVertex3f(m_fX,			m_fY + m_fHeight, m_fZ); 
		glTexCoord2f(0.0f, 0.0f); glVertex3f(m_fX,			m_fY,			m_fZ); 
		 
	glEnd(); 
 
	// Bind the FRONT texture of the sky map to the FRONT side of the box 
	glBindTexture(GL_TEXTURE_2D, m_Txt[FRONT_ID].GetTxtID()); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
 
 
	// Start drawing the side as a QUAD 
	glBegin(GL_QUADS);	 
	 
		// Assign the texture coordinates and vertices for the FRONT Side 
		glTexCoord2f(1.0f, 0.0f); glVertex3f(m_fX,			m_fY,			m_fZ + m_fLen); 
		glTexCoord2f(1.0f, 1.0f); glVertex3f(m_fX,			m_fY + m_fHeight, m_fZ + m_fLen); 
		glTexCoord2f(0.0f, 1.0f); glVertex3f(m_fX + m_fWidth, m_fY + m_fHeight, m_fZ + m_fLen);  
		glTexCoord2f(0.0f, 0.0f); glVertex3f(m_fX + m_fWidth, m_fY,			m_fZ + m_fLen); 
	glEnd(); 
 
	// Bind the BOTTOM texture of the sky map to the BOTTOM side of the box 
	glBindTexture(GL_TEXTURE_2D, m_Txt[BOTTOM_ID].GetTxtID()); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
 
	// Start drawing the side as a QUAD 
//	glBegin(GL_QUADS);		 
	 
		// Assign the texture coordinates and vertices for the BOTTOM Side 
//		/*glTexCoord2f(1.0f, 0.0f); */glVertex3f(m_fX,			m_fY,			m_fZ); 
//		/*glTexCoord2f(1.0f, 1.0f);*/ glVertex3f(m_fX,			m_fY,			m_fZ + m_fLen); 
//		/*glTexCoord2f(0.0f, 1.0f);*/ glVertex3f(m_fX + m_fWidth, m_fY,			m_fZ + m_fLen);  
//		/*glTexCoord2f(0.0f, 0.0f); */glVertex3f(m_fX + m_fWidth, m_fY,			m_fZ); 
//	glEnd(); 
 
	// Bind the TOP texture of the sky map to the TOP side of the box 
	glBindTexture(GL_TEXTURE_2D, m_Txt[TOP_ID].GetTxtID()); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
 
	// Start drawing the side as a QUAD 
	glBegin(GL_QUADS);		 
		 
		// Assign the texture coordinates and vertices for the TOP Side 
		glTexCoord2f(0.0f, 1.0f); glVertex3f(m_fX + m_fWidth, m_fY + m_fHeight, m_fZ); 
		glTexCoord2f(0.0f, 0.0f); glVertex3f(m_fX + m_fWidth, m_fY + m_fHeight, m_fZ + m_fLen);  
		glTexCoord2f(1.0f, 0.0f); glVertex3f(m_fX,			m_fY + m_fHeight,	m_fZ + m_fLen); 
		glTexCoord2f(1.0f, 1.0f); glVertex3f(m_fX,			m_fY + m_fHeight,	m_fZ); 
		 
	glEnd(); 
 
	// Bind the LEFT texture of the sky map to the LEFT side of the box 
	glBindTexture(GL_TEXTURE_2D,m_Txt[LEFT_ID].GetTxtID()); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
 
	// Start drawing the side as a QUAD 
	glBegin(GL_QUADS);		 
		 
		// Assign the texture coordinates and vertices for the LEFT Side 
		glTexCoord2f(1.0f, 1.0f); glVertex3f(m_fX,			m_fY + m_fHeight,	m_fZ);	 
		glTexCoord2f(0.0f, 1.0f); glVertex3f(m_fX,			m_fY + m_fHeight,	m_fZ + m_fLen);  
		glTexCoord2f(0.0f, 0.0f); glVertex3f(m_fX,			m_fY,			m_fZ + m_fLen); 
		glTexCoord2f(1.0f, 0.0f); glVertex3f(m_fX,			m_fY,			m_fZ);		 
		 
	glEnd(); 
 
	// Bind the RIGHT texture of the sky map to the RIGHT side of the box 
	glBindTexture(GL_TEXTURE_2D, m_Txt[RIGHT_ID].GetTxtID()); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
 
	// Start drawing the side as a QUAD 
	glBegin(GL_QUADS);		 
 
		// Assign the texture coordinates and vertices for the RIGHT Side 
		glTexCoord2f(0.0f, 0.0f); glVertex3f(m_fX + m_fWidth, m_fY,			m_fZ); 
		glTexCoord2f(1.0f, 0.0f); glVertex3f(m_fX + m_fWidth, m_fY,			m_fZ + m_fLen); 
		glTexCoord2f(1.0f, 1.0f); glVertex3f(m_fX + m_fWidth, m_fY + m_fHeight,	m_fZ + m_fLen);  
		glTexCoord2f(0.0f, 1.0f); glVertex3f(m_fX + m_fWidth, m_fY + m_fHeight,	m_fZ); 
	glEnd(); 
	glDisable(GL_TEXTURE_2D);	 
} 
//x,y,z 为中心点坐标 
void CSkyBox::Init(float x, float y, float z, float width, float height, float length) 
{ 
	m_fWidth=width;m_fHeight=height;m_fLen=length; 
	m_fX = x - width  / 2; 
	m_fY = y - height / 2; 
	m_fZ = z - length / 2; 
	m_Txt[BACK_ID].LoadGLTextures("SkyBox\\Back.bmp"); 
	m_Txt[FRONT_ID].LoadGLTextures("SkyBox\\Front.bmp"); 
	m_Txt[BOTTOM_ID].LoadGLTextures("SkyBox\\Bottom.bmp"); 
	m_Txt[TOP_ID].LoadGLTextures("SkyBox\\Top.bmp"); 
	m_Txt[LEFT_ID].LoadGLTextures("SkyBox\\Left.bmp"); 
	m_Txt[RIGHT_ID].LoadGLTextures("SkyBox\\Right.bmp"); 
 
/*	m_Txt[BACK_ID].MakeTextureBind("SkyBox/skyb2.bmp",1,0); 
	m_Txt[FRONT_ID].MakeTextureBind("SkyBox/skyb0.bmp",1,0); 
//	m_Txt[BOTTOM_ID].MakeTextureBind("SkyBox/skyb2.bmp",1,0); 
	m_Txt[TOP_ID].MakeTextureBind("SkyBox/skyb4.bmp",1,0); 
	m_Txt[LEFT_ID].MakeTextureBind("SkyBox/skyb3.bmp",1,0); 
	m_Txt[RIGHT_ID].MakeTextureBind("SkyBox/skyb1.bmp",1,0); 
*/ 
}