资源描述
广西师范大学 计算机科学与信息工程学院 08计应 晓晓版
计算机图形学实验——画立方体(通过视向变换等)
具体实现如下(仅供参考)
一、添加一个头文件Data.h
Data文件包含立方体数据:
具体代码:
double Vertex[8][3]={
{0,20,0},{30,20,0},{30,20,15},{0,20,15},
{0,0,0},{30,0,0},{30,0,15},{0,0,15}};
int Edge[12][2]={
{0,1},{1,2},{2,3},{3,0},
{4,5},{5,6},{6,7},{7,4},
{0,4},{1,5},{2,6},{3,7}};
double Vertex1[8][3];
//视点
double Eye[3]={20.0f,30.0f,56.1f};
//窗口数据
//int Window[4]={-30,5,-15,3};
int Window[4]={-20,-15,-2,-20};
//视区数据
int ViewPort[4]={200,250,180,15};
二、View类头文件
6_Draw cubeView.h
// 6_Draw cubeView.h : interface of the CMy6_DrawcubeView class
//
/////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_6_DRAWCUBEVIEW_H__940E4DA8_261A_46F3_82C2_259B9B805817__INCLUDED_)
#define AFX_6_DRAWCUBEVIEW_H__940E4DA8_261A_46F3_82C2_259B9B805817__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMy6_DrawcubeView : public CView
{
protected: // create from serialization only
CMy6_DrawcubeView();
DECLARE_DYNCREATE(CMy6_DrawcubeView)
// Attributes
public:
CMy6_DrawcubeDoc* GetDocument();
bool draw;
void drawCube();
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMy6_DrawcubeView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CMy6_DrawcubeView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CMy6_DrawcubeView)
afx_msg void Ondrawcube();
afx_msg void OnUpdatedrawcube(CCmdUI* pCmdUI);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#ifndef _DEBUG // debug version in 6_Draw cubeView.cpp
inline CMy6_DrawcubeDoc* CMy6_DrawcubeView::GetDocument()
{ return (CMy6_DrawcubeDoc*)m_pDocument; }
#endif
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_6_DRAWCUBEVIEW_H__940E4DA8_261A_46F3_82C2_259B9B805817__INCLUDED_)
三、View 类实现代码:
6_Draw cubeView.cpp
代码:
// 6_Draw cubeView.cpp : implementation of the CMy6_DrawcubeView class
//
#include "stdafx.h"
#include "6_Draw cube.h"
#include "data.h"
#include <math.h>
#include "6_Draw cubeDoc.h"
#include "6_Draw cubeView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMy6_DrawcubeView
IMPLEMENT_DYNCREATE(CMy6_DrawcubeView, CView)
BEGIN_MESSAGE_MAP(CMy6_DrawcubeView, CView)
//{{AFX_MSG_MAP(CMy6_DrawcubeView)
ON_COMMAND(drawcube, Ondrawcube)
ON_UPDATE_COMMAND_UI(drawcube, OnUpdatedrawcube)
//}}AFX_MSG_MAP
// Standard printing commands
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()
/////////////////////////////////////////////////////////////////////////////
// CMy6_DrawcubeView construction/destruction
CMy6_DrawcubeView::CMy6_DrawcubeView()
{
// TODO: add construction code here
draw=true;
}
CMy6_DrawcubeView::~CMy6_DrawcubeView()
{
}
BOOL CMy6_DrawcubeView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMy6_DrawcubeView drawing
void CMy6_DrawcubeView::OnDraw(CDC* pDC)
{
CMy6_DrawcubeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CClientDC dc(this);
dc.TextOut(10,10,"计算计图形学作业六:画立方体");
dc.TextOut(10,30,"姓名:晓晓");
dc.TextOut(10,50,"学号:200812301004");
if(draw)
{
//dc.TextOut(100,100,"fdsfsdf");
drawCube();
}
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMy6_DrawcubeView printing
BOOL CMy6_DrawcubeView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMy6_DrawcubeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMy6_DrawcubeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMy6_DrawcubeView diagnostics
#ifdef _DEBUG
void CMy6_DrawcubeView::AssertValid() const
{
CView::AssertValid();
}
void CMy6_DrawcubeView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMy6_DrawcubeDoc* CMy6_DrawcubeView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy6_DrawcubeDoc)));
return (CMy6_DrawcubeDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMy6_DrawcubeView message handlers
void CMy6_DrawcubeView::Ondrawcube()
{
// TODO: Add your command handler code here
draw=!draw;
CMy6_DrawcubeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDoc->UpdateAllViews(NULL);
}
void CMy6_DrawcubeView::OnUpdatedrawcube(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if(draw)
{
pCmdUI->SetCheck(1);
}
else
{pCmdUI->SetCheck(0);}
}
void CMy6_DrawcubeView::drawCube()
{
//读入模型
//定义视点
//视向变换
double V[8][3];
int i;
double a=sqrt(Eye[0]*Eye[0]+Eye[1]*Eye[1]),
b=sqrt(Eye[0]*Eye[0]+Eye[1]*Eye[1]+Eye[2]*Eye[2]);
for(i=0;i<8;i++)
{
V[i][0]= Vertex[i][0]*(-Eye[1]/a)
+ Vertex[i][1]*(Eye[0]/a) ;
V[i][1]= Vertex[i][0]*(-Eye[0]*Eye[2]/(a*b))
+ Vertex[i][1]*(Eye[1]*Eye[2]/(a*b))
+ Vertex[i][2]*a/b;
V[i][2]= Vertex[i][0]*(-Eye[0]/b)
+ Vertex[i][1]*(-Eye[1]/b)
+ Vertex[i][2]*(-Eye[2]/b);
}
//投影到XY平面
//窗口视区变换
double a1=(ViewPort[1]-ViewPort[0])/(Window[1]-Window[0]);
double b1=ViewPort[0]-a1*Window[0];
double c1=(ViewPort[3]-ViewPort[2])/(Window[3]-Window[2]);
double d1=ViewPort[2]-c1*Window[2];
for(i=0;i<8;i++)
{
V[i][0]=a1*V[i][0]+b1;
V[i][1]=c1*V[i][1]+d1;
}
//画出投影图
CClientDC dc(this);
for(i=0;i<12;i++)
{
dc.MoveTo((int)V[Edge[i][0]][0],(int)V[Edge[i][0]][1]);
dc.LineTo((int)V[Edge[i][1]][0],(int)V[Edge[i][1]][1]);
}
}
三、效果图
展开阅读全文