title: Setup of matrix for instance shader
body: <p>I want to draw instanced cubes.</p>
  
  <p>I can call <code>GL.DrawArraysInstanced(PrimitiveType.Triangles, 0, 36, 2);</code> successfully.</p>
  
  <p>My problem is that all the cubes are drawn at the same position and same rotation. How can i change that individually for every cube?</p>
  
  <p>To create different positions and so on, i need a matrix for each cube, right? I created this:</p>
  
  <pre><code>Matrix4[] Matrices = new Matrix4[]{
    Matrix4.Identity, //do nothing
    Matrix4.Identity * Matrix4.CreateTranslation(1,0,0) //move a little bit
  };
  
  GL.BindBuffer(BufferTarget.ArrayBuffer, matrixBuffer);
  GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(sizeof(float) * 16 * Matrices.Length), Matrices, BufferUsageHint.StaticDraw);
  </code></pre>
  
  <p>This should create a buffer where i can store my matrices. <code>matrixBuffer</code> is the pointer to my buffer. Im not sure if the size is correct, i took float * 4 (for Vector4) * 4 (for 4 vectors) * array-size.</p>
  
  <p>Draw Loop:</p>
  
  <pre><code>GL.BindBuffer(BufferTarget.ArrayBuffer, matrixBuffer);
  GL.VertexAttribPointer(3, 4, VertexAttribPointerType.Float, false, 0, 0);
  //GL.VertexAttribDivisor(3, ?);
  GL.EnableVertexAttribArray(3);
  
  GL.DrawArraysInstanced(PrimitiveType.Triangles, 0, 36, 2);
  </code></pre>
  
  <p>Any higher number than 4 in <code>VertexAttribPointer(..., 4, VertexattribPointerType.Float, ...);</code> causes a crash. I tought i have to set that value to 16?</p>
  
  <p>Im not sure if i need a VertexAttribDivisor, probably i need this every 36th vertex so i call <code>GL.VertexAttribDivisor(3, 36);</code>? But when i do that, i can't see any cube.</p>
  
  <p>My vertex shader:</p>
  
  <pre><code>#version 330 core
  layout(location = 0) in vec3 position;
  layout(location = 1) in vec4 color;
  layout(location = 2) in vec2 texCoord;
  layout(location = 3) in mat4 instanceMatrix;
  
  uniform mat4 projMatrix;
  
  out vec4 vColor;
  out vec2 texCoords[];
  
  void main(){
    gl_Position = instanceMatrix * projMatrix * vec4(position, 1.0);
    //gl_Position = projMatrix * vec4(position, 1.0);
    texCoords[0] = texCoord;
    vColor = color;
  }
  </code></pre>
  
  <p>So my questions:</p>
  
  <ul>
  <li>What is wrong with my code?</li>
  <li>Why can i set the size-parameter of VertexAttribPointer only to 4?</li>
  <li>What is the correct value for VertexAttribDivisor?</li>
  </ul>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>Based on the answer of Andon M. Coleman i made this changes:</p>
  
  <pre><code>GL.BindBuffer(BufferTarget.UniformBuffer, matrixBuffer);
  GL.BufferData(BufferTarget.UniformBuffer, (IntPtr)(sizeof(float) * 16), IntPtr.Zero, BufferUsageHint.DynamicDraw);
  //Bind Buffer to Binding Point
  GL.BindBufferBase(BufferRangeTarget.UniformBuffer, matrixUniform, matrixBuffer);
  
  matrixUniform = GL.GetUniformBlockIndex(shaderProgram, "instanceMatrix");
  //Bind Uniform Block to Binding Point
  GL.UniformBlockBinding(shaderProgram, matrixUniform, 0);
  
  GL.BufferSubData(BufferTarget.UniformBuffer, IntPtr.Zero, (IntPtr)(sizeof(float) * 16 * Matrices.Length), Matrices);
  </code></pre>
  
  <p>And shader:</p>
  
  <pre><code>#version 330 core
  layout(location = 0) in vec4 position; //gets vec3, fills w with 1.0
  layout(location = 1) in vec4 color;
  layout(location = 2) in vec2 texCoord;
  
  uniform mat4 projMatrix;
  uniform UniformBlock
  { mat4 instanceMatrix[]; };
  
  out vec4 vColor;
  out vec2 texCoords[];
  
  void main(){
    gl_Position = projMatrix * instanceMatrix[0] * position;
    texCoords[0] = texCoord;
    vColor = color;
  }
  </code></pre>