Exemple #1
0
void L3DS::ReadFaceList(const LChunk &chunk, LMesh &mesh)
{
    // variables 
    unsigned short count, t;    
    uint i;
    LTri tri;
    LChunk ch;
    char str[20];
    //uint mat;

    // consistency checks
    if (chunk.id != TRI_FACELIST)
    {
        ErrorMsg("L3DS::ReadFaceList - internal error: wrong chunk passed as parameter");
        return;
    }
    GotoChunk(chunk);
    tri.smoothingGroups = 1;
    // read the number of faces
    count = ReadShort();
    mesh.SetTriangleArraySize(count);
    for (i=0; i<count; i++)
    {
        tri.a = ReadShort();
        tri.b = ReadShort();
        tri.c = ReadShort();
        ReadShort();
        mesh.SetTri(tri, i);
    }
    // now read the optional chunks
    ch = ReadChunk();
    int mat_id;
    while (ch.end <= chunk.end)
    {
        switch (ch.id)
        {
        case TRI_MAT_GROUP:
            ReadASCIIZ(str, 20);
			mat_id=0;
			if (FindMaterial(str)!=NULL)
				mat_id = FindMaterial(str)->GetID();
            mesh.AddMaterial(mat_id);
            count = ReadShort();
            for (i=0; i<count; i++) 
            {
                t = ReadShort();
                mesh.GetTri(t).materialId = mat_id;
            }                
            break;
        case TRI_SMOOTH_GROUP:
            for (i=0; i<mesh.GetTriangleCount(); i++)
                mesh.GetTri(i).smoothingGroups = (ulong) ReadInt();
            break;
        }
        SkipChunk(ch);
        ch = ReadChunk();
    }
}
void Scene::AddTexturedObject(const std::string fname, Material* material, Shapes &objects, const std::string textName, const Point &ofs) const
{
	size_t index = Shape::GetUniqueID();

	// —читывание меша из файла
	L3DS *l3ds = new L3DS(fname.c_str());
	if(!l3ds || !l3ds->GetMeshCount())
		throw Error("Error in loading extern files");

	for(int i = 0; i<l3ds->GetMeshCount(); i++) {
		LMesh *mesh = l3ds->GetMesh(i);

		Texture *texture = new Texture(textName.c_str());

		for(int j = 0; j<mesh->GetTriangleCount(); j++) {
			LTriangle tr = mesh->GetTriangle(j);

			Point a(mesh->GetVertex(tr.a).x, mesh->GetVertex(tr.a).y, mesh->GetVertex(tr.a).z);
			Point b(mesh->GetVertex(tr.b).x, mesh->GetVertex(tr.b).y, mesh->GetVertex(tr.b).z);
			Point c(mesh->GetVertex(tr.c).x, mesh->GetVertex(tr.c).y, mesh->GetVertex(tr.c).z);	

			TextureCoords tA(mesh->GetUV(tr.a).u, mesh->GetUV(tr.a).v);
			TextureCoords tB(mesh->GetUV(tr.b).u, mesh->GetUV(tr.b).v);
			TextureCoords tC(mesh->GetUV(tr.c).u, mesh->GetUV(tr.c).v);

			tA.InvertU();
			tB.InvertU();
			tC.InvertU();

			tA*=8;
			tB*=8;
			tC*=8;

			if (Triangle::IsValidTrangle(a,b,c))
				objects.push_back(new Triangle(a-ofs, b-ofs, c-ofs, material, index, texture, tA, tB, tC));
		}
	}
}
void Scene::AddObject(const std::string& fname,Material* material, Shapes &objects, const Point &ofs) const
{
	size_t index = Shape::GetUniqueID();

	// —читывание меша из файла
	L3DS *l3ds = new L3DS(fname.c_str());
	if(!l3ds || !l3ds->GetMeshCount())
		throw Error("Error in loading extern files");

	for(int i = 0; i<l3ds->GetMeshCount(); i++) {
		LMesh *mesh = l3ds->GetMesh(i);

		for(int j = 0; j<mesh->GetTriangleCount(); j++) {
			LTriangle tr = mesh->GetTriangle(j);

			Point a(mesh->GetVertex(tr.a).x, mesh->GetVertex(tr.a).y, mesh->GetVertex(tr.a).z);
			Point b(mesh->GetVertex(tr.b).x, mesh->GetVertex(tr.b).y, mesh->GetVertex(tr.b).z);
			Point c(mesh->GetVertex(tr.c).x, mesh->GetVertex(tr.c).y, mesh->GetVertex(tr.c).z);	

			if (Triangle::IsValidTrangle(a,b,c))
				objects.push_back(new Triangle(a-ofs, b-ofs, c-ofs, material, index));
		}
	}
}