[774] | 1 | #include "GeoMesh.h"
|
---|
| 2 |
|
---|
| 3 | using namespace Geometry;
|
---|
| 4 | using namespace std;
|
---|
| 5 |
|
---|
[980] | 6 | //---------------------------------------------------------------------------
|
---|
| 7 | // Cosntructor.
|
---|
| 8 | //---------------------------------------------------------------------------
|
---|
[774] | 9 | Mesh::Mesh():
|
---|
[980] | 10 | mVertexBuffer(0), mSubMeshCount(0), mSubMesh(0), hasSkeleton(false)
|
---|
[774] | 11 | {
|
---|
[980] | 12 | mMeshBounds.maxX = 0.0;
|
---|
| 13 | mMeshBounds.maxY = 0.0;
|
---|
| 14 | mMeshBounds.maxZ = 0.0;
|
---|
| 15 | mMeshBounds.minX = 0.0;
|
---|
| 16 | mMeshBounds.minY = 0.0;
|
---|
| 17 | mMeshBounds.minZ = 0.0;
|
---|
| 18 | mMeshBounds.scaleFactor = 0.0;
|
---|
[774] | 19 | }
|
---|
| 20 |
|
---|
[980] | 21 | //---------------------------------------------------------------------------
|
---|
| 22 | // Destroyer.
|
---|
| 23 | //---------------------------------------------------------------------------
|
---|
[774] | 24 | Mesh::~Mesh()
|
---|
| 25 | {
|
---|
| 26 | delete[] mSubMesh;
|
---|
| 27 | delete mVertexBuffer;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 |
|
---|
[980] | 31 | //---------------------------------------------------------------------------
|
---|
| 32 | // Copy constructor.
|
---|
| 33 | //---------------------------------------------------------------------------
|
---|
[774] | 34 | Mesh::Mesh(const Mesh &objmesh)
|
---|
| 35 | {
|
---|
| 36 | // constructor copia
|
---|
[980] | 37 | mVertexBuffer = new VertexBuffer();
|
---|
| 38 | mSubMeshCount = objmesh.mSubMeshCount;
|
---|
| 39 | mSubMesh = new SubMesh[objmesh.mSubMeshCount];
|
---|
| 40 |
|
---|
| 41 | // Fill up bounding box settings.
|
---|
| 42 | mMeshBounds.maxX = objmesh.mMeshBounds.maxX;
|
---|
| 43 | mMeshBounds.maxY = objmesh.mMeshBounds.maxY;
|
---|
| 44 | mMeshBounds.maxZ = objmesh.mMeshBounds.maxZ;
|
---|
| 45 | mMeshBounds.minX = objmesh.mMeshBounds.minX;
|
---|
| 46 | mMeshBounds.minY = objmesh.mMeshBounds.minY;
|
---|
| 47 | mMeshBounds.minZ = objmesh.mMeshBounds.minZ;
|
---|
| 48 | mMeshBounds.radius = objmesh.mMeshBounds.radius;
|
---|
| 49 | mMeshBounds.scaleFactor = objmesh.mMeshBounds.scaleFactor;
|
---|
| 50 |
|
---|
| 51 | // For each submesh.
|
---|
| 52 | for(size_t i = 0; i < objmesh.mSubMeshCount; i++)
|
---|
[774] | 53 | {
|
---|
[980] | 54 | mSubMesh[i].mSharedVertexBuffer = false;
|
---|
| 55 | mSubMesh[i].mVertexBuffer = new VertexBuffer();
|
---|
| 56 | mSubMesh[i].mVertexBuffer->mPosition = new Vector3[objmesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
| 57 | mSubMesh[i].mVertexBuffer->mNormal = new Vector3[objmesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
| 58 | mSubMesh[i].mVertexBuffer->mTexCoords = new Vector2[objmesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
| 59 | mSubMesh[i].mVertexBuffer->mVertexCount = objmesh.mSubMesh[i].mVertexBuffer->mVertexCount;
|
---|
| 60 | mSubMesh[i].mVertexBuffer->mVertexInfo = objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo;
|
---|
| 61 | mSubMesh[i].mType = objmesh.mSubMesh[i].mType;
|
---|
| 62 | strcpy(mSubMesh[i].mMaterialName,objmesh.mSubMesh[i].mMaterialName);
|
---|
[774] | 63 |
|
---|
[980] | 64 | if(objmesh.mSubMesh[i].mSharedVertexBuffer)
|
---|
[774] | 65 | {
|
---|
[980] | 66 | for(size_t s = 0;
|
---|
| 67 | s<objmesh.mSubMesh[i].mVertexBuffer->mVertexCount;
|
---|
| 68 | s++)
|
---|
[774] | 69 | {
|
---|
[980] | 70 | // Copy mPosition.
|
---|
| 71 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_POSITION)
|
---|
[774] | 72 | {
|
---|
| 73 | mSubMesh[i].mVertexBuffer->mPosition[s].x = objmesh.mVertexBuffer->mPosition[s].x;
|
---|
| 74 | mSubMesh[i].mVertexBuffer->mPosition[s].y = objmesh.mVertexBuffer->mPosition[s].y;
|
---|
| 75 | mSubMesh[i].mVertexBuffer->mPosition[s].z = objmesh.mVertexBuffer->mPosition[s].z;
|
---|
| 76 | }
|
---|
[980] | 77 |
|
---|
| 78 | // Copy mNormal.
|
---|
| 79 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_NORMAL)
|
---|
[774] | 80 | {
|
---|
| 81 | mSubMesh[i].mVertexBuffer->mNormal[s].x = objmesh.mVertexBuffer->mNormal[s].x;
|
---|
| 82 | mSubMesh[i].mVertexBuffer->mNormal[s].y = objmesh.mVertexBuffer->mNormal[s].y;
|
---|
| 83 | mSubMesh[i].mVertexBuffer->mNormal[s].z = objmesh.mVertexBuffer->mNormal[s].z;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[980] | 86 | // Copy mTexCoords.
|
---|
| 87 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_TEXCOORDS)
|
---|
[774] | 88 | {
|
---|
| 89 | mSubMesh[i].mVertexBuffer->mTexCoords[s].x = objmesh.mVertexBuffer->mTexCoords[s].x;
|
---|
| 90 | mSubMesh[i].mVertexBuffer->mTexCoords[s].y = objmesh.mVertexBuffer->mTexCoords[s].y;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | objmesh.mSubMesh[i].mSharedVertexBuffer = false;
|
---|
| 94 | }
|
---|
| 95 | else
|
---|
| 96 | {
|
---|
[980] | 97 | for(size_t s = 0; s < objmesh.mSubMesh[i].mVertexBuffer->mVertexCount; s++)
|
---|
[774] | 98 | {
|
---|
[980] | 99 | // Copy mPosition.
|
---|
| 100 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_POSITION)
|
---|
[774] | 101 | {
|
---|
| 102 | mSubMesh[i].mVertexBuffer->mPosition[s].x = objmesh.mSubMesh[i].mVertexBuffer->mPosition[s].x;
|
---|
| 103 | mSubMesh[i].mVertexBuffer->mPosition[s].y = objmesh.mSubMesh[i].mVertexBuffer->mPosition[s].y;
|
---|
| 104 | mSubMesh[i].mVertexBuffer->mPosition[s].z = objmesh.mSubMesh[i].mVertexBuffer->mPosition[s].z;
|
---|
| 105 | }
|
---|
[980] | 106 |
|
---|
[774] | 107 | //Copiamos mNormal
|
---|
[980] | 108 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_NORMAL)
|
---|
[774] | 109 | {
|
---|
| 110 | mSubMesh[i].mVertexBuffer->mNormal[s].x = objmesh.mSubMesh[i].mVertexBuffer->mNormal[s].x;
|
---|
| 111 | mSubMesh[i].mVertexBuffer->mNormal[s].y = objmesh.mSubMesh[i].mVertexBuffer->mNormal[s].y;
|
---|
| 112 | mSubMesh[i].mVertexBuffer->mNormal[s].z = objmesh.mSubMesh[i].mVertexBuffer->mNormal[s].z;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | //Copiamos mTexCoords
|
---|
[980] | 116 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & VERTEX_TEXCOORDS)
|
---|
[774] | 117 | {
|
---|
| 118 | mSubMesh[i].mVertexBuffer->mTexCoords[s].x = objmesh.mSubMesh[i].mVertexBuffer->mTexCoords[s].x;
|
---|
| 119 | mSubMesh[i].mVertexBuffer->mTexCoords[s].y = objmesh.mSubMesh[i].mVertexBuffer->mTexCoords[s].y;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | }
|
---|
[980] | 124 |
|
---|
| 125 | // Copy indices.
|
---|
| 126 | mSubMesh[i].mIndexCount = objmesh.mSubMesh[i].mIndexCount;
|
---|
| 127 | mSubMesh[i].mIndex = new Index[mSubMesh[i].mIndexCount];
|
---|
| 128 |
|
---|
| 129 | memcpy(mSubMesh[i].mIndex,objmesh.mSubMesh[i].mIndex,objmesh.mSubMesh[i].mIndexCount*sizeof(Index));
|
---|
[774] | 130 |
|
---|
[980] | 131 | // Copy strips.
|
---|
| 132 | __w64 int offset = 0; // offset between memory positions.
|
---|
[774] | 133 | mSubMesh[i].mStripCount=objmesh.mSubMesh[i].mStripCount;
|
---|
| 134 | if (objmesh.mSubMesh[i].mStripCount>0)
|
---|
| 135 | {
|
---|
| 136 | mSubMesh[i].mStrip = new Index*[objmesh.mSubMesh[i].mStripCount];
|
---|
| 137 |
|
---|
[980] | 138 | offset = &(mSubMesh[i].mIndex[0]) - &(objmesh.mSubMesh[i].mIndex[0]);
|
---|
| 139 | for (size_t j=0;j<objmesh.mSubMesh[i].mStripCount;j++)
|
---|
[774] | 140 | {
|
---|
[980] | 141 | mSubMesh[i].mStrip[j]=objmesh.mSubMesh[i].mStrip[j] + offset;
|
---|
[774] | 142 | }
|
---|
| 143 | }
|
---|
[980] | 144 |
|
---|
| 145 | // Copy submesh bones.
|
---|
| 146 | if (!objmesh.mSubMesh[i].mBones.empty())
|
---|
| 147 | {
|
---|
[985] | 148 | for (unsigned int j = 0; j < objmesh.mSubMesh[i].mBones.size(); j++)
|
---|
[980] | 149 | {
|
---|
| 150 | mSubMesh[i].mBones.push_back(objmesh.mSubMesh[i].mBones[j]);
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
[774] | 153 | }
|
---|
[980] | 154 |
|
---|
| 155 | // Copy skeleton name.
|
---|
| 156 | if (objmesh.hasSkeleton)
|
---|
| 157 | {
|
---|
| 158 | hasSkeleton = true;
|
---|
| 159 |
|
---|
| 160 | strcpy(mSkeletonName,objmesh.mSkeletonName);
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | // Copy mesh bones.
|
---|
| 164 | if (!objmesh.mBones.empty())
|
---|
| 165 | {
|
---|
[985] | 166 | for (unsigned int j = 0; j < objmesh.mBones.size(); j++)
|
---|
[980] | 167 | {
|
---|
| 168 | mBones.push_back(objmesh.mBones[j]);
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[774] | 172 | }
|
---|
| 173 |
|
---|
[980] | 174 | //---------------------------------------------------------------------------
|
---|
| 175 | // Assignment operator.
|
---|
| 176 | //---------------------------------------------------------------------------
|
---|
[774] | 177 | Mesh &Mesh::operator =(const Geometry::Mesh &objmesh)
|
---|
| 178 | {
|
---|
| 179 | // Operador de asignación
|
---|
| 180 | bool copiados = false; // indica si los vértices compartidos han sido copiados
|
---|
| 181 | mVertexBuffer = new VertexBuffer();
|
---|
| 182 | mSubMeshCount = objmesh.mSubMeshCount;
|
---|
| 183 | mSubMesh = new Geometry::SubMesh[objmesh.mSubMeshCount];
|
---|
[980] | 184 |
|
---|
| 185 | // Fill up bounding box settings.
|
---|
| 186 | mMeshBounds.maxX = objmesh.mMeshBounds.maxX;
|
---|
| 187 | mMeshBounds.maxY = objmesh.mMeshBounds.maxY;
|
---|
| 188 | mMeshBounds.maxZ = objmesh.mMeshBounds.maxZ;
|
---|
| 189 | mMeshBounds.minX = objmesh.mMeshBounds.minX;
|
---|
| 190 | mMeshBounds.minY = objmesh.mMeshBounds.minY;
|
---|
| 191 | mMeshBounds.minZ = objmesh.mMeshBounds.minZ;
|
---|
| 192 | mMeshBounds.radius = objmesh.mMeshBounds.radius;
|
---|
| 193 | mMeshBounds.scaleFactor = objmesh.mMeshBounds.scaleFactor;
|
---|
[774] | 194 |
|
---|
[980] | 195 | // For each submesh.
|
---|
[774] | 196 | for(size_t i = 0; i < objmesh.mSubMeshCount; i++)
|
---|
| 197 | {
|
---|
| 198 | mSubMesh[i].mSharedVertexBuffer = objmesh.mSubMesh[i].mSharedVertexBuffer; //.false;
|
---|
| 199 | mSubMesh[i].mVertexBuffer = new Geometry::VertexBuffer();
|
---|
| 200 | mSubMesh[i].mVertexBuffer->mPosition = new Geometry::Vector3[objmesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
| 201 | mSubMesh[i].mVertexBuffer->mNormal = new Geometry::Vector3[objmesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
| 202 | mSubMesh[i].mVertexBuffer->mTexCoords = new Geometry::Vector2[objmesh.mSubMesh[i].mVertexBuffer->mVertexCount];
|
---|
| 203 | mSubMesh[i].mVertexBuffer->mVertexCount = objmesh.mSubMesh[i].mVertexBuffer->mVertexCount;
|
---|
| 204 | mSubMesh[i].mVertexBuffer->mVertexInfo = objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo;
|
---|
[980] | 205 | mSubMesh[i].mType = objmesh.mSubMesh[i].mType;
|
---|
| 206 | strcpy(mSubMesh[i].mMaterialName,objmesh.mSubMesh[i].mMaterialName);
|
---|
[774] | 207 |
|
---|
| 208 | if (objmesh.mSubMesh[i].mSharedVertexBuffer && !copiados)
|
---|
| 209 | {
|
---|
| 210 | mVertexBuffer = mSubMesh[i].mVertexBuffer;
|
---|
| 211 | copiados = true;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | for(size_t s = 0; s < objmesh.mSubMesh[i].mVertexBuffer->mVertexCount; s++)
|
---|
| 215 | {
|
---|
| 216 | //Copiamos mPosition
|
---|
| 217 | //if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & Geometry::VERTEX_POSITION)
|
---|
| 218 | //{
|
---|
| 219 | mSubMesh[i].mVertexBuffer->mPosition[s].x = objmesh.mSubMesh[i].mVertexBuffer->mPosition[s].x;
|
---|
| 220 | mSubMesh[i].mVertexBuffer->mPosition[s].y = objmesh.mSubMesh[i].mVertexBuffer->mPosition[s].y;
|
---|
| 221 | mSubMesh[i].mVertexBuffer->mPosition[s].z = objmesh.mSubMesh[i].mVertexBuffer->mPosition[s].z;
|
---|
| 222 | //}
|
---|
| 223 | //Copiamos mNormal
|
---|
| 224 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & Geometry::VERTEX_NORMAL)
|
---|
| 225 | {
|
---|
| 226 | mSubMesh[i].mVertexBuffer->mNormal[s].x = objmesh.mSubMesh[i].mVertexBuffer->mNormal[s].x;
|
---|
| 227 | mSubMesh[i].mVertexBuffer->mNormal[s].y = objmesh.mSubMesh[i].mVertexBuffer->mNormal[s].y;
|
---|
| 228 | mSubMesh[i].mVertexBuffer->mNormal[s].z = objmesh.mSubMesh[i].mVertexBuffer->mNormal[s].z;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | //Copiamos mTexCoords
|
---|
| 232 | if (objmesh.mSubMesh[i].mVertexBuffer->mVertexInfo & Geometry::VERTEX_TEXCOORDS)
|
---|
| 233 | {
|
---|
| 234 | mSubMesh[i].mVertexBuffer->mTexCoords[s].x = objmesh.mSubMesh[i].mVertexBuffer->mTexCoords[s].x;
|
---|
| 235 | mSubMesh[i].mVertexBuffer->mTexCoords[s].y = objmesh.mSubMesh[i].mVertexBuffer->mTexCoords[s].y;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | // Copiar los indices
|
---|
| 240 | mSubMesh[i].mIndexCount = objmesh.mSubMesh[i].mIndexCount;
|
---|
| 241 | mSubMesh[i].mIndex = new Index[mSubMesh[i].mIndexCount];
|
---|
| 242 |
|
---|
[980] | 243 | memcpy( mSubMesh[i].mIndex,
|
---|
| 244 | objmesh.mSubMesh[i].mIndex,
|
---|
| 245 | objmesh.mSubMesh[i].mIndexCount*sizeof(Geometry::Index));
|
---|
[774] | 246 |
|
---|
| 247 | // Copiar las tiras
|
---|
[980] | 248 | __w64 offset = 0; // desplazamiento entre posiciones de memoria.
|
---|
[774] | 249 | mSubMesh[i].mStripCount = objmesh.mSubMesh[i].mStripCount;
|
---|
| 250 |
|
---|
| 251 | if (objmesh.mSubMesh[i].mStripCount>0)
|
---|
| 252 | {
|
---|
| 253 | mSubMesh[i].mStrip = new Index*[objmesh.mSubMesh[i].mStripCount];
|
---|
[980] | 254 | offset = &(mSubMesh[i].mIndex[0]) - &(objmesh.mSubMesh[i].mIndex[0]);
|
---|
[774] | 255 |
|
---|
| 256 | for (size_t j = 0; j < objmesh.mSubMesh[i].mStripCount; j++)
|
---|
| 257 | {
|
---|
[980] | 258 | mSubMesh[i].mStrip[j] = objmesh.mSubMesh[i].mStrip[j] + offset;
|
---|
[774] | 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[980] | 262 | // Copy submesh bones.
|
---|
[774] | 263 | if (!objmesh.mSubMesh[i].mBones.empty())
|
---|
| 264 | {
|
---|
[985] | 265 | for (unsigned int j = 0; j < objmesh.mSubMesh[i].mBones.size(); j++)
|
---|
[774] | 266 | {
|
---|
| 267 | mSubMesh[i].mBones.push_back(objmesh.mSubMesh[i].mBones[j]);
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[980] | 272 | // Copy skeleton name.
|
---|
[774] | 273 | if (objmesh.hasSkeleton)
|
---|
| 274 | {
|
---|
| 275 | hasSkeleton = true;
|
---|
| 276 |
|
---|
| 277 | strcpy(mSkeletonName,objmesh.mSkeletonName);
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[980] | 280 | // Copy mesh bones.
|
---|
[774] | 281 | if (!objmesh.mBones.empty())
|
---|
| 282 | {
|
---|
[985] | 283 | for (unsigned int j = 0; j < objmesh.mBones.size(); j++)
|
---|
[774] | 284 | {
|
---|
| 285 | mBones.push_back(objmesh.mBones[j]);
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | return *this;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[980] | 292 | //---------------------------------------------------------------------------
|
---|
| 293 | // Load mesh.
|
---|
| 294 | //---------------------------------------------------------------------------
|
---|
[774] | 295 | void Mesh::Load(Serializer &s)
|
---|
| 296 | {
|
---|
| 297 | bool sharedVertexBuffer = false;
|
---|
| 298 |
|
---|
| 299 | //Clear Data
|
---|
| 300 | delete[] mSubMesh; mSubMesh = 0;
|
---|
| 301 | delete mVertexBuffer; mVertexBuffer = 0;
|
---|
| 302 | mSubMeshCount = 0;
|
---|
| 303 |
|
---|
| 304 | s.ReadArray(&sharedVertexBuffer,1);
|
---|
| 305 | if (sharedVertexBuffer)
|
---|
| 306 | {
|
---|
| 307 | mVertexBuffer = new VertexBuffer;
|
---|
| 308 | mVertexBuffer->Load(s);
|
---|
| 309 | }
|
---|
| 310 | s.ReadArray(&mSubMeshCount,1);
|
---|
| 311 | mSubMesh = new SubMesh[mSubMeshCount];
|
---|
| 312 | for(size_t i = 0; i < mSubMeshCount; ++i)
|
---|
| 313 | {
|
---|
| 314 | mSubMesh[i].Load(s);
|
---|
| 315 | if (mSubMesh[i].mSharedVertexBuffer && sharedVertexBuffer)
|
---|
| 316 | {
|
---|
| 317 | mSubMesh[i].mVertexBuffer = mVertexBuffer;
|
---|
| 318 | }
|
---|
| 319 | }
|
---|
| 320 | }
|
---|
| 321 |
|
---|
[980] | 322 | //---------------------------------------------------------------------------
|
---|
| 323 | // Save mesh.
|
---|
| 324 | //---------------------------------------------------------------------------
|
---|
[774] | 325 | void Mesh::Save(Serializer &s)
|
---|
| 326 | {
|
---|
| 327 | bool sharedVertexBuffer = (mVertexBuffer != 0);
|
---|
| 328 | s.WriteArray(&sharedVertexBuffer, 1);
|
---|
| 329 | if (sharedVertexBuffer)
|
---|
| 330 | {
|
---|
| 331 | mVertexBuffer->Save(s);
|
---|
| 332 | }
|
---|
| 333 | s.WriteArray(&mSubMeshCount,1);
|
---|
| 334 | for(size_t i = 0; i < mSubMeshCount; ++i)
|
---|
| 335 | {
|
---|
| 336 | mSubMesh[i].Save(s);
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 |
|
---|
[980] | 340 | //---------------------------------------------------------------------------
|
---|
| 341 | // Export to obj mesh.
|
---|
| 342 | //---------------------------------------------------------------------------
|
---|
[774] | 343 | void Mesh::exportToOBJ(char *nomfich)
|
---|
| 344 | {
|
---|
| 345 | // Genera un fichero obj con vértices, triángulos
|
---|
| 346 |
|
---|
| 347 | std::ofstream obj(nomfich);
|
---|
| 348 | obj << "begin" << std::endl;
|
---|
| 349 | // Vértices
|
---|
[980] | 350 | for (size_t i=0; i < mSubMeshCount; i++)
|
---|
[774] | 351 | {
|
---|
[980] | 352 | for (size_t j=0; j < mSubMesh[i].mVertexBuffer->mVertexCount; j++)
|
---|
[774] | 353 | {
|
---|
[980] | 354 | obj << "v " << mSubMesh[i].mVertexBuffer->mPosition[j].x << " " <<
|
---|
| 355 | mSubMesh[i].mVertexBuffer->mPosition[j].y << " " <<
|
---|
| 356 | mSubMesh[i].mVertexBuffer->mPosition[j].z << " " << std::endl;
|
---|
[774] | 357 | }
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 |
|
---|
| 361 | // Caras
|
---|
[980] | 362 | for (size_t i=0; i < mSubMeshCount; i++)
|
---|
[774] | 363 | {
|
---|
[980] | 364 | for (size_t j=0; j < mSubMesh[i].mIndexCount; j=j+3)
|
---|
[774] | 365 | {
|
---|
[980] | 366 | obj << "f " << mSubMesh[i].mIndex[j]+1 << " " <<
|
---|
| 367 | mSubMesh[i].mIndex[j+1]+1 << " " <<
|
---|
| 368 | mSubMesh[i].mIndex[j+2]+1 << std::endl;
|
---|
[774] | 369 | }
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | obj << "end" << std::endl;
|
---|
| 373 | obj.close();
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[980] | 376 | //---------------------------------------------------------------------------
|
---|
| 377 | // Transform to shared vertex mesh.
|
---|
| 378 | //---------------------------------------------------------------------------
|
---|
| 379 | Mesh *Mesh::toSharedVertex()
|
---|
[774] | 380 | {
|
---|
| 381 | // Move all vertex to the shared vertex buffer.
|
---|
[980] | 382 | Mesh *mesh = new Mesh();
|
---|
[774] | 383 |
|
---|
[980] | 384 | if (mSubMesh[0].mSharedVertexBuffer)
|
---|
[774] | 385 | {
|
---|
| 386 | *mesh = *this;
|
---|
[980] | 387 |
|
---|
[774] | 388 | return mesh;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
[980] | 391 | mesh->mVertexBuffer = new VertexBuffer();
|
---|
| 392 | mesh->mSubMeshCount = mSubMeshCount;
|
---|
| 393 |
|
---|
[774] | 394 | // Reserva memoria para los submeshes
|
---|
[980] | 395 | mesh->mSubMesh = new SubMesh[mSubMeshCount];
|
---|
[774] | 396 |
|
---|
[980] | 397 | // Fill up bounding box settings.
|
---|
| 398 | mesh->mMeshBounds.maxX = mMeshBounds.maxX;
|
---|
| 399 | mesh->mMeshBounds.maxY = mMeshBounds.maxY;
|
---|
| 400 | mesh->mMeshBounds.maxZ = mMeshBounds.maxZ;
|
---|
| 401 | mesh->mMeshBounds.minX = mMeshBounds.minX;
|
---|
| 402 | mesh->mMeshBounds.minY = mMeshBounds.minY;
|
---|
| 403 | mesh->mMeshBounds.minZ = mMeshBounds.minZ;
|
---|
| 404 | mesh->mMeshBounds.radius = mMeshBounds.radius;
|
---|
| 405 | mesh->mMeshBounds.scaleFactor = mMeshBounds.scaleFactor;
|
---|
| 406 |
|
---|
| 407 | // construcción de los submeshes
|
---|
| 408 | long int acumVerts = 0;
|
---|
| 409 |
|
---|
| 410 | for (size_t i = 0; i < mesh->mSubMeshCount; i++)
|
---|
[774] | 411 | {
|
---|
[980] | 412 | mesh->mSubMesh[i].mSharedVertexBuffer = true;
|
---|
| 413 | mesh->mSubMesh[i].mVertexBuffer = mesh->mVertexBuffer;
|
---|
| 414 | mesh->mSubMesh[i].mStripCount = 0;
|
---|
| 415 | mesh->mSubMesh[i].mStrip = NULL;
|
---|
| 416 | mesh->mSubMesh[i].mType = mSubMesh[i].mType;
|
---|
| 417 |
|
---|
| 418 | strcpy(mesh->mSubMesh[i].mMaterialName,mSubMesh[i].mMaterialName);
|
---|
| 419 |
|
---|
[774] | 420 | // copiar los índices
|
---|
[980] | 421 | mesh->mSubMesh[i].mIndexCount = mSubMesh[i].mIndexCount;
|
---|
| 422 | mesh->mSubMesh[i].mIndex = new Index[mSubMesh[i].mIndexCount];
|
---|
[774] | 423 |
|
---|
[980] | 424 | for (size_t j = 0; j < mSubMesh[i].mIndexCount; j++)
|
---|
[774] | 425 | {
|
---|
[980] | 426 | mesh->mSubMesh[i].mIndex[j] = mSubMesh[i].mIndex[j]+acumVerts;
|
---|
[774] | 427 | }
|
---|
| 428 |
|
---|
[985] | 429 | acumVerts += long(mSubMesh[i].mVertexBuffer->mVertexCount);
|
---|
[774] | 430 |
|
---|
| 431 | // Copiar las tiras
|
---|
[980] | 432 | int offset = 0; // desplazamiento entre posiciones de memoria.
|
---|
| 433 |
|
---|
| 434 | mesh->mSubMesh[i].mStripCount = mSubMesh[i].mStripCount;
|
---|
| 435 |
|
---|
| 436 | if (mSubMesh[i].mStripCount > 0)
|
---|
[774] | 437 | {
|
---|
[980] | 438 | mesh->mSubMesh[i].mStrip = new Index*[mSubMesh[i].mStripCount];
|
---|
[774] | 439 |
|
---|
[985] | 440 | offset = int(&(mesh->mSubMesh[i].mIndex[0]) - &(mSubMesh[i].mIndex[0]));
|
---|
[980] | 441 |
|
---|
| 442 | for (size_t j = 0; j < mSubMesh[i].mStripCount; j++)
|
---|
[774] | 443 | {
|
---|
[980] | 444 | mesh->mSubMesh[i].mStrip[j] = mSubMesh[i].mStrip[j] + offset;
|
---|
[774] | 445 | }
|
---|
| 446 | }
|
---|
| 447 | }
|
---|
| 448 |
|
---|
[980] | 449 | mesh->mVertexBuffer->mVertexCount = acumVerts;
|
---|
| 450 | mesh->mVertexBuffer->mVertexInfo = mSubMesh[0].mVertexBuffer->mVertexInfo;
|
---|
[774] | 451 |
|
---|
[980] | 452 | mesh->mVertexBuffer->mPosition = new Vector3[mesh->mVertexBuffer->mVertexCount];
|
---|
| 453 | mesh->mVertexBuffer->mNormal = new Vector3[mesh->mVertexBuffer->mVertexCount];
|
---|
| 454 | mesh->mVertexBuffer->mTexCoords = new Vector2[mesh->mVertexBuffer->mVertexCount];
|
---|
| 455 |
|
---|
[774] | 456 | // copiar los vértices
|
---|
[980] | 457 | acumVerts = 0;
|
---|
| 458 |
|
---|
| 459 | size_t newIndex;
|
---|
| 460 |
|
---|
| 461 | for (size_t i = 0; i < mSubMeshCount; i++)
|
---|
[774] | 462 | {
|
---|
[980] | 463 | for(size_t j = 0; j < mSubMesh[i].mVertexBuffer->mVertexCount; j++)
|
---|
[774] | 464 | {
|
---|
[980] | 465 | newIndex = acumVerts + j;
|
---|
| 466 |
|
---|
| 467 | mesh->mVertexBuffer->
|
---|
| 468 | mPosition[newIndex].x = mSubMesh[i].mVertexBuffer->
|
---|
| 469 | mPosition[j].x;
|
---|
| 470 |
|
---|
| 471 | mesh->mVertexBuffer->
|
---|
| 472 | mPosition[newIndex].y = mSubMesh[i].mVertexBuffer->
|
---|
| 473 | mPosition[j].y;
|
---|
| 474 |
|
---|
| 475 | mesh->mVertexBuffer->
|
---|
| 476 | mPosition[newIndex].z = mSubMesh[i].mVertexBuffer->
|
---|
| 477 | mPosition[j].z;
|
---|
[774] | 478 |
|
---|
[980] | 479 | mesh->mVertexBuffer->
|
---|
| 480 | mNormal[newIndex].x = mSubMesh[i].mVertexBuffer->
|
---|
| 481 | mNormal[j].x;
|
---|
| 482 |
|
---|
| 483 | mesh->mVertexBuffer->
|
---|
| 484 | mNormal[newIndex].y = mSubMesh[i].mVertexBuffer->
|
---|
| 485 | mNormal[j].y;
|
---|
| 486 |
|
---|
| 487 | mesh->mVertexBuffer->
|
---|
| 488 | mNormal[newIndex].z = mSubMesh[i].mVertexBuffer->
|
---|
| 489 | mNormal[j].z;
|
---|
[774] | 490 |
|
---|
[980] | 491 | mesh->mVertexBuffer->
|
---|
| 492 | mTexCoords[newIndex].x = mSubMesh[i].mVertexBuffer->
|
---|
| 493 | mTexCoords[j].x;
|
---|
| 494 |
|
---|
| 495 | mesh->mVertexBuffer->
|
---|
| 496 | mTexCoords[newIndex].y = mSubMesh[i].mVertexBuffer->
|
---|
| 497 | mTexCoords[j].y;
|
---|
[774] | 498 | }
|
---|
[980] | 499 |
|
---|
[985] | 500 | acumVerts += long(mSubMesh[i].mVertexBuffer->mVertexCount);
|
---|
[986] | 501 |
|
---|
| 502 | // Copy submesh bones.
|
---|
| 503 | if (!mSubMesh[i].mBones.empty())
|
---|
| 504 | {
|
---|
| 505 | for (unsigned int j = 0; j < mSubMesh[i].mBones.size(); j++)
|
---|
| 506 | {
|
---|
| 507 | mesh->mSubMesh[i].mBones.push_back(mSubMesh[i].mBones[j]);
|
---|
| 508 | }
|
---|
| 509 | }
|
---|
| 510 |
|
---|
[774] | 511 | }
|
---|
| 512 |
|
---|
[986] | 513 | // Copy skeleton name.
|
---|
| 514 | if (hasSkeleton)
|
---|
| 515 | {
|
---|
| 516 | mesh->hasSkeleton = true;
|
---|
| 517 |
|
---|
| 518 | strcpy(mesh->mSkeletonName,mSkeletonName);
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | // Copy mesh bones.
|
---|
| 522 | if (!mBones.empty())
|
---|
| 523 | {
|
---|
| 524 | for (unsigned int j = 0; j < mBones.size(); j++)
|
---|
| 525 | {
|
---|
| 526 | mesh->mBones.push_back(mBones[j]);
|
---|
| 527 | }
|
---|
| 528 | }
|
---|
| 529 |
|
---|
[774] | 530 | return mesh;
|
---|
| 531 | }
|
---|
| 532 |
|
---|