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

📄 renderer.cpp

📁 Cal3D实现虚拟角色 Cal3D实现虚拟角色
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//****************************************************************************//// renderer.cpp                                                               //// Copyright (C) 2001, 2002 Bruno 'Beosil' Heidelberger                       ////****************************************************************************//// This library is free software; you can redistribute it and/or modify it    //// under the terms of the GNU Lesser General Public License as published by   //// the Free Software Foundation; either version 2.1 of the License, or (at    //// your option) any later version.                                            ////****************************************************************************//#ifdef HAVE_CONFIG_H#include "config.h"#endif//****************************************************************************//// Includes                                                                   ////****************************************************************************//#include "cal3d/error.h"#include "cal3d/renderer.h"#include "cal3d/coremodel.h"#include "cal3d/model.h"#include "cal3d/coremesh.h"#include "cal3d/mesh.h"#include "cal3d/submesh.h"#include "cal3d/skeleton.h"#include "cal3d/bone.h"#include "cal3d/corematerial.h"#include "cal3d/coresubmesh.h"#include "cal3d/physique.h" /*****************************************************************************//** Constructs the renderer instance.  *  * This function is the default constructor of the renderer instance.  *****************************************************************************/CalRenderer::CalRenderer(CalModel* pModel)  : m_pSelectedSubmesh(0){  assert(pModel);  m_pModel = pModel;} /*****************************************************************************//** Copy-constructor for the renderer instance.  *  * This function is the copy constructor of the renderer instance.  * This is useful for multi-pipe parallel rendering.  *****************************************************************************/CalRenderer::CalRenderer(CalRenderer* pRenderer){  m_pModel = pRenderer->m_pModel ;  m_pSelectedSubmesh = pRenderer->m_pSelectedSubmesh ;} /*****************************************************************************//** Initializes the rendering query phase.  *  * This function initializes the rendering query phase. It must be called  * before any rendering queries are executed.  *****************************************************************************/bool CalRenderer::beginRendering(){  // get the attached meshes vector  std::vector<CalMesh *>& vectorMesh = m_pModel->getVectorMesh();  // check if there are any meshes attached to the model  if(vectorMesh.size() == 0)  {    CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);    return false;  }  // select the default submesh  m_pSelectedSubmesh = vectorMesh[0]->getSubmesh(0);  if(m_pSelectedSubmesh == 0) return false;  return true;} /*****************************************************************************//** Finishes the rendering query phase.  *  * This function finishes the rendering query phase. It must be called  * after all rendering queries have been executed.  *****************************************************************************/void CalRenderer::endRendering(){  // clear selected submesh  m_pSelectedSubmesh = 0;} /*****************************************************************************//** Provides access to the ambient color.  *  * This function returns the ambient color of the material of the selected  * mesh/submesh.  *  * @param pColorBuffer A pointer to the user-provided buffer where the color  *                     data is written to.  *****************************************************************************/void CalRenderer::getAmbientColor(unsigned char *pColorBuffer){  // get the core material  CalCoreMaterial *pCoreMaterial;  pCoreMaterial = m_pModel->getCoreModel()->getCoreMaterial(m_pSelectedSubmesh->getCoreMaterialId());  if(pCoreMaterial == 0)  {    // write default values to the color buffer    pColorBuffer[0] = 0;    pColorBuffer[1] = 0;    pColorBuffer[2] = 0;    pColorBuffer[3] = 0;    return;  }  // get the ambient color of the material  CalCoreMaterial::Color& color = pCoreMaterial->getAmbientColor();  // write it to the color buffer  pColorBuffer[0] = color.red;  pColorBuffer[1] = color.green;  pColorBuffer[2] = color.blue;  pColorBuffer[3] = color.alpha;} /*****************************************************************************//** Provides access to the diffuse color.  *  * This function returns the diffuse color of the material of the selected  * mesh/submesh.  *  * @param pColorBuffer A pointer to the user-provided buffer where the color  *                     data is written to.  *****************************************************************************/void CalRenderer::getDiffuseColor(unsigned char *pColorBuffer){  // get the core material  CalCoreMaterial *pCoreMaterial;  pCoreMaterial = m_pModel->getCoreModel()->getCoreMaterial(m_pSelectedSubmesh->getCoreMaterialId());  if(pCoreMaterial == 0)  {    // write default values to the color buffer    pColorBuffer[0] = 192;    pColorBuffer[1] = 192;    pColorBuffer[2] = 192;    pColorBuffer[3] = 192;    return;  }  // get the diffuse color of the material  CalCoreMaterial::Color& color = pCoreMaterial->getDiffuseColor();  // write it to the color buffer  pColorBuffer[0] = color.red;  pColorBuffer[1] = color.green;  pColorBuffer[2] = color.blue;  pColorBuffer[3] = color.alpha;} /*****************************************************************************//** Returns the number of faces.  *  * This function returns the number of faces in the selected mesh/submesh.  *  * @return The number of faces.  *****************************************************************************/int CalRenderer::getFaceCount(){  return m_pSelectedSubmesh->getFaceCount();} /*****************************************************************************//** Provides access to the face data.  *  * This function returns the face data (vertex indices) of the selected  * mesh/submesh. The LOD setting is taken into account.  *  * @param pFaceBuffer A pointer to the user-provided buffer where the face  *                    data is written to.  *  * @return The number of faces written to the buffer.  *****************************************************************************/int CalRenderer::getFaces(CalIndex *pFaceBuffer){  return m_pSelectedSubmesh->getFaces(pFaceBuffer);} /*****************************************************************************//** Returns the number of maps.  *  * This function returns the number of maps in the selected mesh/submesh.  *  * @return The number of maps.  *****************************************************************************/int CalRenderer::getMapCount(){  // get the core material  CalCoreMaterial *pCoreMaterial;  pCoreMaterial = m_pModel->getCoreModel()->getCoreMaterial(m_pSelectedSubmesh->getCoreMaterialId());  if(pCoreMaterial == 0) return 0;  return pCoreMaterial->getMapCount();} /*****************************************************************************//** Provides access to a specified map user data.  *  * This function returns the user data stored in the specified map of the  * material of the selected mesh/submesh.  *  * @param mapId The ID of the map.  *  * @return One of the following values:  *         \li the user data stored in the specified map  *         \li \b 0 if an error happend  *****************************************************************************/Cal::UserData CalRenderer::getMapUserData(int mapId){  // get the core material  CalCoreMaterial *pCoreMaterial;  pCoreMaterial = m_pModel->getCoreModel()->getCoreMaterial(m_pSelectedSubmesh->getCoreMaterialId());  if(pCoreMaterial == 0) return 0;  // get the map vector  std::vector<CalCoreMaterial::Map>& vectorMap = pCoreMaterial->getVectorMap();  // check if the map id is valid  if((mapId < 0) || (mapId >= (int)vectorMap.size()))  {    CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);    return 0;  }  return vectorMap[mapId].userData;} /*****************************************************************************//** Returns the number of attached meshes.  *  * This function returns the number of meshes attached to the renderer  * instance.  *  * @return The number of attached meshes.  *****************************************************************************/int CalRenderer::getMeshCount(){  // get the attached meshes vector  std::vector<CalMesh *>& vectorMesh = m_pModel->getVectorMesh();  return vectorMesh.size();} /*****************************************************************************//** Provides access to the tangent space data.  *  * This function returns the tangent space data of the selected mesh/submesh.  *  * @param mapID  *  * @param pTangentSpaceBuffer A pointer to the user-provided buffer where the normal  *                      data is written to.  *  * @return The number of tangent space written to the buffer.  *****************************************************************************/int CalRenderer::getTangentSpaces(int mapId, float *pTangentSpaceBuffer, int stride){  // get the texture coordinate vector vector  std::vector<std::vector<CalCoreSubmesh::TangentSpace> >& vectorvectorTangentSpace = m_pSelectedSubmesh->getCoreSubmesh()->getVectorVectorTangentSpace();    // check if the map id is valid  if((mapId < 0) || (mapId >= (int)vectorvectorTangentSpace.size()) || !m_pSelectedSubmesh->isTangentsEnabled(mapId))  {        CalError::setLastError(CalError::INVALID_HANDLE, __FILE__, __LINE__);    return -1;  }  // check if the submesh handles vertex data internally  if(m_pSelectedSubmesh->hasInternalData())  {    // get the normal vector of the submesh    std::vector<CalSubmesh::TangentSpace>& vectorTangentSpace = m_pSelectedSubmesh->getVectorVectorTangentSpace()[mapId];    // get the number of normals (= number of vertices) in the submesh    int tangentSpaceCount;    tangentSpaceCount = m_pSelectedSubmesh->getVertexCount();    // copy the internal normal data to the provided normal buffer    	if(stride == sizeof(CalCoreSubmesh::TangentSpace) || stride <= 0)	{		memcpy(pTangentSpaceBuffer, &vectorTangentSpace[0], tangentSpaceCount * sizeof(CalCoreSubmesh::TangentSpace));		}	else	{		char * pBuffer = (char*) pTangentSpaceBuffer;		for(int i=0; i < tangentSpaceCount; ++i)		{			memcpy(&pBuffer[0], &vectorTangentSpace[i], sizeof(CalCoreSubmesh::TangentSpace));			pBuffer+=stride;		}	}    return tangentSpaceCount;  }  // submesh does not handle the vertex data internally, so let the physique calculate it now  return m_pModel->getPhysique()->calculateTangentSpaces(m_pSelectedSubmesh, mapId, pTangentSpaceBuffer, stride);} /*****************************************************************************//** Provides access to the normal data.  *  * This function returns the normal data of the selected mesh/submesh.  *  * @param pNormalBuffer A pointer to the user-provided buffer where the normal  *                      data is written to.  *  * @return The number of normals written to the buffer.  *****************************************************************************/int CalRenderer::getNormals(float *pNormalBuffer, int stride){  // check if the submesh handles vertex data internally  if(m_pSelectedSubmesh->hasInternalData())  {    // get the normal vector of the submesh    std::vector<CalVector>& vectorNormal = m_pSelectedSubmesh->getVectorNormal();    // get the number of normals (= number of vertices) in the submesh    int normalCount;    normalCount = m_pSelectedSubmesh->getVertexCount();    // copy the internal normal data to the provided normal buffer	if(stride == sizeof(CalVector) || stride <= 0)	{		memcpy(pNormalBuffer, &vectorNormal[0], normalCount * sizeof(CalVector));	}	else	{		char * pBuffer = (char*) pNormalBuffer;		for(int i=0; i < normalCount; ++i)		{			memcpy(&pBuffer[0], &vectorNormal[i], sizeof(CalVector));			pBuffer+=stride;		}	}    return normalCount;  }  // submesh does not handle the vertex data internally, so let the physique calculate it now  return m_pModel->getPhysique()->calculateNormals(m_pSelectedSubmesh, pNormalBuffer, stride);} /*****************************************************************************//** Returns the shininess factor.  *  * This function returns the shininess factor of the material of the selected  * mesh/submesh..  *  * @return The shininess factor.

⌨️ 快捷键说明

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