2

I am creating a Modern OpenGL hello world app in WPF using SharpGL samples as a guide.

I seem to be getting an AccessViolation Exception when calling DrawArrays in the Draw method. Having checked the internet - they all suggest it could be the underlying GC relocating arrays. But since I am using VBOs this shouldn't happen, right?

Code:

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




}

}

user2822838
  • 337
  • 1
  • 3
  • 13
  • Nowhere in your code you have specified the vertices colors as well as VAO attributes for the shader, which are a must to get something on screen. – aybe Mar 22 '15 at 14:05
  • Would you be point to me what the VAO attributes for the shaders are? I have added colours similar to how I add the vertices and still no joy. – user2822838 Mar 22 '15 at 14:29
  • [This is an example](https://github.com/aybe/GLA/blob/master/GLADemo/Traditional/GameLineStripDemo.cs#L205) I wrote a while ago using OpenTK and should be relatively easy for you to understand/translate, for OpenGL 3.X. – aybe Mar 22 '15 at 14:34

0 Answers0