研究生计算机图形学课程室内场景OpenGL--实验报告.doc_第1页
研究生计算机图形学课程室内场景OpenGL--实验报告.doc_第2页
研究生计算机图形学课程室内场景OpenGL--实验报告.doc_第3页
研究生计算机图形学课程室内场景OpenGL--实验报告.doc_第4页
研究生计算机图形学课程室内场景OpenGL--实验报告.doc_第5页
已阅读5页,还剩28页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

高级计算机图形学实验报告姓 名: 学号: 班级: 【实验报告要求】实验名称:高级计算机图形学室内场景实验目的:掌握使用OpenGL生成真实感复杂对象的方法,进一步熟练掌握构造实体几何表示法、扫描表示法、八叉树法、BSP树法等建模方法。实验要求:要求利用OpenGL生成一个真实感的复杂对象及其周围场景,并显示观测点变化时的几何变换,要具备在一个纹理复杂的场景中漫游功能。要求使用到光线跟踪算法、纹理映射技术以及实时绘制技术。一、实验效果图图1:正面效果图图2:背面效果图图4:背面效果图图4:室内场景细节效果图图5:场景角度转换效果图二、源文件数据代码:共6个文件,其实现代码如下:1、DlgAbout.cpp#include StdAfx.h#include DlgAbout.hCAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)void CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)END_MESSAGE_MAP()2、FormCommandView.cpp#include stdafx.h#include Tool.h#include MainFrm.h#include FormCommandView.h#include ToolDoc.h#include RenderView.h/ Download by #ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CFormCommandViewIMPLEMENT_DYNCREATE(CFormCommandView, CFormView)CFormCommandView:CFormCommandView(): CFormView(CFormCommandView:IDD)/AFX_DATA_INIT(CFormCommandView)m_Smooth = FALSE;m_Antialias = FALSE;/AFX_DATA_INITCFormCommandView:CFormCommandView()void CFormCommandView:DoDataExchange(CDataExchange* pDX)CFormView:DoDataExchange(pDX);/AFX_DATA_MAP(CFormCommandView)DDX_Control(pDX, IDC_FRAME_COLOR_BACK, m_ControlBackColor);DDX_Check(pDX, IDC_CHECK_SMOOTH, m_Smooth);DDX_Check(pDX, IDC_CHECK_ANTIALIAS, m_Antialias);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CFormCommandView, CFormView)/AFX_MSG_MAP(CFormCommandView)ON_WM_PAINT()ON_WM_LBUTTONUP()ON_BN_CLICKED(IDC_RADIO_MODEL_1, OnRadioModel1)ON_BN_CLICKED(IDC_RADIO_MODEL_2, OnRadioModel2)ON_BN_CLICKED(IDC_CHECK_SMOOTH, OnCheckSmooth)ON_BN_CLICKED(IDC_CHECK_ANTIALIAS, OnCheckAntialias)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CFormCommandView diagnostics#ifdef _DEBUGvoid CFormCommandView:AssertValid() constCFormView:AssertValid();void CFormCommandView:Dump(CDumpContext& dc) constCFormView:Dump(dc);CToolDoc* CFormCommandView:GetDocument() / non-debug version is inlineASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CToolDoc);return (CToolDoc*)m_pDocument;#endif /_DEBUG/ OnPaintvoid CFormCommandView:OnPaint() / Device context for paintingCPaintDC dc(this); / Options are stored in ApplicationCToolApp *pApp = (CToolApp *)AfxGetApp();CRect rect;/ Color backm_ControlBackColor.GetWindowRect(&rect);ScreenToClient(&rect);CBrush BrushBack(pApp-m_OptionColorGlBack);dc.FillRect(&rect,&BrushBack);/ OnLButtonUpvoid CFormCommandView:OnLButtonUp(UINT nFlags, CPoint point) CRect rect;CToolApp *pApp = (CToolApp *)AfxGetApp();/ Option back colorm_ControlBackColor.GetWindowRect(&rect);ScreenToClient(&rect);if(rect.PtInRect(point)CColorDialog dlg(pApp-m_OptionColorGlBack);if(dlg.DoModal()=IDOK)pApp-m_OptionColorGlBack = dlg.GetColor();CRenderView *pView = (CRenderView *)GetRenderView();pView-m_ClearColorRed = (float)GetRValue(pApp-m_OptionColorGlBack) / 255.0f;pView-m_ClearColorGreen = (float)GetGValue(pApp-m_OptionColorGlBack) / 255.0f;pView-m_ClearColorBlue = (float)GetBValue(pApp-m_OptionColorGlBack) / 255.0f;this-InvalidateRect(&rect,FALSE);pView-InvalidateRect(NULL,FALSE);CFormView:OnLButtonUp(nFlags, point);/ GetRenderViewCView *CFormCommandView:GetRenderView() CToolApp *pApp = (CToolApp *)AfxGetApp();CMainFrame *pFrame = (CMainFrame *)pApp-m_pMainWnd;CView *pView = (CView *)pFrame-m_wndSplitter.GetPane(0,1);return pView;/ Modelvoid CFormCommandView:OnRadioModel1() glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);this-GetRenderView()-InvalidateRect(NULL,FALSE); void CFormCommandView:OnRadioModel2() glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);this-GetRenderView()-InvalidateRect(NULL,FALSE); / OnCheckSmoothvoid CFormCommandView:OnCheckSmooth() m_Smooth = !m_Smooth;if(m_Smooth)glShadeModel(GL_SMOOTH);elseglShadeModel(GL_FLAT);this-GetRenderView()-InvalidateRect(NULL,FALSE); / OnCheckAntialias/ Toggle antialiased linesvoid CFormCommandView:OnCheckAntialias() m_Antialias = !m_Antialias;if(m_Antialias)glEnable(GL_LINE_SMOOTH);glEnable(GL_BLEND);glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);glLineWidth(1.5f);elseglDisable(GL_LINE_SMOOTH);glDisable(GL_BLEND);glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);glLineWidth(1.0f);GetRenderView()-InvalidateRect(NULL,FALSE); 3、MainFrm.cpp#include stdafx.h#include Tool.h/ Download by #include MainFrm.h#include FormCommandView.h#include RenderView.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CMainFrameIMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)/AFX_MSG_MAP(CMainFrame)ON_WM_CREATE()ON_WM_PAINT()/AFX_MSG_MAPEND_MESSAGE_MAP()static UINT indicators =ID_SEPARATOR, / status line indicatorID_INDICATOR_CAPS,ID_INDICATOR_NUM,ID_INDICATOR_SCRL,;/ CMainFrame construction/destructionCMainFrame:CMainFrame()CMainFrame:CMainFrame()int CMainFrame:OnCreate(LPCREATESTRUCT lpCreateStruct)if (CFrameWnd:OnCreate(lpCreateStruct) = -1)return -1;if (!m_wndToolBar.Create(this) |!m_wndToolBar.LoadToolBar(IDR_MAINFRAME)TRACE0(Failed to create toolbarn);return -1; / fail to createif (!m_wndStatusBar.Create(this) |!m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT)TRACE0(Failed to create status barn);return -1; / fail to createm_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);return 0;BOOL CMainFrame:PreCreateWindow(CREATESTRUCT& cs)cs.cx = 600; cs.cy = 500;return CFrameWnd:PreCreateWindow(cs);/ CMainFrame diagnostics#ifdef _DEBUGvoid CMainFrame:AssertValid() constCFrameWnd:AssertValid();void CMainFrame:Dump(CDumpContext& dc) constCFrameWnd:Dump(dc);#endif /_DEBUG/ CMainFrame message handlersvoid CMainFrame:OnPaint() CPaintDC dc(this); / device context for paintingBOOL CMainFrame:OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) if (!m_wndSplitter.CreateStatic(this, 1, 2,WS_CHILD | WS_VISIBLE)TRACE(Failed to CreateStaticSplittern);return FALSE;/ First splitter paneif (!m_wndSplitter.CreateView(0, 0,RUNTIME_CLASS(CRenderView), CSize(600,500), pContext)TRACE(Failed to create command view panen);return FALSE;if (!m_wndSplitter.CreateView(0, 1,RUNTIME_CLASS(CFormCommandView), CSize(0,0), pContext)TRACE(Failed to create command view panen);return FALSE;/ Second splitter panereturn TRUE;4、RenderView.cpp#include stdafx.h#include Tool.h#include #include #include #include ToolDoc.h#include RenderView.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endifextern voidRender(void);/ This is the holding space for the landscape WinWidth, WinHeigth;unsigned short int comp = 32; / Scale modifier.unsigned short int temp, texture_mapping = FALSE, land_fogging = TRUE, flat_shading = TRUE;floatangle, Near, ex, ey, ez, cx, cy, cz;/ Initial eye position and vector of sight.static GLfloat speed = 0;/ The following code for mouse routines was contributed./ These are used for the motion function.#define FORWARD 1#define UP 2#define TURNLEFT 3#define LOOKUP 5/ Mouse position and oldmx = 0, oldmy = 0, mb;/ CRenderViewIMPLEMENT_DYNCREATE(CRenderView, CView)BEGIN_MESSAGE_MAP(CRenderView, CView)/AFX_MSG_MAP(CRenderView)ON_WM_DESTROY()ON_WM_SIZE()ON_WM_LBUTTONDOWN()ON_WM_LBUTTONUP()ON_WM_MOUSEMOVE()ON_WM_PAINT()ON_WM_CREATE()/AFX_MSG_MAP/ Standard printing commandsON_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()/ CRenderView construction/destructionCRenderView:CRenderView() / OpenGL m_hGLContext = NULL; m_GLPixelIndex = 0; / Mouse m_LeftButtonDown = FALSE; m_RightButtonDown = FALSE; m_CursorRotation = AfxGetApp()-LoadCursor(IDC_CURSOR_ROTATION); / Colors CToolApp *pApp = (CToolApp *)AfxGetApp(); m_ClearColorRed = GetRValue(pApp-m_OptionColorGlBack); m_ClearColorGreen = GetGValue(pApp-m_OptionColorGlBack); m_ClearColorBlue = GetBValue(pApp-m_OptionColorGlBack); ReadData(); WinWidth=1000; WinHeigth=800; LoadAllTexture(); InitLookAt();/=/ InitGeometry/=void CRenderView:InitGeometry(void) GLfloat fogColor4 = 0.75, 0.75, 1.0, 1.0; speed = 0; srand(224); srand(unsigned)time(NULL); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glEnable(GL_DEPTH_TEST); glShadeModel(GL_FLAT); glFogi(GL_FOG_MODE, GL_LINEAR); glFogfv(GL_FOG_COLOR, fogColor); glFogf(GL_FOG_DENSITY, 0.8f); glFogf(GL_FOG_START, 400.0f); glFogf(GL_FOG_END, 500.0f); glClearColor(0.75f, 0.75f, 1.0f, 1.0f);CRenderView:CRenderView()FreeAllTexture();freelist();BOOL CRenderView:PreCreateWindow(CREATESTRUCT& cs) return CView:PreCreateWindow(cs);/ CRenderView drawingvoid CRenderView:OnDraw(CDC* pDC)BOOL CRenderView:OnPreparePrinting(CPrintInfo* pInfo) return DoPreparePrinting(pInfo);void CRenderView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)void CRenderView:OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ CRenderView diagnostics#ifdef _DEBUGvoid CRenderView:AssertValid() const CView:AssertValid();void CRenderView:Dump(CDumpContext& dc) const CView:Dump(dc);CToolDoc* CRenderView:GetDocument() / non-debug version is inline if (m_pDocument) ASSERT(m_pDocument-IsKindOf(RUNTIME_CLASS(CToolDoc); return (CToolDoc*)m_pDocument; else return NULL;#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; 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; return TRUE;/ Create an OpenGL rendering contextBOOL 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 contextvoid 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; 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);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; break; case UP: ey += amount; break; case LOOKUP: cy += amount; break; if (update) glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(ex, ey, ez, cx, cy, cz, 0.0f,1.0f,0.0f); TEXTURE_2D *TextureList;OBJECT *ObjectList;/* ObjectList0:isolated surfaces*/INT4S ObjectNum; char gEnergyFile30;char sLookAtFN100;char ImageName30; void CRenderView:ReadData() int i,j,l; FILE *fp; char stemp100; POINT3D *plist; INT4U nAllVertexNum; INT4U *pchlist; strcpy(gEnergyFile,room.ed); fp = fopen( gEnergyFile, r ); if ( fp = NULL ) printf( n Can not open energy data file:%sn, gEnergyFile); exit(0); fseek( fp, 0, SEEK_SET); /* read texture list */ fscanf( fp, %s, stemp); while( strcmp( stemp,texnum ) != 0) fscanf( fp, %s, stemp); fscanf( fp, %d, &texnum ); TextureList = (TEXTURE_2D *)malloc( sizeof(TEXTURE_2D)*(texnum+1); for(i=1; ifname, stemp );if ( strcmp( stemp,REPEAT_TEXTURE ) = 0) TextureListi-type = 1;else if ( strcmp( stemp,CLAMP_TEXTURE ) = 0) TextureListi-type = 0; /* Read object list */ fscanf( fp, %s, stemp); while( strcmp( stemp,ObjectNum ) != 0) fscanf(fp,%s,stemp); fscanf( fp, %ld, &ObjectNum); ObjectList = (OBJECT *)malloc( sizeof(OBJECT ) * ObjectNum); for(i = 0; i ObjectNum; i + ) fscanf( fp, %s, stemp); while( strcmp( stemp,SurfaceNum ) != 0) fscanf(fp,%s,stemp); fscanf( fp, %ld, &(Obj

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论