[Qt-interest] How to draw a 2-D matrix as a 3-D surface
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Mon Apr 20 11:10:40 CEST 2009
Fabio Giovagnini wrote on Monday, April 20, 2009 10:37 AM:
> I'd like to draw a graph like the attached without usig open GL and
> without writing tons of code of course. Thanks a lot
Hmmm, to be honest, personally the OpenGL approach would be the easiest here ;) The advantage is that you don't need to worry about 3D -> 2D transformations, because that's what OpenGL is good at (and was designed for in the first place ;)
Here is a rough outline:
- Derive your custom widget from a QGLWidget
- in the initializeGL() method, setup the GL clear colour, shading model (GL_FLAT, for example)
http://doc.trolltech.com/4.5/opengl-hellogl.html#opengl-initialization
- in the resizeGL() setup an "ortographic perspective" (use google to get example code, checkout the Qt examples)
http://doc.trolltech.com/4.5/opengl-hellogl.html#resizing-the-viewport sets up an "orthographic" view, just as the one you
displayed in your screenshot and
- set a fixed camera position which looks at your "elevation field" (the graph) (you could easily add rotation, zoom etc. later on)
- in the paintGL() method, Iterate over all your points in your graph, try to center your graph at 0/0/0 (and your camera direction should point to that origin as well ;)
* x/y are the node coordinates, z is the elevation
* use GL_LINE_STRIP, for example, rendering each line in x-direction, then each line in y-direction
* as z-coordinate your graph value ("elevation") of the node in x/y is used
* also refer to http://doc.trolltech.com/4.5/opengl-hellogl.html#painting-the-scene, for example
- GL functions you are likely to use (for painting)
* glColor
* glVertex
* glBegin, glEnd
- Depending on your units in z-direction you might want to add some scale-factor as well (so you actually see the elevations better).
- http://doc.trolltech.com/4.5/qglwidget.html#renderText is useful to draw axis labels (the axis you also draw with GL_LINE
Check out the Qt examples on how to setup the camera, perspective, background colour etc. For the actual painting I would expect about roughly 50 lines of code
Setup is done in GLWidget#initializeGL(), painting is done in GLWidget#paintGL(), setup the perspective ("orthographic" or "perspective") is done in GLWidget#resizeGL().
Checkout (in german) http://wiki.delphigl.com/index.php/GlBegin for "modes" (GL_LINE, GL_LINE_STRIP, GL_TRIANGLE, ...) description, nice illustrations :)
Also checkout http://nehe.gamedev.net/lesson.asp?index=01 (you can skip the "setup OpenGL on [platform] tutorials - Qt does all that for you ;) For example start with http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=02 - and hey, "Nehe" also provides Qt examples, at the bottom of each tutorial :)
And remember, don't get addicted to the simplicity and elegance of OpenGL ;) (once you get the hang with all this "vector and matrix stuff" ;) And don't worry: if you (don't) see "a black cube in front of a black background with no light, and the camera pointing to the other end of the universe..." well, that's one of the very first lessons every OpenGL (3D programmers in general) went through ;)
Good luck, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
More information about the Qt-interest-old
mailing list