渲染WPF应用程序以获取.jpg / .png的输出



我需要渲染从Autodesk 3ds Max导入的.OBJ以在JPG或PNG文件中获取输出。我该怎么做?我正在使用表达式混合4和WPF Visual Studio。

使用sharpgl,一个C#库,它使在C#应用程序中使用OpenGL非常容易。它确实包括对直接加载.OBJ文件的支持。

double Edit :我已经使用它们的实现替换了我的代码示例。我运行了以下代码,并且确实有效,它加载了.obj并在屏幕上呈现没有问题

xaml ..可以确保您添加

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>

背后的代码

NOTE :我实际上还没有实现图形的保存,但是此时您可以在屏幕上渲染模型的预览。

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;
    }
}

最新更新