Sophie

Sophie

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

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

<p>
 The <code>fastDraw()</code> function keeps interactivity even with large scenes.
</p>
<p>
 This example demonstrates the use of the <code>fastDraw()</code> function, which 
 is called when the camera moves. This function is usefull for displaying very
 complex scene, while keeping an interactive camera motion.
</p>


<h2>fastDraw.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><span class="typ">void </span>fastDraw();
  <span class="key">virtual </span>QString helpString() <span class="typ">const</span>;
};</pre>


<h2>fastDraw.cpp</h2>
<pre>

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

<span class="typ">void </span>Viewer::init()
{
  <span class="com">// Increase the material shininess, so that the difference between
</span>  <span class="com">// the two versions of the spiral is more visible.
</span>  glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, <span class="num">50.0</span>);
  GLfloat specular_color[<span class="num">4</span>] = { <span class="num">0.8</span>, <span class="num">0.8</span>, <span class="num">0.8</span>, <span class="num">1.0 </span>};
  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,  specular_color);

  restoreFromFile();
  help();
}

<span class="typ">static void </span>drawSpiral(<span class="typ">bool </span>simplified = <span class="key">false</span>)
{
  <span class="typ">int </span>nbSteps = <span class="num">600</span>;
  <span class="typ">int </span>nbSub = <span class="num">50</span>;
  <span class="key">if </span>(simplified)
    {
      nbSteps = <span class="num">60</span>;
      nbSub = <span class="num">2</span>;
    }

  <span class="key">for </span>(<span class="typ">float </span>n=<span class="num">0</span>; n&lt;nbSub; ++n)
    {
      glBegin(GL_QUAD_STRIP);
      <span class="key">for </span>(<span class="typ">float </span>i=<span class="num">0.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>radius = <span class="num">1.0 </span>- <span class="num">0.5</span>*ratio;

	  Vec center(radius*cos(angle), ratio-0.<span class="num">5</span>, radius*sin(angle));

	  <span class="key">for </span>(<span class="typ">unsigned short </span>j=<span class="num">0</span>; j&lt;<span class="num">2</span>; ++j)
	    {
	      <span class="typ">float </span>delta = <span class="num">3.0</span>*(n+j)/nbSub;
	      Vec shift(cos(angle)*cos(delta), sin(delta), sin(angle)*cos(delta));

	      glColor3f(<span class="num">1</span>-ratio, (n+j)/nbSub , ratio);
	      glNormal3fv(shift.address());
	      glVertex3fv((center+<span class="num">0.2</span>*shift).address());
	    }
	}
      glEnd();
    }
}

<span class="typ">void </span>Viewer::draw()
{
  drawSpiral();
}

<span class="typ">void </span>Viewer::fastDraw()
{
  drawSpiral(<span class="key">true</span>);
}

QString Viewer::helpString() <span class="typ">const
</span>{
  QString text(<span class="str">&quot;&lt;h2&gt;F a s t D r a w&lt;/h2&gt;&quot;</span>);
  text += <span class="str">&quot;The &lt;i&gt;fastDraw()&lt;/i&gt; function is called instead of &lt;i&gt;draw()&lt;/i&gt; when the camera is manipulated, &quot;</span>;
  text += <span class="str">&quot;thus allowing interactive displacements even with very complex scenes, as soon as you provide a fast drawing function.&quot;</span>;
  <span class="key">return </span>text;
}</pre>


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

<span class="dir">#include </span><span class="dstr">&quot;fastDraw.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>