Setting up a matrix, such as a shader

I want to draw instanced cubes.

I can call successfully GL.DrawArraysInstanced(PrimitiveType.Triangles, 0, 36, 2);.

My problem is that all cubes are drawn in the same position and the same rotation. How can I change this individually for each cube?

To create different positions, etc., I need a matrix for each cube, right? I created this:

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);

This should create a buffer in which I can store my matrices. matrixBuffer- pointer to my buffer. I'm not sure if the size is correct, I took float * 4 (for Vector4) * 4 (for 4 vectors) * array size.

Draw Loop:

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);

Any number greater than 4 in VertexAttribPointer(..., 4, VertexattribPointerType.Float, ...);causes a failure. Am I having difficulty setting this value to 16?

, VertexAttribDivisor, , 36- , GL.VertexAttribDivisor(3, 36);? , .

:

#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;
}

, :

  • ?
  • VertexAttribPointer 4?
  • VertexAttribDivisor?

Edit:

. , :

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);

:

#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;
}
+4
1

, 4-.

4x4 per-vertex - , mat4 4x, vec4.

mat4 vertex:

layout(location = 3) in mat4 instanceMatrix;

, , , 3 16 , . GLSL 4-. , mat4 instanceMatrix 4 .

, instanceMatrix:

layout(location = 3) in vec4 instanceMatrix_Column0;
layout(location = 4) in vec4 instanceMatrix_Column1;
layout(location = 5) in vec4 instanceMatrix_Column2;
layout(location = 6) in vec4 instanceMatrix_Column3;

, , mat4.

do #, :

GL.BindBuffer(BufferTarget.ArrayBuffer, matrixBuffer);
GL.VertexAttribPointer(3, 4, VertexAttribPointerType.Float, false, 64,  0); // c0
GL.VertexAttribPointer(4, 4, VertexAttribPointerType.Float, false, 64, 16); // c1
GL.VertexAttribPointer(5, 4, VertexAttribPointerType.Float, false, 64, 32); // c2
GL.VertexAttribPointer(6, 4, VertexAttribPointerType.Float, false, 64, 48); // c3

, 4 :

GL.VertexAttribDivisor (3, 1);
GL.VertexAttribDivisor (4, 1);
GL.VertexAttribDivisor (5, 1);
GL.VertexAttribDivisor (6, 1);

, 4-, :

layout(location = 0) in vec4 position;

:

gl_Position = instanceMatrix * projMatrix * vec4(position, 1.0);

, OpenGL.

      (0.0, 0.0, 0.0, 1.0)

vec4 GLSL, XYZ, W 1.0.


. . , , , , . , Vertex Shader: gl_InstanceID. , , (minin 16 GL 3.3, 16).

, vec4, , mat4 4x vec4. , .

+7

Source: https://habr.com/ru/post/1540360/


All Articles