Page 1 of 1

Easy Bounding Box Fix

Posted: Sat Oct 03, 2009 11:50 am
by Stroggos
Easy Bounding Box Fix:
I found this by accident and it seemed to work. I'm really not sure how it works
so maybe someone could tell me.

There is only one change in one file throughout the whole tutorial. First find
R_CullBox(). It should look like this:

Code: Select all

qboolean R_CullBox (vec3_t mins, vec3_t maxs)
{
	int		i;

	for (i=0 ; i<4 ; i++)
		if (BoxOnPlaneSide (mins, maxs, &frustum[i]) == 2)
			return true;
	return false;
}
Then change the line:

Code: Select all

if (BoxOnPlaneSide (mins, maxs, &frustum[i]) == 2)
to

Code: Select all

if (BoxOnPlaneSide (mins, maxs, &frustum[i]) == 5)
Compile and run. Goto the final map (end.bsp) and look up at the Shub-Niggurath.

Posted: Sat Oct 03, 2009 5:15 pm
by frag.machine
And what is exactly fixed with this code snippet ? The fact that Shub's bound box does not fit to its model ?

Posted: Sat Oct 03, 2009 5:45 pm
by Team Xlink
He is makeing it so it returns 5 and makes it always visible.

Posted: Sat Oct 03, 2009 6:17 pm
by Spike
It 'fixes' it by disabling culling entirely.

BoxOnPlaneSide returns 1 if part of the box is fully in view, 2 if none of it is (thus cull), and 3 if part of it is. 0 is possible if the box is inside out...

5 will never happen. Thus the box (read: model) will never be culled, and will always be visible.
Note that the BSP culling code also uses this function to cull things. So if you do apply that change, you'll more than double the amount of data sent to the graphics card every frame.



The bug is that the model loader loads every single mdl file with mins & max as '-16 -16 -16' & '16 16 16'. Thus the box sizes used in the call to R_CullBox are wrong.

A better, but still lazy, 'fix' is to remove the condition+call+return in R_DrawAliasModel instead. Just comment out the two lines.

The proper fix is to fix the mdl loader to set the size to model_header->origin & (model_header->origin_scale*255).

And to then maybe hack PF_setmodel so QC still sees models as the old -16 & 16 size (quakerally for one breaks).

Posted: Sat Oct 03, 2009 8:46 pm
by Team Xlink
Spike, the quakerc.org tutorial at:

http://www.quake-1.com/docs/quakesrc.org/71.html

Says to add:

Code: Select all

	aliasbboxmins[0] = aliasbboxmins[1] = aliasbboxmins[2] =  99999;

	aliasbboxmaxs[0] = aliasbboxmaxs[1] = aliasbboxmaxs[2] = -99999;

After this:

Code: Select all

	//

	// load the frames

	//



	posenum = 0;

	pframetype = (daliasframetype_t *)&pintriangles[pheader->numtris];
In this:

Code: Select all

	Mod_LoadAliasModel

So instead of doing this:

Code: Select all

	aliasbboxmins[0] = aliasbboxmins[1] = aliasbboxmins[2] =  99999;

	aliasbboxmaxs[0] = aliasbboxmaxs[1] = aliasbboxmaxs[2] = -99999;
I should do this:

Code: Select all

	aliasbboxmins[0] = aliasbboxmins[1] = aliasbboxmins[2] = 255;
	aliasbboxmaxs[0] = aliasbboxmaxs[1] = aliasbboxmaxs[2] = 0;
That is correct, right?

Posted: Sat Oct 03, 2009 9:01 pm
by Spike
The important bit is this code:
// FIXME: do this right
mod->mins[0] = mod->mins[1] = mod->mins[2] = -16;
mod->maxs[0] = mod->maxs[1] = mod->maxs[2] = 16;
The whole aliasbboxmins / aliasbboxmaxs part of that tutorial you linked is to try to get as small a bounds as possible.
Although that's probably overkill, as the model will generally be expanded to consume the full 0-255 range anyway, in which case mins is 0 and maxs is 255.

But yeah, that tutorial should fix it properly and efficiently.

Posted: Mon Oct 05, 2009 9:02 pm
by r00k
Rich Whitehouse fixed it in his model loader

Code: Select all

	//rww - doing this right (get the max extents of the verts for all poses).
	//could also use a mins/maxs for culling for each pose uniquely, but who cares.
	//continue using 32 as the minimum extent size
	mod->mins[0] = mod->mins[1] = mod->mins[2] = -16.0f;
	mod->maxs[0] = mod->maxs[1] = mod->maxs[2] = 16.0f;
	for (i = 0; i < pheader->numposes; i++)
	{
		int j;
		for (j = 0; j < pheader->numverts; j++) {
			byte *vert = poseverts[i][j].v;
			float v;
			for (k = 0; k < 3; k++)
			{
				v = (float)vert[k];
				v *= pheader->scale[k];
				v += pheader->scale_origin[k];

				if (v < mod->mins[k])
				{
					mod->mins[k] = v;
				}
				if (v > mod->maxs[k])
				{
					mod->maxs[k] = v;
				}
			}
		}
	}
	for (k = 0; k < 3; k++)
	{
		mod->mins[k] -= 32.0f;
		mod->maxs[k] += 32.0f;
	}

	if (mod->maxs[1] > mod->maxs[0])
	{
		mod->maxs[0] = mod->maxs[1];
	}
	else
	{
		mod->maxs[1] = mod->maxs[0];
	}
	if (mod->maxs[2] > mod->maxs[0])
	{
		mod->maxs[0] = mod->maxs[2];
		mod->maxs[1] = mod->maxs[2];
	}
	else
	{
		mod->maxs[2] = mod->maxs[0];
	}
	if (mod->mins[1] < mod->mins[0])
	{
		mod->mins[0] = mod->mins[1];
	}
	else
	{
		mod->mins[1] = mod->mins[0];
	}
	if (mod->mins[2] < mod->mins[0])
	{
		mod->mins[0] = mod->mins[2];
		mod->mins[1] = mod->mins[2];
	}
	else
	{
		mod->mins[2] = mod->mins[0];
	}

	mod->radius = RadiusFromBounds (mod->mins, mod->maxs);

Posted: Thu Oct 08, 2009 10:00 pm
by FrikaC
Weird bit of code. He's got it starting at 32x32x32, finds the maximum extents of the model, extends a further 64x64x64 (to cover up rotations exiting the bounds no doubt (better would be to multiply by the square root of 2), then takes the greatest extent encountered and copies it everywhere (again, rotation).