[Kde-games-devel] Ksudoku, Kubrik 3D background

Ian Wadham ianw2 at optusnet.com.au
Tue Jun 17 07:27:06 CEST 2008


On Fri, 16 May 2008 02:22 am, Matthew Woehlke wrote:
> Your pipeline should look like this:
>
> - load 2d matrix, viewport coordinates
> - enable texturing
> - draw background
> - [disable texturing; if not being used for the scene]
> - load 3d [perspective] matrix, scene coordinates
> - enable lighting, etc
> - draw scene
> - disable lighting, etc
>
Well, I finally sat down to have a go at this yesterday.  After a few hours
fiddling around trying to draw a simple colored rectangle in 2D (even
switching off all of the 3D graphics in Kubrick), I was unable to get
anything drawn in 2D to appear on the QGLWidget.

Finally I decided to check the Qt doco for QGLWidget, followed the More...
link and read down to the section headed "Painting Techniques".

It says there that it is "possible" to draw 2D graphics onto a QGLWidget
subclass, but you must reimplement QGLWidget::paintEvent() and use
QPainter !  Furthermore, the section seems to imply that you can only
paint on top of the 3D stuff, which is no use for a background.  For
instance, the "Overpainting Example" in the OpenGL example programs
shows how to paint translucent 2D bubbles in front of a 3D logo.

So I used the following idea, with some of Matthew's mods:

   - load a background picture into an OpenGL texture
   - compute a rectangle in 3D that will fill the viewport behind the cubes
   - disable lighting
   - enable OpenGL texturing
   - draw the rectangle, applying the OpenGL texture that has the background
   - disable OpenGL texturing
   - enable lighting
   - 3D drawing as usual

That worked fine.   The rectangle has 3D vertices, all with z = 2 * c, where
c is the z-value for the centres of the cubes.  The algebra is a bit tricky,
but the required code is only about 40 lines.  The picture is rendered
faithfully (no distortions or color/lighting changes).  Resizing is blindingly
fast, if you stay with OpenGL's texture-rendering and do not reload SVG.

AFAICT quality is good at all sizes.  I started with a 512x410 image in a
512x512 texture and used window sizes up to full screen on 1280x800.
The image I used was the "desert sands at night" background from
Eugene's Egypt theme for KGoldrunner.  It looked stunning ...

I'll wait a week or so to commit this, as I do not want to release it with the
Beta 2 of KDE 4.1.  Eugene will need time to draw the final background.
Also the code needs polishing and the addition of SVG loading.

All the best, Ian W.


More information about the kde-games-devel mailing list