[183] | 1 | #include "ivmanualmeshloader.h"
|
---|
| 2 | #include "IVReader.h"
|
---|
| 3 |
|
---|
| 4 | IVManualMeshLoader::IVManualMeshLoader()
|
---|
| 5 | {
|
---|
| 6 | data = NULL;
|
---|
| 7 | }
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | IVManualMeshLoader::IVManualMeshLoader(IVMeshData *mData)
|
---|
| 11 | {
|
---|
| 12 | data = mData;
|
---|
| 13 | /* data = new IVMeshData();
|
---|
| 14 | if ((mData->indices != NULL) && (mData->icnt > 0))
|
---|
| 15 | {
|
---|
| 16 | data->icnt = mData->icnt;
|
---|
| 17 | data->indices = new Ogre::Real[mData->icnt];
|
---|
| 18 | memcpy(data->indices, mData->indices, mData->icnt*sizeof(Ogre::Real));
|
---|
| 19 | }
|
---|
| 20 | if ((mData->vertices != NULL) && (mData->vcnt > 0))
|
---|
| 21 | {
|
---|
| 22 | data->vcnt = mData->vcnt;
|
---|
| 23 | data->vertices = new Ogre::Real[mData->vcnt];
|
---|
| 24 | memcpy(data->vertices, mData->vertices, mData->vcnt*sizeof(Ogre::Real));
|
---|
| 25 | }
|
---|
| 26 | if ((mData->normals != NULL) && (mData->vcnt > 0))
|
---|
| 27 | {
|
---|
| 28 | data->ncnt = mData->ncnt;
|
---|
| 29 | data->normals = new Ogre::Real[mData->ncnt];
|
---|
| 30 | memcpy(data->normals, mData->normals, mData->ncnt*sizeof(Ogre::Real));
|
---|
| 31 | }
|
---|
| 32 | if ((mData->texCoords != NULL) && (mData->tcnt > 0))
|
---|
| 33 | {
|
---|
| 34 | data->tcnt = mData->tcnt;
|
---|
| 35 | data->texCoords = new Ogre::Real[mData->tcnt];
|
---|
| 36 | memcpy(data->texCoords, mData->texCoords, mData->tcnt*sizeof(Ogre::Real));
|
---|
| 37 | }
|
---|
| 38 | */
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | IVManualMeshLoader::~IVManualMeshLoader()
|
---|
| 42 | {
|
---|
| 43 | /* if (data != NULL)
|
---|
| 44 | {
|
---|
| 45 | if (data->vertices != NULL) delete [] data->vertices;
|
---|
| 46 | if (data->normals != NULL) delete [] data->normals;
|
---|
| 47 | if (data->texCoords != NULL) delete [] data->texCoords;
|
---|
| 48 | if (data->indices != NULL) delete [] data->indices;
|
---|
| 49 | delete data;
|
---|
| 50 | }
|
---|
| 51 | */
|
---|
| 52 | if (data != NULL)
|
---|
| 53 | {
|
---|
| 54 | data->collapse();
|
---|
| 55 | delete data;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 | Ogre::AxisAlignedBox *IVManualMeshLoader::getBoundingBox()
|
---|
| 61 | {
|
---|
| 62 | return data->boundingBox;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void IVManualMeshLoader::loadResource(Ogre::Resource *resource)
|
---|
| 66 | {
|
---|
| 67 | using namespace Ogre;
|
---|
| 68 | Mesh *pMesh = (Mesh *) resource;
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | Vector3 translation = data->boundingBox->getCenter();
|
---|
| 72 |
|
---|
| 73 | bool nOk = ((data->normals != NULL) && (data->ncnt > 0));
|
---|
| 74 | bool tOk = ((data->texCoords != NULL) && (data->tcnt > 0));
|
---|
| 75 |
|
---|
| 76 | pMesh->sharedVertexData = new VertexData();
|
---|
| 77 |
|
---|
| 78 | VertexDeclaration* vertexDecl = pMesh->sharedVertexData->vertexDeclaration;
|
---|
| 79 |
|
---|
| 80 | size_t currOffset = 0;
|
---|
| 81 | vertexDecl->addElement(0, currOffset, VET_FLOAT3, VES_POSITION);
|
---|
| 82 | currOffset += VertexElement::getTypeSize(VET_FLOAT3);
|
---|
| 83 | if (nOk)
|
---|
| 84 | {
|
---|
| 85 | vertexDecl->addElement(0, currOffset, VET_FLOAT3, VES_NORMAL);
|
---|
| 86 | currOffset += VertexElement::getTypeSize(VET_FLOAT3);
|
---|
| 87 | }
|
---|
| 88 | if (tOk)
|
---|
| 89 | {
|
---|
| 90 | vertexDecl->addElement(0, currOffset, VET_FLOAT2, VES_TEXTURE_COORDINATES);
|
---|
| 91 | currOffset += VertexElement::getTypeSize(VET_FLOAT2);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | pMesh->sharedVertexData->vertexCount = data->vcnt/3;
|
---|
| 95 | HardwareVertexBufferSharedPtr vbuf =
|
---|
| 96 | HardwareBufferManager::getSingleton().
|
---|
| 97 | createVertexBuffer(vertexDecl->getVertexSize(0), pMesh->sharedVertexData->vertexCount,
|
---|
| 98 | HardwareBuffer::HBU_STATIC_WRITE_ONLY, false);
|
---|
| 99 |
|
---|
| 100 | VertexBufferBinding* binding = pMesh->sharedVertexData->vertexBufferBinding;
|
---|
| 101 | binding->setBinding(0, vbuf);
|
---|
| 102 |
|
---|
| 103 | Real* pVertices = static_cast<Real*>(vbuf->lock(HardwareBuffer::HBL_DISCARD));
|
---|
| 104 |
|
---|
| 105 | int offset = 0, size = 3;
|
---|
| 106 | if (nOk) size += 3;
|
---|
| 107 | if (tOk) size += 2;
|
---|
| 108 | for (int i=0; i<data->vcnt/3; i++)
|
---|
| 109 | {
|
---|
| 110 | pVertices[i*size] = data->vertices[i*3] - translation.x;
|
---|
| 111 | pVertices[i*size+1] = data->vertices[i*3+1] - translation.y;
|
---|
| 112 | pVertices[i*size+2] = data->vertices[i*3+2] - translation.z;
|
---|
| 113 |
|
---|
| 114 | offset = 3;
|
---|
| 115 | if (nOk)
|
---|
| 116 | {
|
---|
| 117 | pVertices[i*size+offset] = data->normals[i*3]; offset++;
|
---|
| 118 | pVertices[i*size+offset] = data->normals[i*3+1]; offset++;
|
---|
| 119 | pVertices[i*size+offset] = data->normals[i*3+2]; offset++;
|
---|
| 120 | }
|
---|
| 121 | if (tOk)
|
---|
| 122 | {
|
---|
| 123 | pVertices[i*size+offset] = data->texCoords[i*2]; offset++;
|
---|
| 124 | pVertices[i*size+offset] = -data->texCoords[i*2+1]; offset++;
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | vbuf->unlock();
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 | SubMesh *pSub = pMesh->createSubMesh();
|
---|
| 132 | pSub->operationType = RenderOperation::OT_TRIANGLE_LIST; //;data->roType;
|
---|
| 133 | pSub->useSharedVertices = true;
|
---|
| 134 |
|
---|
| 135 | pSub->indexData->indexCount = data->icnt;
|
---|
| 136 | pSub->indexData->indexBuffer = HardwareBufferManager::getSingleton().
|
---|
| 137 | createIndexBuffer(HardwareIndexBuffer::IT_16BIT,
|
---|
| 138 | pSub->indexData->indexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY, false);
|
---|
| 139 |
|
---|
| 140 | HardwareIndexBufferSharedPtr ibuf = pSub->indexData->indexBuffer;
|
---|
| 141 | unsigned short* pIndices = static_cast<unsigned short*>(ibuf->lock(HardwareBuffer::HBL_DISCARD));
|
---|
| 142 |
|
---|
| 143 | for (i = 0; i < data->icnt; i++) pIndices[i] = data->indices[i];
|
---|
| 144 |
|
---|
| 145 | ibuf->unlock();
|
---|
| 146 |
|
---|
| 147 | AxisAlignedBox bb(data->boundingBox->getMinimum() - translation, data->boundingBox->getMaximum() - translation);
|
---|
| 148 | pMesh->_setBounds(bb);
|
---|
| 149 | Real radius = data->boundingBox->getMinimum().length();
|
---|
| 150 | if (data->boundingBox->getMaximum().length() > radius) radius = data->boundingBox->getMaximum().length();
|
---|
| 151 | pMesh->_setBoundingSphereRadius(radius);
|
---|
| 152 |
|
---|
| 153 | }
|
---|
| 154 |
|
---|