I have a function to read and plot points from external csv.The data resembles to that of a Lidar output. I need to see if there is a way to give different colors to a group of points. Like for all circles in xz plane with radius ranging from 0 - 50 will have blue and 50-10 will have red color and so on.
Here is my code for drawing the points
glBegin(GL_POINTS);
glColor3f(0.961, 0.961, 0.961);
for (int i = 0; i <vx.size(); i++)
{
if (sqrt((vx[i] * vx[i]) + (vz[i] * vz[i]))<96.00)
{
glVertex3f(vx[i], vy[i], vz[i]);
}
}
glEnd();
Where vx,vy and vz are std::vector<float>
datatype which stores the x,y,z values respectively. I have tried giving condition like below
if (sqrt((vx[i] * vx[i]) + (vz[i] * vz[i]))<5.00)
{
glColor3f(1.000, 0.549, 0.000);
glVertex3f(vx[i], vy[i], vz[i]);
}
else if (sqrt((vx[i] * vx[i]) + (vz[i] * vz[i]))>= 5.00 && sqrt((vx[i] * vx[i]) + (vz[i] * vz[i])) <= 10.00)
{
glColor3f(1.000, 0.843, 0.000);
glVertex3f(vx[i], vy[i], vz[i]);
}
else if (sqrt((vx[i] * vx[i]) + (vz[i] * vz[i]))>= 11.00 && sqrt((vx[i] * vx[i]) + (vz[i] * vz[i]))<= 20.00)
{
glColor3f(1.000, 1.000, 0.000);
glVertex3f(vx[i], vy[i], vz[i]);
}
else if (sqrt((vx[i] * vx[i]) + (vz[i] * vz[i]))>= 21.00 && sqrt((vx[i] * vx[i]) + (vz[i] * vz[i]))<=30.00)
{
glColor3f(0.678, 1.000, 0.184);
glVertex3f(vx[i], vy[i], vz[i]);
}
-----------------//till radius <96
glend();
This is generating the desired output for me, but rendering is kind of slow. I have included zoom and pan functions etc, which is lagging because of multiple condition checking. Is there a more efficient way to do this?
But note, a modern solution would be to put all the points in a Vertex Buffer Object and to use a Shader program which calculates the color of the fragment. See also LearnOpenGL - Shades.
Anyway, the major performance impact is caused by the multiple calculation of the Euclidean distance. sqrt
is a very time consuming function.
Since you just want to compare distances, you do not need to calculate the distance at all. Instead of comparing distances you can compare the square of distances. e.g.:
Instead of
if (sqrt((vx[i] * vx[i]) + (vz[i] * vz[i]))<96.00)
do
if ( vx[i]*vx[i] + vz[i]*vz[i] < 96.00*96.00 )
Furthermore, I recommend to simplify the code and to calculate the square of the distance only once:
```cpp
glBegin(GL_POINTS);
for (int i = 0; i <vx.size(); i++)
{
float dist_sq = vx[i]*vx[i] + vz[i]*vz[i];
if (dist_sq < 5.00 * 5.00)
glColor3f(1.000, 0.549, 0.000);
else if (dist_sq < 10.00 * 10.00)
glColor3f(1.000, 0.843, 0.000);
else if (dist_sq < 20.00 * 20.00)
glColor3f(1.000, 1.000, 0.000);
// [...]
glVertex3f(vx[i], vy[i], vz[i]);
}
glEnd();