Sophie

Sophie

distrib > Mandriva > 10.0-com > i586 > by-pkgid > 9347541fe87a5ea3f3b8dbc50f660e8e > files > 144

libQGLViewer-devel-1.3.6-1mdk.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>libQGLViewer manipulatedFrame example</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link href="../qglviewer.css" rel="stylesheet" type="text/css" />
  <link rel="shortcut icon" href="../images/qglviewer.ico" type="image/x-icon" />
  <link rel="icon" href="../images/qglviewer.icon.png" type="image/png" />
</head>
<body>

<table class="banner">
  <tr>
     <td align="center"><a href="../index.html"><b>Home</b></a></td>
     <td align="center"><a href="../refManual/hierarchy.html"><b>Documentation</b></a></td>
     <td align="center"><a href="../download.html"><b>Download</b></a></td>
     <td align="center" class="qindexHL"><a href="index.html"><b>Screenshots</b></a></td>
     <td align="center"><a href="../developer.html"><b>Developer</b></a></td>
   </tr>
</table>

<h1>The manipulatedFrame example</h1>
<center>
  <img src="../images/manipulatedFrame.jpg" width="300" height="200" alt="manipulatedFrame"/>
</center>

<p>
 A <code>ManipulatedFrame</code> can be moved with the mouse in the scene.
</p>
<p>
 A manipulatedFrame converts mouse gestures into Frame displacements. Adding two lines of code will
 allow you to move any object of your scene using the mouse. The button bindings of the
 ManipulatedFrame are the same as for the camera. Spinning is possible.
</p>
<p>
 The frame is drawned at its position using a <code>glMultMatrix</code> of the frame associated
 matrix(). This matrix is changed when the mouse moves, thus changing the position of the frame.
</p>


<h2>manipulatedFrame.h</h2>
<pre>

<span class="dir">#include &lt;QGLViewer/qglviewer.h&gt;
</span>
<span class="key">class </span>Viewer : <span class="key">public </span>QGLViewer
{
<span class="key">protected </span>:
  <span class="key">virtual </span><span class="typ">void </span>init();
  <span class="key">virtual </span><span class="typ">void </span>draw();
  <span class="key">virtual </span>QString helpString() <span class="typ">const</span>;
};</pre>


<h2>manipulatedFrame.cpp</h2>
<pre>

<span class="dir">#include </span><span class="dstr">&quot;manipulatedFrame.h&quot;</span><span class="dir">
</span><span class="dir">#include &lt;math.h&gt;
</span>
<span class="key">using namespace </span>qglviewer;
<span class="key">using namespace </span>std;

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

<span class="typ">void </span>Viewer::init()
{
  <span class="com">// Swap the CAMERA and FRAME state keys (NoButton and Control)
</span>  <span class="com">// Save CAMERA binding first. See setMouseStateKey() documentation.
</span>  setMouseStateKey(QGLViewer::CAMERA, Qt::AltButton);
  setMouseStateKey(QGLViewer::FRAME,  Qt::NoButton);
  setMouseStateKey(QGLViewer::CAMERA, Qt::ControlButton);

  <span class="com">// Add a manipulated frame to the viewer.
</span>  <span class="com">// If you are not &quot;using namespace qglqglviewer&quot;, you need
</span>  <span class="com">// to specify: new qglviewer::ManipulatedFrame().
</span>  setManipulatedFrame(<span class="key">new </span>ManipulatedFrame());

  help();
  restoreFromFile();

  <span class="com">// Make world axis visible
</span>  setDrawAxis();
}

<span class="typ">void </span>Viewer::draw()
{
  <span class="com">// Here we are in the world coordinate system.
</span>  <span class="com">// Add any drawing here, such as drawSpiral();
</span>
  <span class="com">// Save the current model view matrix (not needed here in fact)
</span>  glPushMatrix();

  <span class="com">// Multiply matrix to get in the frame coordinate system.
</span>  glMultMatrixd(manipulatedFrame()-&gt;matrix());

  <span class="com">// Scale down the drawings
</span>  glScalef(<span class="num">0.3</span>, <span class="num">0.3</span>, <span class="num">0.3</span>);

  <span class="com">// Draw an axis using the QGLViewer static function
</span>  drawAxis();

  <span class="com">// Draws a frame-related spiral.
</span>  drawSpiral();

  <span class="com">// Restore the original (world) coordinate system
</span>  glPopMatrix();
}

QString Viewer::helpString() <span class="typ">const
</span>{
  QString text(<span class="str">&quot;&lt;h2&gt;M a n i p u l a t e d F r a m e&lt;/h2&gt;&quot;</span>);
  text += <span class="str">&quot;A &lt;i&gt;ManipulatedFrame&lt;/i&gt; converts mouse gestures into &lt;i&gt;Frame&lt;/i&gt; displacements.&lt;br&gt;&quot;</span>;
  text += <span class="str">&quot;Adding two lines of code will then allow you to move the objects of &quot;</span>;
  text += <span class="str">&quot;your scene using the mouse. The button bindings of the &lt;i&gt;ManipulatedFrame&lt;/i&gt; &quot;</span>;
  text += <span class="str">&quot;are the same than for the camera. Spinning is possible. &quot;</span>;
  text += <span class="str">&quot;Default key bindings have been changed in this example : press &lt;b&gt;Control&lt;/b&gt; &quot;</span>;
  text += <span class="str">&quot;while moving the mouse to move the camera.&quot;</span>;
  <span class="key">return </span>text;
}
</pre>


<h2>main.cpp</h2>
<pre>

<span class="dir">#include </span><span class="dstr">&quot;manipulatedFrame.h&quot;</span><span class="dir">
</span><span class="dir">#include &lt;qapplication.h&gt;
</span>
<span class="typ">int </span>main(<span class="typ">int </span>argc, <span class="typ">char</span>** argv)
{
  <span class="com">// Read command lines arguments.
</span>  QApplication application(argc,argv);

  <span class="com">// Instantiate the viewer.
</span>  Viewer v;

  <span class="com">// Make the viewer window visible on screen.
</span>  v.show();

  <span class="com">// Set the viewer as the application main widget.
</span>  application.setMainWidget(&amp;v);

  <span class="com">// Run main loop.
</span>  <span class="key">return </span>application.exec();
}</pre>



<p>
  <a href="index.html">Go back</a> to the examples main page
</p>

<p>
  <a href="http://validator.w3.org/check/referer"><img src="../images/xhtml.png" alt="Valid XHTML 1.0!" height="31" width="88" border="0"/></a>
  <a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="../images/css.png" width="88" height="31" alt="Valid CSS!" border="0"/></a>
<i>Last modified on Thursday, February 5, 2004.</i>
</p>

</body>
</html>