Sophie

Sophie

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

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>QGLViewer - Commented code</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 class="qindex" href="index.html"><b>Home</b></a></td>
     <td align="center"><a class="qindex" href="refManual/hierarchy.html"><b>Documentation</b></a></td>
     <td align="center"><a class="qindex" href="download.html"><b>Download</b></a></td>
     <td align="center"><a class="qindex" href="examples/index.html"><b>Screenshots</b></a></td>
     <td align="center"><a class="qindex" href="developer.html"><b>Developer</b></a></td>
   </tr>
</table>

<h1>QGLViewer - Commented code</h1>

<h2>Simple example</h2>

In your applications, you will mainly use the <code>QGLViewer</code> class, which opens an OpenGL window
and lets you draw 3D geometry inside. It handles the mouse and keyboard to move the camera and to define shortcuts,
thus providing some classical functions of a 3D viewer (although everything can be redefined). <code>QGLViewer</code> actually
inherits from the Qt's <code>QGLWidget</code> class (use the Qt's <code>assistant</code> to see its documentation).

<br/><br/>

A very simple application will look like <a href="examples/simpleViewer.html">simpleViewer</a>. Here
is a typical header:

<pre>
<strong>class</strong> <font color="#2040a0">Viewer</font> <font color="4444FF">:</font> <strong>public</strong> <font color="#2040a0">QGLViewer</font>
<font color="4444FF"><strong>{</strong></font>
<strong>protected</strong> <font color="4444FF">:</font>
  <strong>void</strong> <font color="#2040a0">draw</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF">;</font>
<font color="4444FF"><strong>}</strong></font><font color="4444FF">;</font>
</pre>
In your header file, you declare a new class (<code>Viewer</code>) which publicly inherits from
<code>QGLViewer</code> and you redefine the <code>draw()</code> function.

<br/><br/>

The <code>draw()</code> function is the heart of your program. Your <code>Viewer</code> implementation is as simple as this:
<pre>
<font color="0000ff"><strong>#include <font color="#008000">&quot;viewer.h&quot;</font></strong></font>

<strong>void</strong> <font color="#2040a0">Viewer</font><font color="4444FF">:</font><font color="4444FF">:</font><font color="#2040a0">draw</font><font color="4444FF">(</font><font color="4444FF">)</font>
<font color="4444FF"><strong>{</strong></font>
  <font color="#444444">// Your OpenGL 3d code goes here.</font>
<font color="4444FF"><strong>}</strong></font>
</pre>

<br/>
You then create a <code>main</code> function which uses the <code>Viewer</code> class (most <code>main</code> functions will be like this, simply cut and paste):
<pre>
<font color="0000ff"><strong>#include <font color="#008000">&lt;qapplication.h&gt;</font></strong></font>
<font color="0000ff"><strong>#include <font color="#008000">&quot;viewer.h&quot;</font></strong></font>

<strong>int</strong> <font color="#2040a0">main</font><font color="4444FF">(</font><strong>int</strong> <font color="#2040a0">argc</font>, <strong>char</strong><font color="4444FF">*</font><font color="4444FF">*</font> <font color="#2040a0">argv</font><font color="4444FF">)</font>
<font color="4444FF"><strong>{</strong></font>
  <font color="#444444">// Read command lines arguments.</font>
  <font color="#2040a0">QApplication</font> <font color="#2040a0">application</font><font color="4444FF">(</font><font color="#2040a0">argc</font>,<font color="#2040a0">argv</font><font color="4444FF">)</font><font color="4444FF">;</font>

  <font color="#444444">// Instantiate the viewer.</font>
  <font color="#2040a0">Viewer</font> <font color="#2040a0">v</font><font color="4444FF">;</font>

  <font color="#444444">// Make the viewer window visible on screen.</font>
  <font color="#2040a0">v</font>.<font color="#2040a0">show</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF">;</font>
  
  <font color="#444444">// Set the viewer as the application main widget.</font>
  <font color="#2040a0">application</font>.<font color="#2040a0">setMainWidget</font><font color="4444FF">(</font><font color="4444FF">&amp;</font><font color="#2040a0">v</font><font color="4444FF">)</font><font color="4444FF">;</font>
  
  <font color="#444444">// Run main loop.</font>
  <strong>return</strong> <font color="#2040a0">application</font>.<font color="#2040a0">exec</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF">;</font>
<font color="4444FF"><strong>}</strong></font>
</pre>

<p>

You just have to concentrate on the scene description and on the OpenGL orders that represent it.
All the rest comes for free : camera displacement with the mouse in revolve or walk through mode,
camera keyFrames paths, screenshots, frame per seconds display, objects of the scene that can be
manipulated with the mouse, and much more.

</p><p>

The <code>draw()</code> function is actually <i>one</i> of the functions that can be redefined to change
the viewer's behavior. You will discover in the <a href="features.html">feature list</a> some of the
other convenient functions.
</p>

<h2>Heritage versus Signals</h2>

There are actually two ways of using the <code>QGLViewer</code> class in your applications :
<ul>
<li> Derive your own viewer class from <code>QGLViewer</code> and overload the methods you need
(<code>init(), draw(), ...</code>). The <a href="examples/simpleViewer.html">simpleViewer</a>
example is an illustration of this method.</li>
<li> Use the QT signal-slot mechanism and connect your own methods to the signals emitted by an
instance of <code>QGLViewer</code>. See the <a href="examples/callback.html">callback</a> example for details.</li>
</ul>

In order to offer those two mechanisms, the <code>QGLViewer</code> class emits different signals in its
key methods. When you derive and overload these functions, you replace this behavior by your own implementation.

<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" alt="Valid CSS!" width="88" height="31" border="0"/></a>
  <i>Last modified on Tuesday, October 21, 2003.</i>
</p>

</body></html>