• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Vexator

Clipping frustum to portal

5 posts in this topic

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:
[list][*]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.[/list]

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:

[url="http://www.youtube.com/watch?v=VPrEfiwt6ro"]http://www.youtube.c...h?v=VPrEfiwt6ro[/url]

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.


[u][b]class Portal[/b][/u]

Declaration:

[code]class Portal
{
public:

vector<vec3> vertices;

void clipToPlane( const vec4 &plane );
};[/code]

Implementation:

[code]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();
}
}[/code]


[u][b]class Frustum[/b][/u]

Declaration:

[code]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];
};[/code]

Implementation:

[code]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 );
}
}
}[/code]
0

Share this post


Link to post
Share on other sites
changed
[code]linePlaneIntersection(plane, v2, v1)[/code]
to
[code]linePlaneIntersection(plane, v2-v1, v1)[/code]
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.
0

Share this post


Link to post
Share on other sites
You don't clip the frustum to the portal. You clip the portal to the frustum and then create a new frustum from the view point to the clipped portal.
0

Share this post


Link to post
Sounds familiar :) Maybe this is useful for you:
[url="http://tower22.blogspot.com/2011/07/x-ray.html"]http://tower22.blogspot.com/2011/07/x-ray.html[/url]

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.
1

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='__Homer__' timestamp='1320831415' post='4882044']
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.
[/quote]

Oh go ahead, demote me back to zero!
-2

Share this post


Link to post
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?

[img]http://ah-fotografie.com/vexator/portal.jpg[/img]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0