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

📄 scenegraph.cpp

📁 Torus 3D Engine is an open-source OpenGL ES 3D Engine for creating your own games in BREW environmen
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ==========================================================================================================
//
// BREW v2.0+ OPENGLES MICROENGINE
//
// ----------------------------------------
//
// Written by Vander Nunes
//
// ==========================================================================================================

#include "scenegraph.h"


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
CSceneItem::CSceneItem()
{
	VectorSet(m_Position, 0,0,0);
	VectorSet(m_Rotation, 0,0,0);
	VectorSet(m_Direction, 0,0,ITOX(-1));

	m_iTextureIndex = -1;
	m_iEnvMapIndex = -1;
	m_jEnvMapMode = ENVMAPMODE_ADD;
	m_dwBSphere = 0;

	m_pParent = NULL;
	m_pPrevious = NULL;
	m_pNext = NULL;
	m_pChild = NULL;
	m_iModelIndex = -1;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
CSceneItem::CSceneItem(CSceneGraph* pScene, short iModelIndex, short iTextureIndex, short iEnvMapIndex, BYTE jEnvMapMode)
{
	VectorSet(m_Position, 0,0,0);
	VectorSet(m_Rotation, 0,0,0);
	VectorSet(m_Direction, 0,0,ITOX(-1));

	m_pParent = NULL;
	m_pPrevious = NULL;
	m_pNext = NULL;
	m_pChild = NULL;
	m_dwBSphere = 0;

	m_iModelIndex = iModelIndex;
	m_iTextureIndex = iTextureIndex;
	m_iEnvMapIndex = iEnvMapIndex;
	m_jEnvMapMode = jEnvMapMode;

	VectorCopy(pScene->Model(iModelIndex)->BBMin(), m_BBMin);
	VectorCopy(pScene->Model(iModelIndex)->BBMax(), m_BBMax);
}


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


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CSceneItem::TranslateTo(int Px, int Py, int Pz)
{
	VectorSet(m_Position, Px,Py,Pz);
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CSceneItem::AddTranslation(int Px, int Py, int Pz)
{
	vec3_t v = { Px, Py, Pz };
	VectorAdd(m_Position, v, m_Position);
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CSceneItem::RotateTo(int Rx, int Ry, int Rz)
{
	VectorSet(m_Rotation, Rx,Ry,Rz);
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CSceneItem::AddRotation(int Rx, int Ry, int Rz)
{
	vec3_t v = { Rx, Ry, Rz };
	VectorAdd(m_Rotation, v, m_Rotation);

	while (m_Rotation[0] >= ITOX(360)) m_Rotation[0] -= ITOX(360);
	while (m_Rotation[0] < 0) m_Rotation[0] += ITOX(360);

	while (m_Rotation[1] >= ITOX(360)) m_Rotation[1] -= ITOX(360);
	while (m_Rotation[1] < 0) m_Rotation[1] += ITOX(360);

	while (m_Rotation[2] >= ITOX(360)) m_Rotation[2] -= ITOX(360);
	while (m_Rotation[2] < 0) m_Rotation[2] += ITOX(360);
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CSceneItem::UpdateBBox()
{
	if (!m_iModelIndex) return;

	matrix_t m = RXMatrix(m_Rotation[0]);
	m = MatrixMultiply(m, RYMatrix(m_Rotation[1]));
	m = MatrixMultiply(m, RZMatrix(m_Rotation[2]));

	MatrixMultiplyVector(m, m_BBMin, m_OBBMin);
	MatrixMultiplyVector(m, m_BBMax, m_OBBMin);
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CSceneItem::Render(CSceneGraph* pScene)
{
	// ---------------------------------------------------------------------------------
	// render model
	// ---------------------------------------------------------------------------------

	CMobModel* pModel = (m_iModelIndex > -1 ? pScene->Model(m_iModelIndex) : NULL);
	CTexture* pTexture = (m_iTextureIndex > -1 ? pScene->Texture(m_iTextureIndex) : NULL);

	//
	// switch states
	//

	/*
		if (pModel->m_bZWrite)
			glEnable(GL_DEPTH_TEST);
		else
			glDisable(GL_DEPTH_TEST);

		if (pModel->m_bBackCull)
			glEnable(GL_CULL_FACE);
		else
			glDisable(GL_CULL_FACE);
	*/

	//
	// render
	//

	//glEnableClientState(GL_COLOR_ARRAY);
	//glColorPointer(4, GL_FIXED, 0, pModel->Colors());

	glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(3, GL_FIXED, 0, pModel->Vertices());

	#if INC_NORMALS
		glEnableClientState(GL_NORMAL_ARRAY);
		glNormalPointer(GL_FIXED, 0, pModel->Normals());
	#endif

	if (pTexture)
	{
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
		glTexCoordPointer(2, GL_FIXED, 0, pModel->TexCoords());
		glBindTexture(GL_TEXTURE_2D, pTexture->TexID());

		#if 0

			if (pTexture->Depth() == 32)
			{
				glEnable(GL_BLEND);
				glBlendFunc(GL_SRC_ALPHA,GL_ONE);
			}
			else
				glDisable(GL_BLEND);

		#endif
	}
	else
	{
		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
		glBindTexture(GL_TEXTURE_2D, 0);
	}

	if (m_jEnvMapMode != ENVMAPMODE_REPLACE)
	{
		#if 1
			glDrawElements(GL_TRIANGLES, pModel->FaceCount()*3, GL_UNSIGNED_SHORT, pModel->Faces());
		#else
			glDisable(GL_TEXTURE_2D);
			glDrawElements(GL_LINES, pModel->FaceCount()*3, GL_UNSIGNED_SHORT, pModel->Faces());
		#endif
	}

	#if INC_NORMALS
		#if INC_ENVMAP
			//
			// environment mapping (software mode)
			//

			if (m_iEnvMapIndex > -1 && m_jEnvMapMode != ENVMAPMODE_NONE)
			{
				if (m_jEnvMapMode != ENVMAPMODE_REPLACE) glEnable(GL_BLEND);

				switch (m_jEnvMapMode)
				{
					case ENVMAPMODE_ADD:
						glBlendFunc(GL_ONE,GL_ONE);
						break;

					case ENVMAPMODE_MULTIPLY:
						glBlendFunc(GL_ZERO,GL_SRC_COLOR);
						break;
				}

				dword x;
				matrix_t m = RYMatrix(-m_Rotation[1]);
				m = MatrixMultiply(m, RXMatrix(m_Rotation[0]));
				m = MatrixMultiply(m, RZMatrix(m_Rotation[2]));

				// rotate all model normals
				for (x=0; x<pModel->VertexCount(); x++)
				{
					// rotate vertex normal
					dword dwNIdx = x * 3;

					vec3_t n1 =
					{
						pModel->Normals()[dwNIdx + 0],
						pModel->Normals()[dwNIdx + 1],
						pModel->Normals()[dwNIdx + 2]
					};

					vec3_t n2;

					MatrixMultiplyVector(m, n1, n2);

					pModel->EnvNormals()[dwNIdx + 0] = n2[0];
					pModel->EnvNormals()[dwNIdx + 1] = n2[1];
					pModel->EnvNormals()[dwNIdx + 2] = n2[2];

					// calculate uv mapping coordinate
					dword dwIdx = x << 1;
					pModel->EnvTexCoords()[dwIdx + 0] = 32768 + (n2[0] >> 1);
					pModel->EnvTexCoords()[dwIdx + 1] = 32768 + (n2[1] >> 1);
				}

				glTexCoordPointer(2, GL_FIXED, 0, pModel->EnvTexCoords());
				glBindTexture(GL_TEXTURE_2D, pScene->Texture(m_iEnvMapIndex)->TexID());
				glNormalPointer(GL_FIXED, 0, pModel->EnvNormals());

				glDrawElements(GL_TRIANGLES, pModel->FaceCount()*3, GL_UNSIGNED_SHORT, pModel->Faces());

				glDisable(GL_BLEND);
			}
		#endif
	#endif
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CSceneItem::UpdateDirection(void)
{
	// Update model rotation matrix
	#define DIR_SIZE ITOX(1)

	matrix_t m;
	m = RXMatrix(m_Rotation[0]);
	m = MatrixMultiply(m, RYMatrix(m_Rotation[1]));

	vec3_t v = { 0, 0, DIR_SIZE };
	MatrixMultiplyVector(m, v, m_Direction);

	UpdateBBox();
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
int* CSceneItem::Position()
{
	return m_Position;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
int* CSceneItem::Rotation()
{
	return m_Rotation;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
int* CSceneItem::Direction()
{
	return m_Direction;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CSceneItem::SetNode(boolean bIsNode)
{
	m_bIsNode = bIsNode;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
boolean CSceneItem::IsNode()
{
	return m_bIsNode;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CSceneItem::SetNext(CSceneItem* pNext)
{
	m_pNext = pNext;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
CSceneItem* CSceneItem::Next()
{
	return m_pNext;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CSceneItem::SetPrevious(CSceneItem* pPrevious)
{
	m_pPrevious = pPrevious;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
CSceneItem* CSceneItem::Previous()
{
	return m_pPrevious;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CSceneItem::SetParent(CSceneItem* pParent)
{
	m_pParent = pParent;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
CSceneItem* CSceneItem::Parent()
{
	return m_pParent;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CSceneItem::SetChild(CSceneItem* pChild)
{
	m_pChild = pChild;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
CSceneItem* CSceneItem::Child()
{
	return m_pChild;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
short CSceneItem::ModelIndex()
{
	return m_iModelIndex;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
short CSceneItem::TextureIndex()
{
	return m_iTextureIndex;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------

⌨️ 快捷键说明

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