0

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?

user541686
  • 205,094
  • 128
  • 528
  • 886
  • You may use a geometry shader with line as input and outputting triangles, but the fastest would be to construct an vertex array. Create first a display list, the improvments will be maybe sufficient for your needs – j-p Apr 29 '14 at 23:20

1 Answers1

1

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

Community
  • 1
  • 1
meandbug
  • 202
  • 2
  • 5
  • Thanks for the answer! Unfortunately I have absolutely no idea how geometry shaders work, since I'm a newbie to graphics... do they need an entirely new language? (The code doesn't look like C++.) Do they need a new compiler? Or are there OpenGL functions I can call to compile that code? – user541686 Apr 30 '14 at 10:06
  • Geometry shaders are a special form of shaders. Shaders are programs run on the graphics card in order to control the rendering. They are the state of art way to render. (As compared to using the fixed function pipeline only). These small programs are loaded from your application and executed for a given draw call. For the language, in the case of opengl you'd use glsl (or cg) as a shading language. It is probably the best way to start with a tutorial on simple rendering using shaders. E.g. http://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/loading.php – meandbug Apr 30 '14 at 10:14
  • No problem. In addition if you really just want to render lines. Use glBegine(GL_LINES) and set the fill mode to line: http://www.felixgers.de/teaching/jogl/polygonFill.html – meandbug Apr 30 '14 at 10:20