void loadTexture2D ( QString fname )
{
	QImage tex, buf;
	
	// Try to load the texture
	// If it fails, fill in a dummy texture
	if ( !buf.load( fname ) ) {
		qWarning( "Could not read image file, using single-color instead." );
		QImage dummy( 128, 128, 32 );
		dummy.fill( Qt::green.rgb() );
		buf = dummy;
	}
	
	// Textures in OpenGL must have dimensions that are powers of 2
	buf.smoothScale(512, 512);
	
	// Use an internal Qt function to convert to the OpenGL texture format
	tex = QGLWidget::convertToGLFormat( buf );  // flipped 32bit RGBA
	
	// Set up the parameters for texturing
	glBindTexture(GL_TEXTURE_2D, numTextures++);
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	
	// Transfer the texture to OpenGL
	glTexImage2D( GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0,
		GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );
}

