This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Skybox With A Single Quad
  Submitted by



A skybox is really a cube map, so if you have access to hardware that does cube mapping, why not use it?

My attempt at this looks like the following:

        Matrix cam, ocam;	// vanilla 4x4 matrix class
        Vector texcoords[4];	// vanilla 4 vectors

        ocam.getModelview();	// save the modelview matrix
        cam=ocam;		// copy it

        cam.c41=0.0;		// remove the translation part
        cam.c42=0.0;
        cam.c43=0.0;

cam.invert3d(); // invert it, assuming some niceties glLoadIdentity(); // no modelview float x=maxd/mind; // mind..maxd are depth range float y=aspect*x; // aspect is aspect ratio (all from glFrustum) // here we generate texture coordinates that point along the // edges of the view frustum (ie including the rotation of // the view frustum, but no translation) texcoords[0]=cam*Vector(-1, aspect, -1); // upper left texcoords[1]=cam*Vector(-1, -aspect,-1); // lower left texcoords[2]=cam*Vector(1, -aspect, -1); // lower right texcoords[3]=cam*Vector(1, aspect, -1); // upper right glDepthFunc(GL_ALWAYS); // don't need to clear depth buffer glDisable(GL_LIGHTING); env.notify(); // this binds the cubemap texture // and sets the texture environment GL_REPLACE glBegin(GL_QUADS); // just draw a full screen glTexCoord3fv(texcoords[0].xyz); // quad using the method of your glVertex3f(-x, y, -maxd); // choice. I've used GL immediate glTexCoord3fv(texcoords[1].xyz); // mode for clarity glVertex3f(-x, -y, -maxd); glTexCoord3fv(texcoords[2].xyz); glVertex3f(x, -y, -maxd); glTexCoord3fv(texcoords[3].xyz); glVertex3f(x, y, -maxd); glEnd();

env.endnotify(); // turn off the texture glEnable(GL_LIGHTING); glDepthFunc(GL_LESS); // normal depth buffering ocam.load(); // restore modelview

This assumes knowledge of the projection matrix (ie it was generated as a perspective frustum) and doesn't attempt to extract it from that matrix. It may not be a performance boost - inverting the matrix might be nearly as costly as generating the extra quads and cube mapping might not be fast on your hardware.

You could also put that inverted modelview matrix onto the texture matrix stack and use TexGen for the coordinates. Whatever floats your boat.

William Brodie-Tyrrell


The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.