MshFile.cpp 18.9 KB
Newer Older
Anakin's avatar
Anakin committed
1
#include "..\Header\MshFile.h"
2
#include "..\Header\tga.h"
Anakin's avatar
Anakin committed
3
#include <QColor>
Anakin's avatar
Anakin committed
4 5 6 7 8 9 10 11

// helper function to save data from file to any variable type
#define F2V(variableName) reinterpret_cast<char*>(&variableName)


/////////////////////////////////////////////////////////////////////////
// public constructor/destructor

Anakin's avatar
Anakin committed
12
MshFile::MshFile(QString path, QObject * parent)
13
	: FileInterface(path, parent)
Anakin's avatar
Anakin committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
{
	import();
}

MshFile::~MshFile()
{
}


/////////////////////////////////////////////////////////////////////////
// private functions

void MshFile::import()
{
	// go to file size information
	m_file.seekg(4);

	std::uint32_t tmp_fileSize;
	std::list<ChunkHeader*> tmp_mainChunks;

	// get all chunks under HEDR
	m_file.read(F2V(tmp_fileSize), sizeof(tmp_fileSize));
	loadChunks(tmp_mainChunks, m_file.tellg(), tmp_fileSize);

	// evaulate HEDR subchunks (= find MSH2)
	for (ChunkHeader* it : tmp_mainChunks)
	{
		if (!strcmp("MSH2", it->name))
		{
			// get all subchunks
			std::list<ChunkHeader*> tmp_msh2Chunks;
			loadChunks(tmp_msh2Chunks, it->position, it->size);

			// evaluate MSH2 subchunks
			analyseMsh2Chunks(tmp_msh2Chunks);

			// clean up
			while (!tmp_msh2Chunks.empty())
			{
				ChunkHeader* curs = tmp_msh2Chunks.front();
				tmp_msh2Chunks.pop_front();
				delete curs;
			}
		}
	}

	// clean up
	while (!tmp_mainChunks.empty())
	{
		ChunkHeader* cur = tmp_mainChunks.front();
		tmp_mainChunks.pop_front();
		delete cur;
	}
}

void MshFile::loadChunks(std::list<ChunkHeader*>& destination, std::streampos start, const std::uint32_t length)
{
	// jump to first chunk
	m_file.seekg(start);

	do
	{
		ChunkHeader* tmp_header = new ChunkHeader();

		// get information
		m_file.read(F2V(tmp_header->name[0]), sizeof(tmp_header->name) - 1);
		m_file.read(F2V(tmp_header->size), sizeof(tmp_header->size));
		tmp_header->position = m_file.tellg();

		// store information
		destination.push_back(tmp_header);

		// jump to next header
		m_file.seekg(tmp_header->size, std::ios_base::cur);

		// out of file. Maybe a size information is corrupted
		if (!m_file.good())
		{
92
			emit sendMessage("WARNING: corrupted file. Trying to continue..", 1);
Anakin's avatar
Anakin committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
			m_file.clear();
			break;
		}

	} while (m_file.tellg() - start != length);
}

void MshFile::analyseMsh2Chunks(std::list<ChunkHeader*>& chunkList)
{
	for (auto& it : chunkList)
	{
		// scene information
		if (!strcmp("SINF", it->name))
		{
			// get SINF subchunks
			std::list<ChunkHeader*> tmp_sinfChunks;
			loadChunks(tmp_sinfChunks, it->position, it->size);

			// evaluate SINF subchunks
			for (auto& it : tmp_sinfChunks)
			{
				if (!strcmp("BBOX", it->name))
				{
					m_file.seekg(it->position);

					// read in the quaternion
					float tmp_quat[4];
					for (int i = 0; i < 4; i++)
						m_file.read(F2V(tmp_quat[i]), sizeof(float));

					m_sceneBbox.rotation.setX(tmp_quat[0]);
					m_sceneBbox.rotation.setY(tmp_quat[1]);
					m_sceneBbox.rotation.setZ(tmp_quat[2]);
					m_sceneBbox.rotation.setScalar(tmp_quat[3]);

					//read in the center
					for (int i = 0; i < 3; i++)
						m_file.read(F2V(m_sceneBbox.center[i]), sizeof(float));

					//read in the extents
					for (int i = 0; i < 3; i++)
						m_file.read(F2V(m_sceneBbox.extents[i]), sizeof(float));
				}
			}

			// clean up SINF subchunks
			for (ChunkHeader* it : tmp_sinfChunks)
				delete it;
		}

		// material list
		else if (!strcmp("MATL", it->name))
		{
			// "useless" information how many MATD follow, jump over it
			m_file.seekg(it->position);
			m_file.seekg(sizeof(std::uint32_t), std::ios_base::cur);

			// get all MATL subchunk
			std::list<ChunkHeader*> tmp_matlChunks;
			loadChunks(tmp_matlChunks, m_file.tellg(), it->size - 4);

			// evaluate MATL subchunks
			for (auto& it : tmp_matlChunks)
			{
				// This shouldn't be anything else than MATD
				if (!strcmp("MATD", it->name))
				{
					// get all subchunks from MATD
					std::list<ChunkHeader*> tmp_matdChunks;
					loadChunks(tmp_matdChunks, it->position, it->size);

164
					m_materials->push_back(Material());
Anakin's avatar
Anakin committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

					// analyse MATD subchunks
					analyseMatdChunks(tmp_matdChunks);

					// clean up MATD subchunks
					while (!tmp_matdChunks.empty())
					{
						ChunkHeader* cur = tmp_matdChunks.front();
						tmp_matdChunks.pop_front();
						delete cur;
					}
				}
			}

			// clean up MATL subchunks
			while (!tmp_matlChunks.empty())
			{
				ChunkHeader* cur = tmp_matlChunks.front();
				tmp_matlChunks.pop_front();
				delete cur;
			}
		}

		// model
		else if (!strcmp("MODL", it->name))
		{
			Model* new_model = new Model;
192 193
			m_currentType = ModelTyp::null;
			m_currentRenderFlag = -1;
Anakin's avatar
Anakin committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

			// get all MODL subchunks
			std::list<ChunkHeader*> tmp_chunks;
			loadChunks(tmp_chunks, it->position, it->size);

			// evaluate MODL subchunks
			analyseModlChunks(new_model, tmp_chunks);

			//clean up MODL subchunks
			while (!tmp_chunks.empty())
			{
				ChunkHeader* cur = tmp_chunks.front();
				tmp_chunks.pop_front();
				delete cur;
			}

			// save Model data
			m_models->push_back(new_model);
		}
	}
}

void MshFile::analyseMatdChunks(std::list<ChunkHeader*>& chunkList)
{
	for (auto& it : chunkList)
	{
Anakin's avatar
Anakin committed
220 221 222
		// name
		if (!strcmp("NAME", it->name))
		{
C-Fu's avatar
C-Fu committed
223 224 225 226 227 228
			m_file.seekg(it->position);
			char* buffer = new char[it->size + 1];
			*buffer = { 0 };
			m_file.read(buffer, it->size);
			m_materials->back().name = buffer;
			delete[] buffer;
Anakin's avatar
Anakin committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
		}

		// data
		else if(!strcmp("DATA", it->name))
		{
			m_file.seekg(it->position);

			// diffuse
			for (unsigned int i = 0; i < 4; i++)
				m_file.read(F2V(m_materials->back().diffuseColor[i]), sizeof(float));

			// specular
			for (unsigned int i = 0; i < 4; i++)
				m_file.read(F2V(m_materials->back().specularColor[i]), sizeof(float));

			// ambient
			for (unsigned int i = 0; i < 4; i++)
				m_file.read(F2V(m_materials->back().ambientColor[i]), sizeof(float));

			// shininess
			m_file.read(F2V(m_materials->back().shininess), sizeof(float));
		}

		// TODO: evaluate specular, gloss,.. and save values
Anakin's avatar
Anakin committed
253
		// attributes
Anakin's avatar
Anakin committed
254
		else if (!strcmp("ATRB", it->name))
Anakin's avatar
Anakin committed
255
		{
Anakin's avatar
Anakin committed
256 257 258
			// get pointer to current material
			Material* curMat = &m_materials->back();

Anakin's avatar
Anakin committed
259 260
			// read the attributes
			m_file.seekg(it->position);
Anakin's avatar
Anakin committed
261
			std::uint8_t flag;
Anakin's avatar
Anakin committed
262
			m_file.read(F2V(flag), sizeof(flag));
Anakin's avatar
Anakin committed
263 264 265
			m_file.read(F2V(curMat->rendertype), sizeof(std::uint8_t));
			m_file.read(F2V(curMat->dataValues[0]), sizeof(std::uint8_t));
			m_file.read(F2V(curMat->dataValues[1]), sizeof(std::uint8_t));
Anakin's avatar
Anakin committed
266

Anakin's avatar
Anakin committed
267 268 269 270 271 272 273 274 275 276 277
			// flags
			// 0: emissive
			// 1: glow
			// 2: single-sided transparency
			// 3: double-sided transparency
			// 4: hard-edged transparency
			// 5: per-pixel lighting
			// 6: additive transparency
			// 7: specular

			for (unsigned int i = 0; i < 8; i++)
Anakin's avatar
Anakin committed
278
				curMat->flags[i] = (std::uint8_t)(flag << (7 - i)) >> 7;
Anakin's avatar
Anakin committed
279

Anakin's avatar
Anakin committed
280
			curMat->transparent = curMat->flags[2] || curMat->flags[3] || curMat->flags[4] || curMat->flags[6] || curMat->rendertype == 4;
Anakin's avatar
Anakin committed
281

Anakin's avatar
Anakin committed
282 283
		}

C-Fu's avatar
C-Fu committed
284
		// texture zero
Anakin's avatar
Anakin committed
285
		else if (!strcmp("TX0D", it->name))
Anakin's avatar
Anakin committed
286
		{
287
			// get the texture name
Anakin's avatar
Anakin committed
288 289 290 291
			m_file.seekg(it->position);
			char* buffer = new char[it->size + 1];
			*buffer = { 0 };
			m_file.read(buffer, it->size);
Anakin's avatar
Anakin committed
292
			m_materials->back().textureName = buffer;
Anakin's avatar
Anakin committed
293
			delete[] buffer;
294

C-Fu's avatar
C-Fu committed
295
			// load the texture if the name is not empty
Anakin's avatar
Anakin committed
296 297
			if (!m_materials->back().textureName.isEmpty())
				loadTexture(m_materials->back().texture, m_filepath + "/" + m_materials->back().textureName);
Anakin's avatar
Anakin committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311
		}
	}
}

void MshFile::analyseModlChunks(Model * dataDestination, std::list<ChunkHeader*>& chunkList)
{
	for (auto& it : chunkList)
	{
		// model type
		if (!strcmp("MTYP", it->name))
		{
			m_file.seekg(it->position);
			std::uint32_t tmp_type;
			m_file.read(F2V(tmp_type), sizeof(tmp_type));
312
			m_currentType = (ModelTyp)tmp_type;
Anakin's avatar
Anakin committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
		}

		// parent name
		else if (!strcmp("PRNT", it->name))
		{
			m_file.seekg(it->position);
			char* buffer = new char[it->size + 1];
			*buffer = { 0 };
			m_file.read(buffer, it->size);
			dataDestination->parent = buffer;
			delete[] buffer;
		}

		// model name
		else if (!strcmp("NAME", it->name))
		{
			m_file.seekg(it->position);
			char* buffer = new char[it->size + 1];
			*buffer = { 0 };
			m_file.read(buffer, it->size);
			dataDestination->name = buffer;
			delete[] buffer;
		}

		// render flags
		else if (!strcmp("FLGS", it->name))
		{
			m_file.seekg(it->position);
341
			m_file.read(F2V(m_currentRenderFlag), sizeof(m_currentRenderFlag));
Anakin's avatar
Anakin committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
		}

		// translation
		else if (!strcmp("TRAN", it->name))
		{
			float tmp_scale[3];
			float tmp_rotation[4];
			float tmp_trans[3];

			m_file.seekg(it->position);

			// read in the data
			for (int i = 0; i < 3; i++)
				m_file.read(F2V(tmp_scale[i]), sizeof(float));

			for (int i = 0; i < 4; i++)
				m_file.read(F2V(tmp_rotation[i]), sizeof(float));

			for (int i = 0; i < 3; i++)
				m_file.read(F2V(tmp_trans[i]), sizeof(float));

Anakin's avatar
Anakin committed
363
			// modify the matrix and quaternion
Anakin's avatar
Anakin committed
364 365
			dataDestination->m4x4Translation.scale(tmp_scale[0], tmp_scale[1], tmp_scale[2]);
			dataDestination->m4x4Translation.translate(tmp_trans[0], tmp_trans[1], tmp_trans[2]);
Anakin's avatar
Anakin committed
366 367
			dataDestination->quadRotation.setVector(QVector3D(tmp_rotation[0], tmp_rotation[1], tmp_rotation[2]));
			dataDestination->quadRotation.setScalar(tmp_rotation[3]);
Anakin's avatar
Anakin committed
368

Anakin's avatar
Anakin committed
369
			dataDestination->m4x4Translation = getParentMatrix(dataDestination->parent) * dataDestination->m4x4Translation;
Anakin's avatar
Anakin committed
370
			dataDestination->quadRotation = getParentRotation(dataDestination->parent) * dataDestination->quadRotation;
Anakin's avatar
Anakin committed
371 372 373 374 375
		}

		// geometry data
		else if (!strcmp("GEOM", it->name))
		{
Anakin's avatar
Anakin committed
376 377 378 379
			// don't get null, bone, shadowMesh and hidden mesh indices
			if (m_currentType == null || m_currentType == bone || m_currentType == shadowMesh || m_currentRenderFlag == 1)
				continue;

Anakin's avatar
Anakin committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
			// get all GEOM subchunks
			std::list<ChunkHeader*> tmp_geomChunks;
			loadChunks(tmp_geomChunks, it->position, it->size);

			// evaluate GEOM subchunks
			analyseGeomChunks(dataDestination, tmp_geomChunks);

			// clean up GEOM subchunks
			while (!tmp_geomChunks.empty())
			{
				ChunkHeader* cur = tmp_geomChunks.front();
				tmp_geomChunks.pop_front();
				delete cur;
			}
		}
	}
}

void MshFile::analyseGeomChunks(Model * dataDestination, std::list<ChunkHeader*>& chunkList)
{
	for (auto& it : chunkList)
	{
		// segment
		if (!strcmp("SEGM", it->name))
		{
			// get all SEGM subchunks
			std::list<ChunkHeader*> tmp_segmChunks;
			loadChunks(tmp_segmChunks, it->position, it->size);

			// evaluate SEGM subchunks
			analyseSegmChunks(dataDestination, tmp_segmChunks);

			// clean up SEGM subchunk
			while (!tmp_segmChunks.empty())
			{
				ChunkHeader* cur = tmp_segmChunks.front();
				tmp_segmChunks.pop_front();
				delete cur;
			}
		}

		// cloth
		else if (!strcmp("CLTH", it->name))
		{
			// get all CLTH subchunks
			std::list<ChunkHeader*> tmp_clthChunks;
			loadChunks(tmp_clthChunks, it->position, it->size);

			// evaluate CLTH subchunks
			analyseClthChunks(dataDestination, tmp_clthChunks);

			// clean up CLTH subchunks
			while (!tmp_clthChunks.empty())
			{
				ChunkHeader* cur = tmp_clthChunks.front();
				tmp_clthChunks.pop_front();
				delete cur;
			}
		}
	}
}

void MshFile::analyseSegmChunks(Model * dataDestination, std::list<ChunkHeader*>& chunkList)
{
	Segment* new_segment = new Segment;

	for (auto& it : chunkList)
	{
		// material index
		if (!strcmp("MATI", it->name))
		{
			m_file.seekg(it->position);
			m_file.read(F2V(new_segment->textureIndex), sizeof(new_segment->textureIndex));
		}

		// position list (vertex)
		else if (!strcmp("POSL", it->name))
		{
			readVertex(new_segment, it->position);
		}

		// normals
Anakin's avatar
Anakin committed
462
		else if (!strcmp("NRML", it->name))
Anakin's avatar
Anakin committed
463
		{
Anakin's avatar
Anakin committed
464 465 466
			std::uint32_t tmp_size;
			m_file.seekg(it->position);
			m_file.read(F2V(tmp_size), sizeof(tmp_size));
Anakin's avatar
Anakin committed
467

Anakin's avatar
Anakin committed
468 469 470
			if (tmp_size < new_segment->vertices.size())
			{
				emit sendMessage("WARNING: too less normals " + QString::number(tmp_size) + " < " + QString::number(new_segment->vertices.size()), 1);
Anakin's avatar
Anakin committed
471

Anakin's avatar
Anakin committed
472 473 474 475 476 477 478 479 480
				for (unsigned int i = new_segment->vertices.size(); i != tmp_size; i--)
					for (unsigned int j = 0; j < 3; j++)
						new_segment->vertices[i - 1].normal[j] = 0;
			}
			else if (tmp_size > new_segment->vertices.size())
			{
				emit sendMessage("WARNING: too many normals " + QString::number(tmp_size) + " > " + QString::number(new_segment->vertices.size()), 1);
				tmp_size = new_segment->vertices.size();
			}
Anakin's avatar
Anakin committed
481

Anakin's avatar
Anakin committed
482 483 484
			for (unsigned int i = 0; i < tmp_size; i++)
				for (unsigned int j = 0; j < 3; j++)
					m_file.read(F2V(new_segment->vertices[i].normal[j]), sizeof(float));
Anakin's avatar
Anakin committed
485 486

		}
Anakin's avatar
Anakin committed
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540

		// uv
		else if (!strcmp("UV0L", it->name))
		{
			readUV(new_segment, it->position);
		}

		// polygons (indices into vertex/uv list)
		else if (!strcmp("STRP", it->name))
		{

			// jump to the data section and read the size;
			std::uint32_t tmp_size;
			m_file.seekg(it->position);
			m_file.read(F2V(tmp_size), sizeof(tmp_size));

			int highBitCount(0);
			QVector<GLuint> tmp_buffer;

			for (unsigned int i = 0; i < tmp_size; i++)
			{
				// ReadData
				std::uint16_t tmp_value;
				m_file.read(F2V(tmp_value), sizeof(tmp_value));

				// Check if highbit is set
				if (tmp_value >> 15)
				{
					highBitCount++;
					// remove the high bit, to get the actually value
					tmp_value = (std::uint16_t(tmp_value << 1) >> 1);
				}

				// save data
				tmp_buffer.push_back((GLuint)tmp_value);

				// if the last 2 highBits are set, it was a new poly
				if (highBitCount == 2)
				{
					// reset highBitCount
					highBitCount = 0;

					if (tmp_buffer.size() == 5)
					{
						for (size_t i = 0; i < 3; i++)
							new_segment->indices.push_back(tmp_buffer.takeFirst());
					}
					else if (tmp_buffer.size() > 5)
					{
						unsigned int tmp_multiPolySize = tmp_buffer.size() - 2;
						// for every triangle of the multi polygon..
						for (unsigned int tri = 0; tri < tmp_multiPolySize - 2; tri++)
							// ..calculate the edge indices
							for (int triEdge = 0; triEdge < 3; triEdge++)
Anakin's avatar
Anakin committed
541
								new_segment->indices.push_back(tmp_buffer[(tri + triEdge - ((tri % 2) * (triEdge - 1) * 2))]);
Anakin's avatar
Anakin committed
542 543

						tmp_buffer.remove(0, tmp_multiPolySize);
Anakin's avatar
Anakin committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
					}

				}	// if 2 high bits are set

			}	// for all values

			// save the last polygon (no 2 high bit followed)
			if (tmp_buffer.size() == 3)
			{
				for (size_t i = 0; i < 3; i++)
					new_segment->indices.push_back(tmp_buffer.takeFirst());
			}
			else if (tmp_buffer.size() > 3)
			{
				unsigned int tmp_multiPolySize = tmp_buffer.size();
				// for every triangle of the multi polygon..
				for (unsigned int tri = 0; tri < tmp_multiPolySize - 2; tri++)
					// ..calculate the edge indices
					for (int triEdge = 0; triEdge < 3; triEdge++)
Anakin's avatar
Anakin committed
563
						new_segment->indices.push_back(tmp_buffer[(tri + triEdge - ((tri % 2) * (triEdge - 1) * 2))]);
Anakin's avatar
Anakin committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
			}
		}
	}

	dataDestination->segmList.push_back(new_segment);
}

void MshFile::analyseClthChunks(Model * dataDestination, std::list<ChunkHeader*>& chunkList)
{
	Segment* new_segment = new Segment;

	for (auto& it : chunkList)
	{
		// texture name
		if (!strcmp("CTEX", it->name))
		{
			// read the texture name
			m_file.seekg(it->position);
			char* buffer = new char[it->size + 1];
			*buffer = { 0 };
			m_file.read(buffer, it->size);

Anakin's avatar
Anakin committed
586 587 588 589
			
			m_materials->push_back(Material());
			m_materials->back().name = "Cloth Material";
			m_materials->back().textureName = QString(buffer);
Anakin's avatar
Anakin committed
590

Anakin's avatar
Anakin committed
591
			m_materials->back().shininess = 10;
592

Anakin's avatar
Anakin committed
593 594 595 596
			if (!m_materials->back().textureName.isEmpty())
				loadTexture(m_materials->back().texture, m_filepath + "/" + m_materials->back().textureName);

			new_segment->textureIndex = m_materials->size() - 1;
Anakin's avatar
Anakin committed
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661

			delete[] buffer;
		}

		// position list (vertex)
		else if (!strcmp("CPOS", it->name))
		{
			readVertex(new_segment, it->position);
		}

		// uv 
		else if (!strcmp("CUV0", it->name))
		{
			readUV(new_segment, it->position);
		}

		// triangles (indices into vertex/uv list)
		else if (!strcmp("CMSH", it->name))
		{
			// jump to the data section and read the size;
			std::uint32_t tmp_size;
			m_file.seekg(it->position);
			m_file.read(F2V(tmp_size), sizeof(tmp_size));

			// for every triangle..
			for (unsigned int i = 0; i < tmp_size * 3; i++)
			{
				std::uint32_t tmp_value;
				m_file.read(F2V(tmp_value), sizeof(std::uint32_t));

				new_segment->indices.push_back((GLuint)tmp_value);
			}
		}
	}

	dataDestination->segmList.push_back(new_segment);
}

void MshFile::readVertex(Segment * dataDestination, std::streampos position)
{
	std::uint32_t tmp_size;
	m_file.seekg(position);
	m_file.read(F2V(tmp_size), sizeof(tmp_size));

	for (unsigned int i = 0; i < tmp_size; i++)
	{
		float tmp[3];
		for (unsigned int j = 0; j < 3; j++)
			m_file.read(F2V(tmp[j]), sizeof(float));
		
		VertexData new_data;
		new_data.position = QVector3D(tmp[0], tmp[1], tmp[2]);

		dataDestination->vertices.push_back(new_data);
	}
}

void MshFile::readUV(Segment * dataDestination, std::streampos position)
{
	std::uint32_t tmp_size;
	m_file.seekg(position);
	m_file.read(F2V(tmp_size), sizeof(tmp_size));

	if (tmp_size < dataDestination->vertices.size())
	{
662
		emit sendMessage("WARNING: too less UVs " + QString::number(tmp_size) + " < " + QString::number(dataDestination->vertices.size()),1);
Anakin's avatar
Anakin committed
663

664 665 666
		for (unsigned int i = dataDestination->vertices.size(); i != tmp_size; i--)
			for (unsigned int j = 0; j < 2; j++)
				dataDestination->vertices[i - 1].texCoord[j] = 0;
Anakin's avatar
Anakin committed
667 668 669
	}
	else if (tmp_size > dataDestination->vertices.size())
	{
670
		emit sendMessage("WARNING: too many UVs " + QString::number(tmp_size) + " > " + QString::number(dataDestination->vertices.size()), 1);
Anakin's avatar
Anakin committed
671 672 673 674 675 676
		tmp_size = dataDestination->vertices.size();
	}

	for (unsigned int i = 0; i < tmp_size; i++)
		for (unsigned int j = 0; j < 2; j++)
			m_file.read(F2V(dataDestination->vertices[i].texCoord[j]), sizeof(float));
Anakin's avatar
Anakin committed
677 678
}

Anakin's avatar
Anakin committed
679
void MshFile::loadTexture(QOpenGLTexture *& destination, QString filepath)
680 681
{
	bool loadSuccess(false);
C-Fu's avatar
C-Fu committed
682
	QImage img = loadTga(filepath, loadSuccess);
683 684

	if (!loadSuccess)
C-Fu's avatar
C-Fu committed
685
	{
686
		emit sendMessage("WARNING: texture not found or corrupted: " + m_materials->back().name, 1);
Anakin's avatar
Anakin committed
687 688 689
		
		img = QImage(1, 1, QImage::Format_RGB32);
		img.fill(QColor(m_materials->back().diffuseColor[0] * 255, m_materials->back().diffuseColor[1] * 255, m_materials->back().diffuseColor[2] * 255));
Anakin's avatar
Anakin committed
690
		m_materials->back().textureName += " *";
C-Fu's avatar
C-Fu committed
691 692
	}
	
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
	// Load image to OglTexture
	QOpenGLTexture* new_texture = new QOpenGLTexture(img.mirrored());

	// Set nearest filtering mode for texture minification
	new_texture->setMinificationFilter(QOpenGLTexture::Nearest);

	// Set bilinear filtering mode for texture magnification
	new_texture->setMagnificationFilter(QOpenGLTexture::Linear);

	// Wrap texture coordinates by repeating
	// f.ex. texture coordinate (1.1, 1.2) is same as (0.1, 0.2)
	new_texture->setWrapMode(QOpenGLTexture::Repeat);

	destination = new_texture;
}

Anakin's avatar
Anakin committed
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
QMatrix4x4 MshFile::getParentMatrix(std::string parent) const
{
	QMatrix4x4 matrix;

	for (auto& it : *m_models)
	{
		if (!strcmp(parent.c_str(), it->name.c_str()))
		{
			matrix = getParentMatrix(it->parent) * it->m4x4Translation;
			break;
		}
	}

	return matrix;
}
Anakin's avatar
Anakin committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739

QQuaternion MshFile::getParentRotation(std::string parent) const
{
	QQuaternion rotation;

	for (auto& it : *m_models)
	{
		if (!strcmp(parent.c_str(), it->name.c_str()))
		{
			rotation = getParentRotation(it->parent) * it->quadRotation;
			break;
		}
	}

	return rotation;
}