⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 skybox.cpp

📁 3D赛车游戏源代码-用Visual Studio 2005
💻 CPP
字号:
#include "StdAfx.h"
#include "SkyBox.h"
#include "../res/Texture.h"

#define GL_CLAMP_TO_EDGE	0x812F	

//-------------------------------------------------------------------------------
// 天空盒全局对象
//-------------------------------------------------------------------------------
CSkyBox g_SkyBox;

//-------------------------------------------------------------------------------
// 构造函数
//-------------------------------------------------------------------------------
CSkyBox::CSkyBox()
{
	ZeroMemory(this, sizeof(this));
}

//-------------------------------------------------------------------------------
// 析构函数
//-------------------------------------------------------------------------------
CSkyBox::~CSkyBox()
{

}

//-------------------------------------------------------------------------------
// 设置一个置顶面的纹理图文件名,并载入
//-------------------------------------------------------------------------------
bool CSkyBox::LoadTexture(ESkyBoxSides eSide, const char *szFilename)
{
	int index = (int)eSide;
	strncpy(m_tTextures[index].name, szFilename, kMaxTextureNameChars);
	m_tTextures[index].name[kMaxTextureNameChars - 1] = 0;
	return g_TextureManager.LoadTexture(&m_tTextures[index]);
}

//-------------------------------------------------------------------------------
// 渲染天空盒子
//-------------------------------------------------------------------------------
void CSkyBox::Render()
{
	float x = m_vPos.x;
	float y = m_vPos.y;
	float z = m_vPos.z;
	float width  = m_vSize.x;
	float height = m_vSize.y;
	float length = m_vSize.z;

	// This centers the sky box around (x, y, z)
	x = x - width  / 2;
	y = y - height / 2;
	z = z - length / 2;

	// 打开纹理映射
	glEnable(GL_TEXTURE_2D);

	// 后面
	glBindTexture(GL_TEXTURE_2D, m_tTextures[ESIDE_BACK].handle);
	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(x,			y,			z);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(x,			y,	z + length); 
	glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width,	y,	z + length);
	glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width,	y,			z);

	glEnd();

	// 前面
	glBindTexture(GL_TEXTURE_2D, m_tTextures[ESIDE_FRONT].handle);
	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(x + width,	y + height,			z);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width,	y + height, z + length);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(x,			y + height, z + length); 
	glTexCoord2f(0.0f, 0.0f); glVertex3f(x,			y + height,			z);
	glEnd();

	// Bind the BOTTOM texture of the sky map to the BOTTOM side of the box
	glBindTexture(GL_TEXTURE_2D, m_tTextures[ESIDE_BOTTOM].handle);
	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);		

	// 下面
	glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width,	y,			z);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width,	y + height,	z);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(x,			y + height,	z); 
	glTexCoord2f(0.0f, 0.0f); glVertex3f(x,			y,			z);
	glEnd();

	// 上面
	glBindTexture(GL_TEXTURE_2D, m_tTextures[ESIDE_TOP].handle);
	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(x,			y,			z + length);
	glTexCoord2f(0.0f, 0.0f); glVertex3f(x,			y + height, z + length); 
	glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width,	y + height,	z + length);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width,	y,			z + length);

	glEnd();

	// 左面
	glBindTexture(GL_TEXTURE_2D, m_tTextures[ESIDE_LEFT].handle);
	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(x,			y,					z);	
	glTexCoord2f(0.0f, 1.0f); glVertex3f(x,			y + height,			z); 
	glTexCoord2f(0.0f, 0.0f); glVertex3f(x,			y + height,	z + length);
	glTexCoord2f(1.0f, 0.0f); glVertex3f(x,			y,			z + length);		

	glEnd();

	// 右面
	glBindTexture(GL_TEXTURE_2D, m_tTextures[ESIDE_RIGHT].handle);
	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(x + width, y,			z + length);
	glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y + height,	z + length);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height,			z); 
	glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y,					z);
	glEnd();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -