77范文网 - 专业文章范例文档资料分享平台

研究生计算机图形学课程室内场景OpenGL--实验报告(4)

来源:网络收集 时间:2019-04-09 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

}

#endif //_DEBUG

// Create OpenGL rendering context

int CRenderView::OnCreate(LPCREATESTRUCT lpCreateStruct) {

if (CView::OnCreate(lpCreateStruct) == -1) return -1;

HWND hWnd = GetSafeHwnd(); HDC hDC = ::GetDC(hWnd);

if(SetWindowPixelFormat(hDC)==FALSE) return 0;

if(CreateViewGLContext(hDC)==FALSE) return 0;

// Default mode

glPolygonMode(GL_FRONT,GL_FILL); glPolygonMode(GL_BACK,GL_FILL); glShadeModel(GL_FLAT);

// light must be disabled // while rendering the terrain

// because it has no normal definition InitGeometry();

glEnable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); return 0; }

BOOL CRenderView::SetWindowPixelFormat(HDC hDC) {

PIXELFORMATDESCRIPTOR pixelDesc;

pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR); pixelDesc.nVersion = 1;

16

pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_STEREO_DONTCARE;

pixelDesc.iPixelType = PFD_TYPE_RGBA; pixelDesc.cColorBits = 32; pixelDesc.cRedBits = 8; pixelDesc.cRedShift = 16; pixelDesc.cGreenBits = 8; pixelDesc.cGreenShift = 8; pixelDesc.cBlueBits = 8; pixelDesc.cBlueShift = 0; pixelDesc.cAlphaBits = 0; pixelDesc.cAlphaShift = 0; pixelDesc.cAccumBits = 64; pixelDesc.cAccumRedBits = 16; pixelDesc.cAccumGreenBits = 16; pixelDesc.cAccumBlueBits = 16; pixelDesc.cAccumAlphaBits = 0; pixelDesc.cDepthBits = 32; pixelDesc.cStencilBits = 8; pixelDesc.cAuxBuffers = 0;

pixelDesc.iLayerType = PFD_MAIN_PLANE; pixelDesc.bReserved = 0; pixelDesc.dwLayerMask = 0; pixelDesc.dwVisibleMask = 0; pixelDesc.dwDamageMask = 0;

m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc); if(m_GLPixelIndex == 0) // Choose default {

m_GLPixelIndex = 1;

if(DescribePixelFormat(hDC,m_GLPixelIndex,

sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0) return FALSE; }

if(!SetPixelFormat(hDC,m_GLPixelIndex,&pixelDesc)) return FALSE;

17

return TRUE; }

// Create an OpenGL rendering context

BOOL CRenderView::CreateViewGLContext(HDC hDC) {

m_hGLContext = wglCreateContext(hDC);

if(m_hGLContext==NULL) return FALSE;

if(wglMakeCurrent(hDC,m_hGLContext)==FALSE) return FALSE;

return TRUE; }

// Cleanup every OpenGL rendering context void CRenderView::OnDestroy() {

if(wglGetCurrentContext() != NULL) wglMakeCurrent(NULL,NULL);

if(m_hGLContext != NULL) {

wglDeleteContext(m_hGLContext); m_hGLContext = NULL; }

CView::OnDestroy(); }

void CRenderView::OnSize(UINT nType, int cx, int cy) {

CView::OnSize(nType, cx, cy);

// Set OpenGL perspective, viewport and mode CSize size(cx,cy); double aspect;

aspect = (cy == 0) ? (double)size.cx : (double)size.cx/(double)size.cy;

18

glViewport(0, 0, (GLsizei) cx, (GLsizei) cy); glMatrixMode(GL_PROJECTION); glLoadIdentity();

gluPerspective(60.0, (GLfloat) cx/(GLfloat) cy, 1.0f, 500.0f);

glMatrixMode(GL_MODELVIEW); glLoadIdentity();

gluLookAt (ex, ey, ez, cx, cy, cz, 0.0f, 1.0f, 0.0f); }

void CRenderView::OnLButtonDown(UINT nFlags, CPoint point) {

m_LeftButtonDown = TRUE; m_LeftDownPos = point;

CView::OnLButtonDown(nFlags, point); }

void CRenderView::OnLButtonUp(UINT nFlags, CPoint point) {

m_LeftButtonDown = FALSE;

CView::OnLButtonUp(nFlags, point); }

void CRenderView::OnMouseMove(UINT nFlags, CPoint point) {

switch(nFlags){

case(MK_LBUTTON): MoveEye(FORWARD,(GLfloat)(oldmy-point.y)/5.0f,1); break;

case(MK_RBUTTON):

MoveEye(TURNLEFT, (GLfloat)(oldmx-point.x), 1); break; }

oldmy = point.y; oldmx = point.x; Invalidate(FALSE);

CView::OnMouseMove(nFlags, point);

19

}

void CRenderView::OnPaint() {

// Device context for painting CPaintDC dc(this);

// Useful in singledoc templates HWND hWnd = GetSafeHwnd(); HDC hDC = ::GetDC(hWnd);

wglMakeCurrent(hDC,m_hGLContext);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glClearColor(m_ClearColorRed,m_ClearColorGreen,m_ClearColorBlue,1.0f); glPushMatrix();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); InitRenderWin(); Render();

// Double buffers SwapBuffers(hDC); }

// Function that moves the eye or turns the angle of sight. // Updates scene if update != 0.

void CRenderView::MoveEye(int type, GLfloat amount, int update) {

GLfloat a;

switch(type){ case FORWARD: a = sqrt((cx-ex)*(cx-ex)+(cz-ez)*(cz-ez)); ex = (amount*(cx-ex)+a*ex) / a; ez = (amount*(cz-ez)+a*ez) / a; cx = (amount*(cx-ex)+a*cx) / a; cz = (amount*(cz-ez)+a*cz) / a; break;

case TURNLEFT:

cx = (cx-ex)*(float)cos(amount/360.0f) + (cz-ez)*(float)sin(amount/360.0f)+ex; cz = (cz-ez)*(float)cos(amount/360.0f) - (cx-ex)*(float)sin(amount/360.0f)+ez;

20

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库研究生计算机图形学课程室内场景OpenGL--实验报告(4)在线全文阅读。

研究生计算机图形学课程室内场景OpenGL--实验报告(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/581201.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: