Jump to content
  • Advertisement
Sign in to follow this  
coollofty

Who can give me some code about clip frustum against portal

This topic is 4279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Thanks, I have downloaded that sample. but it seems only a very based frustum culling tutorial, have not any portal correlative code?
Now problem is: I know how to generate a view frustum from view and projection matrix and culling bounding volume with it. But I dont know how to clip view frustum against rectangle portal, i.e. how to zoom view frustum with rectangle when a frustum through the portal.

Share this post


Link to post
Share on other sites
Basically, you use the current frustum to clip each line of the portal, creating a set of new, clipped portal points. You then use these new points to build a NEW frustum, which you pass on to the next area, and so on.

Here's some sample code from my engine, let me know if you need any help deciphering it. The "positiveSide" variable is "did we encounter this portal through the area on its front side?" It's used to prevent recursion backwards through a portal (ie, towards the camera's origin, instead of away from it).


void Portal::SubmitQuery (const QueryFlags &flags, const MVec3f &origin, const MFrustum &frustum, float frustumLength, bool positiveSide) const
{
// Clip the frustum to the portal!
MPlane::PlaneSide portalSide = this->m_plane.ClassifyPoint(origin);

// Consider this portal only if the origin is on the same side as the area we came from
if ((portalSide == MPlane::PS_FRONT && positiveSide) || (portalSide == MPlane::PS_BACK && !positiveSide))
{
const FrustumPlanes &frustPlanes = frustum.GetPlanes();
std::vector<MVec3f> clippedPoints = this->m_points, tempPoints;
size_t numPts;

for (FrustumPlanes::const_iterator i = frustPlanes.begin(); i != frustPlanes.end(); ++i)
{
numPts = clippedPoints.size();
for (size_t x = 0; x < numPts; x++)
{
const MVec3f &start = clippedPoints[x];
const MVec3f &end = clippedPoints[(x + 1) % numPts];

MPlane::PlaneSide startSide = i->ClassifyPoint(start);
MPlane::PlaneSide endSide = i->ClassifyPoint(end);

// Sutherland-Hodgman Polygon Clipping
if (startSide == MPlane::PS_BACK && endSide == MPlane::PS_BACK) // 1
continue;
else if ((startSide == MPlane::PS_BACK && endSide <= MPlane::PS_FRONT) || // 2
(startSide <= MPlane::PS_FRONT && endSide == MPlane::PS_BACK)) // 3
{
MVec3f pt;

SplitLineByPlane(start, end, *i, pt, NULL);

tempPoints.push_back(pt);
if (startSide == MPlane::PS_BACK)
tempPoints.push_back(end);
}
else if (startSide != MPlane::PS_BACK && endSide != MPlane::PS_BACK) // 4
tempPoints.push_back(end); // Add the end point!
}

clippedPoints = tempPoints;
tempPoints.clear();
}

// Now we have the portal clipped to the frustum. Hurray!
if (clippedPoints.size() >= 3)
{
FrustumPlanes planes;
MPlane nearp = (portalSide == MPlane::PS_BACK) ? MPlane(clippedPoints[0], clippedPoints[1], clippedPoints[2])
: MPlane(clippedPoints[0], clippedPoints[2], clippedPoints[1]);
float newLength = frustumLength - nearp.GetDistance();

planes.push_back(nearp);
planes.push_back(MPlane(-nearp.GetNormal(), newLength));

numPts = clippedPoints.size();
for (size_t x = 0; x < numPts; x++)
planes.push_back((portalSide == MPlane::PS_BACK) ? MPlane(origin, clippedPoints[x], clippedPoints[(x + 1) % numPts]) :
MPlane(origin, clippedPoints[(x + 1) % numPts], clippedPoints[x]));

MFrustum portalFrustum(planes);

// Continue on to the next area!
const Area *recurseArea = positiveSide ? this->m_negativeArea : this->m_positiveArea;
recurseArea->SubmitQuery(flags, origin, portalFrustum, newLength);
}
}
}

Share this post


Link to post
Share on other sites
I had read and understand it. Now I have only one question with such a line in your code:

MFrustum portalFrustum(planes);

I dont know how you generate a frustum from the variable "planes" if "planes.size() > 6"?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!