2014-03-06 35 views

回答

2

使用SharpGL,這是一個C#庫,它使您可以非常容易地在您的C#應用​​程序中使用OpenGL。它包括對直接加載.obj文件的支持。

雙編輯:我已經用我們的代碼示例替換了它們的實現。我已經跑了下面的代碼,它的工作,它加載了一個.OBJ並沒有問題

XAML ..Make確保其呈現在屏幕上添加

xmlns:sharpGL="clr-namespace:SharpGL.WPF;assembly=SharpGL.WPF" 

的窗口屬性

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="30" /> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 
    <StackPanel Grid.Row="0" Orientation="Horizontal"> 
     <Button Content="Add Item" Click="Add_Click" /> 
     <Button Content="Take Screen Shot" Click="ScreenShot_Click" /> 
    </StackPanel> 

    <sharpGL:OpenGLControl Grid.Row="1" Name="myOpenGLControl" 
     OpenGLDraw="OpenGLControl_OpenGLDraw" OpenGLInitialized="OpenGLControl_OpenGLInitialized" 
     RenderContextType="FBO" /> 
</Grid> 

代碼背後

注意:我沒有真正實現圖形的保存,但在這一點上,你可以在屏幕上呈現模型的預覽。

public partial class MainWindow : Window 
{ 
    float rotation_angle = 0f; 
    float rotation_x = 0f; 
    float rotation_y = 0f; 
    float rotation_z = 0f; 

    private List<Polygon> _polygons = new List<Polygon>(); 

    public MainWindow() 
    { 
     InitializeComponent(); 

     myOpenGLControl.OpenGL.Enable(OpenGL.GL_TEXTURE_2D); 
    } 

    private void OpenGLControl_OpenGLDraw(object sender, SharpGL.SceneGraph.OpenGLEventArgs args) 
    { 
     OpenGL gl = args.OpenGL; 

     // Clear The Screen And The Depth Buffer 
     gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); 

     // Move Left And Into The Screen 
     gl.LoadIdentity(); 
     gl.Translate(0.0f, -1.0f, -10.0f); 

     gl.Rotate(rotation_angle, rotation_x, rotation_y, rotation_z); 

     foreach (Polygon polygon in _polygons) 
     { 
      polygon.PushObjectSpace(gl); 
      polygon.Render(gl, SharpGL.SceneGraph.Core.RenderMode.Render); 
      polygon.PopObjectSpace(gl); 
     } 

     rotation_angle += 3; 
     rotation_x = 0; 
     rotation_y = 20; 
     rotation_z = -5; 
    } 

    private void OpenGLControl_OpenGLInitialized(object sender, SharpGL.SceneGraph.OpenGLEventArgs args) 
    { 
     OpenGL gl = args.OpenGL; 

     gl.Enable(OpenGL.GL_DEPTH_TEST); 

     float[] global_ambient = new float[] { 0.5f, 0.5f, 0.5f, 1.0f }; 
     float[] light0pos = new float[] { 0.0f, 5.0f, 10.0f, 1.0f }; 
     float[] light0ambient = new float[] { 0.2f, 0.2f, 0.2f, 1.0f }; 
     float[] light0diffuse = new float[] { 0.3f, 0.3f, 0.3f, 1.0f }; 
     float[] light0specular = new float[] { 0.8f, 0.8f, 0.8f, 1.0f }; 

     float[] lmodel_ambient = new float[] { 0.2f, 0.2f, 0.2f, 1.0f }; 
     gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); 

     gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, global_ambient); 
     gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, light0pos); 
     gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_AMBIENT, light0ambient); 
     gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_DIFFUSE, light0diffuse); 
     gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_SPECULAR, light0specular); 
     gl.Enable(OpenGL.GL_LIGHTING); 
     gl.Enable(OpenGL.GL_LIGHT0); 

     gl.ShadeModel(OpenGL.GL_SMOOTH); 
    } 

    private void Add_Click(object sender, RoutedEventArgs e) 
    { 
     List<Polygon> polygons = LoadScene("ducky.obj"); 
     polygons.ScaleMaxExtentTo(10); 
     _polygons.AddRange(polygons); 
    } 

    private void ScreenShot_Click(object sender, RoutedEventArgs e) 
    { 
     //Here you can use either of the below methods to get the raw pixel data 
     //which needs to be manually formatted for the file type of your choice. 
     //myOpenGLControl.OpenGL.ReadBuffer(); 
     //myOpenGLControl.OpenGL.ReadPixels(); 
    } 

    private List<Polygon> LoadScene(string filePath) 
    { 
     List<Polygon> polygons = new List<Polygon>(); 
     Scene scene = SerializationEngine.Instance.LoadScene(filePath); 
     if (scene != null) 
     { 
      polygons.AddRange(scene.SceneContainer.Traverse<Polygon>()); 
     } 

     return polygons; 
    } 
} 

public static class ExtensionMethods 
{ 
    /// <summary>Scales the polygons to not excede the maximum size in OpenGL Units.</summary> 
    public static void ScaleMaxExtentTo(this List<Polygon> polygons, float maxSize) 
    { 
     foreach (var polygon in polygons) { polygon.ScaleMaxExtentTo(maxSize); } 
    } 

    /// <summary>Scales the polygon to not excede the maximum size in OpenGL Units.</summary> 
    public static void ScaleMaxExtentTo(this Polygon polygon, float maxSize) 
    { 
     float[] extent = new float[3]; 
     polygon.BoundingVolume.GetBoundDimensions(out extent[0], out extent[1], out extent[2]); 
     float maxExtent = extent.Max(); 
     float scaleFactor = maxExtent > maxSize ? maxSize/maxExtent : 1; 
     polygon.Transformation.ScaleX = scaleFactor; 
     polygon.Transformation.ScaleY = scaleFactor; 
     polygon.Transformation.ScaleZ = scaleFactor; 
    } 
} 
+0

發現了一些討論的話題在這裏對此:http://stackoverflow.com/questions/421600/3ds-max-object-to-opengl – ouflak

+0

我不知道這是如何工作。我無法讓它工作。我是一名新手,剛開始使用WPF,所以請幫助 – user2575033

+0

@ user2575033你究竟能不能工作......你能解釋一下你試圖做什麼嗎? – BenVlodgi