Esempio n. 1
0
//LAYOUT BINOBJSB -------------------------------------------------------------------------------->
//Word: nameLength
    //buffer: name
//DWord : #vert
    //vector3: pos, vector3: norm, vector2: tex, DWORD tetra, Vector3 barycentricCoords     : vertex
//DWord : #triangles
    //DWord, DWord, DWord : triangle
//...
bool BinCooker::CookSB()
{
    UserStream stream(m_OutputName.c_str(), false);

    ModelMesh<VertexPNTSoftbody>* mesh = m_pModel->GetSoftbodyDrawMesh();

    //store Name
    stream.storeWord(static_cast<WORD>(mesh->GetName().size()));
    const char* name = mesh->GetName().c_str();
    stream.storeBuffer(name, mesh->GetName().size());

    //store Vertices
    stream.storeDword(mesh->GetNumVertices());
    const vector<VertexPNTSoftbody>& v = mesh->GetVertices();
    for (unsigned int i = 0; i < mesh->GetNumVertices(); ++i)
    {
        stream.storeVector3(v[i].position);
        stream.storeVector3(v[i].normal);
        stream.storeVector2(v[i].tex);
        stream.storeDword(v[i].tetra);
        stream.storeVector3(v[i].barycentricCoords);
    }

    //store Indices
    stream.storeDword(mesh->GetNumIndices() / 3);
    const vector<DWORD>& ind = mesh->GetIndices();
    for (DWORD i = 0; i < mesh->GetNumIndices(); ++i)
    {
        stream.storeDword(ind[i]);
    }
    return true;
}
Esempio n. 2
0
void ModelImporter_Impl::_CalculateModelCenter(ModelScene* scene)
{
	vec3f min(9999.9f, 9999.9f, 9999.9f);
	vec3f max(-9999.9f, -9999.9f, -9999.9f);

	size_t j = 0, c;
	for (unsigned int i = 0; i < scene->numMeshes; ++i)
	{
		ModelMesh* mesh = &scene->Meshes[i];
		auto indices = mesh->GetIndices();

		vertex_attribute* vAttribs = new vertex_attribute[4];
		vAttribs[0] = { LOC_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, 0 };
		vAttribs[1] = { LOC_NORMAL, 3, GL_FLOAT, GL_TRUE, sizeof(float) * 3, sizeof(float) * mesh->numVertices * 3 };
		vAttribs[2] = { LOC_TEXCOORD0, 2, GL_FLOAT, GL_TRUE, sizeof(float) * 2, sizeof(float) * mesh->numVertices * 6 };
		vAttribs[3] = { LOC_TEXCOORD1, 3, GL_FLOAT, GL_TRUE, sizeof(float) * 3, sizeof(float) * mesh->numVertices * 8 };

		float* verts = new float[mesh->numVertices * 11];

		unsigned int idx = 0;
		unsigned int nVerts = mesh->numVertices;
		for (j = 0; j < nVerts; ++j)// FML
		{
			vec3f vert = mesh->Vertices[j];
			verts[j * 3 + 0] = vert.x;
			verts[j * 3 + 1] = vert.y;
			verts[j * 3 + 2] = vert.z;

			vert *= scene->Scale;
			if (vert.x < min.x)
				min.x = vert.x;

			if (vert.y < min.y)
				min.y = vert.y;

			if (vert.z < min.z)
				min.z = vert.z;

			if (vert.x > max.x)
				max.x = vert.x;

			if (vert.y > max.y)
				max.y = vert.y;

			if (vert.z > max.z)
				max.z = vert.z;

			idx += 3;
		}

		for (c = 0; c < (int)mesh->numNormals; ++c)
		{
			verts[j * 3 + 0] = mesh->Normals[c].x;
			verts[j * 3 + 1] = mesh->Normals[c].y;
			verts[j * 3 + 2] = mesh->Normals[c].z;
			++j;
			idx += 3;
		}

		uint32_t nTexCoordsPV = mesh->numTexCoordsPerVertex;
		uint32_t offs = j * 3;
		for(c = 0; c < nVerts; ++c)
		{
			if(nTexCoordsPV > 0)
			{
				verts[offs + (c * 2)] = mesh->TexCoords[0][c].x;
				verts[offs + (c * 2 + 1)] = mesh->TexCoords[0][c].y;
			}

			else
			{
				verts[offs + (c * 2)] = 0.0f;
				verts[offs + (c * 2 + 1)] = 0.0f;
			}

			idx += 2;
			++j;
		}

		for( c = 0; c < mesh->numTangents; ++c )
		{
			verts[idx++] = mesh->Tangents[c].x;
			verts[idx++] = mesh->Tangents[c].y;
			verts[idx++] = mesh->Tangents[c].z;
			++j;
		}

		mesh->GeometryBufferId = createIndexedInterleavedArray(vAttribs, 4, verts, sizeof(float) * mesh->numVertices * 11, BUFFER_STATIC, \
			indices, mesh->numIndices);

		delete[] verts;
		delete[] vAttribs;
	}

	float xSpan = max.x - min.x;
	xSpan *= 0.5;
	float centerX = min.x + xSpan;

	float ySpan = max.y - min.y;
	ySpan *= 0.5;
	float centerY = min.y + ySpan;

	float zSpan = max.z - min.z;
	zSpan *= 0.5;
	float centerZ = min.z + zSpan;

	scene->ModelCenter.set(centerX, centerY, centerZ);
}