2

I like to draw spheres in an OpenGL environment using gluSphere(). However, I wish I could draw a border around the sphere, with the border being a circle aligned perfectly with the camera frame so that the circle is in fact always a circle, and not an ellipse when seen from different angles. I'm using a framework that sets up the OpenGL environment for me and does the handling of the camera. When I'm drawing, I'm always thinking in world coordinates, so I find accomplishing this task difficult. I would love to have a solution embedded into my deepest level of OpenGL drawing commands, like so:

// Draws a sphere with a camera plane aligned border around it (you wish).
void GLLib::drawBorderedSphere(float radius)
{
    static GLUquadric* quadric = gluNewQuadric();
    gluSphere(quadric, radius, 32, 32);

    // Determine a rotation matrix depending on the current camera configuration.
    // glRotate(somehow);
    // drawCircle(radius); <-- this I already have.
}

Can anyone help?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Motionbit
  • 21
  • 1
  • Depending on what you wanted to do with the spheres, you might even be able to render them as impostors using a lot less geometry: http://stackoverflow.com/questions/10488086/drawing-a-sphere-in-opengl-es/10506172#10506172 and then use the bounds of the sphere in the fragment shaders to draw a border. – Brad Larson May 01 '14 at 19:11
  • While drawCircle(radius) is a fine approximation for the silhouette of a sphere in many cases, bear in mind that it is not exactly correct for many spheres and projections. For instance, perspective projections introduce a fisheye effect, such that spheres in the view periphery are non-linearly distorted. And even when looking at a sphere dead-on, the set of points at which the eye-line is tangent to the sphere -- the silhouette -- is a circle whose world-space center is closer than the center of the sphere and whose radius is smaller. Visual example: http://www.dchelsea.com/wp/?p=4575 – jcl Jun 16 '16 at 18:59

1 Answers1

1

Actually the task is quite simple. You know the center point of the sphere and the position of the camera. This gives you the normal vector of the plane, the circle lies in. Now you just need to find two base vectors (ideally orthogonal to each other) within the place to form a coordinate base for the circle. That's easy as well: Take any vector not parallel to the camera to sphere vector (for example the camera "up" vector) and orthogonalize it to the circle normal using Gram-Schmidt orthogonalization, call this the "tangent vector". To get the second circle-plane base vector, called the "bitangent" calculate the cross product of the circle-plane normal and the tangent. As a last step scale the tangent and bitangent to unit length.

i.e.

  1. N = Camera - Circlecenter
  2. T = orthogonalize(N, Camera_Up)
  3. B = cross(N, T)
  4. T' = T/len(T); B' = B/len(B)

Then with T' and B' you can draw the circle:

drawCircle(radius):
    p = []
    for i in 0 .. points:
        a = i*2*pi / points
        p.append( radius*(T*sin(a) + B*cos(a)) )
    draw_line_loop(p)
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • But, I don't know where the camera is. I'm in an OpenGL environment in world coordinates and start drawing say at coordinates (0,0,0). How do I find out the camera vector in world coordinates using OpenGL calls? – Motionbit May 01 '14 at 19:12
  • @user3593481: Well, there is no camera in OpenGL, there are just coordinate transformations. Surely you somehow can access the modelview matrix, which transforms model space coordinates into viewspace coordinates, with the viewer being at the origin. So by multiplying the center point of the sphere with the current modelview projection matrix you get that N-vector. For "Up" you can choose any vector you like to, as long as it's not colinear to N; for example start with N make an U from N with U_i:=N_j with i so the N_i is the smallest element of N and j so that N_j is the biggest element of N. – datenwolf May 01 '14 at 21:12
  • I think it's a little more complicated than what you describe if a perspective projection is used. The circle corresponding to the outline of the sphere is then not on a plane through the center anymore. Still not very difficult, but you'll have to take the distance between camera and sphere center into account as well. – Reto Koradi May 02 '14 at 04:37
  • @RetoKoradi: Yes, you're right when it comes to perspective. But essentially this boils down to finding a corrected circle center and radius, while the rest of the calculations stay the same. – datenwolf May 02 '14 at 08:37