Orthographic display

Dear Avogadro Developers,

i was very excited to see a molecular design tool with decent vector
graphics export! Thanks for that!

Now I want to have my structures in an orthographic view (for top/side
view). I tried to edit glwidget.cpp from the stable release as below.

It now seems to display stuff orthographically but it looks like depth
queuing doesn’t work anymore. Bond sticks are above their atoms and
atoms in the back are above atoms in the front. (see attached picture).

I am not that familiar with OpenGL. Do you have any quick hints on how
to fix this?

Thank you so much!
Martin

glwidget.cpp:
//I just added the two glOrtho() calls.

void GLWidget::paintGL()
{
  resizeGL(width(), height()); // fix for bug #1797069. don't remove!
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

  // setup the OpenGL projection matrix using the camera
  glMatrixMode( GL_PROJECTION );
  glLoadIdentity();
  glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  d->camera->applyPerspective();

  // setup the OpenGL modelview matrix using the camera
  glMatrixMode( GL_MODELVIEW );
  glLoadIdentity();
  d->camera->applyModelview();

  render();
}

void GLWidget::paintGL2()
{
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

  // setup the OpenGL projection matrix using the camera
  glMatrixMode( GL_PROJECTION );
  glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  glPushMatrix();
  glLoadIdentity();
  d->camera->applyPerspective();

Now I want to have my structures in an orthographic view (for top/side view). I tried to edit glwidget.cpp from the stable release as below.
It now seems to display stuff orthographically but it looks like depth queuing doesn’t work anymore. Bond sticks are above their atoms and atoms in the back are above atoms in the front. (see attached picture).

It’s not as simple as just that. The camera class would need changes at the very least – there may also be some further code in other places too (look for gluPerspective calls).

For example, here’s a description of the reverse process I found using Google for you:

Some other possibly useful links:

It’s certainly possible to add an orthographic view, but it will require some time to get familiar with OpenGL transformations. As I haven’t touched that code in ages, I’m not the best person to ask.

Hope that helps,
-Geoff

Hi all,

2011/5/20 Geoffrey Hutchison geoff.hutchison@gmail.com:

Now I want to have my structures in an orthographic view (for top/side view). I tried to edit glwidget.cpp from the stable release as below.
It now seems to display stuff orthographically but it looks like depth queuing doesn’t work anymore. Bond sticks are above their atoms and atoms in the back are above atoms in the front. (see attached picture).

I have also needed this from time to time, and the short patch below
will render an orthographic projection properly. The navigation
functions need to be changed (e.g. right now trying to zoom in will
translate the camera…). If someone wants to finish this up to make
it less of hack feel free. Otherwise I might…someday :slight_smile:

diff --git a/libavogadro/src/camera.cpp b/libavogadro/src/camera.cpp
index 6940b98…8e24ef8 100644
— a/libavogadro/src/camera.cpp
+++ b/libavogadro/src/camera.cpp
@@ -189,13 +189,29 @@ namespace Avogadro
if( d->parent == 0 ) return;
if( d->parent->molecule() == 0 ) return;

  • /// @todo This needs to be a member variable + access
    functions/signals/slots
  • bool m_useOrthographicProjection = true;
  • double molRadius = d->parent->radius() + CAMERA_MOL_RADIUS_MARGIN;
    double distanceToMolCenter = distance( d->parent->center() );
    double zNear = std::max( CAMERA_NEAR_DISTANCE,
    distanceToMolCenter - molRadius );
    double zFar = distanceToMolCenter + molRadius;
    double aspectRatio = static_cast(d->parent->width()) /
    d->parent->height();
  • gluPerspective( d->angleOfViewY, aspectRatio, zNear, zFar );
  • glGetDoublev(GL_PROJECTION_MATRIX, d->projection.data());
  • if (m_useOrthographicProjection) {
  •  double halfHeight = molRadius / aspectRatio;
    
  •  GLdouble left  = -molRadius;
    
  •  GLdouble right = molRadius;
    
  •  GLdouble bottom = -halfHeight;
    
  •  GLdouble top = halfHeight;
    
  •  qDebug() << left << right << bottom << top << zNear << zFar;
    
  •  glOrtho(left, right, bottom, top, zNear, zFar);
    
  •  glGetDoublev(GL_PROJECTION_MATRIX, d->projection.data());
    
  • }
  • else {
  •  gluPerspective( d->angleOfViewY, aspectRatio, zNear, zFar );
    
  •  glGetDoublev(GL_PROJECTION_MATRIX, d->projection.data());
    
  • }
    }

Cheers,

Dave

On Fri, May 20, 2011 at 12:12 PM, David Lonie loniedavid@gmail.com wrote:

the short patch below
will render an orthographic projection properly.

I think GMail mangled that diff up pretty badly – I’ve attached a
plain text version.

Dave

Hello all,

I am one more user interested in the orthographic projection of
chemical structures (especially periodic ones).

Some time ago I have implemented this feature as a hack, but during
the last few days I have found time to finish it, finally. It allows
to switch between perspective and orthographic projection via “View >
Projection”. And it seems all manipulation operation (zoom, rotation,
translation, etc.) works for me.

The patch is already at Gerrit! Any comment and further suggestion are welcome.

Cheers,

Raimondas

to switch between perspective and orthographic projection via “View >
Projection”. And it seems all manipulation operation (zoom, rotation,
translation, etc.) works for me.

I haven’t reviewed the code itself yet, but the feature doesn’t quite work yet. In particular, I’ve tried 3 structures so far, and with each, I can easily use the zoom and/or rotate modes of the navigate tool to end up in an invalid view.

What do I mean? Take testfiles/benzene.fchk. Rotate for a while, and suddenly the screen goes blank. I’m not even sure what happened, because I can’t get the camera back to display the molecule with the View-> Center command.

I think both the camera code and the navigate tool will need further “defensive” code to prevent the OpenGL view from becoming corrupt.

-Geoff

Hello once again,

of course, the camera class is a better palce to put this. This patch
helps me for now, I don’t really need to move/rotate the structure,
just have a picture. Thank you so far!

cheers,
Martin

David Lonie loniedavid@gmail.com:

I think GMail mangled that diff up pretty badly – I’ve attached a
plain text version.

Dave