source: GTP/trunk/Lib/Vis/OnlineCullingCHC/IVReader/src/ivmanualmeshloader.cpp @ 1139

Revision 1139, 4.7 KB checked in by mattausch, 18 years ago (diff)
Line 
1#include "ivmanualmeshloader.h"
2#include "ivreader.h"
3
4IVManualMeshLoader::IVManualMeshLoader()
5{
6        data = NULL;
7}
8
9
10IVManualMeshLoader::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
41IVManualMeshLoader::~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
60Ogre::AxisAlignedBox *IVManualMeshLoader::getBoundingBox()
61{
62        return data->boundingBox;
63}
64
65
66void IVManualMeshLoader::loadResource(Ogre::Resource *resource)
67{
68        using namespace Ogre;
69        Mesh *pMesh = (Mesh *) resource;
70
71
72        Vector3 translation = data->boundingBox->getCenter();
73
74        bool nOk = ((data->normals != NULL) && (data->ncnt > 0));
75        bool tOk = ((data->texCoords != NULL) && (data->tcnt > 0));
76       
77        pMesh->sharedVertexData = new VertexData();
78
79        VertexDeclaration* vertexDecl = pMesh->sharedVertexData->vertexDeclaration;
80
81        size_t currOffset = 0;
82        vertexDecl->addElement(0, currOffset, VET_FLOAT3, VES_POSITION);
83        currOffset += VertexElement::getTypeSize(VET_FLOAT3);
84        if (nOk)
85        {
86                vertexDecl->addElement(0, currOffset, VET_FLOAT3, VES_NORMAL);
87                currOffset += VertexElement::getTypeSize(VET_FLOAT3);
88        }
89    if (tOk)
90        {
91                vertexDecl->addElement(0, currOffset, VET_FLOAT2, VES_TEXTURE_COORDINATES);
92                currOffset += VertexElement::getTypeSize(VET_FLOAT2);
93        }
94
95        pMesh->sharedVertexData->vertexCount = data->vcnt/3;
96        HardwareVertexBufferSharedPtr vbuf =
97                HardwareBufferManager::getSingleton().
98                createVertexBuffer(vertexDecl->getVertexSize(0), pMesh->sharedVertexData->vertexCount,
99                        HardwareBuffer::HBU_STATIC_WRITE_ONLY, false);
100
101        VertexBufferBinding* binding = pMesh->sharedVertexData->vertexBufferBinding;
102        binding->setBinding(0, vbuf);
103
104        Real* pVertices = static_cast<Real*>(vbuf->lock(HardwareBuffer::HBL_DISCARD));
105
106        int offset = 0, size = 3;
107        if (nOk) size += 3;
108        if (tOk) size += 2;
109        for (int i=0; i<data->vcnt/3; i++)
110        {
111                pVertices[i*size]   = data->vertices[i*3]   - translation.x;
112                pVertices[i*size+1] = data->vertices[i*3+1] - translation.y;
113                pVertices[i*size+2] = data->vertices[i*3+2] - translation.z;
114
115                offset = 3;
116                if (nOk)
117                {
118                        pVertices[i*size+offset] = data->normals[i*3];   offset++;
119                        pVertices[i*size+offset] = data->normals[i*3+1]; offset++;
120                        pVertices[i*size+offset] = data->normals[i*3+2]; offset++;
121                }
122                if (tOk)
123                {
124                        pVertices[i*size+offset] = data->texCoords[i*2];   offset++;
125                        pVertices[i*size+offset] = -data->texCoords[i*2+1]; offset++;
126                }
127        }
128
129        vbuf->unlock();
130
131
132        SubMesh *pSub = pMesh->createSubMesh();
133        pSub->operationType = RenderOperation::OT_TRIANGLE_LIST; //;data->roType;
134        pSub->useSharedVertices = true;
135
136        pSub->indexData->indexCount = data->icnt;
137        pSub->indexData->indexBuffer = HardwareBufferManager::getSingleton().
138                createIndexBuffer(HardwareIndexBuffer::IT_16BIT,
139                        pSub->indexData->indexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY, false);
140
141        HardwareIndexBufferSharedPtr ibuf = pSub->indexData->indexBuffer;
142        unsigned short* pIndices = static_cast<unsigned short*>(ibuf->lock(HardwareBuffer::HBL_DISCARD));
143
144        for (i = 0; i < data->icnt; i++) pIndices[i] = data->indices[i];
145       
146        ibuf->unlock();
147               
148        AxisAlignedBox bb(data->boundingBox->getMinimum() - translation, data->boundingBox->getMaximum() - translation);
149        //AxisAlignedBox bb(data->boundingBox->getMinimum(), data->boundingBox->getMaximum());
150        pMesh->_setBounds(bb);
151        Real radius = data->boundingBox->getMinimum().length();
152        if (data->boundingBox->getMaximum().length() > radius) radius = data->boundingBox->getMaximum().length();
153        pMesh->_setBoundingSphereRadius(radius);
154
155}
Note: See TracBrowser for help on using the repository browser.