www.pudn.com > SurfWareNew06.3.5(LYW).rar > Camera.CPP


#include "stdafx.h" 
 
#include  
#include "camera.h" 
#include  
 
//------------------------------------------------------------- 
// For Camera class 
//------------------------------------------------------------- 
#define MAX_SPAN		1000 
#define MAX_ZOOM		20000 
#define MIN_ZOOM		1 
 
#define INIT_X			-1000 
#define INIT_Y			1000 
#define INIT_Z			1000 
 
GCamera::GCamera(void) 
{ 
	m_fovy=50; 
} 
 
GCamera::~GCamera() 
{ 
} 
 
void GCamera::projection(GLenum mode)  
{ 
	//switch to projection 
   	glMatrixMode(GL_PROJECTION); 
   	glLoadIdentity(); 
 
	//apply projective matrix 
	m_orthoLeft		=  - m_width/2; 
	m_orthoRight	=  m_width/2; 
	m_orthoBottom	=  - m_height/2; 
	m_orthoTop		=  m_height/2; 
 
 
	if(m_pViewPort==ID_VIEW_CAMERA) 
		gluPerspective(m_fovy,m_width/m_height,1,10000); 
	else 
		glOrtho(m_orthoLeft,m_orthoRight,m_orthoBottom,m_orthoTop,m_near,m_far); 
 
	gluLookAt(m_eye.GetX(),m_eye.GetY(),m_eye.GetZ(),m_ref.GetX(),m_ref.GetY(),m_ref.GetZ(),m_vecUp.GetX(), m_vecUp.GetY(), m_vecUp.GetZ());	 
} 
 
void GCamera::set_screen( int x, int y)  
{  
	glViewport(0,0,x,y); 
	if(y==0) y=1; 
	float ratio = (float)x/(float)y; 
	m_width *= (float)x/m_screen[0]; 
	m_height *= (float)y/m_screen[1]; 
	m_width =  m_height*ratio; 
	m_screen[0] = (float)x; 
	m_screen[1] = (float)y;  
} 
 
 
void GCamera::init() 
{ 
	m_eye = Point(-INIT_X,0,0); 
	m_ref = Point(0,0,0); 
	m_far = 1000000; 
	m_near= -100000; 
 
	m_width = 2400.0; 
	m_height = 2400.0; 
 
	m_vecUp =Point(0,1,0); 
	 
	m_screen[0] = 400; 
	m_screen[1] = 400; 
} 
 
 
void GCamera::set_eye(double eye_x,double eye_y,double eye_z) 
{ 
	m_eye.SetX(eye_x); 
	m_eye.SetY(eye_y); 
	m_eye.SetZ (eye_z); 
} 
 
void GCamera::set_ref(float ref_x,float ref_y,float ref_z) 
{ 
	m_ref.SetX(ref_x); 
	m_ref.SetY(ref_y); 
	m_ref.SetZ(ref_z); 
} 
 
void GCamera::set_vecUp(float up_dx,float up_dy,float up_dz) 
{ 
	m_vecUp.SetX(up_dx); 
	m_vecUp.SetY(up_dy); 
	m_vecUp.SetZ(up_dz); 
} 
 
 
void GCamera::get_view_rect(float& width,float& height) 
{ 
	width = m_width; 
	height = m_height; 
} 
 
void GCamera::SetViewDirection() 
{ 
	 
	float r = GetObserveDistance(); 
 
	if(IS_ZERO(r)) r = 50.0; 
	if( r > 10000)  r = 10000; 
	 
	switch(m_pViewPort){ 
	case ID_VIEW_FRONT: 
		m_eye = m_ref + Point(0,0,r); 
		m_vecUp = Point(0,1,0); 
		break; 
	case ID_VIEW_TOP: 
		m_eye = m_ref + Point(0,r,0); 
		m_vecUp = Point(0,0,1); 
		break; 
	case ID_VIEW_CAMERA: 
		m_eye = m_ref + Point(r,r,-r); 
		m_vecUp = Point(0,1,0); 
		break; 
	} 
} 
 
float GCamera::GetObserveDistance() 
{ 
	Point vec; 
	Point org(0,0,0); 
 
	vec = m_ref - m_eye; 
	return (float)vec.GetDistance(org); 
} 
 
void GCamera::Span(CPoint mousedown,CPoint point) 
{ 
	if(m_pViewPort==ID_VIEW_CAMERA) 
		return; 
 
	SetCursor(::LoadCursor(NULL,MAKEINTRESOURCE(32649))); 
 
	CSize movement = mousedown - point; 
 
	double x,y,z,x1,y1,z1; 
	x=m_eye.GetX(); 
	y=m_eye.GetY(); 
	z=m_eye.GetZ(); 
	x1=m_ref.GetX(); 
	y1=m_ref.GetY(); 
	z1=m_ref.GetZ(); 
	 
 
	 
	switch(m_pViewPort) 
	{ 
	case ID_VIEW_SIDE: 
		z+=-movement.cx*5; 
		y+=-movement.cy*5; 
		z1+=-movement.cx*5; 
		y1+=-movement.cy*5; 
		break; 
	case ID_VIEW_FRONT: 
		x+=movement.cx*5; 
		y+=-movement.cy*5; 
		x1+=movement.cx*5; 
		y1+=-movement.cy*5; 
		break;		 
	case ID_VIEW_TOP: 
		x+=-movement.cx*5; 
		z+=-movement.cy*5; 
		x1+=-movement.cx*5; 
		z1+=-movement.cy*5; 
	} 
 
	if (fabs(x)>MAX_SPAN) 
	{ 
		x=MAX_SPAN; 
		return;	 
	} 
	 
	if(fabs(y)>MAX_SPAN) 
	{ 
		y=MAX_SPAN; 
		return;	 
	} 
 
	if(fabs(z)>MAX_SPAN) 
	{ 
		z=MAX_SPAN; 
		return;	 
	} 
	set_eye((float)x,(float)y,(float)z); 
	set_ref((float)x1,(float)y1,(float)z1); 
 
} 
 
void GCamera::Orbit(CPoint mousedown, CPoint point) 
{ 
	if(m_pViewPort!=ID_VIEW_CAMERA) 
		return; 
 
	float angH,angV; 
	 
	CSize rotate = mousedown - point; 
 
	angH=(rotate.cx<0)?(-10/180.f):(10/180.0f); 
	angV=(rotate.cy<0)?(-10/180.f):(10/180.0f); 
		 
	double x,y,z; 
  
	if(abs(rotate.cx)>abs(rotate.cy)) 
	{ 
		SetCursor(::LoadCursor(NULL,MAKEINTRESOURCE(32653))); 
		x=m_eye.GetX()*cos(angH)+m_eye.GetZ()*sin(angH); 
		z=m_eye.GetZ()*cos(angH)-m_eye.GetX()*sin(angH); 
		y=m_eye.GetY(); 
	}else{ 
		SetCursor(::LoadCursor(NULL,MAKEINTRESOURCE(32652))); 
 
		double x1,z1; 
 
		int dx1=(m_eye.GetX()>0)?1:-1; 
		int dz1=(m_eye.GetZ()>0)?1:-1; 
 
		double ang=atan(m_eye.GetZ()/m_eye.GetX()); 
 
		x=m_eye.GetX()*cos(ang)+m_eye.GetZ()*sin(ang); 
		z=m_eye.GetZ()*cos(ang)-m_eye.GetX()*sin(ang); 
 
		x1=x;z1=z; 
			 
		x=x1*cos(angV)-m_eye.GetY()*sin(angV); 
		y=m_eye.GetY()*cos(angV)+x1*sin(angV); 
 
		x1=x; 
			 
		x=x1*cos(-ang)+z1*sin(-ang); 
		z=z1*cos(-ang)-x1*sin(-ang); 
 
		int dx2=(x>0)?1:-1; 
		int dz2=(z>0)?1:-1; 
 
		if (!(dx1+dx2)||!(dz1+dz2))  
			OverTurn(); 
	} 
 
	set_eye((float)x,(float)y,(float)z); 
} 
 
void GCamera::Zoom(short zDelta) 
{ 
	float ratio=(zDelta>0)?1.1f:0.9f; 
	static BOOL zoomable=TRUE; 
	 
	if(m_pViewPort==ID_VIEW_CAMERA) 
	 ; 
	else 
	{ 
		zoomable=(m_width*ratio>MAX_ZOOM 
			||m_height*ratio>MAX_ZOOM 
			||m_height*ratio0)?1:-1; 
 
	double ang=atan(m_eye.GetZ()/m_eye.GetX()); 
	 
	x=m_eye.GetX()*cos(ang)+m_eye.GetZ()*sin(ang); 
	z=m_eye.GetZ()*cos(ang)-m_eye.GetX()*sin(ang); 
	x1=x;z1=z; 
		 
	k=x1/m_eye.GetY(); 
 
	y=m_eye.GetY()+sig*50; 
	 
	x=k*y; 
	x1=x; 
			 
	x=x1*cos(-ang)+z1*sin(-ang); 
	z=z1*cos(-ang)-x1*sin(-ang); 
 
	if (x!=0&&y!=0&&z!=0)  
		set_eye(x,y,z); 
}