#include "ObjManualMeshLoader.h" #include "Triangle3.h" #include "IntersectableWrapper.h" #include "BvHierarchy.h" #include "OgreLogManager.h" #define USE_HBL_DISCARD 1 ObjManualMeshLoader::ObjManualMeshLoader(GtpVisibilityPreprocessor::BvhLeaf *obj): mObject(obj) { } ObjManualMeshLoader::~ObjManualMeshLoader() { } void ObjManualMeshLoader::loadResource(Ogre::Resource *resource) { using namespace Ogre; Mesh *mesh = (Mesh *) resource; SubMesh* submesh = mesh->createSubMesh(); const int triCount = (int)mObject->mObjects.size(); const int vertexCount = triCount * 3; // We must create the vertex data, indicating how many vertices there will be submesh->useSharedVertices = false; submesh->vertexData = new VertexData(); submesh->vertexData->vertexStart = 0; submesh->vertexData->vertexCount = vertexCount; static const unsigned short source = 0; size_t offset = 0; // We must now declare what the vertex data contains VertexDeclaration* declaration = submesh->vertexData->vertexDeclaration; offset += declaration->addElement(source, offset, VET_FLOAT3, VES_POSITION).getSize(); offset += declaration->addElement(source, offset, VET_FLOAT3, VES_NORMAL).getSize(); //offset += declaration->addElement(source, offset, VET_FLOAT2,VES_TEXTURE_COORDINATES).getSize(); HardwareVertexBufferSharedPtr vbuffer = HardwareBufferManager::getSingleton(). createVertexBuffer(declaration->getVertexSize(source), submesh->vertexData->vertexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY); // we get access to the buffer to fill it. During so we record the bounding box. AxisAlignedBox aabox; submesh->vertexData->vertexBufferBinding->setBinding(source, vbuffer); #if USE_HBL_DISCARD float* vdata = static_cast(vbuffer->lock(HardwareBuffer::HBL_DISCARD)); #else float* vdata = static_cast(vbuffer->lock(HardwareBuffer::HBL_NORMAL)); #endif GtpVisibilityPreprocessor::ObjectContainer:: const_iterator oit, oit_end = mObject->mObjects.end(); for (oit = mObject->mObjects.begin(); oit != oit_end; ++ oit) { GtpVisibilityPreprocessor::TriangleIntersectable *tObj = static_cast(*oit); const GtpVisibilityPreprocessor::Triangle3 tri = tObj->GetItem(); const GtpVisibilityPreprocessor::Vector3 n = tri.GetNormal(); for (int i = 0; i < 3; ++ i) { Vector3 vtx(tri.mVertices[i].x, tri.mVertices[i].y, tri.mVertices[i].z); *(vdata ++) = vtx.x; *(vdata ++) = vtx.y; *(vdata ++) = vtx.z; *(vdata ++) = n.x; *(vdata ++) = n.y; *(vdata ++) = n.z; aabox.merge(vtx); } } vbuffer->unlock(); #if 0 RenderOperation rop; submesh->_getRenderOperation(rop); rop.useIndexes = false; #endif ////// //-- Creates the index data const int indexCount = vertexCount; submesh->indexData->indexStart = 0; submesh->operationType = RenderOperation::OT_TRIANGLE_LIST; // we use an index for every vertex submesh->indexData->indexCount = indexCount; submesh->indexData->indexBuffer = HardwareBufferManager::getSingleton(). createIndexBuffer(HardwareIndexBuffer::IT_16BIT, submesh->indexData->indexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY); #if USE_HBL_DISCARD uint16* idata = static_cast(submesh->indexData->indexBuffer->lock(HardwareBuffer::HBL_DISCARD)); #else uint16* idata = static_cast(submesh->indexData->indexBuffer->lock(HardwareBuffer::HBL_NORMAL)); #endif for (int j = 0; j < indexCount; ++ j) { idata[j] = j; } submesh->indexData->indexBuffer->unlock(); submesh->setMaterialName("BaseWhite"); // indicate the bounding box mesh->_setBounds(aabox); mesh->_setBoundingSphereRadius((aabox.getMaximum() - aabox.getMinimum()).length() / 2.0); }