opentk / LearnOpenTK

A port of learnopengl.com's tutorials to OpenTK and C#.
Creative Commons Attribution 4.0 International
464 stars 115 forks source link

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. #62

Closed Hann1bal closed 2 years ago

Hann1bal commented 2 years ago

When i'm try to use code from chapter 1 leasson 2, i got error Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

GameEngine.cs

` using System; using System.Drawing; using GameTry.Common; using OpenTK; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL4; using OpenTK.Windowing.Common; using OpenTK.Windowing.Desktop; using OpenTK.Windowing.GraphicsLibraryFramework; namespace GameTry { public sealed class GameEngine : GameWindow { private readonly float[] _vertices = { -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f, };

    private Shader _shader;
    private int _vertexBufferObject;
    private int _vertexArrayObject;

    public GameEngine(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings) : base(
        gameWindowSettings, nativeWindowSettings)
    {
    }

    /// <summary>Run immediately after Run() is called.</summary>
    /// <footer><a href="https://www.google.com/search?q=OpenTK.Windowing.Desktop.GameWindow.OnLoad">`GameWindow.OnLoad` on google.com</a></footer>
    protected override void OnLoad()
    {
        base.OnLoad();
        GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        _vertexBufferObject = GL.GenBuffer();
        GL.BufferData(BufferTarget.ArrayBuffer, _vertices.Length * sizeof(float), _vertices,
            BufferUsageHint.StaticDraw);
        CursorVisible = true;
        _vertexArrayObject = GL.GenVertexArray();
        GL.BindVertexArray(_vertexArrayObject);
        GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0);
        GL.EnableVertexAttribArray(0);
        _shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
        _shader.Use();
    }

    protected override void OnRenderFrame(FrameEventArgs e)
    {
        base.OnRenderFrame(e);
        GL.Clear(ClearBufferMask.ColorBufferBit);

        _shader.Use();

        GL.BindVertexArray(_vertexArrayObject);
        GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
        SwapBuffers();
    }

    protected override void OnUpdateFrame(FrameEventArgs e)
    {
        base.OnUpdateFrame(e);
        HandleKeybord();
    }

    private void HandleKeybord()
    {
        var input = KeyboardState;
        if (input.IsKeyDown(Keys.Escape))
        {
            Close();
        }
    }

    protected override void OnResize(ResizeEventArgs e)
    {
        base.OnResize(e);
        GL.Viewport(0, 0, Size.X, Size.Y);
    }

    protected override void OnUnload()
    {
        // Unbind all the resources by binding the targets to 0/null.
        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        GL.BindVertexArray(0);
        GL.UseProgram(0);

        // Delete all the resources.
        GL.DeleteBuffer(_vertexBufferObject);
        GL.DeleteVertexArray(_vertexArrayObject);

        GL.DeleteProgram(_shader.Handle);

        base.OnUnload();
    }
}

}`

` using System; using OpenTK.Mathematics; using OpenTK.Windowing.Common; using OpenTK.Windowing.Desktop;

namespace GameTry { class Programm {

    public static void Main(string[] args)
    {
        var nativeWindowSettings = new NativeWindowSettings()
        {
            Size = new Vector2i(1920, 1024),
            Title = "FirstGameEngineTry",
            Flags = ContextFlags.ForwardCompatible
        };
        using (GameEngine gameEngine = new(GameWindowSettings.Default, nativeWindowSettings))
        {
            gameEngine.Run();
        }
    }
}

}`

what i do wrong?

Hann1bal commented 2 years ago

UPD. When i getting error when call GL.DrawArrays(PrimitiveType.Triangles, 0, 3);

NogginBops commented 2 years ago

It looks to me like you are not binding the buffer before calling glBufferData

Hann1bal commented 2 years ago

It looks to me like you are not binding the buffer before calling glBufferData

Oh sry it's my mistake))