PDA

View Full Version : Simple portal renderer



vgo
07-03-2007, 10:13 AM
I was thinking about implementing a simple portal renderer for my competition entry, but I ran into some problems.

The idea was to have sectors which are actually collections of objects that are rendered, these are linked to portals that tell what other sectors might be visible for the current one. I'd let OpenGL to do the actual culling of the vertices, but still try to somewhat limit the number of tris to draw.

For the portal renderer to work properly first I need to check the facing of the portal polygons to see if they even can be visible, then I check if they are inside the view frustum and do some clipping if needed.

But I can't get even the first part working properly, for some reason the portal polygons are visible when they shouldn't and vice versa. :D

Here's how I calculate the planes for the portals:

function CalcFaceNormal(V1, V2, V3: TVector3f): TVector4f;
var
rx1, ry1, rz1, rx2, ry2, rz2: TFloat;
A, B, C, D: TFloat;
len: TFloat;
begin
rx1 := V2.x - V1.x;
ry1 := V2.y - V1.y;
rz1 := V2.z - V1.z;
rx2 := V3.x - V1.x;
ry2 := V3.y - V1.y;
rz2 := V3.z - V1.z;
A := ry1 * rz2 - ry2 * rz1;
B := rz1 * rx2 - rz2 * rx1;
C := rx1 * ry2 - rx2 * ry1;
len := sqrt(A * A + B * B + C * C);
A := A / len; B := B / len; C:= C / len;
D := A * V2.x + B * V2.y + C * V2.z;
Result := _Vector4f(A, B, C, D);
end;


Here's how I check if the plane is visible, the fViewVector is taken from the modelview matrix and FaceNormal is calculated with the function above:

function TFrustum.FaceVisible(FaceNormal: TVector4f): Boolean;
begin
Result := FaceNormal.x * fViewVector.x + FaceNormal.y * fViewVector.y + FaceNormal.z * fViewVector.z - FaceNormal.w >= 0;
end;


What I'm doing wrong?

JSoftware
07-03-2007, 10:46 AM
Why do you calculate the w component? Unless you do some software rendering I would just set it to 0

vgo
08-03-2007, 10:20 AM
Now I've set the w component to 0, there's still some situations where the test fails, but these are quite rare. It'll have to do for now.

What I have so far is that I've modified my 3D model import code to create portals from brushes that are tagged as portals. Haven't figured out a way to create these automatically so I'm placing all the portals manually, actually it's not that bad because I don't have to worry that much for overdrawing stuff.

Now the next problem is that how can I figure out which sector I'm in? If I set a starting position and tell it what sector it's in, then the rest is easy when I move from sector to sector through the portals. But if I have a free moving camera that can go outside the sectors, how can I figure out when I'm inside one of the sectors again? This would also be useful when placing objects on the maps so that I don't have to tell each object what sector they belong to. :)

The sectors doesn't necessarily form "convex" hulls (or what are they called) so I can't just check the camera location against all face normals in a sector to figure out the sector I'm supposed to be in.