Sophie

Sophie

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

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

<p>
 Using the Qt signal-slot mechanism to connect a QGLViewer and your scene.
</p>
<p>
 This very simple application uses the Qt signal-slot callback mechanism to link the QGLViewer and
 your Scene. The two class hence remain totally independent. The <code>drawNeeded()</code>
 QGLViewer signal is connected to the Scene <code>drawScene()</code> slot.
</p>


<h2>callback.h</h2>
<pre>

<span class="dir">#include &lt;qobject.h&gt;
</span>
<span class="key">class </span>QGLViewer;

<span class="com">// The Scene class must derive from QObject in order to allow a
</span><span class="com">// signal slot mechanism.
</span><span class="key">class </span>Scene : <span class="key">public </span>QObject
{
  Q_OBJECT        <span class="com">// must include this in order to use Qt signals/slots
</span>
<span class="key">public </span>:
  Scene(<span class="typ">const </span>QGLViewer* <span class="typ">const </span>v);

<span class="key">public </span>slots:
  <span class="typ">void </span>drawScene();
};</pre>


<h2>callback.cpp</h2>
<pre>

<span class="dir">#include &lt;QGLViewer/qglviewer.h&gt;
</span><span class="dir">#include </span><span class="dstr">&quot;callback.h&quot;</span><span class="dir">
</span><span class="dir">#include &lt;math.h&gt;
</span>
Scene::Scene(<span class="typ">const </span>QGLViewer* <span class="typ">const </span>v)
{
  <span class="com">// Connect the viewer signal to our draw function slot
</span>  connect(v, SIGNAL(drawNeeded()), <span class="key">this</span>, SLOT(drawScene()));
}

<span class="com">// Draws a spiral
</span><span class="typ">void </span>Scene::drawScene()
{
  <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 &lt;QGLViewer/qglviewer.h&gt;
</span><span class="dir">#include &lt;qapplication.h&gt;
</span><span class="dir">#include &lt;qmessagebox.h&gt;
</span><span class="dir">#include </span><span class="dstr">&quot;callback.h&quot;</span><span class="dir">
</span>
<span class="key">using namespace </span>std;

<span class="typ">void </span>help()
{
  QString text(<span class="str">&quot;&lt;h2&gt;C a l l b a c k&lt;/h2&gt;&quot;</span>);
  text += <span class="str">&quot;This example is conceptually the same as simpleViewer.&quot;</span>;
  text += <span class="str">&quot;The difference is that it uses the Qt signal/slot mechanism &quot;</span>;
  text += <span class="str">&quot;instead of deriving the QGLViewer class. &quot;</span>;
  text += <span class="str">&quot;A Scene::draw() function is connected to the QGLViewer::drawNeeded() &quot;</span>;
  text += <span class="str">&quot;signal. The two classes are otherwise completely independant.&quot;</span>;

  QMessageBox::information(NULL, <span class="str">&quot;Callback exemple&quot;</span>, text);
}

<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>  QGLViewer v;

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

  <span class="com">// Restore the previous viewer state.
</span>  v.restoreFromFile();

  <span class="com">// Create a scene, giving a pointer to the associated viewer.
</span>  Scene s(&amp;v);

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

  help();

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