目 录
1、课程设计目的…………………………………………………1
2、系统功能介绍…………………………………………………1
3、程序代码和分析………………………………………………2
4、总结……………………………………………………………4
5、参考文献………………………………………………………4
6.源程序………………………………………………………4
- 1 -
计算机图形学课程设计报告
1. 课程设计目的
本课程主要内容包括计算机图形学的研究内容、发展与应用,图形输入输出设备,图形显示原理,图形软件标准,基本图形生成算法,图形几何变换与裁剪,自由曲线和曲面,三维实体造型,分形几何造型,分形艺术,隐藏面消除,光照模型,颜色模型,光线跟踪,纹理细节模拟,常用的计算机动画技术和软件等。 在学期期末时按课程要求进行运动,提高学生对计算机图形学知识的了解与运用技巧同时通过此次课程设计提高动手实践能力与学习分析能力这就是本次的课程设计的目的。
2.课程设计描述及要求
此次课程设计的课题为利用VC++6.0和插件OPENGL制作三维模型。本设计主要通过建立MFC工程,在工程里建立一个三维模型然后再进行旋转,飞行等运动,来建立一个动态的三维模型。
主要步骤如下:
1:工程的建立
2:三维模型的建立和映射
3:三维模型的运动
一:工程的建立
1:系统配置。先对机子安装VC++6.0.在建立工程前,本实验需要添加OPENGL插件,故需要在Windows环境下安装GLUT
步骤如下:
1、将下载的压缩包解开,将得到5个文件
2、在“我的电脑”中搜索“gl.h”,并找到其所在文件夹(如果是VisualStudio2005,则应该是其安装目录下面的“VC\PlatformSDK\include\gl文件夹”)。把解压得到的glut.h放到这个文件夹。
3、把解压得到的glut.lib和glut32.lib放到静态函数库所在文件夹(如果是VisualStudio2005,则应该是其安装目录下面的“VC\lib”文件夹)。
4、把解压得到的glut.dll和glut32.dll放到操作系统目录下面的system32文件夹内。(典型的位置为:C:\Windows\System32)然后建立一个OpenGL工程测试
这里以VisualStudio2005为例。
选择File->New->Project,然后选择Win32 Console Application,选择一个名字,然后按OK。 在谈出的对话框左边点Application Settings,找到Empty project并勾上,选择Finish。 然后向该工程添加一个代码文件,取名为“OpenGL.c”,注意用.c来作为文件结尾。
2:建立工程。新建一个MFC文件。命名为plmy2.而其中的三维建模画图主要程序在程序plmyView.cpp附录下,主要步骤如下:
建立模型------?着色----?旋转------飞机飞行
1设置背景
void CPlmyView::OnDraw(CDC* pDC)
{
CPlmyDoc* pDoc = GetDocument();
- 1 -
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
///////////////////////////////////////
static BOOL bBusy = FALSE;
if(bBusy) return;
bBusy = TRUE;
glClearColor(0.2f,0.2f,0.5f,1.0f);//设置背景颜色
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除颜色缓存和深度缓存
glMatrixMode(GL_MODELVIEW);//glMatrixMode设置当前矩阵模式
glLoadIdentity();//将当前的用户坐标系的原点移到了屏幕中心:类似于一个复位操作
mydraw();
glFinish();
SwapBuffers(wglGetCurrentDC());
bBusy = FALSE;
////////////////////////////////////////
}
2 画挂饰运动的方法
void CPlmyView::myplane()
{
glPushMatrix();//入栈
glTranslatef(ABS(rx)*cosf((rangplane2+rangx)*6.283f/360.0f),
0.0f,
-ABS(rx)*sinf((rangplane2+rangx)*6.283f/360.0f));//移动 glRotated((rangplane2+rangx),0.0,1.0,0.0);//y
glRotated(-90.0f,0.0,1.0,0.0);//y
glRotated(rangx,1.0,0.0,0.0);//绕x旋转
glShadeModel(GL_SMOOTH);//放大缩小
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
for(int i=0;i<2;i++){
glBegin(GL_TRIANGLE_FAN);//画图
glColor3f(1.0f,0.5f,1.0f);
glVertex3f(-0.18f,0.0f,0.0f);
glColor3f(1.0f,0.5f,1.0f);
glVertex3f(0.1f,0.0f,-0.1f);
glColor3f(1.0f,0.8f,1.0f);
glVertex3f(0.09f,0.0f,-0.01f);
- 2 -
glColor3f(1.0f,0.3f,1.0f);
glVertex3f(0.08f,-0.05f,0.0f);
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(0.09f,0.0f,0.01f);
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(0.1f,0.0f,0.1f);
glEnd();
glShadeModel(GL_FLAT);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); };
glPopMatrix();
}
3:void CPlmyView::plane2()//画飞机图
4:void CPlmyView::fly()//飞机飞翔方法
5:void CPlmyView::OnTimer(UINT nIDEvent) //设置触发函数 图片如下:
- 3 -
3.系统开发环境
开发工具:Microsoft Visual Basic 6.0
开发环境:Windows 2000 Server
操作系统:Microsoft Windows XP
总结
由于属于初学VC且时间有限,所以对于前台的制作难免也诸多的不足,很多东西都要马上学习查资料并进行初步调试即拿出来使用。而制作出的前台功能单一结构简单是一大缺憾,没有时间进行精加工与进一步的调试补全源程序的不足之处使之更加完善。
不过通过这次的课程设计,使我们的自学能力与动手实践能力得到相当大的提高,编程并不是靠看书本就能学会的,而是要靠一步步的调试一条条指令的修改试运行中学习进步的,盲目的看书并不能提高自己水平。从这次的课程设计我们知道了自己的不足,学习的知识不够充分,对于现有资源材料的利用学习率太
- 4 -
低等方面的缺点,对今后的学习和工作进步都有非常大的帮助。
参考文献:
《计算机图形学》 罗笑男,王若梅
《C++面向对象程序设计》徐孝凯,张纪勇主编
《交互式计算机图形学-自顶向下方法与OpenGL应用》(美)安杰尔著
附录:源程序代码清单
Source Files:
MainFrm.cpp
plmy.cpp
plmy.rc
plmyDoc.cpp
plmyView.cpp
StdAfx.cpp
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "plmy.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_WM_GETMINMAXINFO()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
- 5 -
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
fullscreenkey=FALSE;
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
// if (!m_wndStatusBar.Create(this) ||
if (!m_wndStatusBar.Create(this, WS_CHILD | CBRS_BOTTOM) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
- 6 -
//定义状态栏
if(m_wndStatusBar){
m_wndStatusBar.SetPaneInfo(0,0,SBPS_STRETCH,50); m_wndStatusBar.SetPaneInfo(1,0,0,50);
m_wndStatusBar.SetPaneInfo(2,0,SBPS_STRETCH,50); m_wndStatusBar.SetPaneInfo(3,0,SBPS_STRETCH,50); };
//全拼显示
fullscreenkey=TRUE;
CRect winme,clientme;
GetWindowRect(&winme);
RepositionBars(0,0xffff,AFX_IDW_PANE_FIRST,reposQuery,&clientme); ClientToScreen(&clientme);
int fullwide=GetSystemMetrics(SM_CXSCREEN); int fullhide=GetSystemMetrics(SM_CYSCREEN);
mefullscreen.left=winme.left-clientme.left;
mefullscreen.top=winme.top-clientme.top;
mefullscreen.right=winme.right-clientme.right+fullwide;
mefullscreen.bottom=winme.bottom-clientme.bottom+fullhide;
WINDOWPLACEMENT winfull;
winfull.length=sizeof(WINDOWPLACEMENT);
winfull.flags=0;
winfull.showCmd=SW_SHOWNORMAL;
winfull.rcNormalPosition=mefullscreen;
SetWindowPlacement(&winfull);
// TODO: Delete these three lines if you don't want the toolbar to // be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
- 7 -
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::xyshow(int x, int y)
{
CString strsa;
if(m_wndStatusBar){
m_wndStatusBar.SetPaneText(1," ");
strsa.Format("x=%d",x);
m_wndStatusBar.SetPaneText(2,strsa);
strsa.Format("y=%d",y);
m_wndStatusBar.SetPaneText(3,strsa);
};
}
void CMainFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) {
// TODO: Add your message handler code here and/or call default if(fullscreenkey)
{
lpMMI->ptMaxSize.x=mefullscreen.Width();
lpMMI->ptMaxSize.y=mefullscreen.Height();
lpMMI->ptMaxPosition.x=mefullscreen.left;
- 8 -
lpMMI->ptMaxPosition.y=mefullscreen.top;
lpMMI->ptMaxTrackSize.x=mefullscreen.Width();
lpMMI->ptMaxTrackSize.y=mefullscreen.Height();
}
CFrameWnd::OnGetMinMaxInfo(lpMMI);
}
// plmy.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "plmy.h"
#include "MainFrm.h"
#include "plmyDoc.h"
#include "plmyView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPlmyApp
BEGIN_MESSAGE_MAP(CPlmyApp, CWinApp)
//{{AFX_MSG_MAP(CPlmyApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup) END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPlmyApp construction
- 9 -
CPlmyApp::CPlmyApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CPlmyApp object
CPlmyApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CPlmyApp initialization
BOOL CPlmyApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following // the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL #else
Enable3dControlsStatic(); // Call this when linking to MFC statically #endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate // such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CPlmyDoc),
- 10 -
RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CPlmyView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it. m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
- 11 -
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) {
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX) {
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// App command to run the dialog
void CPlmyApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
///////////////////////////////////////////////////////////////////////////// // CPlmyApp message handlers
// plmyDoc.cpp : implementation of the CPlmyDoc class //
#include "stdafx.h"
#include "plmy.h"
#include "plmyDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
- 12 -
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPlmyDoc
IMPLEMENT_DYNCREATE(CPlmyDoc, CDocument)
BEGIN_MESSAGE_MAP(CPlmyDoc, CDocument)
//{{AFX_MSG_MAP(CPlmyDoc)
// NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPlmyDoc construction/destruction
CPlmyDoc::CPlmyDoc()
{
// TODO: add one-time construction code here
}
CPlmyDoc::~CPlmyDoc()
{
}
BOOL CPlmyDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CPlmyDoc serialization
- 13 -
void CPlmyDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
///////////////////////////////////////////////////////////////////////////// // CPlmyDoc diagnostics
#ifdef _DEBUG
void CPlmyDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CPlmyDoc::Dump(CDumpContext& dc) const {
CDocument::Dump(dc);
}
#endif //_DEBUG
///////////////////////////////////////////////////////////////////////////// // CPlmyDoc commands
// plmyView.cpp : implementation of the CPlmyView class //
#include "stdafx.h"
#include "plmy.h"
#include "MainFrm.h"
#include "Math.h"
#include "plmyDoc.h"
#include "plmyView.h"
- 14 -
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPlmyView
IMPLEMENT_DYNCREATE(CPlmyView, CView)
BEGIN_MESSAGE_MAP(CPlmyView, CView)
//{{AFX_MSG_MAP(CPlmyView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_COMMAND(ID_BUTTONfull, OnBUTTONfull)
ON_COMMAND(ID_BUTTONplane, OnBUTTONplane)
ON_WM_TIMER()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CPlmyView construction/destruction
CPlmyView::CPlmyView()
{
// TODO: add construction code here设置参数
my_pDC=NULL;
mytbar=FALSE;
mysbar=FALSE;
rangx=0.0f;
rangplane2=0.0f;
rangfly=0.0f;
rangxkey=1;
rangflykey=0;
- 15 -
rx=1.0f;
rxkey=1;
#define ABS(x) ((x)<0 ? -(x) : (x)>0 ? (x) : 0)
}
CPlmyView::~CPlmyView()
{
}
BOOL CPlmyView::PreCreateWindow(CREATESTRUCT& cs)//初始化窗口 {
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style=WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CPlmyView drawing
void CPlmyView::OnDraw(CDC* pDC)
{
CPlmyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
///////////////////////////////////////
static BOOL bBusy = FALSE;
if(bBusy) return;
bBusy = TRUE;
glClearColor(0.2f,0.2f,0.5f,1.0f);//设置背景颜色
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除颜色缓存和深度缓存
glMatrixMode(GL_MODELVIEW);//glMatrixMode设置当前矩阵模式
glLoadIdentity();//将当前的用户坐标系的原点移到了屏幕中心:类似于一个复位操作
mydraw();
- 16 -
glFinish();
SwapBuffers(wglGetCurrentDC());
bBusy = FALSE;
////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////
// CPlmyView printing
BOOL CPlmyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CPlmyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) {
// TODO: add extra initialization before printing
}
void CPlmyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) {
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CPlmyView diagnostics
#ifdef _DEBUG
void CPlmyView::AssertValid() const
{
CView::AssertValid();
}
void CPlmyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CPlmyDoc* CPlmyView::GetDocument() // non-debug version is inline {
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPlmyDoc))); return (CPlmyDoc*)m_pDocument;
- 17 -
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CPlmyView message handlers
void CPlmyView::myfirst()
{
PIXELFORMATDESCRIPTOR pfd;
int n;
HGLRC hrc;
my_pDC = new CClientDC(this);
ASSERT(my_pDC != NULL);
if (!mypixelformat())
return;
n =::GetPixelFormat(my_pDC->GetSafeHdc());
::DescribePixelFormat(my_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);
hrc = wglCreateContext(my_pDC->GetSafeHdc());
wglMakeCurrent(my_pDC->GetSafeHdc(), hrc);
GetClientRect(&my_oldRect);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void CPlmyView::mydraw()//画图
{
glTranslatef(0.0f,0.0f,-7.0f);
glEnable(GL_LINE_STIPPLE);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
myplane();
plane2();
glDisable(GL_LINE_SMOOTH);
glDisable(GL_POINT_SMOOTH);
glDisable(GL_LINE_STIPPLE);
- 18 -
}
BOOL CPlmyView::mypixelformat()
{
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int pixelformat;
if ( (pixelformat = ChoosePixelFormat(my_pDC->GetSafeHdc(), &pfd)) == 0 ) {
MessageBox("ChoosePixelFormat failed");
return FALSE;
}
if (SetPixelFormat(my_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE) {
MessageBox("SetPixelFormat failed");
return FALSE;
}
return TRUE;
}
int CPlmyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- 19 -
{
if (CView::OnCreate(lpCreateStruct) == -1) return -1;
// TODO: Add your specialized creation code here myfirst();
SetTimer(1, 120, NULL);
return 0;
}
void CPlmyView::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here KillTimer(1);
HGLRC hrc;
hrc = ::wglGetCurrentContext();
::wglMakeCurrent(NULL, NULL);
if (hrc)
::wglDeleteContext(hrc);
if (my_pDC)
delete my_pDC;
}
void CPlmyView::OnSize(UINT nType, int cx, int cy) {
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here if(cy > 0)
{
my_oldRect.right = cx;
my_oldRect.bottom = cy;
glMatrixMode(GL_PROJECTION); - 20 -
glLoadIdentity();
glFrustum(-1.0,1.0,-1.0*cy/cx,1.0*cy/cx,5.0,9.0);
glViewport(0, 0, cx, cy);
}
RedrawWindow();
}
BOOL CPlmyView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
// return CView::OnEraseBkgnd(pDC);
}
void CPlmyView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMainFrame* pframe=(CMainFrame*)AfxGetApp()->m_pMainWnd; pframe->xyshow(point.x,point.y);
if(pframe->fullscreenkey){
if(point.y<5){
if(!mytbar){mytbar=TRUE;pframe->PostMessage(WM_COMMAND,ID_VIEW_TOOLBAR);};
}else{
if(mytbar){mytbar=FALSE;pframe->PostMessage(WM_COMMAND,ID_VIEW_TOOLBAR);};
};
};
CView::OnMouseMove(nFlags, point);
}
void CPlmyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMainFrame* pframe=(CMainFrame*)AfxGetApp()->m_pMainWnd; - 21 -
pframe->xyshow(point.x,point.y);
if(pframe->fullscreenkey){
if(!mysbar){pframe->PostMessage(WM_COMMAND,ID_VIEW_STATUS_BAR);mysbar=TRUE;};
};
CView::OnLButtonDown(nFlags, point);
}
void CPlmyView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMainFrame* pframe=(CMainFrame*)AfxGetApp()->m_pMainWnd; pframe->xyshow(point.x,point.y);
if(pframe->fullscreenkey){
if(mysbar){pframe->PostMessage(WM_COMMAND,ID_VIEW_STATUS_BAR);mysbar=FALSE;};
};
CView::OnLButtonUp(nFlags, point);
}
void CPlmyView::OnBUTTONfull()
{
// TODO: Add your command handler code here
CMainFrame* pframe=(CMainFrame*)AfxGetApp()->m_pMainWnd; if(pframe->fullscreenkey){
if(!mysbar){
pframe->PostMessage(WM_COMMAND,ID_VIEW_STATUS_BAR);mysbar=TRUE;
};
}else{
if(mysbar){
pframe->PostMessage(WM_COMMAND,ID_VIEW_STATUS_BAR);mysbar=FALSE;
};
};
pframe->fullscreenkey=!pframe->fullscreenkey;
}
- 22 -
void CPlmyView::OnBUTTONplane()
{
// TODO: Add your command handler code here
rangflykey=!rangflykey;
Invalidate(FALSE);
}
void CPlmyView::myplane()
{
glPushMatrix();//入栈
glTranslatef(ABS(rx)*cosf((rangplane2+rangx)*6.283f/360.0f), 0.0f,
-ABS(rx)*sinf((rangplane2+rangx)*6.283f/360.0f));//移动 glRotated((rangplane2+rangx),0.0,1.0,0.0);//y
glRotated(-90.0f,0.0,1.0,0.0);//y
glRotated(rangx,1.0,0.0,0.0);//绕x旋转
glShadeModel(GL_SMOOTH);//放大缩小
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
for(int i=0;i<2;i++){
glBegin(GL_TRIANGLE_FAN);//画图
glColor3f(1.0f,0.5f,1.0f);
glVertex3f(-0.18f,0.0f,0.0f);
glColor3f(1.0f,0.5f,1.0f);
glVertex3f(0.1f,0.0f,-0.1f);
glColor3f(1.0f,0.8f,1.0f);
glVertex3f(0.09f,0.0f,-0.01f);
glColor3f(1.0f,0.3f,1.0f);
glVertex3f(0.08f,-0.05f,0.0f);
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(0.09f,0.0f,0.01f);
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(0.1f,0.0f,0.1f);
glEnd();
glShadeModel(GL_FLAT);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
};
glPopMatrix();
- 23 -
}
void CPlmyView::plane2()//画飞机图
{
glPushMatrix();
glScalef(0.2f,0.2f,0.2f);
glTranslatef(2.6f,2.6f,0.0f),
glRotated(-rangplane2,0.0,1.0,0.0);
glLineStipple(1,0x0202);
glLineWidth (1.5f);
glColor3f(1.0f,0.8f,0.0f);
glBegin(GL_LINES);
glVertex3f(0.0f,1.0f,-0.05f);
glVertex3f(0.0f,-2.0f,-0.05f);
glEnd();
glLineStipple(1,0xFFFF);
glLineWidth (0.5f);
glShadeModel(GL_SMOOTH);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glPushMatrix();
glRotated(-80.0f,1.0,0.0,0.0);
glTranslatef(0.0f,-1.4f,-1.6f),
fly();
glRotated(60.0f,1.0,0.0,0.0);
glTranslatef(0.0f,0.2f,-1.0f),
fly();
glPopMatrix();
for(int i=0;i<4;i++){
glBegin(GL_TRIANGLE_STRIP);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,0.5f,0.0f);
glColor3f(1.0f,0.2f,0.0f);
glVertex3f(1.0f,0.3f,0.0f);
glVertex3f(0.0f,0.3f,1.0f);
glEnd();
glBegin(GL_QUADS);
glColor3f(1.0f,0.2f,0.0f);
glVertex3f(1.0f,0.3f,0.0f);
glVertex3f(0.0f,0.3f,1.0f);
glColor3f(1.0f,0.4f,0.0f);
glVertex3f(0.0f,-0.3f,1.0f);
glColor3f(1.0f,0.6f,0.0f);
glVertex3f(1.0f,-0.3f,0.0f);
- 24 -
glEnd();
glRotated(90,0.0,1.0,0.0); };
glBegin(GL_QUADS);
glColor3f(1.0f,0.6f,0.0f); glVertex3f(1.0f,-0.3f,0.0f); glVertex3f(0.0f,-0.3f,1.0f); glVertex3f(-1.0f,-0.3f,0.0f); glColor3f(1.0f,0.4f,0.0f); glVertex3f(0.0f,-0.3f,-1.0f); glEnd();
glTranslatef(0.0f,-1.6f,0.0f),
glColor3f(1.0f,0.6f,0.0f); glBegin(GL_QUAD_STRIP); glVertex3f(0.0f,1.0f,0.0f);
glColor3f(1.0f,0.0f,0.0f); glVertex3f(-1.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f); glVertex3f(0.0f,-1.0f,0.0f); glVertex3f(1.0f,0.0f,-0.1f);
glColor3f(1.0f,0.6f,0.0f); glVertex3f(0.0f,-1.0f,-0.1f);
glColor3f(1.0f,0.0f,0.0f); glVertex3f(0.0f,1.0f,-0.1f);
glVertex3f(-1.0f,0.0f,-0.1f); glVertex3f(0.0f,1.0f,0.0f); glVertex3f(-1.0f,0.0f,0.0f); glEnd();
glBegin(GL_QUADS);
glVertex3f(0.0f,1.0f,0.0f); glVertex3f(1.0f,0.0f,0.0f); glVertex3f(1.0f,0.0f,-0.1f); glVertex3f(0.0f,1.0f,-0.1f); glEnd();
glBegin(GL_QUADS);
glVertex3f(0.0f,-1.0f,0.0f); - 25 -
glVertex3f(-1.0f,0.0f,0.0f);
glVertex3f(-1.0f,0.0f,-0.1f);
glVertex3f(0.0f,-1.0f,-0.1f);
glEnd();
glPopMatrix();
}
void CPlmyView::fly()//飞机飞翔方法 {
glPushMatrix();
glTranslatef(0.0f,0.0f,0.01f),
glScalef(1.6f,1.6f,1.6f);
glRotated(-ABS(rangfly),0.0,1.0,0.0); glShadeModel(GL_SMOOTH);
glBegin(GL_POLYGON);
glColor3f(0.0f,0.2f,1.0f);
glVertex3f(0.0f,0.1f,0.0f);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(0.2f,0.2f,0.0f);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(0.15f,-0.1f,0.0f);
glColor3f(0.0f,0.2f,1.0f);
glVertex3f(0.0f,0.0f,0.0f);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(0.15f,-0.15f,0.0f);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(0.16f,-0.25f,0.0f);
glColor3f(0.0f,0.2f,1.0f);
glVertex3f(0.0f,-0.03f,0.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f,0.0f,0.01f),
glScalef(1.6f,1.6f,1.6f);
glRotated(ABS(rangfly),0.0,1.0,0.0); glBegin(GL_POLYGON);
glColor3f(0.0f,0.2f,1.0f);
glVertex3f(0.0f,0.1f,0.0f);
glColor3f(0.0f,0.2f,1.0f);
glVertex3f(0.0f,-0.03f,0.0f);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(-0.16f,-0.25f,0.0f);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(-0.15f,-0.15f,0.0f);
glColor3f(0.0f,0.2f,1.0f);
- 26 -
glVertex3f(0.0f,0.0f,0.0f);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(-0.15f,-0.1f,0.0f);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(-0.2f,0.2f,0.0f);
glEnd();
glPopMatrix();
}
void CPlmyView::OnTimer(UINT nIDEvent) //设置触发函数
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1){
if(rangflykey){
if(ABS(rangx)==26.0f){rangxkey=-rangxkey;};
rangx=rangx+0.5f*rangxkey;
if(ABS(rx)>=0.8f){rxkey=-rxkey;}else{
if(ABS(rx)<=0.5f){rxkey=-rxkey;}};
rx=rx-0.001f*rxkey;
if(rangplane2==360.0f){rangplane2=5.0f;}else{rangplane2=rangplane2+5.0f;}; };
if(ABS(rangfly)==90.0f){rangfly=-rangfly;};
rangfly=rangfly+30.0f;
Invalidate(FALSE);
};
CView::OnTimer(nIDEvent);
}
// stdafx.cpp : source file that includes just the standard includes
// plmy.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
- 27 -
目录1课程设计目的12系统功能介绍13程序代码和分析24总结45参考文献46源程序41计算机图形学课程设计报告1课程设计目的本课程…
图形学课程设计多边形剪裁和填充图形软件设计1一题目内容说明1交互式地实现多边形的裁剪和填充2功能要求1窗口功能设计2实现鼠标画多边…
课程设计报告题目旋转四面体摘要本文主要描述了计算机图形学中,运用C++和OpenGL来实现的绘制一个实体四面体。其中简单的用glu…
攀枝花学院学生课程设计论文题目图书馆图书管理系统学生姓名学号所在院系计算机学院专业计算机科学与技术班级20xx级1班指导教师罗学刚…
计算机图形学课程设计摘要巩固和实践计算机图形学课程中的理论和算法学习表现计算机图形学算法的技巧培养认真学习积极探索的精神总体要求策…
实验报告实验课程:计算机图形学学生姓名:XXX学号:XXX专业班级:计算机科学与技术X班20XX年XX月XX日目录实验一直线和圆的…
课程设计报告学生姓名学院班级题目刘名凤学号0809290102理学院信计081奥运五环指导教师常志文职称教授邓冠男职称助教20xx…
北京联合大学应用文理学院实验报告课程名称实验实训名称班级信息与计算科学20xx级姓名学号同组者实验实训日期完成日期本实验实训所用学…
福建农林大学计算机与信息学院课程名称姓名系专业年级学号指导教师职称实验报告计算机图形学洪世玉计算机计算机科学与技术10级10226…
计算机图形学实验报告河南理工大学测绘学院计算机图形学实验报告学号姓名成绩评语交报告日期20xx年6月25日计算机图形学实验报告实验…
通过一个学期的学习,了解了什么是计算机图形学、什么是图形API、为什么需要计算机图形学以及计算机图形学在各个领域的应用。计算机图形…