Posts RSS Comments RSS Del.icio.us Digg Technorati Blinklist Furl reddit 79 Posts and Comments till now
This wordpress theme is downloaded from wordpress themes website.

Qt OpenGL model viewer (STL)

My previous post contains notes for some reading I did to learn about Qt’s OpenGL support.  The post before that mentions integrating Open Asset Import Library (assimp) into my OpenGL SDL project.  Continuing on both themes, I wrote (from scratch) a simple STL model viewer with Qt (when I wrote this post, I used Qt Creator 3.2.0 with Qt 5.3.1).  Uses modern OpenGL (no legacy fixed function).  Uses Qt’s OpenGL classes layer when available.  Otherwise, it uses standard OpenGL.

Features include:
* load STL models and lays them out in a row with bounding box based spacing
* mouse left rotate model, mouse right translate camera, mouse wheel zoom camera, WASD rotates orbital camera (spherical coordinates)
* basic Phong lighting
* use per-face normals in STL file, or generate per-face normals, or generate per-vertex normals
* uniform scales each model within bounding box range 15x15x15 to 25x25x25
* calculate surface area, calculate volume

image image image

For this project, I originally thought of Qt as mostly an alternative to SDL – it gives an OpenGL context and user input (mouse, keyboard).  However, Qt also focuses on cross-platform GUI support, and on classes that are an alternative to C++ standard library (eg QString, QVector, QHash, QFile).  Qt has helper functions related to 3D math (QVector3D, QMatrix4x4) that integrate well with Qt’s OpenGL classes (QOpenGLWidget, QOpenGLShaderProgram, QOpenGLShader) (for my SDL project, I used GLM).  Finally, I used the cross-platform IDE Qt Creator (for my SDL project, I used Visual Studio for Windows, GCC for Linux, XCode for Mac OS X, XCode for iOS, Android SDK with ADT for Android).  So Qt made a lot of things simpler for this project.  The project’s C++ code is only about 1000 lines and one .ui file (note most of those lines are comments and formatting).  It was faster and easier to develop than the cross-platform SDL project.  The .pro build file (Qt qmake project file) is amazingly small (just a few lines).  I faced less platform-specific nuances, build setup, #ifdef’s, etc (with Qt versus my SDL project).

For volume, I summed the signed volume of a tetrahedron (for each triangle).  See (Efficient Feature Extraction for 2D/3D Objects in Mesh Representation, Cha Zhang and Tsuhan Chen ) ( https://stackoverflow.com/questions/1406029/how-to-calculate-the-volume-of-a-3d-mesh-object-the-surface-of-which-is-made-up –> http://research.microsoft.com/en-us/um/people/chazhang/publications/icip01_ChaZhang.pdf ) ( http://n-e-r-v-o-u-s.com/blog/?p=4415 ) .

For generating smooth normals…  STL specifies a triangle list with per-face normals (or we generate per-face normals).  We create a QHash that maps vertex position to (sum of un-normalized face normals).  The magnitude of cross product equals the area of a parallelogram with the vectors for sides, and that area is just double the area of the triangle.  Then we iterate the QHash (using the list of positions as the keys), and normalize each value (sum of un-normalized face normals), and append that value to the normals list (QVector<QVector3D>).

More on generating smooth normals…  If you look at my smooth normals screenshot, you’ll see that the cube doesn’t look very good – the lighting is strange and the edges look rounded.  Another idea I’ll plan to try later is an angle-weighted average.  Some links I found useful…

This post likes angle-weighted average:
http://meshlabstuff.blogspot.com/2009/04/on-computation-of-vertex-normals.html

This paper compares methods and seems to like MWA (mean weighted by angle):
http://users.tricity.wsu.edu/~bobl/personal/mypubs/2003_vertnorm_tvc.pdf

This post mentions more ideas (including weight-by-angle):
http://steve.hollasch.net/cgindex/geometry/surfnorm.html

This excellent tutorial argues that angle weights are better than area weights:
http://www.bytehazard.com/articles/vertnorm.html

This tutorial video describes using smooth shading for shallow angles:
https://www.youtube.com/watch?v=PMgjVJogIbc

Here’s another algorithm that uses angle as a threshold:
https://user.xmission.com/~nate/smooth.html

Generating normals should be a fun topic to learn more about.  It sounds like there might not be one standard simple solution.  So I’ll plan to look for more reading etc with searches like (research smooth normals) and (research generating normals).

Trackback this post | Feed on Comments to this post

Leave a Reply