Home Documentation Download Screenshots Developper

manipulatedFrame

manipulatedFrame

A ManipulatedFrame can be moved with the mouse in the scene.

This example illustrates the mouse driven displacement of a frame in the scene. The frame is moved by pressing the Alt key while using the mouse (the key can be changed through the constant FRAME_MOUSE_MOVE_STATE_KEY).

The frame is drawned at its position using a glMultMatrix of the frame associated matrix. This matrix will be changed when the mouse moves, thus changing the position of the frame.

We use a glPushMatrix and glPopMatrix to save and restore the model view matrix. It is not needed here as nothing is drawn after the frame, but it is a good habbit for the general cases.

manipulatedFrame.h

#include "qglviewer.h"

class Viewer : public QGLViewer
{
protected :
  void init();
  void draw();
  void help();
};

main.cpp

#include "manipulatedFrame.h"
#include <qapplication.h>

int main(int argc, char** argv)
{
  // Read command lines arguments.
  QApplication application(argc,argv);

  // Instantiate the viewer.
  Viewer v;

  // Make the viewer window visible on screen.
  v.show();

  // Set the viewer as the application main widget.
  application.setMainWidget(&v);
  
  // Run main loop.
  return application.exec();
}

manipulatedFrame.cpp

#include "manipulatedFrame.h"
#include <math.h>

using namespace qglviewer;
using namespace std;

static void drawSpiral()
{
  const float nbSteps = 500.0;
  glBegin(GL_QUAD_STRIP);
  for (float i=0; i<nbSteps; ++i)
    {
      float ratio = i/nbSteps;
      float angle = 20.0*ratio;
      float c = cos(angle);
      float s = sin(angle);
      float r1 = 1.0 - 0.8*ratio;
      float r2 = 0.8 - 0.8*ratio;
      float alt = ratio - 0.5;
      const float nor = .5;
      const float up = sqrt(1.0-nor*nor);
      glColor3f(1-ratio, .2 , ratio);
      glNormal3f(nor*c, up, nor*s);
      glVertex3f(r1*c, alt, r1*s);
      glVertex3f(r2*c, alt+0.05, r2*s);
    }
  glEnd();
}

void Viewer::init()
{
  // Make world axis visible
  setDrawAxis();

  // Uncomment this line if the Alt-click is intercepted by your window manager
  // setMouseStateKey(QGLViewer::FRAME,  Qt::ControlButton);

  
  // Add a manipulated frame to the viewer.
  // If you are not "using namespace qglqglviewer", you need
  // to specify: new qglviewer::ManipulatedFrame().
  setManipulatedFrame(new ManipulatedFrame());

  help();
}

void Viewer::draw()
{
  // Here we are in the world coordinate system.
  // Add any drawing here, such as drawSpiral();
  
  // Switch to the selected frame coordinate system
  // First select the model view matrix (useless here as preDraw() is unchanged)
  glMatrixMode(GL_MODELVIEW);
  
  // Save the current model view matrix.
  glPushMatrix();
  
  // Change matrix to get in the frame coordinate system.
  glMultMatrixd(manipulatedFrame()->matrix());
  
  // Scale down the drawings
  glScalef(0.5,0.5,0.5);

  // Draw an axis using the QGLViewer static function
  drawAxis();

  // Draw a (frame-related) spiral.
  drawSpiral();
  
  // Restore the original (world) coordinate systems
  glPopMatrix();
}

void Viewer::help()
{
  cout << endl << "\t\t- -  M a n i p u l a t e d F r a m e  - -" << endl << endl;
  cout << "Press Alt while moving the mouse to move the local frame." << endl;
  cout << "The button bindings are the same as for the camera. Spinning is possible." << endl << endl;
  cout << "Your window manager may intercept the Alt-click, and you may have to edit" << endl;
  cout << "manipulatedFrame.cpp or to change your window behavior (modifier key) configuration" << endl << endl;
}

Back to the main page

Valid XHTML 1.0! Valid CSS!