class Frustrum{
static Vector4D[] Planes;
static public void Update(){ //is called every frame after the view is set with gluLookAt()
Matrix4 modl = Matrix4.ModelviewMatrix();
Matrix4 proj = Matrix4.ProjectionMatrix();
Matrix4 clip = modl*proj;
Planes = new Vector4D[6];
Planes[0] = new Vector4D(clip[3]-clip[0],clip[7]-clip[4],clip[11]-clip[8],clip[15]-clip[12]);
Planes[1] = new Vector4D(clip[3]+clip[0],clip[7]+clip[4],clip[11]+clip[8],clip[15]+clip[12]);
Planes[2] = new Vector4D(clip[3]+clip[1],clip[7]+clip[5],clip[11]+clip[9],clip[15]+clip[13]);
Planes[3] = new Vector4D(clip[3]-clip[1],clip[7]-clip[6],clip[11]-clip[9],clip[15]-clip[13]);
Planes[4] = new Vector4D(clip[3]-clip[2],clip[7]-clip[6],clip[11]-clip[10],clip[15]-clip[14]);
Planes[5] = new Vector4D(clip[3]+clip[2],clip[7]+clip[6],clip[11]+clip[10],clip[15]+clip[14]);
foreach(Vector4D i in Planes){
float magnitude = (float)Math.Sqrt(i.x*i.x+i.y*i.y+i.z*i.z);
i.x /= magnitude;
i.y /= magnitude;
i.z /= magnitude;
i.w /= magnitude;
}
}
static public bool PointInFrustrum(Vector3D v1){
for(int i = 0;i<6;i++){
if((v1.x*Planes[i].x+v1.y*Planes[i].y+v1.z*Planes[i].z + Planes[i].w)<=0){
return false;
}
}
return true;
}
static public bool SphereInFrustrum(Vector3D v1, float radius){
for(int i = 0;i<6;i++){
float d = v1.x*Planes[i].x+v1.y*Planes[i].y+v1.z*Planes[i].z + Planes[i].w;
if(d<=0){
return false;
}
}
return true;
}
}
class Matrix4{
float[] Data = new float[16];
public Matrix4(float[] d){
Array.Copy(d,Data,16);
}
public static Matrix4 Multiply(Matrix4 m1, Matrix4 m2){
Matrix4 mx = new Matrix4();
mx.SetZero();
for(int i = 0;i<4;i++){
for(int j = 0; j < 4;j++){
for(int k = 0;k<4;k++){
mx[i,j] += m1[i,k] * m2[k,j];
}
}
return mx;
}
public static Matrix4 ModelviewMatrix(){
float[] d = new float[16];
GL.glGetFloatv(GL.GL_MODELVIEW_MATRIX,d);
return new Matrix4(d);
}
public static Matrix4 ProjectionMatrix(){
float[] d = new float[16];
GL.glGetFloatv(GL.GL_PROJECTION_MATRIX,d);
return new Matrix4(d);
public static Matrix4 ModelviewMatrix(){
float[] d = new float[16];
GL.glGetFloatv(GL.GL_MODELVIEW_MATRIX,d);
return new Matrix4(d);
}
public static Matrix4 ProjectionMatrix(){
float[] d = new float[16];
GL.glGetFloatv(GL.GL_PROJECTION_MATRIX,d);
return new Matrix4(d);
}
public static Matrix4 operator*(Matrix4 m1,Matrix4 m2){
return Multiply(m1,m2);
}
public float this[int i]{
get{
return Data[i];
}
set{
Data[i] = value;
}
}
public float this[int row,int column]{
get{
return Data[column*4+row];
}
set{
Data[column*4+row] = value;
}
}
}
To test I am drawing a couple of sphers and try to clip. However, often Spheres that should be drawn are clipped as well...
Any help would be greatly appreciated.
[edited by - Annon on November 2, 2003 11:16:26 PM]
Frustum Culling Problem
I am trying to implement a frustrum culling system. I have read several tutorials and think that I understand pretty well. However, somehow my code doesn't work. I suspect that there is one small mistake somewhere in my code that keeps it from working properly.
Code is in C#:
in SphereInFrustrum: "if(d<=0)" should be something like "if(d<=-radius)"
You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement