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

📄 texture.cpp

📁 Torus3D.rar,BREW平台的3D游戏引擎的一个实例.喜欢的朋友可以下载
💻 CPP
字号:
// ==========================================================================================================
//
// BREW v2.0+ OPENGLES MICROENGINE
//
// ----------------------------------------
//
// Written by Vander Nunes
//
// ==========================================================================================================

#include "texture.h"


// -------------------------------------------------------------------
//
//
//
// -------------------------------------------------------------------
CTexture::CTexture()
{
	m_wWidth = m_wHeight = m_jDepth = 0;
}


// -------------------------------------------------------------------
//
//
//
// -------------------------------------------------------------------
CTexture::~CTexture()
{
}


// -------------------------------------------------------------------
//
// Load tga texture from file and upload to the card
//
// -------------------------------------------------------------------
boolean CTexture::Load(AEEApplet* pApplet, char *szPakFile, char *szFile, boolean bGenMipMap)
{
	CVfs Vfs;
	if (!Vfs.Unpack(szPakFile, szFile, pApplet))
		return FALSE;

	CTGA Tga;
	boolean Ok = Tga.LoadFromVfs(&Vfs);
	Vfs.Finish();
	if (!Ok) return FALSE;

	LoadFromMemory(&Tga, bGenMipMap);

	// release texture from main memory
	Tga.Reset();

	return TRUE;
}


// -------------------------------------------------------------------
//
// Upload tga texture to the card
//
// -------------------------------------------------------------------
boolean CTexture::LoadFromMemory(CTGA* pTga, boolean bGenMipMap)
{
	// set texture parameters
	glGenTextures(1, &m_iTexID);
	glBindTexture(GL_TEXTURE_2D, m_iTexID);
	glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	if (bGenMipMap)
	{
		glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
		glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	}
	else
	{
		glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	}

	glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT /*GL_CLAMP_TO_EDGE*/);
	glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT /*GL_CLAMP_TO_EDGE*/);

	char jLevel = 0;

	m_wWidth = pTga->Width();
	m_wHeight = pTga->Height();
	m_jDepth = pTga->Depth();

	do
	{
		// upload texture to video card
		switch(pTga->Depth())
		{
			case 8:
			{
				dword dwSize = pTga->Width()*pTga->Height();

				if (bGenMipMap)
				{
					word wWidth = m_wWidth;
					word wHeight = m_wHeight;
					while (wWidth > 2 && wHeight > 2)
					{
						jLevel++;
						wWidth >>= 1;
						wHeight >>= 1;
						dwSize += (wWidth * wHeight);
					}
				}

				// create a temporary palette+pixels buffer
				byte* pBuf = new byte[dwSize+768];

				if (pBuf)
				{
					API_MEMCPY(pBuf, pTga->Palette(), 768);

					dword dwOffset = 768;
					API_MEMCPY(&pBuf[dwOffset], pTga->Pixels(), pTga->Width()*pTga->Height());

					if (bGenMipMap)
					{
						do
						{
							dwOffset += (pTga->Width()*pTga->Height());
							pTga->HalfSize();
							API_MEMCPY(&pBuf[dwOffset], pTga->Pixels(), pTga->Width()*pTga->Height());
						} while (pTga->Width() > 2 && pTga->Height() > 2);
					}

					glCompressedTexImage2D(GL_TEXTURE_2D, -jLevel, GL_PALETTE8_RGB8_OES, m_wWidth, m_wHeight, 0, dwSize+768, pBuf);

					// release temporary buffer
					DeletePtrArray(pBuf);
				}
				break;
			}

			case 24:
			{
				glTexImage2D(GL_TEXTURE_2D, jLevel, GL_RGB, pTga->Width(), pTga->Height(), 0, GL_RGB, GL_UNSIGNED_BYTE, pTga->Pixels());
				break;
			}

			case 32:
			{
				glTexImage2D(GL_TEXTURE_2D, jLevel, GL_RGBA, pTga->Width(), pTga->Height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, pTga->Pixels());
				break;
			}
		}

		if (bGenMipMap && pTga->Depth() > 8 && pTga->Width() > 2 && pTga->Height() > 2)
		{
			jLevel++;
			pTga->HalfSize();
		}
		else break;

	} while (bGenMipMap);

	return TRUE;
}


// -------------------------------------------------------------------
//
//
//
// -------------------------------------------------------------------
unsigned int CTexture::TexID()
{
	return m_iTexID;
}


// -------------------------------------------------------------------
//
//
//
// -------------------------------------------------------------------
word CTexture::Width()
{
	return m_wWidth;
}


// -------------------------------------------------------------------
//
//
//
// -------------------------------------------------------------------
word CTexture::Height()
{
	return m_wHeight;
}


// -------------------------------------------------------------------
//
//
//
// -------------------------------------------------------------------
byte CTexture::Depth()
{
	return m_jDepth;
}

⌨️ 快捷键说明

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