<!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 simpleViewer 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 simpleViewer example</h1> <center> <img src="../images/simpleViewer.jpg" width="300" height="200" alt="simpleViewer"/> </center> <p> The simplest application example: 20 lines of code and yet all the power ! </p> <p> A Viewer class is derived from QGLViewer and its <code>draw()</code> function is overloaded to specify the user defined OpenGL orders that describe the scene. </p> <p> This is the first example you should try, as it explains some of the default keyboard shortcuts and the mouse behavior of the viewer. </p> <p> This example can be cut and pasted to start the development of a new application. </p> <h2>simpleViewer.h</h2> <pre> <span class="dir">#include <QGLViewer/qglviewer.h> </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>draw(); <span class="key">virtual </span><span class="typ">void </span>init(); <span class="key">virtual </span>QString helpString() <span class="typ">const</span>; };</pre> <h2>simpleViewer.cpp</h2> <pre> <span class="dir">#include </span><span class="dstr">"simpleViewer.h"</span><span class="dir"> </span><span class="dir">#include <math.h> </span> <span class="key">using namespace </span>std; <span class="com">// Draws a spiral </span><span class="typ">void </span>Viewer::draw() { <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<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.0</span>-ratio, <span class="num">0.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">// Restore previous viewer state. </span> restoreFromFile(); help(); } QString Viewer::helpString() <span class="typ">const </span>{ QString text(<span class="str">"<h2>S i m p l e V i e w e r</h2>"</span>); text += <span class="str">"Use the mouse to move the camera around the object. "</span>; text += <span class="str">"You can revolve around, zoom and translate with the three buttons. "</span>; text += <span class="str">"Left and middle buttons pressed together rotate around the camera z axis<br><br>"</span>; text += <span class="str">"Pressing <b>Alt</b> and one of the function key (<b>F1</b>..<b>F12</b>) defines a camera keyFrame. "</span>; text += <span class="str">"Simply press the function key again to restore it. Several keyFrames define a "</span>; text += <span class="str">"camera path. Paths are saved when you quit the application.<br><br>"</span>; text += <span class="str">"Press <b>F</b> to display the frame rate, <b>A</b> for the world axis, "</span>; text += <span class="str">"<b>Alt+Return</b> for full screen mode and <b>Control+S</b> to save a snapshot.<br><br>"</span>; text += <span class="str">"A double click aligns the closer axis with the camera (left, if close enough), fits "</span>; text += <span class="str">"the zoom of the camera (middle) or re-center the scene (right).<br>"</span>; text += <span class="str">"A left double click while right button is pressed defines the <i>revolveAroundPoint</i>."</span>; text += <span class="str">"With middle button pressed instead, it zooms on the pixel.<br><br>"</span>; text += <span class="str">"See also the <b>Shortcuts</b> and <b>Mouse</b> tabs and the documentation web pages.<br>"</span>; text += <span class="str">"Press <b>Escape</b> to exit the viewer."</span>; <span class="key">return </span>text; }</pre> <h2>main.cpp</h2> <pre> <span class="dir">#include </span><span class="dstr">"simpleViewer.h"</span><span class="dir"> </span><span class="dir">#include <qapplication.h> </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(&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>