Rendering OpenGL instances with color and instance offset

Hey. I am trying to display many axis-aligned cubes using glDrawArraysInstanced (). Each cube of a fixed size may vary depending on its central position and color. Also, each cube accepts only a few different colors. Therefore, I want to potentially display millions of cubes with the following instance data:

struct CubeInfo {
  Eigen::Vector3f center; // center of the cube (x,y,z)
  int labelId;  // label of the cube which affects its color
};

So, I use the following vertex shader:

#version 330

uniform mat4 mvp_matrix;

//regular vertex attributes
layout(location = 0) in vec3 vertex_position;

// Per Instance variables
layout(location = 1) in vec3 cube_center;
layout(location = 2) in int cube_label;

// color out to frag shader
out vec4 color_out;

void main(void) {
  // Add offset cube_center
  vec4 new_pos = vec4(vertex_position + cube_center, 1);
  // Calculate vertex position in screen space
  gl_Position = mvp_matrix * new_pos;

  // Set color_out based on label
  switch (cube_label) {
  case 1:
    color_out = vec4(0.5, 0.25, 0.5, 1);
    break;
  case 2:
    color_out = vec4(0.75, 0.0, 0.0, 1);
    break;
  case 3:
    color_out = vec4(0.0, 0.0, 0.5, 1);
    break;
  case 4:
    color_out = vec4(0.75, 1.0, 0.0, 1);
    break;
  default:
    color_out = vec4(0.5, 0.5, 0.5, 1); // Grey
    break;
  }
}

and corresponding fragment shader:

#version 330

in vec4 color_out;
out vec4 fragColor;

void main()
{
    // Set fragment color from texture
    fragColor = color_out;
}

However, color_out always accepts the default value by default, although the cube_label values ​​are between 1 and 4. This is my problem. Am I doing something wrong in the shader above **? **

cubeInfo vbo 1-4. , : enter image description here

, Qt QGLShaderProgram QGLBuffer wrapper:

  // Enable back face culling
  glEnable(GL_CULL_FACE);
  cubeShaderProgram_.bind();

  // Set the vertexbuffer stuff (Simply 36 vertices for cube)
  cubeVertexBuffer_.bind();
  cubeShaderProgram_.setAttributeBuffer("vertex_position", GL_FLOAT, 0, 3, 0);
  cubeShaderProgram_.enableAttributeArray("vertex_position");
  cubeVertexBuffer_.release();

  // Set the per instance buffer stuff
  cubeInstanceBuffer_.bind();

  cubeShaderProgram_.setAttributeBuffer("cube_center", GL_FLOAT, offsetof(CubeInfo,center), 3, sizeof(CubeInfo));
  cubeShaderProgram_.enableAttributeArray("cube_center");
  int center_location = cubeShaderProgram_.attributeLocation("cube_center");
  glVertexAttribDivisor(center_location, 1);

  cubeShaderProgram_.setAttributeBuffer("cube_label", GL_INT, offsetof(CubeInfo,labelId), 1, sizeof(CubeInfo));
  cubeShaderProgram_.enableAttributeArray("cube_label");
  int label_location = cubeShaderProgram_.attributeLocation("cube_label");
  glVertexAttribDivisor(label_location, 1);

  cubeInstanceBuffer_.release();

  // Do Instanced Renering
  glDrawArraysInstanced(GL_TRIANGLES, 0, 36, displayed_num_cubes_ );

  cubeShaderProgram_.disableAttributeArray("vertex_position");
  cubeShaderProgram_.disableAttributeArray("cube_center");
  cubeShaderProgram_.disableAttributeArray("cube_label");

  cubeShaderProgram_.release();

( ), Minecraft?

CubeInfo.labelId int float cube_label float, !!. ? GLSL suppoers int. , labelId/cube_label int/short.

Update2:

GL_FLOAT GL_INT , .

cubeShaderProgram_.setAttributeBuffer("cube_label", GL_INT, offsetof(CubeInfo,labelId), 1, sizeof(CubeInfo));
+4
2

, , . Qt setAttributeBuffer , glVertexAttribPointer , float, in float, in int ( , , undefined).

( float GL 3+), glVertexAttribIPointer ( I , D in double, GL_DOUBLE ). , , Qt, GL 3+, , . , :

glVertexAttribIPointer(cubeShaderProgram_.attributeLocation("cube_label"), 
                       1, GL_INT, sizeof(CubeInfo), 
                       static_cast<const char*>(0)+offsetof(CubeInfo,labelId));

cubeShaderProgram_.setAttributeBuffer in float.

+1

, , , :

void main()
{
   gl_FragColor = color_out;
}

, cube_label , ( ). , , .

color_out = vec4(float(cube_label) / 4.0, 0, 0, 1.0); 

Update2

Intel GPU (). , , . , NVIDIA, int float.

0

All Articles