关于Portal的一些代码
2011-11-08 15:12
211 查看
RE: Calculation of new View Frustum
Hi.
We implemented a portal rendering system in Haddd 1.6 but it was discarded in the end.
Here is the constructor that creates a new Frustum from a given portal:
Code:
/// <summary>
/// Creates an instance of HFrustum from the given portal
/// </summary>
public HFrustum(HPortal portal, Vector3 cameraPosition)
{
planes = new List<Plane>(portal.Vertices.Count);
// Distance from the portal plane to the camera position
float distance = portal.Plane.Dot(cameraPosition);
// If the camera is behind the portal, that is, if the camera position is not on the side
// the portal's plane normal is pointing to, we have to invert the order of the vertices so the
// frustum planes (their normals) are facing in the correct direction
bool reverse = distance < 0.0f;
int i, i2, vertexIndex, nextVertexIndex;
for (i = 0; i < portal.Vertices.Count; i++)
{
// Index to the next vertex in the portal
i2 = (i + 1) % portal.Vertices.Count;
// Calculate vertex indices depending if we have to invert the order of the portal vertices
vertexIndex = reverse ? portal.Vertices.Count - i - 1 : i;
nextVertexIndex = reverse ? portal.Vertices.Count - i2 - 1 : i2;
// Reference to the vertices
Vector3 v1 = portal.Vertices[vertexIndex];
Vector3 v2 = portal.Vertices[nextVertexIndex];
// Calculates a frustum plane given the three points we have
Plane plane = Plane.FromPoints(cameraPosition, v1, v2);
planes.Add(plane);
}
}
In HPortal, "Vertices" is the list of vertices that makes up the portal (they have to be in the correct order, I can't remember if it was clock-wise o counter clock-wise) and "Plane" is just the plane where the portal is contained.
Hope that helps.
========================================
Once again i'm stuck,
The frustum code works perfectly, but i'm attempting to clip my portals before that are fed through to this function.
I'm battling with clipping the portal polygon to the original planes of the frustum (Left, Right, Top and Bottom).
I'm using the JMath.ClipToPlane, but i'm not to sure how to clip the polygon to all the planes easily with correct results,
and efficiently.
If any of you can point me in the right direction, that would be fantastic
Thanks once again in advance.
--------
Hi.
Those are methods from our old portal class:
Code:
public void ClipToPlane(Plane plane)
{
if (!(plane.A != 0 || plane.B != 0 || plane.C != 0))
return;
List<Vector3> auxVerts = new List<Vector3>();
for (int i = 0; i < vertices.Count; i++)
{
int i2 = (i + 1) % vertices.Count;
Vector3 v1 = vertices[i];
Vector3 v2 = vertices[i2];
float dist1 = plane.Dot(v1);
float dist2 = plane.Dot(v2);
if (dist1 < 0 && dist2 < 0)
continue;
if (dist1 > 0 && dist2 > 0)
{
auxVerts.Add(v1);
}
else if (dist1 > 0)
{
auxVerts.Add(v1);
auxVerts.Add(Plane.IntersectLine(plane, v1, v2));
}
else
{
auxVerts.Add(Plane.IntersectLine(plane, v1, v2));
}
}
if (auxVerts.Count >= 3)
vertices = auxVerts;
else
vertices.Clear();
}
public static HPortal ClipToFrustum(HPortal portal, HFrustum frustum)
{
HPortal oPortal = (HPortal)portal.Clone();
foreach (Plane plane in frustum.Planes)
{
// The method ClipToPlane clips the portal and returns the section that is in the positive side of the plane,
// that is, the side where the plane's normal is pointing to. But in the case of frustum clipping, the
// positive side of the planes is outwards so that's why we have to invert the frustum plane's normal
// so that the section we obtain from the clipped portal is the one inside the frustum and not outside.
oPortal.ClipToPlane(Plane.Scale(plane, -1.0f));
}
return oPortal;
}
Hi.
We implemented a portal rendering system in Haddd 1.6 but it was discarded in the end.
Here is the constructor that creates a new Frustum from a given portal:
Code:
/// <summary>
/// Creates an instance of HFrustum from the given portal
/// </summary>
public HFrustum(HPortal portal, Vector3 cameraPosition)
{
planes = new List<Plane>(portal.Vertices.Count);
// Distance from the portal plane to the camera position
float distance = portal.Plane.Dot(cameraPosition);
// If the camera is behind the portal, that is, if the camera position is not on the side
// the portal's plane normal is pointing to, we have to invert the order of the vertices so the
// frustum planes (their normals) are facing in the correct direction
bool reverse = distance < 0.0f;
int i, i2, vertexIndex, nextVertexIndex;
for (i = 0; i < portal.Vertices.Count; i++)
{
// Index to the next vertex in the portal
i2 = (i + 1) % portal.Vertices.Count;
// Calculate vertex indices depending if we have to invert the order of the portal vertices
vertexIndex = reverse ? portal.Vertices.Count - i - 1 : i;
nextVertexIndex = reverse ? portal.Vertices.Count - i2 - 1 : i2;
// Reference to the vertices
Vector3 v1 = portal.Vertices[vertexIndex];
Vector3 v2 = portal.Vertices[nextVertexIndex];
// Calculates a frustum plane given the three points we have
Plane plane = Plane.FromPoints(cameraPosition, v1, v2);
planes.Add(plane);
}
}
In HPortal, "Vertices" is the list of vertices that makes up the portal (they have to be in the correct order, I can't remember if it was clock-wise o counter clock-wise) and "Plane" is just the plane where the portal is contained.
Hope that helps.
========================================
Once again i'm stuck,
The frustum code works perfectly, but i'm attempting to clip my portals before that are fed through to this function.
I'm battling with clipping the portal polygon to the original planes of the frustum (Left, Right, Top and Bottom).
I'm using the JMath.ClipToPlane, but i'm not to sure how to clip the polygon to all the planes easily with correct results,
and efficiently.
If any of you can point me in the right direction, that would be fantastic
Thanks once again in advance.
--------
Hi.
Those are methods from our old portal class:
Code:
public void ClipToPlane(Plane plane)
{
if (!(plane.A != 0 || plane.B != 0 || plane.C != 0))
return;
List<Vector3> auxVerts = new List<Vector3>();
for (int i = 0; i < vertices.Count; i++)
{
int i2 = (i + 1) % vertices.Count;
Vector3 v1 = vertices[i];
Vector3 v2 = vertices[i2];
float dist1 = plane.Dot(v1);
float dist2 = plane.Dot(v2);
if (dist1 < 0 && dist2 < 0)
continue;
if (dist1 > 0 && dist2 > 0)
{
auxVerts.Add(v1);
}
else if (dist1 > 0)
{
auxVerts.Add(v1);
auxVerts.Add(Plane.IntersectLine(plane, v1, v2));
}
else
{
auxVerts.Add(Plane.IntersectLine(plane, v1, v2));
}
}
if (auxVerts.Count >= 3)
vertices = auxVerts;
else
vertices.Clear();
}
public static HPortal ClipToFrustum(HPortal portal, HFrustum frustum)
{
HPortal oPortal = (HPortal)portal.Clone();
foreach (Plane plane in frustum.Planes)
{
// The method ClipToPlane clips the portal and returns the section that is in the positive side of the plane,
// that is, the side where the plane's normal is pointing to. But in the case of frustum clipping, the
// positive side of the planes is outwards so that's why we have to invert the frustum plane's normal
// so that the section we obtain from the clipped portal is the one inside the frustum and not outside.
oPortal.ClipToPlane(Plane.Scale(plane, -1.0f));
}
return oPortal;
}
相关文章推荐
- 关于移动网站网页的基本代码结构和一些移动网站开发建议
- php关于代码规范的一些小总结
- 一些关于C#发送邮件的代码
- 数据结构关于树的一些递归函数代码
- IOS中关于百度推送的一些配置代码和步骤
- 关于一些代码中定义为空的宏ngx_cdecl的作用
- 关于屏幕的尺寸调节的一些代码
- 关于代码规范的一些参考
- 杂记_一些关于python代码风格汇总_不断更新
- 关于NHibernate的一些代码
- 关于java反射的一些代码
- 关于代码重构的一些思考
- 代码编写中关于通用定义的一些个性规约
- 关于js处理的一些公用代码收集.(代码+demo)
- unity关于代码控制ugui显示隐藏的一些心得
- [swift3.0]关于ios 忽略文件 和 提交代码总是提交一些不必要的文件处理
- 关于JAVA像C语言那样从控制台接收输入的字符的方法(一些代码转载)
- 关于线程的一些概念和关于线程返回值的简单示例代码
- Android-一些关于代码优化的事儿