3D Skyboxes

Discuss programming topics for the various GPL'd game engine sources.
Sajt
Posts: 1215
Joined: Sat Oct 16, 2004 3:39 am

Post by Sajt »

The reason for using skyrooms is to include actual geometry like mountains and distant buildings. Your 'skydome' idea sounds pretty much like what Quake has by default. The reason for multiple skyrooms is you can have some nearby distant buildings in one, then distant distant buildings in another, or something similar. Mappers could think of more ingenious and trippy uses for it.

A skyroom is not 'worse' than a '2D skybox'. A regular skybox can be replicated by just making a textured box and putting the view origin in the middle, and setting the parallax scroll factor (or whatever) to 0.

The only benefit I can see for the 'only one skyroom allowed' hack is that Quake has no good way for you to specify which skyroom a sky polygon should point to... (barring more hacks)
F. A. Špork, an enlightened nobleman and a great patron of art, had a stately Baroque spa complex built on the banks of the River Labe.
Preach
Posts: 122
Joined: Thu Nov 25, 2004 7:20 pm

Post by Preach »

But you could put the near-distant and the distant buildings into the same skyroom and achieve the same effect, it's unnecessary.
Sajt
Posts: 1215
Joined: Sat Oct 16, 2004 3:39 am

Post by Sajt »

Your face is unnecessary.
F. A. Špork, an enlightened nobleman and a great patron of art, had a stately Baroque spa complex built on the banks of the River Labe.
mh
Posts: 2292
Joined: Sat Jan 12, 2008 1:38 am

Post by mh »

Spike wrote:Just draw the sky surfaces like normal... except draw them first and turn the colour buffer writes off. glColorMask(0,0,0,0). then when you draw the rest of the world, the normal z buffer tests will prevent anything behind the sky from being drawn.
That's an interesting trick. You can also use a variation on it to mask out sky polys that would otherwise be covered by the world, very useful if you have to render a full rotating sky box or sky sphere.

The process is:

* Draw regular sky polys with colormask 0.
* Invert the depth func.
* Draw the box/sphere/whatever. For a dual layer box/sphere, draw the back layer nearer than the front, to match the inverted depth func.
* Switch the depth func back to normal.
* Draw regular sky polys with colormask 0 again.
* Draw the world.

Here it is in the start room of e4m7 (with the world render turned off):

Image

And here's code for a working implementation (based off my previous sky sphere tutorial on QuakeSrc.org, so if you've implemented that you should be able to do this too):

Code: Select all

/*
=================
R_DrawSkyChain
=================
*/
cvar_t r_skyspeed = {"r_skyspeed", "2.5", true};

float rotateBack = 0;
float rotateFore = 0;

float skytexes[440];
float skyverts[660];

void R_DrawSkyArrays (int texnum, float bigscale, float smallscale, float rotatefactor)
{
	// go to a new matrix
	glPushMatrix ();

	// center it on the players position, scale it, orient so the poles are unobtrusive,
	// make it not always at right angles to the player, and rotate it around the poles
	glTranslatef (r_origin[0], r_origin[1], r_origin[2]);
	glTranslatef (0, 0, -500);
	glScalef (bigscale, bigscale, smallscale);
	glRotatef (-90, 1, 0, 0);
	glRotatef (-22, 0, 1, 0);
	glRotatef (rotatefactor, 0, 0, 1);

	// bind the correct texture
	glBindTexture (GL_TEXTURE_2D, texnum);

	// draw the sphere
	// (if this barfs on your implementation, just unroll the single call into 10 batches of 22)
	glDrawArrays (GL_TRIANGLE_STRIP, 0, 220);

	// restore the previous matrix
	glPopMatrix ();
}


void R_ClipSky (msurface_t *skychain)
{
	int i;
	glvertex_t *v;
	msurface_t	*surf;
	glpoly_t *p;

	// disable texturing and writes to the color buffer
	glDisable (GL_TEXTURE_2D);
	glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

	for (surf = skychain; surf; surf = surf->texturechain)
	{
		for (p = surf->polys; p; p = p->next)
		{
			int vpos = 0;

			for (i = 0, v = p->verts; i < p->numverts; i++, v++)
			{
				VArrayVerts[vpos++] = v->tv[0];
				VArrayVerts[vpos++] = v->tv[1];
				VArrayVerts[vpos++] = v->tv[2];
			}

			glDrawArrays (GL_TRIANGLE_FAN, 0, p->numverts);
		}
	}

	// revert
	glEnable (GL_TEXTURE_2D);
	glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

	// need to reset primary colour to full as well
	// as colormask can set it to black on some implementations
	glColor3f (1, 1, 1);
}


void R_DrawSkyChain (msurface_t *skychain)
{
	// sky scaling
	float fullscale;
	float halfscale;
	float reducedfull;
	float reducedhalf;

	// do these calcs even if we're not drawing
	// sky rotation
	// bound rotation speed 0 to 100
	if (r_skyspeed.value < 0) r_skyspeed.value = 0;
	if (r_skyspeed.value > 100) r_skyspeed.value = 100;

	// always rotate even if we're paused!!!
	rotateBack = anglemod (realtime * (2.5 * r_skyspeed.value));
	rotateFore = anglemod (realtime * (4.0 * r_skyspeed.value));

	// no sky to draw
	if (!skychain) return;

	// need this as a baseline for everything
	glEnableClientState (GL_VERTEX_ARRAY);
	glVertexPointer (3, GL_FLOAT, sizeof (float) * 3, VArrayVerts);

	// write the regular sky polys into the depth buffer to get a baseline
	R_ClipSky (skychain);

	// calculate the scales in proportion to the far clipping plane for the world
	fullscale = r_farclip / 4096;
	halfscale = r_farclip / 8192;
	reducedfull = (r_farclip / 4096) * 0.9;
	reducedhalf = (r_farclip / 8192) * 0.9;

	// switch the depth func so that the regular polys will prevent sphere polys outside their area reaching the framebuffer
	glDepthFunc (GL_GEQUAL);
	glDepthMask (GL_FALSE);

	// sky texture scaling
	// note that in terms of size this is *NOT* visually the same as the "classic" quake sky - drawing it on a 
	// proper sphere generates an "obviously a tiled texture" look if we shrink it more, and looks very wrong, 
	// so we go for somewhat larger instead.  increase to 8, 4, 4 if that really bothers you.
	glMatrixMode (GL_TEXTURE);
	glLoadIdentity ();
	glScalef (6, 3, 3);
	glMatrixMode (GL_MODELVIEW);

	// switch vertex pointers
	glVertexPointer (3, GL_FLOAT, 3 * sizeof (float), skyverts);

	glEnableClientState (GL_TEXTURE_COORD_ARRAY);
	glTexCoordPointer (2, GL_FLOAT, 2 * sizeof (float), skytexes);

	// background
	// here we invert the sphere sizes to match the inverted depth func, so that the smaller sphere is actually the back texture!
	R_DrawSkyArrays (solidskytexture->texnum, reducedfull, reducedhalf, -rotateBack);

	// enable blending for the alpha sky
	glEnable (GL_BLEND);
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	// foreground
	// here we invert the sphere sizes to match the inverted depth func, so that the larger sphere is actually the front texture!
	R_DrawSkyArrays (alphaskytexture->texnum, fullscale, halfscale, -rotateFore);

	// back to normal mode
	glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glDisable (GL_BLEND);

	// done with texturing
	glDisableClientState (GL_TEXTURE_COORD_ARRAY);

	// revert the texture matrix
	glMatrixMode (GL_TEXTURE);
	glLoadIdentity ();
	glMatrixMode (GL_MODELVIEW);

	// revert the depth func
	glDepthFunc (GL_LEQUAL);
	glDepthMask (GL_TRUE);

	// now write the regular polys one more time to clip world geometry
	glVertexPointer (3, GL_FLOAT, sizeof (float) * 3, VArrayVerts);

	R_ClipSky (skychain);

	// done!  phew!  opengl-fu at it's finest
	glDisableClientState (GL_VERTEX_ARRAY);

	// run a pipeline flush
	GL_FlushOrFinish (false);
}
frag.machine
Posts: 2126
Joined: Sat Nov 25, 2006 1:49 pm

Post by frag.machine »

Hey, MH is in da house! :D
I know FrikaC made a cgi-bin version of the quakec interpreter once and wrote part of his website in QuakeC :) (LordHavoc)
leileilol
Posts: 2783
Joined: Fri Oct 15, 2004 3:23 am

Post by leileilol »

I remember seeing a gl_skyroom.c in mrGlQuake once
i should not be here
Post Reply