Example #1
0
 /// <summary>
 /// Initialize the renderer
 /// </summary>
 /// <param name="c">The configuration for rendering</param>
 /// <param name="s">The scene to render</param>
 public Renderer(Configuration c, Scene s)
 {
     config = c;
     scene = s;
 }
        /// <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;
        }