OpenGL新手入门实验2view类的代码.docx
《OpenGL新手入门实验2view类的代码.docx》由会员分享,可在线阅读,更多相关《OpenGL新手入门实验2view类的代码.docx(18页珍藏版)》请在冰豆网上搜索。
OpenGL新手入门实验2view类的代码
//Exam3View.cpp:
implementationoftheCExam3Viewclass
//
#include
#include
#include
#include
#include"stdafx.h"
#include"Exam3.h"
#include"Exam3Doc.h"
#include"Exam3View.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CExam3View
IMPLEMENT_DYNCREATE(CExam3View,CView)
BEGIN_MESSAGE_MAP(CExam3View,CView)
//{{AFX_MSG_MAP(CExam3View)
ON_WM_DESTROY()
ON_WM_CREATE()
ON_WM_SIZE()
ON_COMMAND(IDM_TRIANGLESTRIP,OnTrianglestrip)
ON_UPDATE_COMMAND_UI(IDM_TRIANGLESTRIP,OnUpdateTrianglestrip)
ON_COMMAND(IDM_TRIANGLES,OnTriangles)
ON_UPDATE_COMMAND_UI(IDM_TRIANGLES,OnUpdateTriangles)
ON_COMMAND(IDM_TRIANGLEFAN,OnTrianglefan)
ON_UPDATE_COMMAND_UI(IDM_TRIANGLEFAN,OnUpdateTrianglefan)
ON_COMMAND(IDM_QUADSTRIP,OnQuadstrip)
ON_UPDATE_COMMAND_UI(IDM_QUADSTRIP,OnUpdateQuadstrip)
ON_COMMAND(IDM_QUADRILATERS,OnQuadrilaters)
ON_UPDATE_COMMAND_UI(IDM_QUADRILATERS,OnUpdateQuadrilaters)
ON_COMMAND(IDM_POINTS,OnPoints)
ON_UPDATE_COMMAND_UI(IDM_POINTS,OnUpdatePoints)
ON_COMMAND(IDM_PLOYGONSTIPPLE,OnPloygonstipple)
ON_UPDATE_COMMAND_UI(IDM_PLOYGONSTIPPLE,OnUpdatePloygonstipple)
ON_COMMAND(IDM_PLOYGON,OnPloygon)
ON_UPDATE_COMMAND_UI(IDM_PLOYGON,OnUpdatePloygon)
ON_COMMAND(IDM_NONCONVEX,OnNonconvex)
ON_UPDATE_COMMAND_UI(IDM_NONCONVEX,OnUpdateNonconvex)
ON_COMMAND(IDM_LINESRIPPLE,OnLinesripple)
ON_UPDATE_COMMAND_UI(IDM_LINESRIPPLE,OnUpdateLinesripple)
ON_COMMAND(IDM_LINES,OnLines)
ON_UPDATE_COMMAND_UI(IDM_LINES,OnUpdateLines)
ON_COMMAND(IDM_LINELOOP,OnLineloop)
ON_UPDATE_COMMAND_UI(IDM_LINELOOP,OnUpdateLineloop)
ON_COMMAND(IDM_CUBE,OnCube)
ON_UPDATE_COMMAND_UI(IDM_CUBE,OnUpdateCube)
//}}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()
/////////////////////////////////////////////////////////////////////////////
//CExam3Viewconstruction/destruction
CExam3View:
:
CExam3View()
{
m_twoDShape=Points;
}
CExam3View:
:
~CExam3View()
{
}
BOOLCExam3View:
:
PreCreateWindow(CREATESTRUCT&cs)
{
returnCView:
:
PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
//CExam3Viewdrawing
voidCExam3View:
:
OnDraw(CDC*pDC)
{
CExam3Doc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
Draw();//调用Draw()函数
}
/////////////////////////////////////////////////////////////////////////////
//CExam3Viewprinting
BOOLCExam3View:
:
OnPreparePrinting(CPrintInfo*pInfo)
{
returnDoPreparePrinting(pInfo);
}
voidCExam3View:
:
OnBeginPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/)
{
//TODO:
addextrainitializationbeforeprinting
}
voidCExam3View:
:
OnEndPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/)
{
//TODO:
addcleanupafterprinting
}
/////////////////////////////////////////////////////////////////////////////
//CExam3Viewdiagnostics
#ifdef_DEBUG
voidCExam3View:
:
AssertValid()const
{
CView:
:
AssertValid();
}
voidCExam3View:
:
Dump(CDumpContext&dc)const
{
CView:
:
Dump(dc);
}
CExam3Doc*CExam3View:
:
GetDocument()//non-debugversionisinline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CExam3Doc)));
return(CExam3Doc*)m_pDocument;
}
#endif//_DEBUG
/////////////////////////////////////////////////////////////////////////////
//CExam3Viewmessagehandlers
voidCExam3View:
:
OnDestroy()
{
CView:
:
OnDestroy();
:
:
wglMakeCurrent(NULL,NULL);
:
:
wglDeleteContext(m_hRC);
}
intCExam3View:
:
OnCreate(LPCREATESTRUCTlpCreateStruct)
{
if(CView:
:
OnCreate(lpCreateStruct)==-1)
return-1;
gl_Init();//初始化OpenGL绘图环境
return0;
//固定模式,必须这样自写的不用修改
}
voidCExam3View:
:
OnSize(UINTnType,intcx,intcy)
{//每次都是需要修改的~!
CView:
:
OnSize(nType,cx,cy);
glViewport(0,0,cx,cy);//设定视口大小
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-cx/2,cx/2,-cy/2,cy/2,-10,10);//设置用户定义的绘图区域
//glFrustum();
//glOtho2D();设置投影方式
//TODO:
Addyourmessagehandlercodehere
}
voidCExam3View:
:
Draw()
{
glClearColor(1.0f,1.0f,1.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);//置窗口底色为白色
glColor3f(0.0f,0.0f,0.0f);//设置绘图颜色为黑色
//可以将这几句话看成是一个整体,不需要改动直接用于Draw()函数中即可
switch(m_twoDShape)
{
casePoints:
DrawPoints();break;
caseLines:
DrawLines();break;
caseLinesripple:
DrawLinestipple();break;
caseLinestrip:
DrawLinestrip();break;
caseLineloop:
DrawLineloop();break;
casePloygon:
DrawPloygon();break;
casePloygonstipple:
DrawPloygonstipple();break;
caseTriangles:
DrawTriangles();break;
caseTrianglestrip:
DrawTrianglestrip();break;
caseTrianglefan:
DrawTriangleFan();break;
caseQuadrilaters:
DrawQuadrilaters();break;
caseQuadstrip:
DrawQuadStrip();break;
caseNonconvex:
DrawNonconvex();break;
caseCube:
DrawCube();break;
}
//SwapBuffers(m_hDC);
//glFlush();//显示上述OpenGL命令的执行效果
//这两句话要么加载在调用函数处,要么加载在函数体内,但是不能重复加载
}
voidCExam3View:
:
gl_Init()
{
PIXELFORMATDESCRIPTORpfd={
sizeof(PIXELFORMATDESCRIPTOR),//数据结构大小
1,//数据结构版本号
PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,//缓冲区可以在窗口上绘图,支持OpenGL绘图
24,//深度颜色缓冲区位数
0,0,0,0,0,0,//忽略颜色位
0,0,0,//没有非透明度缓存,忽略移位位,无累加缓存
0,0,0,0,//忽略累加位
32,//32位深度缓存
0,//无模板缓存
0,//无辅助缓存
PFD_MAIN_PLANE,//主层
0,//保留
0,0,0,//忽略层,可见性和损毁掩模
};
//得到当前正在使用的绘图设备句柄
m_hDC=GetDC()->GetSafeHdc();
/*根据当前绘图设备的特性,为之选择一个与pfd所指定的格式相匹配的像素格式,存于nPixelFormat中。
*/
intnPixelFormat=:
:
ChoosePixelFormat(m_hDC,&pfd);
//将当前绘图设备的像素格式设为nPixelFormat
:
:
SetPixelFormat(m_hDC,nPixelFormat,&pfd);
//根据当前绘图设备,创建一个像素格式与之相匹配的OpenGL的绘图上下文
m_hRC=:
:
wglCreateContext(m_hDC);
/*指定OpenGL的绘图上下文是m_hRC,并且建立它与当前绘图设备m_hDC间的连接,下列函数执行后,所有绘图命令的结果都出现在设备m_hDC上*/
:
:
wglMakeCurrent(m_hDC,m_hRC);
//gl_Enable()函数的调用做深度测试来消隐
}
voidCExam3View:
:
gl_Point(GLintx,GLinty)
{
glBegin(GL_POINTS);//画一个点
glVertex2d(x,y);//默认颜色为黑色
glEnd();
}
voidCExam3View:
:
OnTrianglestrip()
{
m_twoDShape=Trianglestrip;
Invalidate();
//经典句型,凡是点击相应程序均有这句话,但是声明的变量名和枚举类型名不一样
}
voidCExam3View:
:
OnUpdateTrianglestrip(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
if(m_twoDShape==Trianglestrip)
pCmdUI->SetCheck(TRUE);
else
pCmdUI->SetCheck(FALSE);
//经典的更新函数体(具体用法同上)
}
voidCExam3View:
:
OnTriangles()
{
//TODO:
Addyourcommandhandlercodehere
m_twoDShape=Triangles;
Invalidate();
}
voidCExam3View:
:
OnUpdateTriangles(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
if(m_twoDShape==Triangles)
pCmdUI->SetCheck(TRUE);
else
pCmdUI->SetCheck(FALSE);
}
voidCExam3View:
:
OnTrianglefan()
{
//TODO:
Addyourcommandhandlercodehere
m_twoDShape=Trianglefan;
Invalidate();
}
voidCExam3View:
:
OnUpdateTrianglefan(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
}
voidCExam3View:
:
OnQuadstrip()
{
//TODO:
Addyourcommandhandlercodehere
m_twoDShape=Quadstrip;
Invalidate();
}
voidCExam3View:
:
OnUpdateQuadstrip(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
}
voidCExam3View:
:
OnQuadrilaters()
{
//TODO:
Addyourcommandhandlercodehere
m_twoDShape=Quadrilaters;
Invalidate();
}
voidCExam3View:
:
OnUpdateQuadrilaters(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
if(m_twoDShape==Quadrilaters)
pCmdUI->SetCheck(TRUE);
else
pCmdUI->SetCheck(FALSE);
}
voidCExam3View:
:
OnPoints()
{
//TODO:
Addyourcommandhandlercodehere
m_twoDShape=Points;
Invalidate();
}
voidCExam3View:
:
OnUpdatePoints(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
if(m_twoDShape==Points)
pCmdUI->SetCheck(TRUE);
else
pCmdUI->SetCheck(FALSE);
}
voidCExam3View:
:
OnPloygonstipple()
{
//TODO:
Addyourcommandhandlercodehere
m_twoDShape=Ploygonstipple;
Invalidate();
}
voidCExam3View:
:
OnUpdatePloygonstipple(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
if(m_twoDShape==Ploygonstipple)
pCmdUI->SetCheck(TRUE);
else
pCmdUI->SetCheck(FALSE);
}
voidCExam3View:
:
OnPloygon()
{
//TODO:
Addyourcommandhandlercodehere
m_twoDShape=Ploygon;
Invalidate();
}
voidCExam3View:
:
OnUpdatePloygon(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
if(m_twoDShape==Ploygon)
pCmdUI->SetCheck(TRUE);
else
pCmdUI->SetCheck(FALSE);
}
voidCExam3View:
:
OnNonconvex()
{
//TODO:
Addyourcommandhandlercodehere
m_twoDShape=Nonconvex;
Invalidate();
}
voidCExam3View:
:
OnUpdateNonconvex(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
if(m_twoDShape==Nonconvex)
pCmdUI->SetCheck(TRUE);
else
pCmdUI->SetCheck(FALSE);
}
voidCExam3View:
:
OnLinesripple()
{
//TODO:
Addyourcommandhandlercodehere
m_twoDShape=Linesripple;
Invalidate();
}
voidCExam3View:
:
OnUpdateLinesripple(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
if(m_twoDShape==Linesripple)
pCmdUI->SetCheck(TRUE);
else
pCmdUI->SetCheck(FALSE);
}
voidCExam3View:
:
OnLines()
{
//TODO:
Addyourcommandhandlercodehere
m_twoDShape=Lines;
Invalidate();
}
voidCExam3View:
:
OnUpdateLines(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUIhandlercodehere
if(m_twoDShape==Lines)
pCmdUI->SetCheck(TRUE);
else
pCmdUI->SetCheck(FALSE);
}
voidCExam3View:
:
OnLineloop()
{
//TODO:
Addyourcommandhandlercodehere
m_twoDShape=Lineloop;
Invalidate();
}
voidCExam3View:
:
OnUpdateLineloop(CCmdUI*pCmdUI)
{
//TODO:
AddyourcommandupdateUI