SharpGL- DrawArrays提供AccessViolationException

时间:2015-03-22 08:41:56

标签: c# wpf sharpgl

我正在使用SharpGL样本作为指南在WPF中创建一个Modern OpenGL hello world应用程序。

我在Draw方法中调用DrawArrays时似乎遇到了AccessViolation异常。检查了互联网 - 他们都建议它可能是底层GC重定位阵列。但由于我使用的是维生素生态系统,这不应该发生,对吧?

代码:

XAML:

<Window x:Class="FirstSharpGlAppWpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sgl="clr-namespace:SharpGL.WPF;assembly=SharpGL.WPF"
    Title="MainWindow" Height="350" Width="525">

    <Grid>
    <sgl:OpenGLControl  x:Name="openGLCtrl" 
                        OpenGLDraw="openGLCtrl_OpenGLDraw" 
                        OpenGLInitialized="openGLCtrl_OpenGLInitialized" 
                        RenderContextType="FBO" 
                        DrawFPS="True" />

    </Grid>
</Window>

XAML.cs

using SharpGL;
using SharpGL.Shaders;
using SharpGL.VertexBuffers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FirstSharpGlAppWpf
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    VertexBufferArray _vao;
    VertexBuffer _vbo;
    ShaderProgram _shaderProgram;
    float[] _vertices;


    public MainWindow()
    {
        InitializeComponent();



    }

    private void openGLCtrl_OpenGLDraw(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
    {
        //  Get the OpenGL instance that's been passed to us.
        OpenGL gl = args.OpenGL;

        gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

        _shaderProgram.Bind(gl);

        _vao.Bind(gl);

        gl.DrawArrays(OpenGL.GL_POINTS, 0, _vertices.Length);

        _vao.Unbind(gl);

        _shaderProgram.Unbind(gl);

    }


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

        _vertices = new float[]{
           -0.5f, -0.5f, 0.0f,
            0.5f, -0.5f, 0.0f,
            0.0f,  0.5f, 0.0f
        };

        ShaderCreator sc = new ShaderCreator();
        _shaderProgram = new ShaderProgram();
        sc.Create(gl, _shaderProgram);

        _vao = new VertexBufferArray();
        _vao.Create(gl);
        _vao.Bind(gl);

        _vbo = new VertexBuffer();
        _vbo.Create(gl);
        _vbo.Bind(gl);
        _vbo.SetData(gl, 0, _vertices, false, 0);


        _vbo.Unbind(gl);
        _vao.Unbind(gl);
    }




}

}

0 个答案:

没有答案
相关问题