/// <summary> /// Constructs an instance of the <see cref="Triangle"/> class. The points /// must be defined in counter-clockwise order /// </summary> /// <param name="p0">First vertex</param> /// <param name="p1">Second vertex</param> /// <param name="p2">Third vertex</param> /// <param name="mat">The material for the triangle</param> public Triangle(Vertex p0, Vertex p1, Vertex p2, Material mat) { _p0 = p0; _p1 = p1; _p2 = p2; _normal = Vector3.Cross((_p2.Position - _p1.Position), (_p0.Position - _p1.Position)); _area = _normal.Magnitude; _normal = _normal.Normalized; Material = mat; }
/// <summary> /// Does the conversion /// </summary> /// <param name="data">The obj data to convert</param> /// <returns>The constructed Scene</returns> public static Scene Convert(ObjData data) { Scene s = new Scene(); Dictionary<string, Material> materials = new Dictionary<string, Material>(); Dictionary<string, Bitmap> bitmaps = new Dictionary<string, Bitmap>(); foreach (var mat in data.materials) { LoadBitmap(mat.AlphaTextureMap, bitmaps); LoadBitmap(mat.AmbientTextureMap, bitmaps); LoadBitmap(mat.BumpMap, bitmaps); LoadBitmap(mat.DiffuseTextureMap, bitmaps); LoadBitmap(mat.DisplacementMap, bitmaps); LoadBitmap(mat.SpecularCoefficientMap, bitmaps); LoadBitmap(mat.SpecularTextureMap, bitmaps); Material m = new Material(); m.AlphaMap = bitmaps[mat.AlphaTextureMap]; m.AmbientMap = bitmaps[mat.AmbientTextureMap]; m.BumpMap = bitmaps[mat.AmbientTextureMap]; m.DiffuseMap = bitmaps[mat.AmbientTextureMap]; m.DisplacementMap = bitmaps[mat.AmbientTextureMap]; m.SpecularCoefficientMap = bitmaps[mat.AmbientTextureMap]; m.SpecularMap = bitmaps[mat.AmbientTextureMap]; m.Texture = bitmaps[mat.DiffuseTextureMap]; m.AmbientColor = mat.Ambient; m.DiffuseColor = mat.Diffuse; m.SpecularCoefficient = mat.SpecularCoefficient; m.SpecularColor = mat.Specular; m.Transparency = mat.Transparency; m.Name = mat.Name; materials[m.Name] = m; } foreach (var f in data.faces) { Vertex v1 = new Vertex(); Vertex v2 = new Vertex(); Vertex v3 = new Vertex(); v1.Position = data.vertices[f.Vert1.vertex - 1]; v2.Position = data.vertices[f.Vert2.vertex - 1]; v3.Position = data.vertices[f.Vert3.vertex - 1]; v1.Normal = data.normals[f.Vert1.normal - 1]; v2.Normal = data.normals[f.Vert2.normal - 1]; v3.Normal = data.normals[f.Vert3.normal - 1]; // Only set the texture coordinates if they were set if (f.Vert1.texCoord > 0 && f.Vert2.texCoord > 0 && f.Vert3.texCoord > 0) { v1.TexCoord = data.texCoords[f.Vert1.texCoord - 1]; v2.TexCoord = data.texCoords[f.Vert2.texCoord - 1]; v3.TexCoord = data.texCoords[f.Vert3.texCoord - 1]; } Triangle t = new Triangle(v1, v2, v3); if (!string.IsNullOrEmpty(f.Material)) { t.Material = materials[f.Material]; } s.Triangles.Add(t); } return s; }