stin8er's picture

Threading with OpenTK GameWindow

Hello Community,

I have been having some problems lately trying to buffer some vertex data.

Well I have been having trouble even generating the Buffers.

The only time when anything like bind buffer works is when the gamewindow is first opened in the default constructor.

The code I use there is:

             public GraphicsEngine(int x, int y, GraphicsMode m, string title, GameWindowFlags gmf) : base(x, y, m, title, gmf)
        {
            //Make full Screen?
            GL.Enable(EnableCap.DepthTest);
            GL.GenBuffers(1, bufferoneHandle);
        }

The exception occuring is: AccessViolationException - Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I have tried to fix it by moving the code around but the exception just occurs on another part of the code eg the enableclientstate or bindbuffer or bufferdata.

I am not sure if I am missing anything obvious!? If any more info is needed please let me know.

The following is the main code I use:

public void DrawArrayBeginMode(List<Vector3d> vector)
        {
            GL.EnableClientState(ArrayCap.VertexArray);  | EXCEPTION HITS HERE TOO! AccessViolationException
            //GL.GenBuffers(1, bufferoneHandle);      |           Used here to no avail, 
            GL.BindBuffer(BufferTarget.ArrayBuffer, bufferoneHandle[0]);
            GL.BufferData<Vector3d>(BufferTarget.ArrayBuffer, new IntPtr(vector.Count * Vector3d.SizeInBytes), vector.ToArray(), BufferUsageHint.StreamDraw);
            GL.VertexPointer(3, VertexPointerType.Double, 0, 0);
 
            if (skeleton) GL.DrawArrays(BeginMode.LineStrip, 0, vector.Count);
            else if (shape == "Points")
            {
                GL.DrawArrays(BeginMode.Points, 0, vector.Count);
            }
            else if (shape == "Lines")
            {
                GL.DrawArrays(BeginMode.Lines, 0, vector.Count);
            }
            else if (shape == "Triangles")
            {
                GL.DrawArrays(BeginMode.Triangles, 0, vector.Count);
            }
            else if (shape == "Quads")
            {
                GL.DrawArrays(BeginMode.Quads, 0, vector.Count);
            }
            else if (shape == "LinesAdjacency")
            {
                GL.DrawArrays(BeginMode.LinesAdjacency, 0, vector.Count);
            }
            else if (shape == "LineStrip")
            {
                GL.DrawArrays(BeginMode.LineStrip, 0, vector.Count);
            }
            else if (shape == "LineStripAdjacency")
            {
                GL.DrawArrays(BeginMode.LineStripAdjacency, 0, vector.Count);
            }
            else if (shape == "Polygon")
            {
                GL.DrawArrays(BeginMode.Polygon, 0, vector.Count);
            }
            else if (shape == "QuadStrip")
            {
                GL.DrawArrays(BeginMode.QuadStrip, 0, vector.Count);
            }
            else if (shape == "TriangleFan")
            {
                GL.DrawArrays(BeginMode.TriangleFan, 0, vector.Count);
            }
            else if (shape == "TrianglesAdjacency")
            {
                GL.DrawArrays(BeginMode.TrianglesAdjacency, 0, vector.Count);
            }
            else if (shape == "TriangleStrip")
            {
                GL.DrawArrays(BeginMode.TriangleStrip, 0, vector.Count);
            }
            else if (shape == "TriangleStripAdjacency")
            {
                GL.DrawArrays(BeginMode.TriangleStripAdjacency, 0, vector.Count);
            }
            else if (shape == "LineLoop")
            {
                GL.DrawArrays(BeginMode.LineLoop, 0, vector.Count);
            }
 
            GL.DisableClientState(ArrayCap.VertexArray);
           // GL.DeleteBuffers(1, ref bufferoneHandle);
        }

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
stin8er's picture

Edit I was calling the Method in the wrong place :(

In my defense it was quite late...

Though I get the same error now but only when I try and draw the array:

GL.GenBuffers(1, bufferoneHandle);
 
            GL.EnableClientState(ArrayCap.VertexArray);
            GL.BindBuffer(BufferTarget.ArrayBuffer, bufferoneHandle[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(vector.Length * Vector3d.SizeInBytes), vector, BufferUsageHint.StreamDraw);
            GL.VertexPointer(3, VertexPointerType.Double, 0, 0);
 
//Neither way work
                GL.Begin(BeginMode.Triangles);
                GL.Color3(Color.White);
                GL.Vertex3(1,1,0);
                GL.Vertex3(0,0,0);
                GL.Vertex3(1,0,0);
                GL.End();
 
//This doesn't work either, these were run separately and together.
            if (skeleton) GL.DrawArrays(BeginMode.LineStrip, 0, vector.Length);
            if (shape == "Points")
            {
                GL.DrawArrays(BeginMode.Points, 0, vector.Length);
            }
            else if (shape == "Lines")
            {
                GL.DrawArrays(BeginMode.Lines, 0, vector.Length);
            }
            else if (shape == "Triangles")
            {
                GL.DrawArrays(BeginMode.Triangles, 0, vector.Length);
            }
            else if (shape == "Quads")
            {
                GL.DrawArrays(BeginMode.Quads, 0, vector.Length);
            }
            else if (shape == "LinesAdjacency")
            {
                GL.DrawArrays(BeginMode.LinesAdjacency, 0, vector.Length);
            }
            else if (shape == "LineStrip")
            {
                GL.DrawArrays(BeginMode.LineStrip, 0, vector.Length);
            }
            else if (shape == "LineStripAdjacency")
            {
                GL.DrawArrays(BeginMode.LineStripAdjacency, 0, vector.Length);
            }
            else if (shape == "Polygon")
            {
                GL.DrawArrays(BeginMode.Polygon, 0, vector.Length);
            }
            else if (shape == "QuadStrip")
            {
                GL.DrawArrays(BeginMode.QuadStrip, 0, vector.Length);
            }
            else if (shape == "TriangleFan")
            {
                GL.DrawArrays(BeginMode.TriangleFan, 0, vector.Length);
            }
            else if (shape == "TrianglesAdjacency")
            {
                GL.DrawArrays(BeginMode.TrianglesAdjacency, 0, vector.Length);
            }
            else if (shape == "TriangleStrip")
            {
                GL.DrawArrays(BeginMode.TriangleStrip, 0, vector.Length);
            }
            else if (shape == "TriangleStripAdjacency")
            {
                GL.DrawArrays(BeginMode.TriangleStripAdjacency, 0, vector.Length);
            }
            else if (shape == "LineLoop")
            {
                GL.DrawArrays(BeginMode.LineLoop, 0, vector.Length);
            }
 
            GL.DeleteBuffers(1, bufferoneHandle);
            GL.DisableClientState(ArrayCap.VertexArray);