Render a 3D model with the same size regardless of camera position

920 views Asked by At

I've got a particular model that acts as controls in the viewer. The user can click on different parts of it to perform transformations on another model in the viewer (like controls/handles in applications like Unity or Blender).

We'd like the controls to remain the same size regardless how zoomed in/out the camera is. I've tried scaling the size of it based on the distance between the object and the camera but it isn't quite right. Is there a standard way of accomplishing something like this?

The controls are rendered using the fixed pipeline, but we've got other components using the programmable pipeline.

2

There are 2 answers

3
Xirema On

The easy answer is "use the programmable pipeline" because it's not that difficult to write

if(normalObject) {
    gl_Position = projection * view * model * vertex;
} else {
    gl_Position = specialMVPMatrix * vertex;
}

Whereas you'll spend a lot more code trying to get this to work in the Fixed-Function-Pipeline and plenty more CPU cycles rendering it.

0
Reto Koradi On

With the fixed pipeline, the easiest way to do this is to simply not apply any transformations when you render the controls:

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

// draw controls

glPopMatrix();

glMatrixMode(GL_MODELVIEW);
glPopMatrix();

The glPushMatrix()/glPopMatrix() calls will make sure that the previous matrices are restored at the end of this code fragment.

With no transformation at all, the range of coordinates mapped to the window will be [-1.0 .. 1.0] in both coordinate directions. If you need anything else, you can apply the necessary transformations before you start drawing the controls.