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

2019-04-09 17:43

}

#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


研究生计算机图形学课程室内场景OpenGL--实验报告(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:六爻卦: 用神、元神、忌神、仇神、月建、日辰名词解释与应用

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: