Sophie

Sophie

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

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 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 &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>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">&quot;textureViewer.h&quot;</span><span class="dir">
</span>
<span class="dir">#include &lt;qimage.h&gt;
</span><span class="dir">#include &lt;qfiledialog.h&gt;
</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">&quot;.&quot;</span>, <span class="str">&quot;Images (*.png *.xpm *.jpg)&quot;</span>, <span class="key">this</span>, <span class="str">&quot;Choose&quot;</span>, <span class="str">&quot;Select an image&quot;</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 &lt;&lt; <span class="str">&quot;Unable to load &quot;</span> &lt;&lt; name &lt;&lt; <span class="str">&quot;, unsupported file format&quot;</span> &lt;&lt; endl;
      <span class="key">return</span>;
    }

  cout &lt;&lt; <span class="str">&quot;Loading &quot;</span> &lt;&lt; name &lt;&lt; <span class="str">&quot;, &quot;</span> &lt;&lt; img.width() &lt;&lt; <span class="str">&quot;x&quot;</span> &lt;&lt; img.height() &lt;&lt; <span class="str">&quot; pixels&quot;</span> &lt;&lt; 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>&lt;&lt;(<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>&lt;&lt;(<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 &lt;&lt; <span class="str">&quot;Image size set to &quot;</span> &lt;&lt; newWidth &lt;&lt; <span class="str">&quot;x&quot;</span> &lt;&lt; newHeight &lt;&lt; <span class="str">&quot; pixels&quot;</span> &lt;&lt; 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-&gt;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">&quot;&lt;h2&gt;T e x t u r e V i e w e r&lt;/h2&gt;&quot;</span>);
  text += <span class="str">&quot;This pedagogical example illustrates how to texture map a polygon.&lt;br&gt;&quot;</span>;
  text += <span class="str">&quot;The Qt &lt;i&gt;QImage&lt;/i&gt; class and its &lt;i&gt;convertToGLFormat()&lt;/i&gt; function are used &quot;</span>;
  text += <span class="str">&quot;to load an image in any format. The image is resized so that its dimensions &quot;</span>;
  text += <span class="str">&quot;are powers of two if needed. Feel free to cut and paste.&lt;br&gt;&quot;</span>;
  text += <span class="str">&quot;Press &lt;b&gt;L&lt;/b&gt;(oad) to load a new image.&quot;</span>;
  <span class="key">return </span>text;
}</pre>


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

<span class="dir">#include </span><span class="dstr">&quot;textureViewer.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, show it on screen.
</span>  Viewer viewer;
  viewer.show();

  <span class="com">// Set the viewer as the application main widget.
</span>  application.setMainWidget(&amp;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>