计算机图形学资料.docx
《计算机图形学资料.docx》由会员分享,可在线阅读,更多相关《计算机图形学资料.docx(19页珍藏版)》请在冰豆网上搜索。
计算机图形学资料
计算机图形学
——投影变换
姓名:
学号:
班级:
一大班二小班
一、实现目标
将屏幕垂直划分为两部分,左半部分绘制三维坐标系和长方体,右半部分绘制二维坐标系及三视图。
首先设置成长方体,在绘制绘制三视图
二、算法设计
1.在屏幕左半部分绘制三维坐标系。
2.在屏幕右半部分绘制二维坐标系。
3.读入长方体的三维顶点齐次坐标及其面坐标,并将三维顶点齐次坐标变换为相应的屏幕二维齐次坐标,并在屏幕左半部分绘制长方体。
4.分别给主视图、俯视图和侧视图变换矩阵的非零元素赋值。
5.将原始图形的顶点集合齐次坐标矩阵和三视图变换矩阵相乘,结果赋予变换后的三视图顶点集合齐次坐标矩阵。
其中,
主视图变换矩阵为:
俯视图变换矩阵为:
主视图变换矩阵为:
6.根据变换后的顶点集合齐次坐标矩阵的元素绘制三视图。
三、源代码及注释
Group7View.h文件
lassCMygroup7View:
publicCView
{
protected:
//createfromserializationonly
CMygroupView();
DECLARE_DYNCREATE(CMygroupView)
//Attributes
public:
CMygroup7Doc*GetDocument();
//Operations
public:
intViewKind;
//Overrides
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CMygroup7View)
public:
virtualvoidOnDraw(CDC*pDC);//overriddentodrawthisview
virtualBOOLPreCreateWindow(CREATESTRUCT&cs);
protected:
virtualBOOLOnPreparePrinting(CPrintInfo*pInfo);
virtualvoidOnBeginPrinting(CDC*pDC,CPrintInfo*pInfo);
virtualvoidOnEndPrinting(CDC*pDC,CPrintInfo*pInfo);
//}}AFX_VIRTUAL
//Implementation
public:
voidClearMatrix(doubleA[4][4]);//清除矩阵函数
voidCalculate(doubleP[][4],doubleT[][4]);//两个矩阵相乘函数
doublePt3D[8][4];
doublePt2D[8][2];
intMaxY;
intMaxX;
intFt[6][5];
voidTransform3DTo2D(constdoubleP3D[][4],doubleP2D[][2],constintn);//三维变二维函数
voidReadPoint();//读入顶点坐标函数
voidReadFace();//读入面坐标函数
voidKeepOriginalMatrix(doubleOrig[][4],doubleDest[][4]);
voidGetMaxY();//获得屏幕的最大y值函数
voidGetMaxX();//获得屏幕的最大x值函数
voidDrawTriView(doubleTri[8][4]);//绘制三视图调用函数
voidDrawLine(CPointp[],intnum);//绘制多边形边线函数
voidDrawCube();//绘制立方体函数
virtual~CMygroup7View();
#ifdef_DEBUG
virtualvoidAssertValid()const;
virtualvoidDump(CDumpContext&dc)const;
#endif
protected:
//Generatedmessagemapfunctions
protected:
//{{AFX_MSG(CMygroup7View)
afx_msgvoidOnDrawHView();
afx_msgvoidOnDrawWView();
afx_msgvoidOnDrawVView();
afx_msgvoidOnDrawresource();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef_DEBUG//debugversioningroup7View.cpp
inlineCMygroup7Doc*CMygroup7View:
:
GetDocument()
{return(CMygroup7Doc*)m_pDocument;}
#endif
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.
#endif//!
defined(AFX_group7VIEW_H__15A74253_C6B0_462A_83DC_3F71E576EE24__INCLUDED_)
group7View.cpp文件
//group7View.cpp:
implementationoftheCMygroup7Viewclass
//
#include"stdafx.h"
#include"group7.h"
#include"group7Doc.h"
#include"group7View.h"
#defineROUND(a)int(a+0.5)//四舍五入
#definePI3.1415926//圆周率
#include"math.h"//数学头文件
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CMygroup7View
IMPLEMENT_DYNCREATE(CMygroup7View,CView)
BEGIN_MESSAGE_MAP(CMygroup7View,CView)
//{{AFX_MSG_MAP(CMygroup7View)
ON_COMMAND(ID_DRAW_H_VIEW,OnDrawHView)
ON_COMMAND(ID_DRAW_W_VIEW,OnDrawWView)
ON_COMMAND(ID_DRAW_V_VIEW,OnDrawVView)
ON_COMMAND(ID_DRAWRESOURCE,OnDrawresource)
//}}AFX_MSG_MAP
//Standardprintingcommands
ON_COMMAND(ID_FILE_PRINT,CView:
:
OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT,CView:
:
OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW,CView:
:
OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CMygroup7Viewconstruction/destruction
CMygroup7View:
:
CMygroup7View()
{
//TODO:
addconstructioncodehere
}
CMygroup7View:
:
~CMygroup7View()
{
}
BOOLCMygroup7View:
:
PreCreateWindow(CREATESTRUCT&cs)
{
//TODO:
ModifytheWindowclassorstylesherebymodifying
//theCREATESTRUCTcs
returnCView:
:
PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
//CMygroup7Viewdrawing
voidCMygroup7View:
:
OnDraw(CDC*pDC)
{
//CMygroup7Doc*pDoc=GetDocument();
//ASSERT_VALID(pDoc);
GetMaxX();GetMaxY();
CPenMyPen3d,MyPen2d,MyPen,*OldPen;
MyPen3d.CreatePen(PS_SOLID,1,RGB(0,0,255));
OldPen=pDC->SelectObject(&MyPen3d);
pDC->MoveTo(MaxX/4,MaxY/2);//绘制y轴
pDC->LineTo(MaxX/2-25,MaxY/2);
pDC->TextOut(MaxX/2-50,MaxY/2-20,"y");
pDC->MoveTo(MaxX/4,MaxY/2);//绘制z轴
pDC->LineTo(MaxX/4,0);
pDC->TextOut(MaxX/4-20,10,"z");
pDC->MoveTo(MaxX/4,MaxY/2);//绘制x轴
pDC->LineTo(MaxX/4-MaxY/2,MaxY);//夹角为135°
pDC->TextOut(MaxX/4-MaxY/2+70,MaxY-30,"x");
pDC->TextOut(MaxX/4-20,MaxY/2-10,"0");
pDC->TextOut(20,20,"长方体");
pDC->SelectObject(OldPen);
MyPen3d.DeleteObject();
MyPen.CreatePen(PS_SOLID,3,RGB(0,255,0));
OldPen=pDC->SelectObject(&MyPen);
pDC->MoveTo(MaxX/2-25,0);//绘制分割线
pDC->LineTo(MaxX/2-25,MaxY);
pDC->SelectObject(OldPen);
MyPen.DeleteObject();
if(ViewKind==1)
{
MyPen2d.CreatePen(PS_SOLID,1,RGB(0,0,255));
OldPen=pDC->SelectObject(&MyPen2d);
pDC->MoveTo(MaxX/2-25,MaxY/2);//绘制x轴
pDC->LineTo(3*MaxX/4,MaxY/2);
pDC->TextOut(MaxX/2-15,MaxY/2-20,"x");
pDC->MoveTo(3*MaxX/4,MaxY/2);//绘制z轴
pDC->LineTo(3*MaxX/4,0);
pDC->TextOut(3*MaxX/4-20,10,"z");
pDC->MoveTo(3*MaxX/4,MaxY/2);//绘制右y轴
pDC->LineTo(MaxX,MaxY/2);
pDC->TextOut(MaxX-20,MaxY/2-20,"y");
pDC->MoveTo(3*MaxX/4,MaxY/2);//绘制下y轴
pDC->LineTo(3*MaxX/4,MaxY);
pDC->TextOut(3*MaxX/4-20,MaxY-20,"y");
pDC->TextOut(MaxX/2+10,20,"三视图");
pDC->SelectObject(OldPen);
MyPen2d.DeleteObject();
}
else
{
MyPen2d.CreatePen(PS_SOLID,1,RGB(0,0,255));
OldPen=pDC->SelectObject(&MyPen2d);
pDC->MoveTo(3*MaxX/4,MaxY/2);//绘制z轴
pDC->LineTo(3*MaxX/4,0);
pDC->TextOut(3*MaxX/4-20,10,"z");
pDC->MoveTo(3*MaxX/4,MaxY/2);//绘制右y轴
pDC->LineTo(MaxX,MaxY/2);
pDC->TextOut(MaxX-20,MaxY/2-20,"y");
pDC->TextOut(MaxX/2+10,20,"投影图");
pDC->SelectObject(OldPen);
MyPen2d.DeleteObject();
}
CMygroup7Doc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
//TODO:
adddrawcodefornativedatahere
}
/////////////////////////////////////////////////////////////////////////////
//CMygroup7Viewprinting
BOOLCMygroup7View:
:
OnPreparePrinting(CPrintInfo*pInfo)
{
//defaultpreparation
returnDoPreparePrinting(pInfo);
}
voidCMygroup7View:
:
OnBeginPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/)
{
//TODO:
addextrainitializationbeforeprinting
}
voidCMygroup7View:
:
OnEndPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/)
{
//TODO:
addcleanupafterprinting
}
/////////////////////////////////////////////////////////////////////////////
//CMygroup7Viewdiagnostics
#ifdef_DEBUG
voidCMygroup7View:
:
AssertValid()const
{
CView:
:
AssertValid();
}
voidCMygroup7View:
:
Dump(CDumpContext&dc)const
{
CView:
:
Dump(dc);
}
CMygroup7Doc*CMygroup7View:
:
GetDocument()//non-debugversionisinline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMygroup7Doc)));
return(CMygroup7Doc*)m_pDocument;
}
#endif//_DEBUG
/////////////////////////////////////////////////////////////////////////////
//CMygroup7Viewmessagehandlers
voidCMygroup7View:
:
OnDrawHView()//绘制H面
{
if(ViewKind!
=1)
{
ViewKind=1;
RedrawWindow();
}
ReadPoint();
ReadFace();
DrawCube();
doubleTH[4][4];//俯视图变换矩阵
doublePtemp[8][4];
ClearMatrix(TH);
TH[0][0]=1;
TH[1][2]=-1;
TH[3][2]=-50;
TH[3][3]=1;
KeepOriginalMatrix(Pt3D,Ptemp);
Calculate(Ptemp,TH);
DrawTriView(Ptemp);
//TODO:
Addyourcommandhandlercodehere
}
voidCMygroup7View:
:
OnDrawWView()//绘制W面
{
if(ViewKind!
=1)
{
ViewKind=1;
RedrawWindow();
}
ReadPoint();
ReadFace();
DrawCube();
doubleTW[4][4];//俯视图变换矩阵
doublePtemp[8][4];
ClearMatrix(TW);
TW[1][0]=-1;
TW[2][2]=1;
TW[3][0]=-50;
TW[3][3]=1;
KeepOriginalMatrix(Pt3D,Ptemp);
Calculate(Ptemp,TW);
DrawTriView(Ptemp);
//TODO:
Addyourcommandhandlercodehere
}
//绘制立方体函数
voidCMygroup7View:
:
DrawCube()
{
CPointp[5];//定义多边形顶点数组
Transform3DTo2D(Pt3D,Pt2D,8);
intTotalEdge,PointNumber;
for(intface=0;face<6;face++)
{
TotalEdge=Ft[face][0];//每个面的总边数
for(intedge=1;edge<=TotalEdge;edge++)//边循环
{
PointNumber=Ft[face][edge];//面的顶点号
p[edge]=CPoint(ROUND(MaxX/4+Pt2D[PointNumber][0]),ROUND(MaxY/2+Pt2D[PointNumber][1]));
}
DrawLine(p,TotalEdge);//绘制多边形
}
}
//绘制多边形边线函数
voidCMygroup7View:
:
DrawLine(CPointp[],intnum)
{
CClientDCdc(this);
for(inti=1;i<=num;i++)
if(i==1)
dc.MoveTo(p[1]);
else
dc.LineTo(p[i]);
dc.LineTo(p[1]);
}
//绘制三视图调用函数
voidCMygroup7View:
:
DrawTriView(doubleTri[][4])
{
CPointp[5];
intTotalEdge,PointNumber;
for(intface=0;face<6;face++)
{
TotalEdge=Ft[face][0];//每个面的总边数
for(intedge=1;edge<=TotalEdge;edge++)//边循环
{
PointNumber=Ft[face][edge];//面的顶点号
p[edge]=CPoint(ROUND(3*MaxX/4-Tri[PointNumber][0]),ROUND(MaxY/2-Tri[PointNumber][2]));
}
DrawLine(p,TotalEdge);
}
}
//获得屏幕的最大x值函数
voidCMygroup7View:
:
GetMaxX()
{
CRectRect;
GetClientRect(&Rect);
MaxX=Rect.right;
}
//获得屏幕的最大y值函数
voidCMygroup7View:
:
GetMaxY()
{
CRectRect;
GetClientRect(&Rect);
MaxY=Rect.bottom;
}
voidCMygroup7View:
:
KeepOriginalMatrix(doubleOrig[][4],doubleDest[][4])
{
inti,j;
for(i=0;i<8;i++)
for(j=0;j<4;j++)
Dest[i][j]=Orig[i][j];
}
//读入面坐标函数
voidCMygroup7View:
:
ReadFace()
{
Ft[0][0]=4;Ft[0][1]=0;Ft[0][2]=1;Ft[0][3]=2;Ft[0][4]=3;
Ft[1][0]=4;Ft[1][1]=3;Ft[1][2]=0;Ft[1][3]=4;Ft[1][4]=7;
Ft[2][0]=4;Ft[2][1]=7;Ft[2][2]=4;Ft[2][3]=5;Ft[2][4]=6;
Ft[3][0]=4;Ft[3][1]=6;Ft[3][2]=2;Ft[3][3]=1;Ft[3][4]=5;
Ft[4][0]=4;Ft[4][1]=5;Ft[4][2]=1;Ft[4][3]=0;Ft[4][4]=4;
Ft[5][0]=4;Ft[5][1]=6;Ft[5][2]=2;Ft[5][3]=3;Ft[5][4]=7;
}
//读入顶点坐标函数
voidCMygroup7View:
:
ReadPoint()
{
Pt3D[0][0]=0;Pt3D[0][1]=0;Pt3D[0][2]=0;Pt3D[0][3]=1;
Pt3D[1][0]=100;Pt3D[1][1]=0;Pt3D[1][2]=0;Pt3D[1][3]=1;
Pt3D[2][0]=100;Pt3D[2][1