title: SharpGL: Simple shading and vbo not working
body: <p>I am using SharpGL for my project, which will involve using glsl version 4.2. </p>
  
  <p>I have tried a few examples over the past few days, none have been working. Similar code written for OpenTK have worked for me, so I have no idea what I am doing wrong, but in SharpGL nothing is drawn to the screen if I am using shaders and VBO. Either I am losing my mind or something isn't working correctly with SharpGL itself, which I doubt.</p>
  
  <p>I would rather use SharpGL for its WPF control. I am attaching a very simple example code. I would appreciate any help on the matter. Note that the same code with minor changes works in OpenTK.</p>
  
  <p><em>MainWindow.xaml.cs</em></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Windows;
  
  using System.Runtime.InteropServices;
  using System.IO;
  
  using SharpGL;
  using SharpGL.SceneGraph;
  
  namespace SharpGLTest
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          OpenGL gl;
          uint theProgram, positionBufferObject;
          string strVertexShader = "test.vert";
          string strFragmentShader = "test.frag";
  
          public MainWindow()
          {
              InitializeComponent();
          }
  
          private void OpenGLControl_OpenGLInitialized(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
          {
              gl = args.OpenGL;
  
              InitializeVertexBuffer();
              InitializeProgram();
          }
  
          private uint CreateProgram(List&lt;uint&gt; shaderList)
          {
              uint program = gl.CreateProgram();
  
              foreach (uint shader in shaderList)
                  gl.AttachShader(program, shader);
  
              gl.LinkProgram(program);
  
              ProgramErrorInfo(program);
  
              foreach (uint shader in shaderList)
                  gl.DetachShader(program, shader);
  
              return program;
          }
  
          private uint CreateShader(uint eShaderType, string strShaderFile)
          {
              uint shader = gl.CreateShader(eShaderType);
              string[] strFileData = { File.ReadAllText(strShaderFile) };
  
              gl.ShaderSource(shader, strFileData);
  
              gl.CompileShader(shader);
  
              ShaderErrorInfo(shader);
  
              return shader;
          }
  
          private void InitializeProgram()
          {
              List&lt;uint&gt; shaderList = new List&lt;uint&gt;();
  
              shaderList.Add(CreateShader(OpenGL.GL_VERTEX_SHADER, strVertexShader));
              shaderList.Add(CreateShader(OpenGL.GL_FRAGMENT_SHADER, strFragmentShader));
  
              theProgram = CreateProgram(shaderList);
  
              foreach (uint shader in shaderList)
                  gl.DeleteShader(shader);
  
          }
  
          private void InitializeVertexBuffer()
          {
              float[] vertexPositions = {
                  0.75f, 0.75f, 0.0f, 1.0f,
                  0.75f, -0.75f, 0.0f, 1.0f,
                  -0.75f, -0.75f, 0.0f, 1.0f,
              };
              GCHandle handle = GCHandle.Alloc(vertexPositions, GCHandleType.Pinned);
              IntPtr vertexPtr = handle.AddrOfPinnedObject();
              var size = Marshal.SizeOf(typeof(float)) * vertexPositions.Length;
  
              uint[] pbo = new uint[1];
              gl.GenBuffers(1, pbo);
              positionBufferObject = pbo[0];
  
              gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, positionBufferObject);
              gl.BufferData(OpenGL.GL_ARRAY_BUFFER, size, vertexPtr, OpenGL.GL_STATIC_DRAW);
              gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0);
  
              handle.Free();
          }
  
          private void OpenGLControl_OpenGLDraw(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
          {
              gl.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
              gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT);
  
              gl.UseProgram(theProgram);
  
              gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, positionBufferObject);
              gl.EnableVertexAttribArray(0);
              gl.VertexAttribPointer(0, 4, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
  
              gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 3);
  
              gl.DisableVertexAttribArray(0);
              gl.UseProgram(0);
  
              gl.Flush();
          }
  
          private void OpenGLControl_Resized(object sender, OpenGLEventArgs args)
          {
              gl.Viewport(0, 0, gl.RenderContextProvider.Width, gl.RenderContextProvider.Height);
          }
  
          private bool ShaderErrorInfo(uint shaderId)
          {
              StringBuilder builder = new StringBuilder(2048);
              gl.GetShaderInfoLog(shaderId, 2048, IntPtr.Zero, builder);
              string res = builder.ToString();
              if (!res.Equals(""))
              {
                  System.Console.WriteLine(res);
                  return false;
              }
  
              return true;
          }
  
          private bool ProgramErrorInfo(uint programId)
          {
              StringBuilder builder = new StringBuilder(2048);
              gl.GetProgramInfoLog(programId, 2048, IntPtr.Zero, builder);
              string res = builder.ToString();
              if (!res.Equals(""))
              {
                  System.Console.WriteLine(res);
                  return false;
              }
  
              return true;
          }
  
      }
  }
  </code></pre>
  
  <p><em>test.vert</em></p>
  
  <pre><code>#version 330
  
  layout(location = 0) in vec4 position;
  void main()
  {
      gl_Position = position;
  }
  </code></pre>
  
  <p><em>test.frag</em></p>
  
  <pre><code>#version 330
  
  out vec4 outputColor;
  void main()
  {
     outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
  }
  </code></pre>
  
  <p><em>MainWindow.xaml</em></p>
  
  <pre><code>&lt;Window x:Class="SharpGLTest.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525"
          xmlns:sharpGL="clr-namespace:SharpGL.WPF;assembly=SharpGL.WPF"&gt;
      &lt;Grid&gt;
          &lt;sharpGL:OpenGLControl x:Name="OpenGLControl"
              OpenGLInitialized="OpenGLControl_OpenGLInitialized"
              OpenGLDraw="OpenGLControl_OpenGLDraw" 
              RenderContextType="FBO"
              Resized="OpenGLControl_Resized"
                                 /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>