1 | #include "ObjReader.h"
|
---|
2 | #include "gzstream.h"
|
---|
3 | #include "Triangle3.h"
|
---|
4 | #include "IntersectableWrapper.h"
|
---|
5 |
|
---|
6 |
|
---|
7 | ObjReader::ObjReader()
|
---|
8 | {}
|
---|
9 |
|
---|
10 |
|
---|
11 | ObjReader::~ObjReader()
|
---|
12 | {}
|
---|
13 |
|
---|
14 |
|
---|
15 | bool ObjReader::LoadFile(const string &filename,
|
---|
16 | GtpVisibilityPreprocessor::ObjectContainer &objects) const
|
---|
17 | {
|
---|
18 | igzstream samplesIn(filename.c_str());
|
---|
19 |
|
---|
20 | if (!samplesIn.is_open())
|
---|
21 | return false;
|
---|
22 |
|
---|
23 | // read in triangle size
|
---|
24 | int numTriangles;
|
---|
25 |
|
---|
26 | samplesIn.read(reinterpret_cast<char *>(&numTriangles), sizeof(int));
|
---|
27 | objects.reserve(numTriangles);
|
---|
28 |
|
---|
29 | while (1)
|
---|
30 | {
|
---|
31 | GtpVisibilityPreprocessor::Triangle3 tri;
|
---|
32 |
|
---|
33 | samplesIn.read(reinterpret_cast<char *>(tri.mVertices + 0),
|
---|
34 | sizeof(GtpVisibilityPreprocessor::Vector3));
|
---|
35 | samplesIn.read(reinterpret_cast<char *>(tri.mVertices + 1),
|
---|
36 | sizeof(GtpVisibilityPreprocessor::Vector3));
|
---|
37 | samplesIn.read(reinterpret_cast<char *>(tri.mVertices + 2),
|
---|
38 | sizeof(GtpVisibilityPreprocessor::Vector3));
|
---|
39 |
|
---|
40 | // end of file reached
|
---|
41 | if (samplesIn.eof())
|
---|
42 | break;
|
---|
43 |
|
---|
44 | GtpVisibilityPreprocessor::TriangleIntersectable *obj =
|
---|
45 | new GtpVisibilityPreprocessor::TriangleIntersectable(tri);
|
---|
46 | objects.push_back(obj);
|
---|
47 | }
|
---|
48 |
|
---|
49 | return true;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | const std::vector<Ogre::Entity *> &ObjReader::GetObjects() const
|
---|
54 | {
|
---|
55 | return mObjects;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | Ogre::Entity *ObjReader::CreateEntity(const std::string &name)
|
---|
60 | // ObjMeshData *mData,
|
---|
61 | // Ogre::Vector3 *translation)
|
---|
62 | {
|
---|
63 | using namespace Ogre;
|
---|
64 | Entity *entity;
|
---|
65 |
|
---|
66 |
|
---|
67 | std::string meshName = name.substr(name.find('/',0), name.length()) + "/Mesh";
|
---|
68 | std::string entityName = name + "/Entity";
|
---|
69 |
|
---|
70 | MeshPtr mesh = MeshManager::getSingleton().createManual(meshName, "ObjGroup");
|
---|
71 | SubMesh* submesh = mesh->createSubMesh();
|
---|
72 | int nbVertices = 99992;
|
---|
73 | // We must create the vertex data, indicating how many vertices there will be
|
---|
74 | submesh->useSharedVertices = false;
|
---|
75 | submesh->vertexData = new VertexData();
|
---|
76 | submesh->vertexData->vertexStart = 0;
|
---|
77 | submesh->vertexData->vertexCount = nbVertices;
|
---|
78 |
|
---|
79 | static const unsigned short source = 0;
|
---|
80 | size_t offset = 0;
|
---|
81 |
|
---|
82 | VertexDeclaration* declaration = HardwareBufferManager::getSingleton().createVertexDeclaration();
|
---|
83 |
|
---|
84 | offset += declaration->addElement(source,offset,VET_FLOAT3,VES_POSITION).getSize();
|
---|
85 | // offset += declaration->addElement(source,offset,VET_FLOAT3,VES_NORMAL).getSize();
|
---|
86 | // offset += declaration->addElement(source,offset,VET_FLOAT2,VES_TEXTURE_COORDINATES).getSize();
|
---|
87 |
|
---|
88 |
|
---|
89 | int numVertices = 0;
|
---|
90 |
|
---|
91 | HardwareVertexBufferSharedPtr vbuffer =
|
---|
92 | HardwareBufferManager::getSingleton().createVertexBuffer(declaration->getVertexSize(source), // size of one whole vertex
|
---|
93 | submesh->vertexData->vertexCount, // number of vertices
|
---|
94 | HardwareBuffer::HBU_STATIC_WRITE_ONLY, // usage
|
---|
95 | false); // no shadow buffer
|
---|
96 |
|
---|
97 |
|
---|
98 | // No we get access to the buffer to fill it. During so we record the bounding box.
|
---|
99 | AxisAlignedBox aabox;
|
---|
100 |
|
---|
101 | float* vdata = static_cast<float*>(vbuffer->lock(HardwareBuffer::HBL_DISCARD));
|
---|
102 |
|
---|
103 | for (size_t i = 0; i < nbVertices; ++ i)
|
---|
104 | {
|
---|
105 | // Position
|
---|
106 | Vector3 position;// = 20.0f*positions[i];
|
---|
107 | *vdata++ = position.x;
|
---|
108 | *vdata++ = position.y;
|
---|
109 | *vdata++ = position.z;
|
---|
110 | aabox.merge(position);
|
---|
111 | // Normal
|
---|
112 | Vector3 normal = position.normalisedCopy();
|
---|
113 | *vdata++ = normal.x;
|
---|
114 | *vdata++ = normal.y;
|
---|
115 | *vdata++ = normal.z;
|
---|
116 |
|
---|
117 | // Texture coordinate
|
---|
118 | Vector2 tcoordinate;// = tcoordinates[i];
|
---|
119 | *vdata ++ = tcoordinate.x;
|
---|
120 |
|
---|
121 | *vdata ++ = tcoordinate.y;
|
---|
122 |
|
---|
123 | }
|
---|
124 |
|
---|
125 | vbuffer->unlock();
|
---|
126 |
|
---|
127 | // We must indicate the bounding box
|
---|
128 | //mesh->_setBounds(aabox);
|
---|
129 |
|
---|
130 | //mesh->_setBoundingSphereRadius((aabox.getMaximum()-aabox.getMinimum()).length()/2.0);
|
---|
131 |
|
---|
132 | mesh->load();
|
---|
133 |
|
---|
134 | // Create an entity with the mesh
|
---|
135 | //entity = mSceneManager->createEntity(meshName, entityName);
|
---|
136 |
|
---|
137 | return entity;
|
---|
138 | } |
---|