FrustumCull.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:5k
- // FrustumCull.cpp: implementation of the CFrustumCull class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "FrustumCull.h"
- #include <math.h>
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CFrustumCull::CFrustumCull()
- {
- m_HalfFOV=40;
- m_ViewerRotate=0;
- m_ViewDistance=30;
- m_ViewerX=m_ViewerZ=0;
- m_ViewLeftX=m_ViewLeftZ=0;
- m_ViewRightX=m_ViewRightZ=0;
- m_FarFoucsX=m_FarFoucsZ=0;
- m_BackLeftX=m_BackLeftZ=0;
- m_BackRightX=m_BackRightZ=0;
- m_MaxX=m_MaxZ=0;
- m_MinX=m_MinZ=256;
-
- for(int i=0;i<128;i++)
- {
- m_LeftArray[i]=0;
- m_RightArray[i]=0;
- }
- }
- void CFrustumCull::InitFrustumCull(int halfFOV,int distance)
- {
- m_HalfFOV=halfFOV;
- m_ViewDistance=distance;
- if(m_ViewDistance>125)m_ViewDistance=125;
- }
- CFrustumCull::~CFrustumCull()
- {
- }
- void CFrustumCull::UpdateFrustumCull(float angle)
- {
- /////////////update viewer rotate //////
- m_ViewerRotate=angle;
- /////////////////////////////////////////////
- ///////////// Calculate Frustum /////////////
- /////////////////////////////////////////////
- ///////////////get view back,left and right
- m_BackLeftX =-int(4*sinf((m_ViewerRotate+150)*0.01745329f));
- m_BackLeftZ =-int(4*cosf((m_ViewerRotate+150)*0.01745329f));
- m_BackRightX=-int(4*sinf((m_ViewerRotate-150)*0.01745329f));
- m_BackRightZ=-int(4*cosf((m_ViewerRotate-150)*0.01745329f));
- m_FarFoucsX =-int(m_ViewDistance*sinf(m_ViewerRotate*0.01745329f));
- m_FarFoucsZ =-int(m_ViewDistance*cosf(m_ViewerRotate*0.01745329f));
- m_ViewLeftX =-int(m_ViewDistance*sinf((m_ViewerRotate+m_HalfFOV)*0.01745329f));
- m_ViewLeftZ =-int(m_ViewDistance*cosf((m_ViewerRotate+m_HalfFOV)*0.01745329f));
- m_ViewRightX=-int(m_ViewDistance*sinf((m_ViewerRotate-m_HalfFOV)*0.01745329f));
- m_ViewRightZ=-int(m_ViewDistance*cosf((m_ViewerRotate-m_HalfFOV)*0.01745329f));
- ////////////////get Rectangle boundary
- m_MaxX=m_MaxZ=0;
- m_MinX=m_MinZ=256;
- if(m_ViewLeftX>m_MaxX)m_MaxX=m_ViewLeftX;
- if(m_ViewLeftZ>m_MaxZ)m_MaxZ=m_ViewLeftZ;
- if(m_ViewRightX>m_MaxX)m_MaxX=m_ViewRightX;
- if(m_ViewRightZ>m_MaxZ)m_MaxZ=m_ViewRightZ;
- if(m_FarFoucsX>m_MaxX)m_MaxX=m_FarFoucsX;
- if(m_FarFoucsZ>m_MaxZ)m_MaxZ=m_FarFoucsZ;
- if(m_BackLeftX>m_MaxX)m_MaxX=m_BackLeftX;
- if(m_BackLeftZ>m_MaxZ)m_MaxZ=m_BackLeftZ;
- if(m_BackRightX>m_MaxX)m_MaxX=m_BackRightX;
- if(m_BackRightZ>m_MaxZ)m_MaxZ=m_BackRightZ;
- if(m_ViewLeftX<m_MinX)m_MinX=m_ViewLeftX;
- if(m_ViewLeftZ<m_MinZ)m_MinZ=m_ViewLeftZ;
- if(m_ViewRightX<m_MinX)m_MinX=m_ViewRightX;
- if(m_ViewRightZ<m_MinZ)m_MinZ=m_ViewRightZ;
- if(m_FarFoucsX<m_MinX)m_MinX=m_FarFoucsX;
- if(m_FarFoucsZ<m_MinZ)m_MinZ=m_FarFoucsZ;
- if(m_BackLeftX<m_MinX)m_MinX=m_BackLeftX;
- if(m_BackLeftZ<m_MinZ)m_MinZ=m_BackLeftZ;
- if(m_BackRightX<m_MinX)m_MinX=m_BackRightX;
- if(m_BackRightZ<m_MinZ)m_MinZ=m_BackRightZ;
- /////////////////Fill array//////////////////////////////
- BresenhamFill(m_ViewLeftX,m_ViewLeftZ,m_FarFoucsX,m_FarFoucsZ,
- m_LeftArray);
- BresenhamFill(m_FarFoucsX,m_FarFoucsZ,m_ViewRightX,m_ViewRightZ,
- m_RightArray);
- ///////////// GOOD WORK !!!!
- ///////////// Now ,we get two boundary array
- }
- void CFrustumCull::BresenhamFill(int startx,int startz,int endx,int endz, char *pArray)
- {
- ///////////// Bresenham Line
- int dx=1;
- int dz=1;
- int x, z;
- int d;
- int SignX,SignZ;
- bool bChange;
- if(endx>startx)
- {
- dx=endx-startx;
- SignX=1;
- }
- else
- {
- dx=startx-endx;
- SignX=-1;
- }
- if(endz>startz)
- {
- dz=endz-startz;
- SignZ=1;
- }
- else
- {
- dz=startz-endz;
- SignZ=-1;
- }
- x=startx;
- z=startz;
- if(dz>dx)
- {
- int temp;
- temp=dx; dx=dz;dz=temp;
- bChange=true;
- pArray[124]=dx+1; //存储个数
- pArray[125]=startz; //起始
- pArray[126]=endz; //结束
- pArray[127]=1; //Z轴加
- }
- else
- {
- bChange=false;
- pArray[124]=dx+1; //存储个数
- pArray[125]=startx; //起始
- pArray[126]=endx; //结束
- pArray[127]=0; //X轴加
- }
- d=2*dz-dx;
- ////////////////////////
- for(int i=0;i<dx+1;i++)
- {
- if(bChange)
- pArray[i]=x;
- else
- pArray[i]=z;
- if(d>=0)
- {
- if(bChange) x+=SignX;
- else z+=SignZ;
- d=d-dx*2;
- }
- if(bChange)z+=SignZ;
- else x+=SignX;
- d=d+dz*2;
- }
- }