<!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 textureViewer 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 textureViewer example</h1> <center> <img src="../images/textureViewer.jpg" width="300" height="200" alt="textureViewer"/> </center> <p> Loads image files and textures map polygons with it. </p> <p> Pedagogical example that shows how to apply a texture on a 3D object. Inpired from the Qt texture example. </p> <p> The Qt QImage class and its <code>convertToGLFormat()</code> function are used to load any image format. The image is resized so that its dimensions are powers of two if needed. Feel free to cut and paste. </p> <h2>textureViewer.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>init(); <span class="key">virtual </span><span class="typ">void </span>draw(); <span class="key">virtual </span><span class="typ">void </span>keyPressEvent(QKeyEvent *e); <span class="key">virtual </span>QString helpString() <span class="typ">const</span>; <span class="typ">void </span>loadImage(); <span class="key">private </span>: <span class="typ">float </span>ratio, u_max, v_max; };</pre> <h2>textureViewer.cpp</h2> <pre> <span class="dir">#include </span><span class="dstr">"textureViewer.h"</span><span class="dir"> </span> <span class="dir">#include <qimage.h> </span><span class="dir">#include <qfiledialog.h> </span> <span class="key">using namespace </span>qglviewer; <span class="key">using namespace </span>std; <span class="typ">void </span>Viewer::init() { restoreFromFile(); <span class="com">// Enable GL textures </span> glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glEnable( GL_TEXTURE_2D ); <span class="com">// Nice texture coordinate interpolation </span> glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); u_max = <span class="num">1.0</span>; v_max = <span class="num">1.0</span>; ratio = <span class="num">1.0</span>; loadImage(); help(); } <span class="typ">void </span>Viewer::draw() { <span class="com">// Display the quad </span> glNormal3f(<span class="num">0.0</span>, <span class="num">0.0</span>, <span class="num">1.0</span>); glBegin(GL_QUADS); glTexCoord2f(<span class="num">0.0</span>, <span class="num">1.0</span>-v_max); glVertex2f(-u_max*ratio,-v_max); glTexCoord2f(<span class="num">0.0</span>, <span class="num">1.0</span>); glVertex2f(-u_max*ratio, v_max); glTexCoord2f(u_max, <span class="num">1.0</span>); glVertex2f( u_max*ratio, v_max); glTexCoord2f(u_max, <span class="num">1.0</span>-v_max); glVertex2f( u_max*ratio,-v_max); glEnd(); } <span class="typ">void </span>Viewer::loadImage() { QString name = QFileDialog::getOpenFileName(<span class="str">"."</span>, <span class="str">"Images (*.png *.xpm *.jpg)"</span>, <span class="key">this</span>, <span class="str">"Choose"</span>, <span class="str">"Select an image"</span>); <span class="com">// In case of Cancel </span> <span class="key">if </span>(name.isEmpty()) <span class="key">return</span>; QImage img(name); <span class="key">if </span>(img.isNull()) { cerr << <span class="str">"Unable to load "</span> << name << <span class="str">", unsupported file format"</span> << endl; <span class="key">return</span>; } cout << <span class="str">"Loading "</span> << name << <span class="str">", "</span> << img.width() << <span class="str">"x"</span> << img.height() << <span class="str">" pixels"</span> << endl; <span class="com">// 1E-3 needed. Just try with width=128 and see ! </span> <span class="typ">int </span>newWidth = <span class="num">1</span><<(<span class="typ">int</span>)(<span class="num">1</span>+log(img.width() <span class="num">-1</span>+<span class="num">1</span>E-3) / log(<span class="num">2.0</span>)); <span class="typ">int </span>newHeight = <span class="num">1</span><<(<span class="typ">int</span>)(<span class="num">1</span>+log(img.height()<span class="num">-1</span>+<span class="num">1</span>E-3) / log(<span class="num">2.0</span>)); u_max = img.width() / (<span class="typ">float</span>)newWidth; v_max = img.height() / (<span class="typ">float</span>)newHeight; <span class="key">if </span>((img.width()!=newWidth) || (img.height()!=newHeight)) { cout << <span class="str">"Image size set to "</span> << newWidth << <span class="str">"x"</span> << newHeight << <span class="str">" pixels"</span> << endl; img = img.copy(<span class="num">0</span>, <span class="num">0</span>, newWidth, newHeight); } ratio = newWidth / (<span class="typ">float</span>)newHeight; QImage glImg = QGLWidget::convertToGLFormat(img); <span class="com">// flipped 32bit RGBA </span> <span class="com">// Bind the img texture... </span> glTexImage2D(GL_TEXTURE_2D, <span class="num">0</span>, <span class="num">4</span>, glImg.width(), glImg.height(), <span class="num">0</span>, GL_RGBA, GL_UNSIGNED_BYTE, glImg.bits()); } <span class="typ">void </span>Viewer::keyPressEvent(QKeyEvent *e) { <span class="key">switch </span>(e->key()) { <span class="key">case </span>Qt::Key_L : loadImage(); <span class="key">break</span>; <span class="key">default</span>: QGLViewer::keyPressEvent(e); } } QString Viewer::helpString() <span class="typ">const </span>{ QString text(<span class="str">"<h2>T e x t u r e V i e w e r</h2>"</span>); text += <span class="str">"This pedagogical example illustrates how to texture map a polygon.<br>"</span>; text += <span class="str">"The Qt <i>QImage</i> class and its <i>convertToGLFormat()</i> function are used "</span>; text += <span class="str">"to load an image in any format. The image is resized so that its dimensions "</span>; text += <span class="str">"are powers of two if needed. Feel free to cut and paste.<br>"</span>; text += <span class="str">"Press <b>L</b>(oad) to load a new image."</span>; <span class="key">return </span>text; }</pre> <h2>main.cpp</h2> <pre> <span class="dir">#include </span><span class="dstr">"textureViewer.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, show it on screen. </span> Viewer viewer; viewer.show(); <span class="com">// Set the viewer as the application main widget. </span> application.setMainWidget(&viewer); <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>