Sophie

Sophie

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

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 multiView 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 multiView example</h1>
<center>
  <img src="../images/multiView.jpg" width="300" height="200" alt="multiView"/>
</center>

<p>
 A multi-view application, with constrained camera displacements.
</p>
<p>
 Four viewers are created, each displaying the same scene. The camera displacements are constrained
 for three of the viewers to create the classical top, front, side views. The last viewer is a
 classical 3D viewer.
</p>


<h2>multiView.h</h2>
<pre>

<span class="dir">#include &lt;QGLViewer/qglviewer.h&gt;
</span>
<span class="key">class </span>Scene
{
<span class="key">public</span>:
  <span class="typ">void </span>draw() <span class="typ">const</span>;
};

<span class="key">class </span>Viewer : <span class="key">public </span>QGLViewer
{
<span class="key">public</span>:
  Viewer(<span class="typ">const </span>Scene* <span class="typ">const </span>s, <span class="typ">int </span>type, QWidget* parent);

<span class="key">protected </span>:
  <span class="key">virtual </span><span class="typ">void </span>draw();

<span class="key">private</span>:
  <span class="typ">const </span>Scene* <span class="typ">const </span>scene_;
};
</pre>


<h2>multiView.cpp</h2>
<pre>

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

Viewer::Viewer(<span class="typ">const </span>Scene* <span class="typ">const </span>s, <span class="typ">int </span>type, QWidget* parent)
  : QGLViewer(parent), scene_(s)
{
  <span class="com">// Previous viewer geometry and window position
</span>  restoreFromFile();

  setDrawAxis();
  setDrawGrid();

  <span class="key">if </span>(type &lt; <span class="num">3</span>)
    {
      <span class="com">// Move camera according to viewer type (on X, Y or Z axis)
</span>      camera()-&gt;setPosition((type==<span class="num">0</span>)? <span class="num">1.0 </span>: <span class="num">0.0</span>, (type==<span class="num">1</span>)? <span class="num">1.0 </span>: <span class="num">0.0</span>, (type==<span class="num">2</span>)? <span class="num">1.0 </span>: <span class="num">0.0</span>);
      camera()-&gt;lookAt(sceneCenter());

      camera()-&gt;setType(Camera::ORTHO);
      camera()-&gt;showEntireScene();

      <span class="com">// Forbid rotation
</span>      WorldConstraint* constraint = <span class="key">new </span>WorldConstraint();
      constraint-&gt;setRotationConstraintType(AxisPlaneConstraint::FORBIDDEN);
      camera()-&gt;frame()-&gt;setConstraint(constraint);
    }
}

<span class="typ">void </span>Viewer::draw()
{
  scene_-&gt;draw();
}

<span class="com">// Draws a spiral
</span><span class="typ">void </span>Scene::draw() <span class="typ">const
</span>{
  <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();
}</pre>


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

<span class="dir">#include </span><span class="dstr">&quot;multiView.h&quot;</span><span class="dir">
</span><span class="dir">#include &lt;qapplication.h&gt;
</span><span class="dir">#include &lt;qsplitter.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">// Create Splitters
</span>  QSplitter *hSplit  = <span class="key">new </span>QSplitter(Qt::Vertical);
  QSplitter *vSplit1 = <span class="key">new </span>QSplitter(hSplit);
  QSplitter *vSplit2 = <span class="key">new </span>QSplitter(hSplit);

  <span class="com">// Create the scene
</span>  Scene* s = <span class="key">new </span>Scene();

  <span class="com">// Instantiate the viewers.
</span>  Viewer side  (s,<span class="num">0</span>,vSplit1);
  Viewer top   (s,<span class="num">1</span>,vSplit1);
  Viewer front (s,<span class="num">2</span>,vSplit2);
  Viewer viewer(s,<span class="num">3</span>,vSplit2);

  <span class="com">// Set main QSplitter as the main widget.
</span>  hSplit-&gt;show();
  application.setMainWidget(hSplit);

  <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>