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;
int labelId;
};
So, I use the following vertex shader:
#version 330
uniform mat4 mvp_matrix;
layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 cube_center;
layout(location = 2) in int cube_label;
out vec4 color_out;
void main(void) {
vec4 new_pos = vec4(vertex_position + cube_center, 1);
gl_Position = mvp_matrix * new_pos;
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);
break;
}
}
and corresponding fragment shader:
#version 330
in vec4 color_out;
out vec4 fragColor;
void main()
{
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. , :

, Qt QGLShaderProgram QGLBuffer wrapper:
glEnable(GL_CULL_FACE);
cubeShaderProgram_.bind();
cubeVertexBuffer_.bind();
cubeShaderProgram_.setAttributeBuffer("vertex_position", GL_FLOAT, 0, 3, 0);
cubeShaderProgram_.enableAttributeArray("vertex_position");
cubeVertexBuffer_.release();
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();
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));