Hi,
To identify a 3D object in the view, I transform the object identifier into color. As the identifier is an integer, it is converted to RGBA (integer to 4 floats values) in the shader. When the number of vertices is large, rendering is greatly slowed down. For real color rendering and 3D object identifier color rendering I use the same shader. To switch between renderings I just use a uniform variable and it works fine.
The behavior I would expect is fast rendering in real color and slow rendering in calculated id colors.
It turns out that rendering is still slow.
The uniform variable is set correctly with glUniform1i () before each call to glDrawElements () and whatever its value, everything behaves as if the color calculation was invoked for each vertex.
With the shader that follows, when the uniform variable “isColorId” is set to 1, the 3D objects are made slightly shifted with the color calculated from the identifier. When “isColorId” is set to 0 the 3D objects are rendered in their place and in black.
This is the expected visual behavior but even with isColorId == 0 rendering performances are slow.
#version 330 core
layout(location = 0) in vec3 vertex;
layout(location = 1) in uint objectid;
layout(location = 2) in vec4 color;
out vec4 vertexColor;
uniform mat4 mvp;
uniform int isColorId;
const uint UINT_0XFF = uint(255);
const float FLOAT_255 = float(255);
vec4 getColorFromId(uint id) {
float a = 1;
float b = float(id & UINT_0XFF) / FLOAT_255;
float v = float((id >> 8) & UINT_0XFF) / FLOAT_255;
float r = float((id >> 16) & UINT_0XFF) / FLOAT_255;
return vec4(r, v, b, a);
}
void main() {
if (isColorId == 1) {
vertexColor = getColorFromId(objectid);
gl_Position = mvp * vec4(vertex.x + 33, vertex.y + 33, vertex.z, 1.0);
}
else {
vertexColor = vec4(0,0,0,1);
gl_Position = mvp * vec4(vertex, 1.0);
}
}
This shader behaves as if it were invoked once for each vertex with isColorId == 1 (whereas its value is in principle 0) but without returning the result in the view then invoked again with isColorId == 0 (the correct value) and the correct result in the view.
If I replace the test on “if (isColorId == 1)” by “if (false)”, rendering by color calculated from the identifier is no longer possible since isColorId is no longer tested but the performance returns to those expected for real color rendering.
void main() {
if (false) {
vertexColor = getColorFromId(objectid);
gl_Position = mvp * vec4(vertex.x + 33, vertex.y + 33, vertex.z, 1.0);
}
else {
vertexColor = vec4(0,0,0,1);
gl_Position = mvp * vec4(vertex, 1.0);
}
}
I use OpenGL 3.3 on mac with Radeon Pro 580 8 Go.
Any idea?