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 ); } } }