opengl6elbow源代码 vs.docx
- 文档编号:18005425
- 上传时间:2023-04-24
- 格式:DOCX
- 页数:67
- 大小:83.07KB
opengl6elbow源代码 vs.docx
《opengl6elbow源代码 vs.docx》由会员分享,可在线阅读,更多相关《opengl6elbow源代码 vs.docx(67页珍藏版)》请在冰豆网上搜索。
opengl6elbow源代码vs
//VRworkView.cpp:
implementationoftheCVRworkViewclass
//
#include
#include"stdafx.h"
#include"VRwork.h"
#include"VRworkDoc.h"
#include"VRworkView.h"
#include"LeftOption.h"
#include"OpenGLInit.h"
#include
#include
#include
#pragmacomment(lib,"glaux.lib")
#pragmacomment(lib,"opengl32.lib")
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CVRworkView
IMPLEMENT_DYNCREATE(CVRworkView,CView)
BEGIN_MESSAGE_MAP(CVRworkView,CView)
//{{AFX_MSG_MAP(CVRworkView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_TIMER()
//}}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()
CVRworkView:
:
CVRworkView():
m_hRC(0),m_pDC(0),m_ErrorStrings(_ErrorStrings[0])
{//默?
认¨?
情¨¦况?
下?
,ê?
不?
显?
示º?
坐Á?
标À¨º轴¨¢
showCoordinate=FALSE;
rotX=0.0f;
rotZ=0.0f;
}
CVRworkView:
:
~CVRworkView()
{
}
BOOLCVRworkView:
:
PreCreateWindow(CREATESTRUCT&cs)//定¡§义°?
窗ä¡ã口¨²
{
cs.style|=WS_CLIPSIBLINGS|WS_CLIPCHILDREN;
returnCView:
:
PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
//CVRworkViewdrawing
voidCVRworkView:
:
OnDraw(CDC*pDC)//绘?
制?
图ª?
像?
{
CVRworkDoc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
:
:
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();//将?
当Ì¡À前¡ã矩?
阵¨®压1入¨?
矩?
阵¨®堆?
栈?
//视º¨®点Ì?
移°?
动¡¥
glRotatef(rotX,1.0f,0.0f,0.0f);
glRotatef(rotZ,0.0f,0.0f,1.0f);
{
:
:
glPushMatrix();
//在¨²这a里¤?
绘?
制?
静2止1物?
体¬?
RenderStockScene();
:
:
glPopMatrix();//将?
当Ì¡À前¡ã矩?
阵¨®弹Ì¡¥出?
矩?
阵¨®堆?
栈?
}
{
:
:
glPushMatrix();
//在¨²这a里¤?
绘?
制?
移°?
动¡¥物?
体¬?
RenderScene();
:
:
glPopMatrix();
}
glPopMatrix();
:
:
glFinish();
if(FALSE==:
:
SwapBuffers(m_pDC->GetSafeHdc()))
SetError(7);
//DrawScene();
}
/////////////////////////////////////////////////////////////////////////////
//CVRworkViewprinting
//BOOLCVRworkView:
:
OnPreparePrinting(CPrintInfo*pInfo)
//{
////defaultpreparation
//returnDoPreparePrinting(pInfo);
//}
//voidCVRworkView:
:
OnBeginPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/)
//{
////TODO:
addextrainitializationbeforeprinting
//}
//voidCVRworkView:
:
OnEndPrinting(CDC*/*pDC*/,CPrintInfo*/*pInfo*/)
//{
////TODO:
addcleanupafterprinting
//}
/////////////////////////////////////////////////////////////////////////////
//CVRworkViewdiagnostics
#ifdef_DEBUG
voidCVRworkView:
:
AssertValid()const
{
CView:
:
AssertValid();
}
voidCVRworkView:
:
Dump(CDumpContext&dc)const
{
CView:
:
Dump(dc);
}
CVRworkDoc*CVRworkView:
:
GetDocument()//non-debugversionisinline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CVRworkDoc)));
return(CVRworkDoc*)m_pDocument;
}
#endif//_DEBUG
/////////////////////////////////////////////////////////////////////////////
//CVRworkViewmessagehandlers
intCVRworkView:
:
OnCreate(LPCREATESTRUCTlpCreateStruct)
{
if(CView:
:
OnCreate(lpCreateStruct)==-1)
return-1;
InitalizeOpenGL();
SetTimer(0,30,NULL);//每?
30MS触䣤发¤¡é定¡§时º¡À器¡Â
return0;
}
voidCVRworkView:
:
OnDestroy()
{
CView:
:
OnDestroy();
KillTimer
(1);
if(FALSE==:
:
wglMakeCurrent(0,0))
SetError
(2);
if(FALSE==:
:
wglDeleteContext(m_hRC))
SetError(6);
if(m_pDC)
deletem_pDC;
}
voidCVRworkView:
:
OnSize(UINTnType,intcx,intcy)//设¦¨¨置?
视º¨®口¨²
{
CView:
:
OnSize(nType,cx,cy);
GLdoubleaspect_ratio;
if(0>=cx||0>=cy)
return;
SetupViewport(cx,cy);
//computetheaspectratio
aspect_ratio=(GLdouble)cx/(GLdouble)cy;
:
:
glMatrixMode(GL_PROJECTION);
:
:
glLoadIdentity();
//TheglLoadIdentity
//functionreplacesthecurrentmatrixwiththeidentitymatrix
SetupViewingFrustum(aspect_ratio);
SetupViewingTransform();
:
:
glMatrixMode(GL_MODELVIEW);
//Appliessubsequentmatrixoperationstothemodelviewmatrixstack
:
:
glLoadIdentity();
SetupLighting();
}
constchar*constCVRworkView:
:
_ErrorStrings[]={
{"NoError"},//0
{"UnabletogetaDC"},//1
{"ChoosePixelFormatfailed"},//2
{"SelectPixelFormatfailed"},//3
{"wglCreateContextfailed"},//4
{"wglMakeCurrentfailed"},//5
{"wglDeleteContextfailed"},//6
{"SwapBuffersfailed"},//7
};
voidCVRworkView:
:
SetError(inte)
{
//iftherewasnopriviouserror,
//thensavethisone
if(_ErrorStrings[0]==m_ErrorStrings)
m_ErrorStrings=_ErrorStrings[e];
}
BOOLCVRworkView:
:
InitalizeOpenGL()
{
m_pDC=newCClientDC(this);
if(NULL==m_pDC){//failedtogetDC
SetError
(1);
returnFALSE;
}
if(!
SetupPixelFormat())
returnFALSE;
if(0==(m_hRC=
:
:
wglCreateContext(m_pDC->GetSafeHdc()))){
SetError(4);
returnFALSE;
}
if(FALSE==
:
:
wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC)){
SetError(5);
returnFALSE;
}
:
:
glClearColor(0.0f,0.0f,0.0f,0.0f);
:
:
glClearDepth(1.0f);
:
:
glEnable(GL_DEPTH_TEST);
returnTRUE;
}
BOOLCVRworkView:
:
SetupPixelFormat()
{
PIXELFORMATDESCRIPTORpfd=
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|//Thebuffercandrawtoawindowordevicesurface
PFD_SUPPORT_OPENGL|//ThebuffersupportsOpenGLdrawing.
PFD_DOUBLEBUFFER,//Thebufferisdouble-buffered.ThisflagandPFD_SUPPORT_GDIaremutuallyexclusiveinthecurrentgenericimplementation.
PFD_TYPE_RGBA,//RGBApixels.Eachpixelhasfourcomponentsinthisorder:
red,green,blue,andalpha.
24,
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
intpixelformat;
if(0==(pixelformat=
:
:
ChoosePixelFormat(m_pDC->GetSafeHdc(),&pfd)))
{
SetError
(2);
returnFALSE;
}
if(FALSE==:
:
SetPixelFormat(m_pDC->GetSafeHdc(),
pixelformat,&pfd))
{
SetError(3);
returnFALSE;
}
returnTRUE;
}
BOOLCVRworkView:
:
SetupViewport(intcx,intcy)
{glViewport(0,0,cx,cy);
returnTRUE;
}
//设¦¨¨置?
视º¨®见?
空?
间?
的Ì?
平?
截?
头ª¡¤体¬?
BOOLCVRworkView:
:
SetupViewingFrustum(GLdoubleaspect_ratio)
{
//selectadefaultviewingvolume
gluPerspective(40.0f,aspect_ratio,0.1f,20.0f);
returnTRUE;
}
BOOLCVRworkView:
:
SetupViewingTransform()
{
inti;
GLfloatfovy=30.0;//画-角?
GLfloateye[3];
GLfloatcenter[3]={0.0f,0.0f,1.0f};
GLfloateye_dir[3];
GLfloatup[3];
GLfloatnorm,dist;
eye[0]=0.0;
eye[1]=5.0;
eye[2]=2.0;
for(i=0;i<3;i++)
eye_dir[i]=center[i]-eye[i];
dist=(GLfloat)sqrt(eye_dir[0]*eye_dir[0]+eye_dir[1]*eye_dir[1]+eye_dir[2]*eye_dir[2]);
for(i=0;i<3;i++)
eye_dir[i]/=(GLfloat)dist;
//向¨°上¦?
的Ì?
方¤?
向¨°向¨°量¢?
的Ì?
设¦¨¨定¡§
up[0]=-eye_dir[0]*eye_dir[2];
up[1]=-eye_dir[1]*eye_dir[2];
up[2]=eye_dir[0]*eye_dir[0]+eye_dir[1]*eye_dir[1];
norm=up[1]*up[1]+up[1]*up[1]+up[2]*up[2];
norm=(GLfloat)sqrt(norm);
for(i=0;i<3;i++)
up[i]/=norm;
gluLookAt(eye[0],eye[1],eye[2],
center[0],center[1],center[2],
up[0],up[1],up[2]);
returnTRUE;
}
BOOLCVRworkView:
:
SetupLighting()
{
//设¦¨¨置?
光a照?
GLfloatmodel_ambient[]={2.0f,2.0f,2.0f,1.0f};
GLfloatlight_position0[]={1.0f,0.0f,5.0f,0.0f};
GLfloatlight_color0[]={1.0f,1.0f,1.0f,1.0f};
//环¡¤境3光a
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,model_ambient);
//光a源¡ä位?
置?
glLightfv(GL_LIGHT0,GL_POSITION,light_position0);
//光a源¡ä颜?
色¦?
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_color0);
//打䨰开a光a源¡ä
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
returnTRUE;
}
BOOLCVRworkView:
:
RenderScene()
{
//绕¨?
向¨°量¢?
(1,0,0)旋y转Áa90度¨¨
:
:
glRotatef(90.0f,1.0f,0.0f,0.0f);
//设¦¨¨置?
当Ì¡À前¡ã颜?
色¦?
为a红¨¬色¦?
:
:
glColor3f(1.0f,0.0f,0.0f);
//
:
:
auxWireSphere(0.5);
:
:
glColor3f(0.5f,0.5f,1.0f);
:
:
auxWireCube(1.0);
returnTRUE;
}
BOOLCVRworkView:
:
RenderStockScene()
{
returnTRUE;
}
voidCVRworkView:
:
OnTimer(UINTnIDEvent)//定¡§时º¡À器¡Â函¡¥数ºy
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
//CVRworkDoc*pDoc=(CVRworkDoc*)CView:
:
GetDocument();
//
//m_ft1kj=pDoc->m_ft1k;//增?
加¨®旋y转Áa角?
度¨¨
//m_ft2kj=pDoc->m_ft2k;
//m_ft3kj=pDoc->m_ft3k;
//m_ft4kj=pDoc->m_ft4k;
//m_ft5kj+=pDoc->m_ft5k;
//m_nLighting=pDoc->m_nLighting;
//if(0==pDoc->m_nPattern)
//m_nPattern=GL_LINE_LOOP;
//else
//m_nPattern=GL_TRIANGLES;
//
//InvalidateRect(NULL,FALSE);//视º¨®图ª?
重?
绘?
,ê?
调Ì¡Â用®?
了¢?
OnDraw()
CView:
:
OnTimer(nIDEvent);
}
//BOOLCVRworkView:
:
DrawScene()
//{
////在¨²屏¨¢幕?
上¦?
显?
出?
图ª?
形?
//if(m_hglrc)//设¦¨¨置?
当Ì¡À前¡ã绘?
图ª?
设¦¨¨备À?
为aOpenGL的Ì?
设¦¨¨备À?
情¨¦景¡ã对?
象¨®
//wglMakeCurrent(m_pDC->GetSafeHdc(),m_hglrc);
//else
//return0;
//
//glDrawBuffer(GL_BACK);//指?
定¡§在¨²后¨®台¬¡§缓o存ä?
中D绘?
制?
图ª?
形?
//glLoadIdentity();//初?
始º?
化¡¥变À?
换?
矩?
阵¨®
//
//glClearColor(0.0,0.0,0.0,0.0);
//glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//
//glPushMatrix();
//glShadeModel(GL_SMOOTH);
////设¦¨¨置?
光a照?
//DrawArm();
//doublefloorSize;
//drawFloor(2.5);
//glPopMatrix();
//glFlush();
//
//SwapBuffers(wglGetCurrentDC());//交?
换?
前¡ã后¨®缓o存ä?
//glDrawBuffer(GL_FRONT);//绘?
制?
前¡ã景¡ã
//}
voidCVRworkView:
:
CalcNormal(GLfloat*p0,GLfloat*p1,GLfloat*p2,GLfloat*normal)
{
inti;
GLfloatnorm;
GLfloatv0[3],v1[3];
for(i=0;i<3;i++){
v0[i]=p2[i]-p1[i];
v1[i]=p0[i]-p1[i];
}
normal[0]=v0[1]*v1[2]-v0[2]*v1[1];
normal[1]=v0[2]*v1[0]-v0[0]*v1[2];
normal[2]=v0[0]*v1[1]-v0[1]*v1[0];
//法¤¡§线?
的Ì?
正y规?
化¡¥,ê?
也°2就¨ª是º?
单Ì£¤位?
化¡¥
norm=(GLfloat)sqrt((double)(normal[0]*normal[0]
+normal[1]*
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- opengl6elbow源代码 vs opengl6elbow 源代码
![提示](https://static.bdocx.com/images/bang_tan.gif)