user541686
user541686

Reputation: 210445

How to efficiently draw 3D lines using gluCylinder?

I have this code that draws a 3D line between two points:

void glLine(Point3D (&i)[2], double const width, int const slices = 360)
{
    double const d[3] = { i[1].X - i[0].X, i[1].Y - i[0].Y, i[1].Z - i[0].Z };
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    GLUquadric *const quadric = gluNewQuadric()
    double z[3] = { 0, 0, 1 };
    double const angle = acos(dot(z, d) / sqrt(dot(d, d) * dot(z, z)));
    cross(z, d);
    glTranslated(i[0].X, i[0].Y, i[0].Z);
    glRotated(angle * 180 / M_PI, z[0], z[1], z[2]);
    gluCylinder(quadric, width / 2, width / 2, sqrt(dot(d, d)), slices, 1);
    glPopMatrix();
    gluDeleteQuadric(quadric);
}

The trouble is, it's extremely slow because of the math that computes the rotation from the unit z vector to the given direction.

How can I make OpenGL (hopefully, the GPU) perform the arithmetic instead of the CPU?

Upvotes: 0

Views: 855

Answers (1)

meandbug
meandbug

Reputation: 202

Do not render lines using cylinders. Rather render quads using geometry shaders, facing the user with correct screen space scaling.

Have a look here: Wide lines in geometry shader behaves oddly

Upvotes: 1

Related Questions