Exemplo n.º 1
0
void ModelMd2::animate(const float dt)
{
	if (m_isAnimationOn)
	{
		m_time += dt * m_animationSpeed;
	}

	float fframe = m_animations[m_currentAnimationName].start + m_time * 45;
	int f1 = (int) (fframe);
	int f2 = f1 + 1;
	float int_time = fframe - f1;

	if (f2 > m_animations[m_currentAnimationName].start + m_animations[m_currentAnimationName].end)
	{
		if (f1 > m_animations[m_currentAnimationName].start + m_animations[m_currentAnimationName].end)
		{
			f1 = m_animations[m_currentAnimationName].start + m_animations[m_currentAnimationName].end;

			if (m_isAnimationLooped)
			{
				m_time = 0;
			}
			else
			{
				m_isAnimationOn = false;
			}
		}
		f2 = m_animations[m_currentAnimationName].start;
	}


	int v_i = 0;
	for (int p_i = 0; p_i < m_numTriangles; p_i++)
	{
		// Vertex 1
		m_pVertexArray[v_i] = interpolate(m_pVertices[f1 * m_numVertices + m_pTriangles[p_i].vertIdx[2]], m_pVertices[f2 * m_numVertices + m_pTriangles[p_i].vertIdx[2]], int_time);

		// Vertex 2
		m_pVertexArray[v_i + 1] = interpolate(m_pVertices[ f1 * m_numVertices + m_pTriangles[p_i].vertIdx[1] ], m_pVertices[f2 * m_numVertices + m_pTriangles[p_i].vertIdx[1]], int_time);

		// Vertex 3
		m_pVertexArray[v_i + 2] = interpolate(m_pVertices[ f1 * m_numVertices + m_pTriangles[p_i].vertIdx[0] ], m_pVertices[f2 * m_numVertices + m_pTriangles[p_i].vertIdx[0]], int_time);

		//tangentArray[v_i] = tangentArray[v_i + 1] = tangentArray[v_i + 2] = interpolate(tangents[ f1 * numVertices + triangles[p_i].vertIdx[0] ], tangents[f2 * numVertices + triangles[p_i].vertIdx[0]], int_time);
		vec3 tangent, bitangent;
		calculateTangent(m_pVertexArray[v_i], m_pVertexArray[v_i + 1], m_pVertexArray[v_i + 2], m_pTexcoordArray[v_i], m_pTexcoordArray[v_i + 1], m_pTexcoordArray[v_i + 2], tangent, bitangent);
		m_pTangentArray[v_i] = m_pTangentArray[v_i + 1] = m_pTangentArray[v_i + 2] = tangent;

		v_i += 3;
	}
}
Exemplo n.º 2
0
void EditPointTool::setupAngleHelperFromHoverObject()
{
	Q_ASSERT(hover_object->getType() == Object::Path);
	
	angle_helper->clearAngles();
	bool forward_ok = false;
	auto part = hover_object->asPath()->findPartForIndex(hover_point);
	MapCoordF forward_tangent = part->calculateTangent(hover_point, false, forward_ok);
	if (forward_ok)
		angle_helper->addAngles(-forward_tangent.angle(), M_PI/2);
	bool backward_ok = false;
	MapCoordF backward_tangent = part->calculateTangent(hover_point, true, backward_ok);
	if (backward_ok)
		angle_helper->addAngles(-backward_tangent.angle(), M_PI/2);
	
	if (forward_ok && backward_ok)
	{
		double angle = (-backward_tangent.angle() - forward_tangent.angle()) / 2;
		angle_helper->addAngle(angle);
		angle_helper->addAngle(angle + M_PI/2);
		angle_helper->addAngle(angle + M_PI);
		angle_helper->addAngle(angle + 3*M_PI/2);
	}
}
Exemplo n.º 3
0
bool ModelMd2::load(const char* filename, TextureDirectory& textureDirectory, const bool justData)
{
	std::ifstream file(filename, std::ios::in | std::ios::binary);

	if (file.fail())
	{
		return false;
	}

	int fileSize;
	file.seekg(0, std::ios::end);
	fileSize = file.tellg();
	file.seekg(0, std::ios::beg);

	char* buffer = new char[fileSize];
	file.read((char*) buffer, fileSize);
	file.close();

	// header
	Header* header = reinterpret_cast<Header*>(buffer);
	if ((header->magic != MD2_MAGIC_NUM) && (header->version != 8))
	{
		file.close();
		return false;
	}

	m_numTexCoords = header->numTexCoords;
	m_numVertices  = header->numVertices;
	m_numTriangles = header->numTriangles;

	int numFrames = header->numFrames;

	// texcoords
	TexcoordMd2* tmptex = new TexcoordMd2[m_numTexCoords];
	memcpy(tmptex, &buffer[header->offsetTexCoords], m_numTexCoords * sizeof(TexcoordMd2));

	m_pTexcoords = new texCoord[m_numTexCoords];
	for (int i = 0; i < m_numTexCoords; i++)
	{
		m_pTexcoords[i].u = (float) tmptex[i].s / header->skinWidth;
		m_pTexcoords[i].v = -(float) tmptex[i].t / header->skinHeight;
	}
	delete[] tmptex;
	m_pTexcoordArray = new texCoord[m_numTriangles * 3];							// one vertex can have more texture coordinates (eg. a corner of a cube)

	// triangles
	m_pTriangles = new TriangleMd2[m_numTriangles];
	memcpy(m_pTriangles, &buffer[header->offsetTriangles], m_numTriangles * sizeof(TriangleMd2));

	// vertices
	m_pVertices = new vec3[numFrames * m_numVertices];
	m_pVertexArray = new vec3[m_numTriangles * 3];

	// normal vectors
	m_pNormals = new vec3[numFrames * m_numVertices];
	m_pNormalArray = new vec3[m_numTriangles * 3];

	// tangent vectors
	m_pTangents = new vec3[numFrames * m_numVertices];
	m_pTangentArray = new vec3[m_numTriangles * 3];

	// precalculated normal vectors
	float anorms[162][3] =
	{
#include "anorms.h"
	};

	FrameMd2* frame;
	std::string lastname;
	std::string filteredLastname;

	// animációk, vertexek kiolvasása, beállítása
	for (int i = 0; i < numFrames; i++)
	{
		frame = (FrameMd2*) &buffer[ header->offsetFrames + i * header->frameSize ];

		// az azonos nevű frameket egy animációba pakoljuk (pl. stand_1, stand_2 ...)
		if (lastname == "" || strncmp(lastname.c_str(), frame->name, lastname.size()) > 0)
		{
			lastname = frame->name;

			if (lastname[lastname.size() - 2] == '0')			// eg. stand01
			{
				lastname.resize(lastname.size() - 2);
			}
			else
			{
				lastname.resize(lastname.size() - 1);
			}

			filteredLastname = lastname;
			filteredLastname.resize( std::remove_if(filteredLastname.begin(), filteredLastname.end(), filter) - filteredLastname.begin() );

			m_numAnims++;
			m_animations[filteredLastname].start = i;
			m_animations[filteredLastname].end   = -1;
		}
		m_animations[filteredLastname].end++;

		for (int j = 0; j < m_numVertices; j++)
		{
			m_pVertices[ i * m_numVertices + j ].x = (frame->verts[j].v[0] * frame->scale[0]) + frame->translate[0];
			m_pVertices[ i * m_numVertices + j ].y = (frame->verts[j].v[1] * frame->scale[1]) + frame->translate[1];
			m_pVertices[ i * m_numVertices + j ].z = (frame->verts[j].v[2] * frame->scale[2]) + frame->translate[2];

			m_pVertices[ i * m_numVertices + j ] = transformVector(m_pVertices[ i * m_numVertices + j ], vec3(0.0f), vec3(90, 90, 0));

			const float* normals = anorms[frame->verts[j].normalIndex];
			m_pNormals[ i * m_numVertices + j ] = vec3(normals[0], normals[1], normals[2]);
		}


		// calculate tangent vectors
		vec3 tangent, bitangent;
		for (int j = 0; j < m_numTriangles; j++)
		{
			calculateTangent(m_pVertices[ i * m_numVertices + m_pTriangles[j].vertIdx[2] ], m_pVertices[ i * m_numVertices + m_pTriangles[j].vertIdx[1] ], m_pVertices[ i * m_numVertices + m_pTriangles[j].vertIdx[0] ],
			                 m_pTexcoords[ m_pTriangles[j].texIdx[2] ], m_pTexcoords[ m_pTriangles[j].texIdx[1] ], m_pTexcoords[ m_pTriangles[j].texIdx[0] ],
			                 tangent, bitangent);

			m_pTangents[ i * m_numVertices + m_pTriangles[j].vertIdx[0] ] = m_pTangents[ i * m_numVertices + m_pTriangles[j].vertIdx[1] ] = m_pTangents[ i * m_numVertices + m_pTriangles[j].vertIdx[2] ] = tangent;
		}
	}

	int v_i = 0;
	for (int j = 0; j < m_numTriangles; j++)
	{
		m_pNormalArray[v_i] = m_pNormals[ m_pTriangles[j].vertIdx[2] ];
		m_pNormalArray[v_i + 1] = m_pNormals[ m_pTriangles[j].vertIdx[1] ];
		m_pNormalArray[v_i + 2] = m_pNormals[ m_pTriangles[j].vertIdx[0] ];

		m_pTexcoordArray[v_i] = m_pTexcoords[ m_pTriangles[j].texIdx[2] ];
		m_pTexcoordArray[v_i + 1] = m_pTexcoords[ m_pTriangles[j].texIdx[1] ];
		m_pTexcoordArray[v_i + 2] = m_pTexcoords[ m_pTriangles[j].texIdx[0] ];

		v_i += 3;
	}

	// load textures
	if (!justData)
	{
		// the name of the texture is usually not stored n the file
		char textureNameTmp[64];
		memcpy(textureNameTmp, &buffer[header->offsetSkins], 64 * sizeof(char));


		std::string locationtemp = utils::file::getDir(std::string(filename));
		std::string textureName(textureNameTmp);
		utils::toLowerCase(textureName);

		std::string textemp = locationtemp + textureName;

		if (textemp.length() > locationtemp.length())
		{
			// texture map
			loadTexture((char*) textemp.c_str(), m_decalMap, textureDirectory);

			// normalheight map
			std::string nhtemp = locationtemp + utils::file::getFileName(textemp) + "_nh.png";

			if (!loadTexture((char*) nhtemp.c_str(), m_normalHeightMap, textureDirectory))
			{
				// no nh texture -> use the blank texture
				m_normalHeightMap = 0;
			}
		}
	}

	delete[] buffer;

	return true;
}