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();
    }
}
bool DsView::loadFile(QString name){
  if(_scene.LoadFile(name.latin1())){
   
   mx=my=mz=0.0;
   int nVerticeTotal = 0;
   _max=0;
   
   for(int i=0;i<_scene.GetMeshCount();i++){
    LMesh * mesh = _scene.GetMesh(i);
    nVerticeTotal =+ mesh->GetVertexCount();
    for(int j=0;j<mesh->GetVertexCount();j++){
     LVector3 v = mesh->GetVertex(j);
     mx =+ v.x;
     my =+ v.y;
     mz =+ v.z;
    }
    
   }
   
   mx = mx/nVerticeTotal;
   #ifdef MYDEBUG
   qDebug(QString::number(mx).latin1());
   qDebug(QString::number(nVerticeTotal).latin1());
   #endif
   my = my/nVerticeTotal;
   #ifdef MYDEBUG
   qDebug(QString::number(my).latin1());
   #endif
   mz = mz/nVerticeTotal;
   #ifdef MYDEBUG
   qDebug(QString::number(mz).latin1());
   #endif
   for(int i=0;i<_scene.GetMeshCount();i++){
    LMesh * mesh = _scene.GetMesh(i);
    for(int j=0;j<mesh->GetVertexCount();j++){
     LVector3 v = mesh->GetVertex(j);
     _max = max(max(max(abs(v.x-mx),abs(v.y-my)),abs(v.z-mz)),_max);
    }
   }
   
   _ortho = _max;
   updateGL();
   
   return true;
  }else{ 
   
   return false;
  }
  
}
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));
		}
	}
}
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));
		}
	}
}
Exemple #5
0
void L3DS::ReadMesh(const LChunk &parent)
{
    unsigned short count, i;
    LVector4 p;
    LMatrix4 m;
    LVector2 t;
    p.w = 1.0f;
    LMesh mesh;
    mesh.SetName(m_objName);
    GotoChunk(parent);
    LChunk chunk = ReadChunk();
    while (chunk.end <= parent.end)
    {
        switch (chunk.id)
        {
        case TRI_VERTEXLIST:
            count = ReadShort();
            mesh.SetVertexArraySize(count);
            for (i=0; i < count; i++)
            {
                p.x = ReadFloat();
                p.y = ReadFloat();
                p.z = ReadFloat();
                mesh.SetVertex(p, i);
            }
            break;
        case TRI_FACEMAPPING:
            count = ReadShort();
            if (mesh.GetVertexCount() == 0)
                mesh.SetVertexArraySize(count);
            for (i=0; i < count; i++)
            {
                t.x = ReadFloat();
                t.y = ReadFloat();
                mesh.SetUV(t, i);
            }
            break;
        case TRI_FACELIST:
            ReadFaceList(chunk, mesh);
            break;
        case TRI_MATRIX:
            m._11 = ReadFloat();
            m._12 = ReadFloat();
            m._13 = ReadFloat();

            m._21 = ReadFloat();
            m._22 = ReadFloat();
            m._23 = ReadFloat();

            m._31 = ReadFloat();
            m._32 = ReadFloat();
            m._33 = ReadFloat();

            m._41 = ReadFloat();
            m._42 = ReadFloat();
            m._43 = ReadFloat();

            m._14 = 0.0f;
            m._24 = 0.0f;
            m._34 = 0.0f;
            m._44 = 1.0f;

            mesh.SetMatrix(m);
            break;
        default:
            break;
        }
        SkipChunk(chunk);
        if (chunk.end >= parent.end)
            break;
        chunk = ReadChunk();
    }
    m_meshes.push_back(mesh);
}
Exemple #6
0
void L3DS::ReadMesh(const LChunk &parent)
{

//ErrorMsg("RND:: Ok, try load mesh...\n");

    unsigned short count, i;
    LVector4 p;
    LMatrix4 m;
    LVector2 t;
    p.w = 1.0f;
    LMesh mesh;
    mesh.SetName(m_objName);
    GotoChunk(parent);
    LChunk chunk = ReadChunk();
    while (chunk.end <= parent.end)
    {
//ErrorMsg("RND:: Ochunk.end <= parent.end\n");

        switch (chunk.id)
        {
        case TRI_VERTEXLIST:
//ErrorMsg("RND:: case TRI_VERTEXLIST\n");
            count = ReadShort();
            mesh.SetVertexArraySize(count);
            for (i=0; i < count; i++)
            {
                p.x = ReadFloat();
                p.y = ReadFloat();
                p.z = ReadFloat();
                mesh.SetVertex(p, i);
            }
            break;
        case TRI_FACEMAPPING:
//ErrorMsg("RND:: case TRI_FACEMAPPING\n");
            count = ReadShort();
//          printf("RND:: ReadShort");
            if (count == 0 ) break;
            if (mesh.GetVertexCount() == 0)
                mesh.SetVertexArraySize(count);
                printf("count: %d",count);
            for (i=0; i < count; i++)
            {
                t.x = ReadFloat();
                t.y = ReadFloat();
                mesh.SetUV(t, i);
//                printf("RND:: set: %f %f",t.x,t.y);
            }
            break;
        case TRI_FACELIST:
//ErrorMsg("RND:: case TRI_FACELIST\n");
            ReadFaceList(chunk, mesh);
            break;
        case TRI_MATRIX:
//ErrorMsg("RND:: case TRI_MATRIX\n");
            m._11 = ReadFloat();
            m._12 = ReadFloat();
            m._13 = ReadFloat();

            m._21 = ReadFloat();
            m._22 = ReadFloat();
            m._23 = ReadFloat();

            m._31 = ReadFloat();
            m._32 = ReadFloat();
            m._33 = ReadFloat();

            m._41 = ReadFloat();
            m._42 = ReadFloat();
            m._43 = ReadFloat();

            m._14 = 0.0f;
            m._24 = 0.0f;
            m._34 = 0.0f;
            m._44 = 1.0f;

            mesh.SetMatrix(m);
            break;
        default:
            break;
        }
        SkipChunk(chunk);
        if (chunk.end >= parent.end)
            break;
        chunk = ReadChunk();
    }
    m_meshes.push_back(mesh);
}