Posts RSS Comments RSS Digg Technorati Blinklist Furl reddit 95 Posts and 7 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 ) ( –> ) ( ) .

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:

This paper compares methods and seems to like MWA (mean weighted by angle):

This post mentions more ideas (including weight-by-angle):

This excellent tutorial argues that angle weights are better than area weights:

This tutorial video describes using smooth shading for shallow angles:

Here’s another algorithm that uses angle as a threshold:

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