This project is read-only.

Help - VertexPointer Not working

May 8, 2013 at 1:16 PM
Edited May 8, 2013 at 1:16 PM
I'm having some problems getting the vertex pointer and color pointer calls to work.

All I get is a blank screen. Any ideas?

I've thought about going the VBO and VAO routes, but the number of vertices can change and I'm not sure how to handle the dynamic nature with a buffer.
        /// <summary>
        /// Handles the OpenGLDraw event of the openGLControl1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="SharpGL.SceneGraph.OpenGLEventArgs"/> instance containing the event data.</param>
        private void openGLControl_OpenGLDraw(object sender, OpenGLEventArgs args)
        {
            var gl = args.OpenGL;

            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT);
            
            gl.Disable(OpenGL.GL_DEPTH_TEST);

            // Activate the necessary arrays
            gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY);
            gl.EnableClientState(OpenGL.GL_COLOR_ARRAY);

            if (this._viewModel.Agents.Count > 0)
            {
                Vertex[] vArray = this._viewModel.Agents.ConvertAll<Vertex>(i => i.Location).ToArray();
                // Specify the pointers to vertex data (the last parameter is the offset in the buffer array)
                gl.VertexPointer(3, OpenGL.GL_FLOAT, 0, vArray);
                float[] colors = this.GetColors(this._viewModel.Agents.ConvertAll<GLColor>(i => i.DetermineColor()).ToArray());
                IntPtr pointer = GCHandle.Alloc(colors, GCHandleType.Pinned).AddrOfPinnedObject();
                gl.ColorPointer(3, OpenGL.GL_FLOAT, 0, pointer);

                gl.PointSize(1.0f);
                // Draw the points
                gl.DrawArrays(OpenGL.GL_POINT, 0, this._viewModel.Agents.Count);
            }
            // Disable the arrays
            gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY);
            gl.DisableClientState(OpenGL.GL_COLOR_ARRAY);

            gl.Flush();
        }
Thanks.
May 8, 2013 at 10:34 PM
I was able to get the code to work, but now I'm getting a different error:


System.AccessViolationException was unhandled
HResult=-2147467261
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=SharpGL
StackTrace:
   at SharpGL.OpenGL.glDrawArrays(UInt32 mode, Int32 first, Int32 count)
   at SharpGL.OpenGL.DrawArrays(UInt32 mode, Int32 first, Int32 count) in C:\myprojects\SharpGL\Core\SharpGL\OpenGL.cs:line 2196
   at AgentSimulator.MainWindow.openGLControl_OpenGLDraw(Object sender, OpenGLEventArgs args) in C:\myprojects\agentsimulator\AgentSimulator\AgentSimulator\MainWindow.xaml.cs:line 116
   at SharpGL.WPF.OpenGLControl.timer_Tick(Object sender, EventArgs e) in C:\myprojects\SharpGL\Core\SharpGL.WPF\OpenGLControl.xaml.cs:line 133
   at System.Windows.Threading.DispatcherTimer.FireTick(Object unused)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at System.Windows.Application.Run()
   at AgentSimulator.App.Main() in C:\myprojects\agentsimulator\AgentSimulator\AgentSimulator\obj\x86\Debug\App.g.cs:line 0
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
InnerException:
May 10, 2013 at 11:54 AM
I'm looking into this now
May 10, 2013 at 12:48 PM
I figured out the problem. I had to create an array within the view that stores the vertices and the colors and then get the pointer to those. I then copy the data to each of those each draw cycle. For some reason the code does not like it when you recreate the arrays each time. I think it has something to do with the fact we are having to get a pinned pointer from the GC and the array is being garbage collected.
May 12, 2013 at 3:00 PM
It might be good if I can document this, I'll try and find some time to update the documentation as this will be useful for others.