private void buttonOpen_Click( object sender, EventArgs e ) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Open Scene File"; ofd.Filter = "Wavefront OBJ Files|*.obj" + "|All scene types|*.obj"; ofd.FilterIndex = 1; ofd.FileName = ""; if ( ofd.ShowDialog() != DialogResult.OK ) return; WavefrontObj objReader = new WavefrontObj(); objReader.MirrorConversion = false; StreamReader reader = new StreamReader( new FileStream( ofd.FileName, FileMode.Open ) ); int faces = objReader.ReadBrep( reader, scene ); reader.Close(); scene.BuildCornerTable(); int errors = scene.CheckCornerTable( null ); labelFaces.Text = String.Format( "{0} faces, {1} errors", faces, errors ); PrepareDataBuffers(); glControl1.Invalidate(); }
/// <summary> /// Generic OBJ-loading scene definition routine. /// </summary> /// <param name="dir">Viewing direction of a camera.</param> /// <param name="FoVy">Field of View in degrees.</param> /// <param name="correction">Value less than 1.0 brings camera nearer.</param> /// <param name="name">OBJ file-name.</param> /// <param name="names">Substitute file-name[s].</param> /// <returns>Number of triangles.</returns> protected static long SceneObj( IRayScene sc, Vector3d dir, double FoVy, double correction, string name, string[] names, double[] surfaceColor ) { Debug.Assert( sc != null ); Vector3 center = Vector3.Zero; // center of the mesh dir.Normalize(); // normalized viewing vector of the camera float diameter = 2.0f; // default scene diameter int faces = 0; // CSG scene: CSGInnerNode root = new CSGInnerNode( SetOperation.Union ); // OBJ file to read: if ( names.Length == 0 || names[ 0 ].Length == 0 ) names = new string[] { name }; string[] paths = Scenes.SmartFindFiles( names ); if ( paths[ 0 ] == null || paths[ 0 ].Length == 0 ) { for ( int i = 0; i < names.Length; i++ ) if ( names[ i ].Length > 0 ) names[ i ] += ".gz"; paths = Scenes.SmartFindFiles( names ); } if ( paths[ 0 ] == null || paths[ 0 ].Length == 0 ) root.InsertChild( new Sphere(), Matrix4d.Identity ); else { // B-rep scene construction: WavefrontObj objReader = new WavefrontObj(); objReader.MirrorConversion = false; SceneBrep brep = new SceneBrep(); faces = objReader.ReadBrep( paths[ 0 ], brep ); brep.BuildCornerTable(); diameter = brep.GetDiameter( out center ); TriangleMesh m = new TriangleMesh( brep ); root.InsertChild( m, Matrix4d.Identity ); } root.SetAttribute( PropertyName.REFLECTANCE_MODEL, new PhongModel() ); root.SetAttribute( PropertyName.MATERIAL, new PhongMaterial( surfaceColor, 0.2, 0.5, 0.4, 32 ) ); root.SetAttribute( PropertyName.COLOR, surfaceColor ); sc.Intersectable = root; // Background color: sc.BackgroundColor = new double[] { 0.0, 0.05, 0.07 }; // Camera: double dist = (0.5 * diameter * correction) / Math.Tan( MathHelper.DegreesToRadians( (float)(0.5 * FoVy) ) ); Vector3d cam = (Vector3d)center - dist * dir; sc.Camera = new StaticCamera( cam, dir, FoVy ); // Light sources: sc.Sources = new LinkedList<ILightSource>(); sc.Sources.Add( new AmbientLightSource( 0.8 ) ); Vector3d lightDir = Vector3d.TransformVector( dir, Matrix4d.CreateRotationY( -2.0 ) ); lightDir = Vector3d.TransformVector( lightDir, Matrix4d.CreateRotationZ( -0.8 ) ); sc.Sources.Add( new PointLightSource( (Vector3d)center + diameter * lightDir, 1.0 ) ); return faces; }
private void buttonSaveOBJ_Click( object sender, EventArgs e ) { if ( scene == null || scene.Triangles < 1 ) return; SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "Save OBJ file"; sfd.Filter = "OBJ Files|*.obj"; sfd.AddExtension = true; sfd.FileName = ""; if ( sfd.ShowDialog() != DialogResult.OK ) return; WavefrontObj objWriter = new WavefrontObj(); objWriter.MirrorConversion = true; StreamWriter writer = new StreamWriter( new FileStream( sfd.FileName, FileMode.Create ) ); objWriter.WriteBrep( writer, scene ); writer.Close(); }