C# SharpGL-DrawArrays提供AccessViolationException
我正在WPF中创建一个现代OpenGL hello world应用程序,使用SharpGL示例作为指南 在Draw方法中调用DrawArray时,我似乎遇到了AccessViolation异常。检查过互联网后,他们都认为这可能是底层GC重新定位阵列。但既然我使用的是VBOs,这就不应该发生,对吗 代码: XAML: XAML.csC# SharpGL-DrawArrays提供AccessViolationException,c#,wpf,sharpgl,C#,Wpf,Sharpgl,我正在WPF中创建一个现代OpenGL hello world应用程序,使用SharpGL示例作为指南 在Draw方法中调用DrawArray时,我似乎遇到了AccessViolation异常。检查过互联网后,他们都认为这可能是底层GC重新定位阵列。但既然我使用的是VBOs,这就不应该发生,对吗 代码: XAML: XAML.cs using SharpGL; using SharpGL.Shaders; using SharpGL.VertexBuffers; using System; us
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);
}
}
}在您的代码中,您没有为着色器指定顶点颜色以及VAO属性,这是在屏幕上显示某些内容所必须的。您能告诉我着色器的VAO属性是什么吗?我添加了类似于添加顶点的颜色,但仍然没有乐趣。我不久前用OpenTK写的,对于OpenGL3.X来说,应该比较容易理解/翻译。
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);
}
}