www.pudn.com > final_6.rar > TopView.cpp


// TopView.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "CG.h" 
#include "TopView.h" 
#include "CGDoc.h" 
#include "Operation.h" 
#include "SetPoint.h" 
#include "SetShapeDialog.h" 
#include "BitmapDlg.h" 
#include "BitmapTexture.h" 
#include "Attach.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CTopView 
 
IMPLEMENT_DYNCREATE(CTopView, CView) 
 
CTopView::CTopView() 
{ 
   g_pTopView=this; 
   startPoint=new CGPoint(); 
   prePoint = NULL; 
} 
 
CTopView::~CTopView() 
{ 
 
} 
 
 
BEGIN_MESSAGE_MAP(CTopView, CView) 
	//{{AFX_MSG_MAP(CTopView) 
	ON_WM_LBUTTONDOWN() 
	ON_WM_MOUSEMOVE() 
	ON_WM_LBUTTONUP() 
	ON_WM_RBUTTONDOWN() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CTopView drawing 
 
void CTopView::OnDraw(CDC* pDC) 
{ 
	CDocument* pDoc = GetDocument(); 
	// TODO: add draw code here 
    CRect rc; 
	GetClientRect(&rc); 
   	pDC->FillRect(rc,backBrush); 
 
	pDC->TextOut(10,10,"Top View"); 
	pDC->SelectObject(frame); 
	pDC->SetViewportOrg(rc.right/2,rc.bottom/2); 
    pDC->MoveTo(0,0);pDC->LineTo(0,1000); 
	pDC->MoveTo(0,0);pDC->LineTo(1000,0); 
    pDC->MoveTo(0,0);pDC->LineTo(-1000,0); 
    pDC->MoveTo(0,0);pDC->LineTo(0,-1000); 
     
	pDC->SelectObject(shape); 
	int x; 
	for (x=0;xSelectObject(bePointed); 
	   else 
		   pDC->SelectObject(shape); 
	    
		    CShape* mShape; 
	        mShape=(CShape*)g_pDoc->m_ShapeArray[x]; 
	    	mShape->DrawTop(pDC,&rc); 
	} 
   CPen light(PS_DASH,2,RGB(255,100,100)); 
    
 
 
	for (x=0;xSelectObject(bePointed); 
	       else pDC->SelectObject(light); 
	       CGLight *mLight; 
		   mLight=(CGLight *)g_pDoc->m_LightArray[x]; 
		   mLight->DrawTop(pDC); 
	} 
 
 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CTopView diagnostics 
 
#ifdef _DEBUG 
void CTopView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CTopView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CTopView message handlers 
 
void CTopView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
      switch (drawMode) 
	  { 
	     case dmNormal: 
			 { 
			   	 break; 
			 } 
         case dmMove: 
			 { 
			     if (MouseOn(point)) 
				{   
			      g_pDoc->UpdateAllViews(NULL); 
				  prePoint=point; 
				} 
			    break; 
			 } 
		 case dmRotate: 
			 { 
			     if (MouseOn(point)) 
				 { 
				   g_pDoc->UpdateAllViews(NULL); 
				   prePoint=point; 
				 } 
				  
				 break; 
			 } 
		 case dmZoom: 
			 { 
			     if (MouseOn(point)) 
				 { 
				   	  g_pDoc->UpdateAllViews(NULL); 
				      prePoint=point;			    
 				 } 
			  	 break; 
			 } 
		case dmExtrude: 
			 { 
			     if (MouseOn(point)) 
				 { 
				   	  g_pDoc->UpdateAllViews(NULL); 
				      prePoint=point;			    
 				 } 
			  	 break; 
			 } 
		case dmLatheX: 
			 { 
			     if (opOn) 
				 { 
					 CShape *pColumn; 
					 pColumn = (CShape*)g_pDoc->m_ShapeArray[opEntityIndex]; 
					 CRect rc; 
					 GetClientRect(&rc); 
					 CPoint point_org; 
					 point_org.x = point.x - rc.right/2; 
					 point_org.y = point.y - rc.bottom/2; 
					 COperation::LatheEntity(pColumn,point_org,TOP,LATHEX); 
					 pColumn->SetBodyFace(); 
					 opPointIndex++; 
					 selectNo = opEntityIndex; 
				   	  g_pDoc->UpdateAllViews(NULL); 
				      prePoint=point;			    
 				 } 
				 if(opPointIndex>=LATI) 
				 { 
					 opOn = false; 
					 opPointIndex = 0; 
					 selectNo = -1; 
				 } 
			  	 break; 
			 } 
		case dmLatheY: 
			 { 
			     if (opOn) 
				 { 
					 CShape *pColumn; 
					 pColumn = (CShape*)g_pDoc->m_ShapeArray[opEntityIndex]; 
					 CRect rc; 
					 GetClientRect(&rc); 
					 CPoint point_org; 
					 point_org.x = point.x - rc.right/2; 
					 point_org.y = point.y - rc.bottom/2; 
					 COperation::LatheEntity(pColumn,point_org,TOP,LATHEY); 
					 pColumn->SetBodyFace(); 
					 opPointIndex++; 
					 selectNo = opEntityIndex; 
				   	  g_pDoc->UpdateAllViews(NULL); 
				      prePoint=point;			    
 				 } 
				 if(opPointIndex>=LATI) 
				 { 
					 opOn = false; 
					 opPointIndex = 0; 
					 selectNo = -1; 
				 } 
			  	 break; 
			 } 
		case dmLatheZ: 
			 { 
			     if (opOn) 
				 { 
					 CShape *pColumn; 
					 pColumn = (CShape*)g_pDoc->m_ShapeArray[opEntityIndex]; 
					 CRect rc; 
					 GetClientRect(&rc); 
					 CPoint point_org; 
					 point_org.x = point.x - rc.right/2; 
					 point_org.y = point.y - rc.bottom/2; 
					 COperation::LatheEntity(pColumn,point_org,TOP,LATHEZ); 
					 pColumn->SetBodyFace(); 
					 opPointIndex++; 
					 selectNo = opEntityIndex; 
				   	  g_pDoc->UpdateAllViews(NULL); 
				      prePoint=point;			    
 				 } 
				 if(opPointIndex>=LATI) 
				 { 
					 opOn = false; 
					 opPointIndex = 0; 
					 selectNo = -1; 
				 } 
			  	 break; 
			 } 
		case dmLoftSample: 
			{ 
				if (loftSampleOn) 
				{ 
					CShape *pColumn; 
					 pColumn = (CShape*)g_pDoc->m_ShapeArray[opEntityIndex]; 
					 CRect rc; 
					 GetClientRect(&rc); 
					 CPoint point_org; 
					 point_org.x = point.x - rc.right/2; 
					 point_org.y = point.y - rc.bottom/2; 
					 COperation::LoftSampleEntity(pColumn,point_org,TOP); 
					 opPointIndex++; 
					 selectNo = opEntityIndex; 
				   	  g_pDoc->UpdateAllViews(NULL); 
				      prePoint=point; 
				} 
				if(opPointIndex>=LONG) 
				 { 
					 loftSampleOn = false; 
					 opPointIndex = 0; 
				 } 
			  	 break; 
			} 
		case dmLoftTrack: 
			{ 
				if(loftTrackOn && opEntityIndex!=-1) 
				{ 
					CShape *pColumn; 
					 pColumn = (CShape*)g_pDoc->m_ShapeArray[opEntityIndex]; 
					 CRect rc; 
					 GetClientRect(&rc); 
					 CPoint point_org; 
					 point_org.x = point.x - rc.right/2; 
					 point_org.y = point.y - rc.bottom/2; 
					 COperation::LoftTrackEntity(pColumn,point_org,prePoint,TOP); 
					 pColumn->SetBodyFace(); 
					 opPointIndex++; 
					 selectNo = opEntityIndex; 
				   	  g_pDoc->UpdateAllViews(NULL); 
				      prePoint=point_org; 
				} 
				if(opPointIndex>=LATI) 
				 { 
					 loftTrackOn = false; 
					 opPointIndex = 0; 
				 } 
			  	 break; 
			} 
         case dmSetPointL: 
		   { 
               if (COperation::PointOnLight(point,TOP)) 
			   { 
			      CSetPoint setPointL; 
				    
				  CGLight *light; 
				  light=(CGLight*)g_pDoc->m_LightArray[selectLight]; 
				  setPointL.m_blue=light->lc.b; 
				  setPointL.m_green=light->lc.g; 
				  setPointL.m_red=light->lc.r; 
				  if (setPointL.DoModal()==IDOK) 
				  { 
					   
					  light->lc.r=setPointL.m_red; 
					  light->lc.g=setPointL.m_green; 
					  light->lc.b=setPointL.m_blue; 
					  selectLight=-1; 
					   
				  } 
			   } 
		       break; 
		   } 
         case dmMovePointL: 
		 { 
		    int mm,nn; 
		        mm=point.x; 
			    nn=point.y; 
			 if (COperation::PointOnLight(point,TOP)) 
			   { 
			      g_pDoc->UpdateAllViews(NULL); 
				  prePoint=point; 
			   }   
			 break; 
		 } 
         case dmSetShape: 
			 { 
			  if (MouseOn(point)) 
				 { 
				   g_pDoc->UpdateAllViews(NULL); 
				   CSetShapeDialog setShape; 
				   if (setShape.DoModal()==IDOK) 
				   { 
				      CShape *shape; 
					  shape=(CShape *)g_pDoc->m_ShapeArray[selectNo]; 
					  shape->cl.r=setShape.m_Red; 
					  shape->cl.g=setShape.m_Green; 
					  shape->cl.b=setShape.m_Blue; 
					  shape->Ka=setShape.m_Ka; 
					  shape->Kd=setShape.m_Kd; 
					  shape->Ks=setShape.m_Ks; 
					  shape->ns=setShape.m_Ns; 
					  shape->trans=setShape.m_tran; 
				   } 
				   selectLight=-1;selectNo=-1; 
			  } 
			      
				 break; 
			 } 
         case dmDelete: 
			 { 
			    if (MouseOn(point)) 
				{ 
				  g_pDoc->m_ShapeArray.RemoveAt(selectNo,1); 
				  selectNo=-1; 
				  index--; 
				} 
		       break; 
			 } 
		 case dmDeleteLight: 
			 { 
			      if (COperation::PointOnLight(point,TOP)) 
			   { 
			      g_pDoc->m_LightArray.RemoveAt(selectLight,1); 
				  selectLight=-1; 
				  lightIndex--; 
			   } 
		       break; 
			 } 
		 case dmSetTexture: 
			 { 
			     if (MouseOn(point)) 
				 { 
				 	 g_pDoc->UpdateAllViews(NULL); 
                     	 CBitmapDlg bd; 
                     if (bd.DoModal()==IDOK) 
					 { 
                         CAttach *attach; 
						 attach=(CAttach *)g_pDoc->m_TextureArray[selectNo]; 
                       if (bd.filename.Compare("")) 
						{ 
						    attach->texture=new CBitmapTexture(bd.filename); 
						    attach->texture->TexType=1; 
						 
						} 
						else 
						{ 
						    attach->texture=new CBitmapTexture(bd.Index); 
						} 
					 } 
                    selectNo=-1; 
  				 } 
			   	 break; 
			 } 
         case dmCancelTexture: 
			 { 
			   if (MouseOn(point)) 
			   { 
			       g_pDoc->UpdateAllViews(NULL); 
			       CAttach *attach; 
				   attach=(CAttach *)g_pDoc->m_TextureArray[selectNo]; 
				   if (attach->texture!=NULL) 
				   { 
				       attach->texture=NULL; 
				   } 
			   } 
			  
			   break; 
			 } 
		 default: 
		 { 
		  
		 } 
	  } 
 	CView::OnLButtonDown(nFlags, point); 
} 
 
 
void CTopView::OnMouseMove(UINT nFlags, CPoint point)  
{   
   switch (drawMode) 
	{ 
	   case dmNormal: 
		   { 
			   
			   break; 
		   } 
	   case dmMove: 
		   { 
			   if (selectNo!=-1) 
			   { 
				  COperation::MoveEntity(point,prePoint,TOP); 
			      g_pDoc->UpdateAllViews(NULL); 
                  prePoint=point; 
			   } 
			    break; 
		   } 
	   case dmRotate: 
		   { 
		       if (selectNo!=-1) 
			   { 
				   COperation::RotateEntity(point,prePoint,TOP); 
				   g_pDoc->UpdateAllViews(NULL); 
                   prePoint=point; 
			   } 
		       break; 
		   } 
	   case dmZoom: 
		   { 
		       if (selectNo!=-1) 
			   { 
				   COperation::ZoomEntity(point,prePoint,TOP); 
				   g_pDoc->UpdateAllViews(NULL); 
                   prePoint=point; 
			   } 
		       break; 
		   } 
		case dmExtrude: 
		   { 
		       if (selectNo!=-1) 
			   { 
				   COperation::ExtrudeEntity(point,prePoint,TOP); 
				   g_pDoc->UpdateAllViews(NULL); 
                   prePoint=point; 
			   } 
		       break; 
		   } 
       case dmMovePointL: 
		   {   
			   if (selectLight!=-1) 
			   { 
				   COperation::MoveLight(point,prePoint,TOP); 
				   g_pDoc->UpdateAllViews(NULL); 
				   prePoint=point; 
			   } 
		       break; 
		   } 
	    
	   default: 
		   {   
		    
		   } 
	} 
	 
   CView::OnMouseMove(nFlags, point); 
} 
 
void CTopView::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
  	 
            selectNo=-1; 
	        selectLight=-1; 
            g_pDoc->UpdateAllViews(NULL); 
            CView::OnLButtonUp(nFlags, point); 
} 
 
BOOL CTopView::MouseOn(CPoint point) 
{ 
  if (index>=1) 
  {   
	CGPoint *mousePoint=new CGPoint(); 
	COperation::ScreenToReal(mousePoint,point,TOP); 
	vector mouseVector; 
	SetStartPoint(mousePoint); 
	COperation::CaculateVector(mouseVector,mousePoint,startPoint); 
   	if (COperation::Ray_Hit(startPoint,mouseVector,TOP)) 
	{ 
   	 return TRUE; 
	} 
    } 
   return FALSE; 
} 
 
void CTopView::SetStartPoint(CGPoint* mousePoint) 
{ 
    startPoint->x=mousePoint->x; 
	startPoint->y=-1000; 
	startPoint->z=mousePoint->z; 
} 
//void CTopView::OnRenderTop()  
//{ 
   /* int x,y; 
    CRect rc; 
	CGPoint tempPoint; 
	vector directVector; 
	color rayColor; 
	CDC *pDC; 
    rayColor.r=0;rayColor.b=0;rayColor.g=0; 
	pDC=this->GetDC(); 
	this->GetClientRect(&rc); 
	 
		for (y=0;y<=rc.bottom;y++) 
               for (x=0;x<=rc.right;x++) 
		{ 
			ZBuffer.r=0;ZBuffer.g=0;ZBuffer.b=0;//背景色 
        	Zt=100000;//初始化 
		    tempPoint.x=x-rc.right/2;tempPoint.z=y-rc.bottom/2;tempPoint.y=0;tempPoint.m=1; 
			SetStartPoint(&tempPoint); 
			COperation::CaculateVector(directVector,&tempPoint,startPoint); 
			TraceRay(startPoint,directVector,&rayColor,x,y); 
		   // pDC->SetPixelV(x,y,RGB(rayColor.r,rayColor.g,rayColor.b)); 
		 	pDC->SetPixelV(x,y,RGB(ZBuffer.r,ZBuffer.g,ZBuffer.b)); 
		    rayColor.r=0;rayColor.g=0;rayColor.b=0; 
		} 
	 selectNo=-1; 
	 selectLight=-1;*/ 
//} 
 
void CTopView::TraceRay(CGPoint *startPoint, vector directVector, color *rayColor,int pX,int pY) 
{ 
    int x; 
    CShape *shape; 
	for (x=0;xm_ShapeArray[x]; 
		if (shape->trans==1) 
		{ 
		  if (COperation::Ray_Hit_Entity(shape, startPoint, directVector,TOP)) 
		  { 
		     if (ttr; 
				ZBuffer.g=(int)rayColor->g; 
				ZBuffer.b=(int)rayColor->b; 
				Zt=tt; 
				rayColor->r=0; 
				rayColor->b=0; 
				rayColor->g=0; 
			 } 
		    xx=0;yy=0;tt=0; 
		  } 
		} 
	} 
    for (x=0;xm_ShapeArray[x]; 
		if (shape->trans!=1) 
		{ 
		  if (COperation::Ray_Hit_Entity(shape, startPoint, directVector,TOP)) 
		  { 
		     if (tttrans*rayColor->r+(1-shape->trans)*ZBuffer.r); 
				ZBuffer.g=(int)(shape->trans*rayColor->g+(1-shape->trans)*ZBuffer.g); 
				ZBuffer.b=(int)(shape->trans*rayColor->b+(1-shape->trans)*ZBuffer.b); 
				Zt=tt; 
				rayColor->r=0; 
				rayColor->b=0; 
				rayColor->g=0; 
			 } 
		    xx=0;yy=0;tt=0; 
		  } 
		} 
	} 
 
 
} 
 
//void CTopView::OnTrans()  //快速zbuffer算法 
//{ 
       
 /*	CDC *pDC; 
	CRect rc; 
	int x,y; 
    int scanY; 
	int MinX,MaxX; 
    color rayColor;  
    CGPoint tempPoint; 
	vector directVector; 
 
	pDC=this->GetDC(); 
	this->GetClientRect(&rc); 
     
    rayColor.r=0;rayColor.b=0;rayColor.g=0; 
	for (y=0;y=MinX+rc.right/2)&&(x<=MaxX+rc.right/2))	 
		   { 
			   tempPoint.x=x-rc.right/2; 
			   tempPoint.z=scanY; 
			   tempPoint.y=0; 
			   tempPoint.m=1; 
			   SetStartPoint(&tempPoint); 
			   COperation::CaculateVector(directVector,&tempPoint,startPoint); 
			   TraceRay(startPoint,directVector,&rayColor,x,y); 
                rayColor.r=0;rayColor.g=0;rayColor.b=0; 
		   } 
           pDC->SetPixelV(x,y,COperation::GammaCorrect(ZBuffer.r,ZBuffer.g,ZBuffer.b)); 
		} 
	} 
    selectNo=-1; 
    selectLight=-1;*/ 
//} 
 
void CTopView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)  
{ 
	 Invalidate(FALSE); 
} 
 
void CTopView::CallRender() 
{ 
    OnRenderTop2(); 
} 
 
void CTopView::OnRenderTop2() 
{ 
   int x,y; 
    CRect rc; 
	CGPoint tempPoint; 
	vector directVector; 
	color rayColor; 
	CDC *pDC; 
    rayColor.r=0;rayColor.b=0;rayColor.g=0; 
	pDC=this->GetDC(); 
	this->GetClientRect(&rc); 
	 
	CShape *shape; 
	for (y=0;ym_ShapeArray[y]; 
	  shape->SetBodyFace(); 
	} 
	 
	for (y=0;y<=rc.bottom;y++) 
               for (x=0;x<=rc.right;x++) 
		{ 
			ZBuffer.r=0;ZBuffer.g=0;ZBuffer.b=0;//背景色 
        	Zt=100000;//初始化 
		    tempPoint.x=x-rc.right/2;tempPoint.z=y-rc.bottom/2;tempPoint.y=0;tempPoint.m=1; 
			SetStartPoint(&tempPoint); 
			COperation::CaculateVector(directVector,&tempPoint,startPoint); 
			TraceRay(startPoint,directVector,&rayColor,x,y); 
		   // pDC->SetPixelV(x,y,RGB(rayColor.r,rayColor.g,rayColor.b)); 
		 	pDC->SetPixelV(x,y,RGB(ZBuffer.r,ZBuffer.g,ZBuffer.b)); 
		    rayColor.r=0;rayColor.g=0;rayColor.b=0; 
		} 
	 selectNo=-1; 
	 selectLight=-1; 
} 
 
void CTopView::OnTrans2() 
{ 
   	CDC *pDC; 
	CRect rc; 
	int x,y; 
    int scanY; 
	int MinX,MaxX; 
    color rayColor;  
    CGPoint tempPoint; 
	vector directVector; 
 
	pDC=this->GetDC(); 
	this->GetClientRect(&rc); 
     
    CShape *shape; 
	for (y=0;ym_ShapeArray[y]; 
	  shape->SetBodyFace(); 
	} 
 
 
    rayColor.r=0;rayColor.b=0;rayColor.g=0; 
	for (y=0;y=MinX+rc.right/2)&&(x<=MaxX+rc.right/2))	 
		   { 
			   tempPoint.x=x-rc.right/2; 
			   tempPoint.z=scanY; 
			   tempPoint.y=0; 
			   tempPoint.m=1; 
			   SetStartPoint(&tempPoint); 
			   COperation::CaculateVector(directVector,&tempPoint,startPoint); 
			   TraceRay(startPoint,directVector,&rayColor,x,y); 
                rayColor.r=0;rayColor.g=0;rayColor.b=0; 
		   } 
           pDC->SetPixelV(x,y,COperation::GammaCorrect(ZBuffer.r,ZBuffer.g,ZBuffer.b)); 
		} 
	} 
    selectNo=-1; 
    selectLight=-1; 
} 
 
void CTopView::OnRButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(opOn) 
	{ 
		opOn = false; 
	} 
	if(loftSampleOn) 
	{ 
		loftSampleOn = false; 
	} 
	if(loftTrackOn) 
	{ 
		loftTrackOn = false; 
	} 
	selectNo = -1; 
	opPointIndex = 0; 
	CView::OnRButtonDown(nFlags, point); 
}