Jump to content

  • Log In with Google      Sign In   
  • Create Account

Clipping frustum to portal


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Vexator   Members   -  Reputation: 138

Like
0Likes
Like

Posted 07 November 2011 - 05:07 AM

Hi everybody!

I'm working on the implementation of portal rendering into my personal engine project. Right now, portals are hard-coded, rectangular polygons.

In general, there are three possible cases:
  • None of the portal's vertices are inside the frustum: the frustum will not be changed.
  • All of the portal's vertices are inside the frustum: compute new frustum planes from portal vertices and camera position.
  • Some of the portal's vertices are inside the frustum: clip frustum planes against portal to compute a set of intersection points between frustum planes and portal vertices. Compute new frustum planes from clipped vertices and camera position.

Cases one and two seem to work fine but if only some of the portal vertices are inside the frustum and the frustum planes have to be clipped against the portal, something goes wrong. As you can see in the video, the intersection points which are generated by intersecting a frustum plane and a portal vertex are not on the portal itself, but move in a half-circle through the corridor instead (the portal is placed bewtween the room and the corridor:

http://www.youtube.c...h?v=VPrEfiwt6ro

I just can't spot the error and I've been on this for a week now - what am I doing wrong? I am thankful for all the help/hints you can give.


class Portal

Declaration:

class Portal
{
	public:

		vector<vec3> vertices;

		void clipToPlane( const vec4 &plane );
};

Implementation:

vec3 linePlaneIntersection( vec4 plane, vec3 ray, vec3 rayOrigin )
{
	const vec3 normal = vec3(plane);

	const float t = (plane.w - glm::dot(normal, rayOrigin)) / glm::dot(normal, ray);

	const vec3 newRay = ray * t;

	return rayOrigin + newRay;
}

void Portal::clipToPlane( const vec4 &plane )
{
	vector<vec3> clipVertices;

	for (int i = 0; i < vertices.size(); i++)
	{
		int i2 = (i + 1) % vertices.size();

		vec3 v1 = vertices[i];
		vec3 v2 = vertices[i2];

		float dist1 = glm::dot( vec3(plane), v1 ) + plane.w;
		float dist2 = glm::dot( vec3(plane), v2 ) + plane.w;

		// both are outside frustum
		if( dist1 < 0 && dist2 < 0 )
			continue;

		// both are inside frustum
		if( dist1 > 0 && dist2 > 0 )
		{
			clipVertices.push_back( v1 );
		}
		// only v1 is inside
		else if( dist1 > 0 )
		{
			clipVertices.push_back( v1 );
			clipVertices.push_back( linePlaneIntersection(plane, v1, v2) );
		}
		// only v2 is inside
		else
		{
			clipVertices.push_back( linePlaneIntersection(plane, v1, v2) );
		}
	}

	if( clipVertices.size() >= 3 )
	{
		vertices = clipVertices;
	}
	else
	{
		vertices.clear();
	}
}


class Frustum

Declaration:

class Frustum : public BoundingVolume
{
	public:

		enum Plane
		{
			PLANE_NEAR,
			PLANE_FAR,
			PLANE_RIGHT,
			PLANE_TOP,
			PLANE_LEFT,
			PLANE_BOTTOM,

			PLANE_COUNT
		};

	public:

		void compute( const mat4 &viewMatrix, const mat4 &projectionMatrix, float fieldOfView, float aspect, const vec2 &clipPlanes, shared_ptr<Portal> portal = shared_ptr<Portal>() );

		/* ... */

   	protected:

		vec4 m_planes[PLANE_COUNT];
};

Implementation:

vec4 computePlane( const vec3 &v1, const vec3 &v2, const vec3 &v3 )
{
	vec3 aux1 = v1 - v2;
	vec3 aux2 = v3 - v2;

	vec3 normal = glm::normalize( glm::cross(aux2, aux1) );

	float d = glm::dot( normal, v2 );

	return vec4( normal, -d );
}

void Frustum::compute( const mat4 &viewMatrix, const mat4 &projectionMatrix, float fieldOfView, float aspect, const vec2 &clipPlanes, shared_ptr<Portal> portal )
{
	// compute tangent in randians
	const float tangent = (float)tan( Degree2Radian(fieldOfView*0.5f) );

	// compute near plane dimensions
	float nearHeight = clipPlanes.x*tangent;
	float nearWidth = nearHeight*aspect;

	// compute far plane dimensions
	float farHeight = clipPlanes.y*tangent;
	float farWidth = farHeight*aspect;

	mat4 viewMatrixInverse = glm::inverse( viewMatrix );
	mat4 viewMatrixTranspose = glm::transpose( viewMatrix );

	const vec3 position = vec3( viewMatrixInverse[3] );
	
	const vec3 xAxis = glm::normalize( vec3(viewMatrixTranspose[0]) );
	const vec3 yAxis = glm::normalize( vec3(viewMatrixTranspose[1]) );
	const vec3 zAxis = glm::normalize( vec3(viewMatrixTranspose[2]) );
	
	// compute centers of near and far planes
	vec3 nearCenter = position + ((-zAxis)*clipPlanes.x);
	vec3 farCenter = position + ((-zAxis)*clipPlanes.y);

	// compute frustum corners on near plane
	vec3 ntl = nearCenter + (yAxis*nearHeight) - (xAxis*nearWidth);
	vec3 ntr = nearCenter + (yAxis*nearHeight) + (xAxis*nearWidth);
	vec3 nbl = nearCenter - (yAxis*nearHeight) - (xAxis*nearWidth);
	vec3 nbr = nearCenter - (yAxis*nearHeight) + (xAxis*nearWidth);

	// compute frustum corners on far plane
	vec3 ftl = farCenter + (yAxis*farHeight) - (xAxis*farWidth);
	vec3 ftr = farCenter + (yAxis*farHeight) + (xAxis*farWidth);
	vec3 fbl = farCenter - (yAxis*farHeight) - (xAxis*farWidth);
	vec3 fbr = farCenter - (yAxis*farHeight) + (xAxis*farWidth);

	// add near and far planes
	m_planes[PLANE_NEAR] = computePlane( ntl, ntr, nbr );
	m_planes[PLANE_FAR] = computePlane( ftr, ftl, fbl );

	// add side planes
	m_planes[PLANE_RIGHT] = computePlane( nbr, ntr, fbr );
	m_planes[PLANE_TOP] = computePlane( ntr, ntl, ftl );
	m_planes[PLANE_LEFT] = computePlane( ntl, nbl, fbl );
	m_planes[PLANE_BOTTOM] = computePlane( nbl, nbr, fbr );

	if( portal )
	{
		// check whether all portal
		// vertices are inside frustum

		bool insideFrustum = true;
		for( int i = 0; i < portal->vertices.size(); i++ )
		{
			if( testIntersection(portal->vertices[i]) == TEST_OUTSIDE )
			{
				insideFrustum = false;

				break;
			}
		}

		// clip portal vertices against frustum planes if they some are not inside frustum
		// if no vertices are generated, frustum is already small enough to fit through portal

		if( !insideFrustum )
		{
			// skip near and far planes!
			for( int i = 2; i < PLANE_COUNT; i++ )
			{
				portal->clipToPlane( m_planes[i] );
			}
		}

		// compute planes from (possibly clipped) portal vertices

		for( int i = 0; i < portal->vertices.size(); i++ )
		{
			int i2 = (i+1) % portal->vertices.size();

			vec3 v1 = position;
			vec3 v2 = portal->vertices[i];
			vec3 v3 = portal->vertices[i2];

			if( (i+2) > PLANE_COUNT )
				break;

			// replace original plane by clipped plane
			m_planes[i+2] = computePlane( v1, v2, v3 );
		}
	}
}

Wunderwerk Engine is an OpenGL-based, shader-driven, cross-platform game engine. It is targeted at aspiring game designers who have been kept from realizing their ideas due to lacking programming skills.

blog.wunderwerk-engine.com

Sponsor:

#2 Vexator   Members   -  Reputation: 138

Like
0Likes
Like

Posted 07 November 2011 - 07:45 AM

changed
linePlaneIntersection(plane, v2, v1)
to
linePlaneIntersection(plane, v2-v1, v1)
and the intersection points are now on the portal's edge like they're supposed to be. the sudden corruption of the frustum (like the one at the end of the video) are still sometimes happening, though, when i move the camera.
Wunderwerk Engine is an OpenGL-based, shader-driven, cross-platform game engine. It is targeted at aspiring game designers who have been kept from realizing their ideas due to lacking programming skills.

blog.wunderwerk-engine.com

#3 spek   Prime Members   -  Reputation: 997

Like
1Likes
Like

Posted 08 November 2011 - 12:43 AM

Sounds familiar :) Maybe this is useful for you:
http://tower22.blogspot.com/2011/07/x-ray.html

It does not test wether a portal intersects(or overlaps) the frustum in a regular way with some function though. Instead you calculate 2D rects on the screen where the portal is, and clip out the rest. Testing wether a portal falls inside a rect becomes childsplay then. However, there is still one occasion I'm having troubles with; very steep angles. If you look in such a way that you see only 1 or 2 of the portal(quad) vertices on the left side of the screen, and having the other vertices behind your back on the right side, the cliprect is calculated wrong. I'm weak in math so maybe you'll find a simple fix for that in no time though.

#4 biki_   Members   -  Reputation: 224

Like
1Likes
Like

Posted 09 November 2011 - 03:23 AM

few tips on implementing portals.
1) don't use 2d approach, doing it in 3d is easier and less messy
2) you will always get problems when camera is very close to portal, treat this case separately, for example give each portal bounding box that adds some space around portal, and when camera is inside box, render both cells on both sides of portal using original frustum
3) don't use near and far clipping planes for portal processing
4) do portal processing as follows:
1. check bounding box of portal, if invisible - done
2. check if camera is inside box , if it is, render cells on both sides of portal using original frustum
3. clip portal to 4 frustum planes
4. for each vertex of clipped portal find distances to frustum planes calculatr 't' for each vertex and pick minimum and maximum value of 't'
for example if distances to horizontal clip planes are d1 and d2
t=d1/(d1+d2) - this tells you position left - right of vertex
5. shrink frustum using calculated t-min and t-max
again if horizontal frustum planes are P1 and P2
P1' = P1*(1-tmin) - P2*tmin
P2' = P2*(tmax-1)+P2*tmax
6. render stuff behind portal using new frustum'

that way you will get new set of 4 clipping planes that project to rectangle on the screen
and without all that 2d crap
i hope it helps

#5 __Homer__   Members   -  Reputation: 58

Like
0Likes
Like

Posted 09 November 2011 - 03:36 AM

Numerical error will still crop up in 3D, you need to implement an Epsilon (thickness for infinite cutting planes) to get around it - see my Journal for useful tips on portal discovery via an intermediate BSP tree, and various tech issues and solves.
In C++, friends have access to your privates.
In ObjAsm, your members are exposed!

#6 Vexator   Members   -  Reputation: 138

Like
0Likes
Like

Posted 16 November 2011 - 02:54 PM

great, thank you all for your input!

the portal is clipped properly now. however, in some cases, more than four clipped vertices are generated. so i compute the bounding box around all these vertices. but now i'm not sure how to decide which four corners to choose from the eight corner vertices of this bounding box. any hints?

Posted Image

Wunderwerk Engine is an OpenGL-based, shader-driven, cross-platform game engine. It is targeted at aspiring game designers who have been kept from realizing their ideas due to lacking programming skills.

blog.wunderwerk-engine.com




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS