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

合集下载

计算机图形学上机实验报告

计算机图形学上机实验报告

计算机图形学实验报告姓名:学号:班级:目录实验一OpenGL程序结构练习 (3)实验二基本图形生成 (6)实验三交互式控制 (9)实验四图形基本变换 (12)实验五三维图形生成及显示 (15)实验六三维图形生成及显示 (19)实验一OpenGL程序结构练习【实验目的】1.熟悉C语言环境下OpenGL的使用方法;2.了解OpenGL程序的基本结构。

【实验原理】绝大多数OpenGL程序具有类似的结构,包含下述函数main():定义回调函数,打开一个或多个具有指定属性的窗口,进入事件循环(最后一条可执行语句)init():设置状态变量、视图、属性、回调、显示函数、输入和窗口函数#include <GL/glut.h> // glut.h includes gl.h and glu.hvoid display(){ ……}void init(){ ……}int main( intargc, char **argv){ ……}【实验内容】1.了解程序中各个结构的功能;2.用OpenGL生成三角形。

【实验步骤及结果】1.导入OpenGL的glut32.lib和glut.h文件:将.lib文件存放到C 语言程序文件夹的Library下,.h文件放到Include下;导入应用程序扩展文件glut32.dll,存放到system文件夹下。

2.打开VC 6.0,新建工程,并命名为text1,如图1.图 13.在工程text1下新建源文件,并命名为text1.cpp。

4.编写代码并编译链接,如图2所示。

图 25.运行,结果如图3所示。

图 3实验二基本图形生成【实验目的】1.熟悉OpenGL的程序结构,并了解各部分的功能。

2.学会应用OpenGL语言绘制出点,线,多边形。

【实验原理】1.GLUT函数glutInit使得应用程序可以获取命令行参数并初始化系统。

glutInitDisplayMode设置窗口的属性、RGB颜色、单缓冲区、属性按照逻辑或组合在一起。

计算机图形学实验报告模板

计算机图形学实验报告模板

巢湖学院计算机图形学实验报告(模板>本课程实验包括:以下为实验二和实验三模板实验一:基本图元绘制一、实验目的了解OpenGL图形软件包绘制图形的基本过程及其程序框架,并在已有的程序框架中添加代码实现直线和圆的生成算法,演示直线和圆的生成过程,从而加深对直线和圆等基本图形生成算法的理解。

b5E2RGbCAP二、实验内容实验操作和步骤:本次实验主要的目的是为了掌握基本画线和画圆算法,对于书上给出的代码,要求通过本次实验来具体的实现。

由于实验已经给出大体的框架,所以只需要按照书上的算法思想来设计具体实现代码,对于直线DDA算法,中点Bresenham算法及其改进算法,以及Bresenham画圆算法都有进一步的体会。

DDA算法是对每一步都要进行增量处理,然后取整,绘制,而Bresenham通过判断误差函数和求取递推公式来实现。

特别是对于整数的选择取舍,以及代码的流程和循环的控制有一个深入的了解。

同时也熟练运用OpenGL基本的绘图函数。

p1EanqFDPw三、体会通过本次实验,我进一步加深了对于基本画图算法的理解。

特别是对于DDA,Bresenham和画圆算法。

其中,DDA算法由于每一步都要处理浮点数的四舍五入,所以在绘图时要进行取整,效率较低,但是代码直观好懂,符合原理。

而对于Bresenham及其改进算法,都是在理论推导的基础上来实现的,然后经过整数化,形成了一个高效率的画图算法,所以需要适当的理解,特别是对于取整操作判断比较巧妙,实现了避免多次判断计算浮点数的目的,所以比较高效。

而绘制圆形的时候,用到的基本思想还是和Bresenham画图算法一样,只不过需要注意的是八分法画圆,这样只需要绘制其中的八分之一就可以利用对称的关系来绘制出整个图形。

而对于是否走下一步,或者是停留,判断的依据还是误差函数,和前面的思想是类似。

另外,通过实验训练了自己的编程能力,同时熟悉了OpenGL绘图的函数和流程,也进一步巩固了相关的知识。

opengl实验报告

opengl实验报告

opengl实验报告OpenGL实验报告引言:OpenGL(Open Graphics Library)是一种跨平台的图形编程接口,被广泛应用于计算机图形学、游戏开发和科学可视化等领域。

本实验报告将介绍我对OpenGL的实验研究和学习成果。

一、实验目的本次实验的主要目的是掌握OpenGL的基本概念和使用方法,了解图形渲染的原理和过程,以及学习如何在OpenGL中创建和操作图形对象。

二、实验环境本次实验使用的是OpenGL的最新版本,并在Windows操作系统下进行开发。

使用的开发工具是Visual Studio和OpenGL的开发库。

三、实验过程1. 熟悉OpenGL的基本概念在开始实验之前,我先学习了OpenGL的基本概念,包括OpenGL的坐标系统、图形渲染管线、着色器等。

了解这些概念对于后续的实验非常重要。

2. 创建窗口和上下文在OpenGL中,我们需要先创建一个窗口和一个OpenGL上下文,以便进行图形渲染。

通过调用相关的OpenGL函数,我成功创建了一个窗口,并初始化了OpenGL的上下文。

3. 绘制基本图形接下来,我开始尝试绘制一些基本的图形,比如点、线和三角形。

通过设置顶点坐标和颜色,我成功绘制出了这些基本图形,并在窗口中显示出来。

4. 添加纹理为了使图形更加逼真和丰富,我学习了如何在OpenGL中添加纹理。

通过加载图片并设置纹理坐标,我成功将纹理贴在了绘制的图形上,使其具有了更加真实的效果。

5. 光照和阴影效果为了增加图形的立体感和真实感,我学习了如何在OpenGL中添加光照和阴影效果。

通过设置光源的位置和属性,以及材质的属性,我成功实现了光照和阴影的效果,使图形看起来更加逼真。

6. 动画效果为了使图形具有动态效果,我学习了如何在OpenGL中实现简单的动画效果。

通过每帧更新顶点的位置和纹理坐标,我成功实现了图形的旋转和平移动画,使其具有了动态的效果。

四、实验结果和分析通过以上的实验过程,我成功掌握了OpenGL的基本概念和使用方法,并实现了一些基本的图形渲染效果。

计算机图形学实验报告三

计算机图形学实验报告三

《计算机图形学》实验报告//glEnable(GL_SCISSOR_TEST);//glScissor(0.0f,0.0f,500,300);glutWireTeapot(0.4);glFlush();}//窗口调整子程序void myReshape(int w, int h){glViewport(500, -300, (GLsizei)w, (GLsizei)h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if (w <= h)glOrtho(-1, 1, -(float)h / w, (float)h / w, -1, 1);elseglOrtho(-(float)w / h, (float)w / h, -1, 1, -1, 0.5);}2,使用opengl函数写一个图形程序,要求分别使用三个光源从一个茶壶的前右上方(偏红色),正左侧(偏绿色)和前左下方(偏蓝色)对于其进行照射,完成程序并观察效果。

}//绘图子程序void display(void){glColor3f(1.0, 1.0, 0.0);glClear(GL_COLOR_BUFFER_BIT);//glMatrixMode(GL_MODELVIEW);//glLoadIdentity();//设置光源的属性1GLfloat LightAmbient1[] = { 1.0f, 0.0f, 0.0f, 1.0f }; //环境光参数 ( 新增 )GLfloat LightDiffuse1[] = { 1.0f, 0.0f, 0.0f, 1.0f }; // 漫射光参数 ( 新增 )GLfloat Lightspecular1[] = { 1.0f, 0.0f, 0.0f, 1.0f }; // 镜面反射GLfloat LightPosition1[] = { 500.0f, 500.0f, 500.0f, 1.0f }; // 光源位置 ( 新增 ) glLightfv(GL_LIGHT0, GL_POSITION, LightPosition1);glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient1); // 设置环境光glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse1); // 设置漫射光glLightfv(GL_LIGHT0, GL_SPECULAR, Lightspecular1);//设置镜面反射光//设置光源的属性2 GLfloat LightAmbient2[] = { 0.0f, 1.0f, 0.0f, 1.0f }; //环境光参数 ( 新增 )代码float dIst = 0.0f;float dIst1 = 0.0f;void RenderScene(void){glClearColor(1.0f, 1.0f, 1.0f, 1.0f);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除颜和深度缓冲区。

计算机图形学实验报告

计算机图形学实验报告

计算机图形学实验报告
在计算机图形学课程中,实验是不可或缺的一部分。

通过实验,我们可以更好地理解课程中所学的知识,并且在实践中掌握这些
知识。

在本次实验中,我学习了如何使用OpenGL绘制三维图形,并了解了一些基本的图形变换和视图变换。

首先,我们需要通过OpenGL的基本命令来绘制基本图形,例
如线段、矩形、圆等。

这些基本的绘制命令需要首先设置OpenGL 的状态,例如绘制颜色、线段宽度等,才能正确地绘制出所需的
图形。

然后,在实验中我们学习了图形的变换。

变换是指通过一定的
规则将图形的形状、位置、大小等进行改变。

我们可以通过平移、旋转、缩放等变换来改变图形。

变换需要按照一定的顺序进行,
例如先进行旋转再进行平移等。

在OpenGL中,我们可以通过设
置变换矩阵来完成图形的变换。

变换矩阵包含了平移、旋转、缩
放等信息,通过矩阵乘法可以完成图形的复合变换。

最后,视图变换是指将三维场景中的图形投影到二维平面上,
成为我们所见到的图形。

在实验中,我们学习了透视投影和正交
投影两种方式。

透视投影是指将场景中的图形按照视点不同而产
生不同的远近缩放,使得图形呈现出三维感。

而正交投影则是简单地将场景中的图形按照平行投影的方式呈现在屏幕上。

在OpenGL中,我们可以通过设置视图矩阵和投影矩阵来完成视图变换。

通过本次实验,我对于计算机图形学有了更深入的了解,并掌握了一些基本的图形绘制和变换知识。

在今后的学习中,我将继续学习更高级的图形绘制技术,并应用于实际的项目中。

计算机形学实训课程学习总结利用OpenGL进行三维渲染与动设计

计算机形学实训课程学习总结利用OpenGL进行三维渲染与动设计

计算机形学实训课程学习总结利用OpenGL进行三维渲染与动设计计算机形学实训课程学习总结——利用OpenGL进行三维渲染与动设计在计算机形学实训课程中,我们学习了如何利用OpenGL进行三维渲染与动设计。

通过这门课程的学习,我对计算机图形学和OpenGL有了更深入的了解,并且掌握了一些基本的三维渲染和动画设计技术。

在本文中,我将总结我在这门课程中的学习和体会。

首先,在课程中我们学习了计算机图形学的基础知识。

我们了解了计算机图形学的基本概念、渲染管线的工作原理以及一些基本的数学知识。

这些基础知识为我们后续的学习和实践打下了坚实的基础。

接着,我们学习了OpenGL的基本用法。

OpenGL是一套跨平台、高性能的图形库,广泛应用于计算机图形学和游戏开发领域。

通过学习OpenGL,我们能够使用它提供的接口函数绘制基本的图形和渲染效果。

我们学习了OpenGL的初始化过程、绘制基本图形的方法以及纹理贴图等高级特性的使用方法。

通过编写代码实践,我们能够将所学的知识应用到实际的图形渲染中,达到自己预期的效果。

在实践环节中,我们进行了三维渲染和动画设计的项目实践。

我们学习了如何使用OpenGL绘制三维模型并进行渲染。

通过导入模型文件、设置材质和光照等参数,我们能够让模型呈现逼真的效果。

同时,我们也学习了如何添加动画效果,让模型在场景中运动起来。

通过控制模型的变换矩阵和设置关键帧,我们能够制作出流畅的动画效果。

除了基本的三维渲染和动画设计,我们还学习了一些高级的图形效果和技术。

例如,我们学习了阴影的渲染方法,包括平面阴影和体积阴影的实现。

我们还学习了粒子系统的设计和实现,通过控制粒子的参数和行为,可以制作出各种特效,如烟雾、火焰等。

在整个实践过程中,我不仅学到了很多有关计算机图形学和OpenGL的知识,还提高了编程能力和问题解决能力。

在实现特定效果的过程中,我们经常会遇到各种各样的问题,如渲染错误、性能问题等。

通过查阅资料和与同学讨论,我学会了如何解决这些问题,并且能够修复错误并改进我的代码。

计算机图形学实验报告

计算机图形学实验报告

计算机图形学实验报告计算机图形学实验 (一) – OpenGL 基础用OPENGL画直线\圆\曲线等二维图1.1 综述这次试验的目的主要是使大家初步熟悉OpenGL 这一图形系统的用法,编程平台是Visual C++,它对OpenGL 提供了完备的支持。

OpenGL 提供了一系列的辅助函数,用于简化Windows 操作系统的窗口操作,使我们能把注意力集中到图形编程上,这次试验的程序就采用这些辅助函数。

本次实验不涉及面向对象编程,不涉及MFC。

1.2 在 VC 中新建项目1.2.1 新建一个项目选择菜单File 中的New 选项,弹出一个分页的对话框,选中页Projects 中的Win32Console Application 项,然后填入你自己的Project name,如Test,回车即可。

VC 为你创建一个工作区(WorkSpace ),你的项目Test 就放在这个工作区里。

1.2.2 为项目添加文件为了使用OpenGL,我们需要在项目中加入三个相关的Lib 文件:glu32.lib、glaux.lib、opengl32.lib,这三个文件位于c:\programfiles\microsoft visualstudio\vc98\lib 目录中。

选中菜单Project->Add To Project->Files 项(或用鼠标右键),把这三个文件加入项目,在FileView 中会有显示。

这三个文件请务必加入,否则编译时会出错。

或者将这三个文件名添加到Project->Setting->Link->Object/library Modules 即可。

点击工具条中New Text File 按钮,新建一个文本文件,存盘为Test.c 作为你的源程序文件,再把它加入到项目中,然后就可以开始编程了。

1.3 一个 OpenGL 的例子及说明1.3.1 源程序请将下面的程序写入源文件Test.c,这个程序很简单,只是在屏幕上画两根线。

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

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

《高级计算机图形学》实验报告姓名:学号:班级:【实验报告要求】实验名称:高级计算机图形学室内场景实验目的:掌握使用OpenGL生成真实感复杂对象的方法,进一步熟练掌握构造实体几何表示法、扫描表示法、八叉树法、BSP树法等建模方法。

实验要求:要求利用OpenGL生成一个真实感的复杂对象及其周围场景,并显示观测点变化时的几何变换,要具备在一个纹理复杂的场景中漫游功能。

要求使用到光线跟踪算法、纹理映射技术以及实时绘制技术。

一、实验效果图图1:正面效果图图2:背面效果图图4:背面效果图图4:室内场景细节效果图图5:场景角度转换效果图二、源文件数据代码:共6个文件,其实现代码如下:1、DlgAbout.cpp#include "StdAfx.h"#include "DlgAbout.h"CAboutDlg::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_INIT}CFormCommandView::~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_MAP}BEGIN_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() const{CFormView::AssertValid();}void CFormCommandView::Dump(CDumpContext& dc) const{CFormView::Dump(dc);}CToolDoc* CFormCommandView::GetDocument() // non-debug version is inline {ASSERT(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);}else{glDisable(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 indicator ID_INDICATOR_CAPS,ID_INDICATOR_NUM,ID_INDICATOR_SCRL,};// CMainFrame construction/destruction CMainFrame::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 toolbar\n");return -1; // fail to create}if (!m_wndStatusBar.Create(this) ||!m_wndStatusBar.SetIndicators(indicators,sizeof(indicators)/sizeof(UINT))){TRACE0("Failed to create status bar\n");return -1; // fail to create}m_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() const{CFrameWnd::AssertValid();}void CMainFrame::Dump(CDumpContext& dc) const{CFrameWnd::Dump(dc);}#endif //_DEBUG// CMainFrame message handlersvoid CMainFrame::OnPaint(){CPaintDC dc(this); // device context for painting}BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) {if (!m_wndSplitter.CreateStatic(this, 1, 2,WS_CHILD | WS_VISIBLE)){TRACE("Failed to CreateStaticSplitter\n");return FALSE;}// First splitter paneif (!m_wndSplitter.CreateView(0, 0,RUNTIME_CLASS(CRenderView), CSize(600,500), pContext)){TRACE("Failed to create command view pane\n");return FALSE;}if (!m_wndSplitter.CreateView(0, 1,RUNTIME_CLASS(CFormCommandView), CSize(0,0), pContext)){TRACE("Failed to create command view pane\n");return FALSE;}// Second splitter panereturn TRUE;}4、RenderView.cpp#include "stdafx.h"#include "Tool.h"#include <string.h>#include <time.h>#include <math.h>#include "ToolDoc.h"#include "RenderView.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifextern void Render(void);// This is the holding space for the landscape colours.int WinWidth, WinHeigth;unsigned short int comp = 32; // Scale modifier.unsigned short int temp, texture_mapping = FALSE,land_fogging = TRUE, flat_shading = TRUE; float angle, 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 button.int 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(){// OpenGLm_hGLContext = NULL;m_GLPixelIndex = 0;// Mousem_LeftButtonDown = FALSE;m_RightButtonDown = FALSE;m_CursorRotation = AfxGetApp()->LoadCursor(IDC_CURSOR_ROTATION);// ColorsCToolApp *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 fogColor[4] = {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 contextint 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 modeglPolygonMode(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 definitionInitGeometry();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;olorBits = 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 modeCSize 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 paintingCPaintDC dc(this);// Useful in singledoc templatesHWND 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 buffersSwapBuffers(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; /* ObjectList[0]:isolated surfaces*/INT4S ObjectNum;char gEnergyFile[30];char sLookAtFN[100];char ImageName[30];void CRenderView::ReadData(){int i,j,l;FILE *fp;char stemp[100];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:%s\n", 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; i<=texnum; i++){TextureList[i] = (TEXTURE_2D *)malloc( sizeof(TEXTURE_2D));fscanf( fp, "%s%s", TextureList[i]->fname, stemp );if ( strcmp( stemp,"REPEAT_TEXTURE" ) == 0)TextureList[i]->type = 1;else if ( strcmp( stemp,"CLAMP_TEXTURE" ) == 0)TextureList[i]->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 ++ ){f scanf( fp, "%s", stemp);w hile( strcmp( stemp,"SurfaceNum" ) != 0) fscanf(fp,"%s",stemp);f scanf( fp, "%ld", &(ObjectList[i].SurfNum) );ObjectList[i].surflist = (SURFACE *)malloc( sizeof(SURFACE) * ObjectList[i].SurfNum);for(j = 0; j < ObjectList[i].SurfNum; j ++ ){/****** Read surface infor ******/fscanf( fp, "%s", stemp);while( strcmp( stemp,"TextureId" ) != 0) fscanf(fp,"%s",stemp);fscanf( fp, "%d", &(ObjectList[i].surflist[j].texId) );fscanf( fp, "%s", stemp);while( strcmp( stemp,"pointnum" ) != 0) fscanf(fp,"%s",stemp);fscanf( fp, "%d", &(ObjectList[i].surflist[j].pointn) );fscanf( fp, "%s", stemp);while( strcmp( stemp,"triangle" ) != 0) fscanf(fp,"%s",stemp);fscanf( fp, "%d", &(ObjectList[i].surflist[j].triangle) );fscanf( fp, "%s", stemp);while( strcmp( stemp,"quadrangle" ) != 0) fscanf(fp,"%s",stemp);fscanf( fp, "%d", &(ObjectList[i].surflist[j].quadric) );/****** Read point list ******/ObjectList[i].surflist[j].pointlist = (POINT3D*)malloc(sizeof(POINT3D) *ObjectList[i].surflist[j].pointn);plist = ObjectList[i].surflist[j].pointlist;for( l = 0; l < ObjectList[i].surflist[j].pointn ; l ++ )fscanf( fp, "%f%f%f%f%f%f%f%f",&(plist[l].r), &(plist[l].g), &(plist[l].b),&(plist[l].u), &(plist[l].v),&(plist[l].x), &(plist[l].y), &(plist[l].z) );/****** Read patchlist ******/nAllVertexNum = ObjectList[i].surflist[j].triangle * 3 +ObjectList[i].surflist[j].quadric *4 ;ObjectList[i].surflist[j].patchlist = (INT4U *)malloc( sizeof(INT4U) * nAllVertexNum);pchlist = ObjectList[i].surflist[j].patchlist;for( l = 0; l < nAllVertexNum; l ++ )fscanf( fp, "%ld", &(pchlist[l]) );}}fclose(fp);}void CRenderView::InitLookAt(){FILE *fp;strcpy(sLookAtFN,"room.lk");fp = fopen(sLookAtFN, "rb");if (fp == NULL){ex = ey = ez =1.0f;cx = cy = cz =0.0f;Near = 0.1f;angle = 30.0f;}else fscanf(fp, "%f%f%f%f%f%f%f%f", &angle, &Near, &ex, &ey, &ez, &cx, &cy, &cz);fclose(fp);}void C RenderView::InitRenderWin(){glShadeModel ( GL_SMOOTH );glDepthFunc ( GL_LESS );glEnable ( GL_DEPTH_TEST );glMatrixMode ( GL_PROJECTION );glLoadIdentity();glMatrixMode ( GL_MODELVIEW );glLoadIdentity();gluPerspective ( angle, (float)WinWidth/(float)WinHeigth, Near, 1000000000.0); gluLookAt( ex, ey, ez, cx, cy, cz, 0.0, 1.0, 0.0);}void C RenderView::Render(void){int i, j, k, l, m, TexIndex;POINT3D *plist;INT4U *pchlist;glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_ACCUM_BUFFER_BIT);for(i = 0; i < ObjectNum; i ++ )for(j = 0; j < ObjectList[i].SurfNum; j ++ ){TexIndex = ObjectList[i].surflist[j].texId;if( TexIndex > 0 )InitTex( TexIndex );plist = ObjectList[i].surflist[j].pointlist;pchlist = ObjectList[i].surflist[j].patchlist;l = 0;for ( k = 0; k < ObjectList[i].surflist[j].triangle; k ++){glBegin( GL_TRIANGLES );for( m = 0; m < 3; m ++ ){glColor3f ( plist[pchlist[l]].r,plist[pchlist[l]].g,plist[pchlist[l]].b );glTexCoord2f( plist[pchlist[l]].u,plist[pchlist[l]].v );glVertex3f( plist[pchlist[l]].x,plist[pchlist[l]].y,plist[pchlist[l]].z );l ++;}/* m */glEnd();}/* k */for ( k = 0; k < ObjectList[i].surflist[j].quadric; k ++){glBegin( GL_QUADS );for( m = 0; m < 4; m ++ ){glColor3f ( plist[pchlist[l]].r,plist[pchlist[l]].g,plist[pchlist[l]].b );glTexCoord2f( plist[pchlist[l]].u,plist[pchlist[l]].v );glVertex3f( plist[pchlist[l]].x,plist[pchlist[l]].y,plist[pchlist[l]].z );l ++;}/* m */glEnd();}/* k */glFlush();CloseTex();}}void CRenderView::freelist(){int i, j;for( i=0; i<ObjectNum; i++){for( j=0; j<ObjectList[i].SurfNum; j++){free(ObjectList[i].surflist[j].pointlist);free(ObjectList[i].surflist[j].patchlist);}free( ObjectList[i].surflist );}free(ObjectList);for(i=1; i<=texnum; i++)free(TextureList[i]);free(TextureList);}extern TEXTURE_2D **TextureList;/********************************//* function : OpenTexImage *//********************************/unsigned char *CRenderView::OpenTexImage( INT2U TexIndex, INT2U *rslx, INT2U *rsly ){unsigned char *image;FILE *fp;INT2U srcx, srcy;INT4U i, j;char ImageName[30];unsigned char *SImageData;int rc;int width, height;strcpy( ImageName, TextureList[TexIndex]->fname);/* load a image */fp = fopen(ImageName,"rb");if(!fp) return 0;fseek(fp,18L,0);rc=fread(&width,sizeof(long),1,fp);rc=fread(&height,sizeof(long),1,fp);*rslx=srcx=width; *rsly=srcy=height;fseek(fp,54L,0);image = (unsigned char *)malloc(width*height*3);rc=fread(image,width*height*3,1,fp);fclose(fp);SImageData = (unsigned char *)malloc(srcx*srcy*3);for(i=0; i<srcx; i++) {for(j=0; j<srcy; j++) {(unsigned char)*(SImageData+i*srcx*3+j*3+0) = (unsignedchar)*(image+i*srcx*3+j*3+2);(unsigned char)*(SImageData+i*srcx*3+j*3+1) = (unsigned char)*(image+i*srcx*3+j*3+1);(unsigned char)*(SImageData+i*srcx*3+j*3+2) = (unsigned char)*(image+i*srcx*3+j*3+0);}}free(image);printf("%s : %ld=%ld\n", ImageName, srcx*srcy*3,i*j*3);return( SImageData );}/********************************//* function : InitTex *//********************************/void C RenderView::InitTex( int TexIndex ){INT2U TextType;unsigned char *ImageData;static int OldIndex = -1;if(TexIndex<=0) return;if(TexIndex == OldIndex){glEnable(GL_TEXTURE_2D);return;}ImageData = ImageDatas[TexIndex-1];TextType = TextureList[TexIndex]->type;glPixelStorei(GL_UNPACK_ALIGNMENT, 1);glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);if( TextType == CLAMP_TEXTURE ){glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);}else{glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);}glTexImage2D( GL_TEXTURE_2D, 0, 3, rslxs[TexIndex-1], rslys[TexIndex-1], 0,GL_RGB, GL_UNSIGNED_BYTE, ImageData );glEnable(GL_TEXTURE_2D);OldIndex = TexIndex;}/********************************//* function : CloseTex *//********************************/void C RenderView::CloseTex(){glDisable(GL_TEXTURE_2D);}void CRenderView::LoadAllTexture(){int i;for (i=0; i <texnum ; i++)ImageDatas[i] = OpenTexImage( i+1, &rslxs[i], &rslys[i] );}void CRenderView::FreeAllTexture(){int i;for (i=0; i <texnum ; i++)free(ImageDatas[i]);}5、StdAfx.cpp#include "stdafx.h"6、Tool.cpp#include "stdafx.h"#include "Tool.h"#include "DlgAbout.h"#include "MainFrm.h"#include "ToolDoc.h"#include "RenderView.h"// Download by #ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif// CToolAppCToolDoc *MyDocument;BEGIN_MESSAGE_MAP(CToolApp, CWinApp)//{{AFX_MSG_MAP(CToolApp)ON_COMMAND(ID_APP_ABOUT, OnAppAbout)//}}AFX_MSG_MAP// Standard file based document commands// Standard print setup commandON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup) END_MESSAGE_MAP()// CToolApp constructionCToolApp::CToolApp(){// Optionsm_OptionColorGlBack = RGB(0,0,255);}// The one and only CToolApp objectCToolApp theApp;// CToolApp initializationBOOL CToolApp::InitInstance(){AfxEnableControlContainer();// Standard initialization// If you are not using these features and wish to reduce the size// of your final executable, you should remove from the following// the specific initialization routines you do not need.#ifdef _AFXDLLEnable3dControls(); // Call this when using MFC in a shared DLL#elseEnable3dControlsStatic(); // Call this when linking to MFC statically#endif// Change the registry key under which our settings are stored.// You should modify this string to be something appropriate// such as the name of your company or organization.SetRegistryKey(_T("3D Toolbox"));LoadStdProfileSettings(10); // Load standard INI file options (including MRU)// Register the application's document templates. Document templates// serve as the connection between documents, frame windows and views.CSingleDocTemplate* pDocTemplate;// create main SDI Frame windowCMainFrame* pMainFrame = new CMainFrame;if (!pMainFrame->LoadFrame(IDR_MAINFRAME))return FALSE;m_pMainWnd = pMainFrame;pDocTemplate = new CSingleDocTemplate(IDR_MODELTYPE,RUNTIME_CLASS(CToolDoc),RUNTIME_CLASS(CMainFrame),RUNTIME_CLASS(CRenderView));AddDocTemplate(pDocTemplate);// The main window has been initialized, so show and update it.//pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);pMainFrame->ShowWindow(SW_SHOW);pMainFrame->UpdateWindow();return TRUE;}// App command to run the dialogvoid CToolApp::OnAppAbout(){CAboutDlg aboutDlg;aboutDlg.DoModal();}6、ToolDoc.cpp#include "stdafx.h"#include "math.h"#include "Tool.h"#include "ToolDoc.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifIMPLEMENT_DYNCREATE(CToolDoc, CDocument)BEGIN_MESSAGE_MAP(CToolDoc, CDocument)//{{AFX_MSG_MAP(CToolDoc)// NOTE - the ClassWizard will add and remove mapping macros here.// DO NOT EDIT what you see in these blocks of generated code!//}}AFX_MSG_MAPEND_MESSAGE_MAP()extern CToolApp theApp;// CToolDoc construction/destruction。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《高级计算机图形学》实验报告姓名:学号:班级:【实验报告要求】实验名称:高级计算机图形学室内场景实验目的:掌握使用OpenGL生成真实感复杂对象的方法,进一步熟练掌握构造实体几何表示法、扫描表示法、八叉树法、BSP树法等建模方法。

实验要求:要求利用OpenGL生成一个真实感的复杂对象及其周围场景,并显示观测点变化时的几何变换,要具备在一个纹理复杂的场景中漫游功能。

要求使用到光线跟踪算法、纹理映射技术以及实时绘制技术。

一、实验效果图图1:正面效果图图2:背面效果图图4:背面效果图图4:室内场景细节效果图图5:场景角度转换效果图二、源文件数据代码:共6个文件,其实现代码如下:1、DlgAbout.cpp#include "StdAfx.h"#include "DlgAbout.h"CAboutDlg::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_INIT}CFormCommandView::~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_MAP}BEGIN_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() const{CFormView::AssertValid();}void CFormCommandView::Dump(CDumpContext& dc) const{CFormView::Dump(dc);}CToolDoc* CFormCommandView::GetDocument() // non-debug version is inline {ASSERT(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);}else{glDisable(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 indicator ID_INDICATOR_CAPS,ID_INDICATOR_NUM,ID_INDICATOR_SCRL,};// CMainFrame construction/destruction CMainFrame::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 toolbar\n");return -1; // fail to create}if (!m_wndStatusBar.Create(this) ||!m_wndStatusBar.SetIndicators(indicators,sizeof(indicators)/sizeof(UINT))){TRACE0("Failed to create status bar\n");return -1; // fail to create}m_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() const{CFrameWnd::AssertValid();}void CMainFrame::Dump(CDumpContext& dc) const{CFrameWnd::Dump(dc);}#endif //_DEBUG// CMainFrame message handlersvoid CMainFrame::OnPaint(){CPaintDC dc(this); // device context for painting}BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) {if (!m_wndSplitter.CreateStatic(this, 1, 2,WS_CHILD | WS_VISIBLE)){TRACE("Failed to CreateStaticSplitter\n");return FALSE;}// First splitter paneif (!m_wndSplitter.CreateView(0, 0,RUNTIME_CLASS(CRenderView), CSize(600,500), pContext)){TRACE("Failed to create command view pane\n");return FALSE;}if (!m_wndSplitter.CreateView(0, 1,RUNTIME_CLASS(CFormCommandView), CSize(0,0), pContext)){TRACE("Failed to create command view pane\n");return FALSE;}// Second splitter panereturn TRUE;}4、RenderView.cpp#include "stdafx.h"#include "Tool.h"#include <string.h>#include <time.h>#include <math.h>#include "ToolDoc.h"#include "RenderView.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifextern void Render(void);// This is the holding space for the landscape colours.int WinWidth, WinHeigth;unsigned short int comp = 32; // Scale modifier.unsigned short int temp, texture_mapping = FALSE,land_fogging = TRUE, flat_shading = TRUE; float angle, 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 button.int 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(){// OpenGLm_hGLContext = NULL;m_GLPixelIndex = 0;// Mousem_LeftButtonDown = FALSE;m_RightButtonDown = FALSE;m_CursorRotation = AfxGetApp()->LoadCursor(IDC_CURSOR_ROTATION);// ColorsCToolApp *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 fogColor[4] = {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 contextint 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 modeglPolygonMode(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 definitionInitGeometry();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;olorBits = 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 modeCSize 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 paintingCPaintDC dc(this);// Useful in singledoc templatesHWND 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 buffersSwapBuffers(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; /* ObjectList[0]:isolated surfaces*/INT4S ObjectNum;char gEnergyFile[30];char sLookAtFN[100];char ImageName[30];void CRenderView::ReadData(){int i,j,l;FILE *fp;char stemp[100];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:%s\n", 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; i<=texnum; i++){TextureList[i] = (TEXTURE_2D *)malloc( sizeof(TEXTURE_2D));fscanf( fp, "%s%s", TextureList[i]->fname, stemp );if ( strcmp( stemp,"REPEAT_TEXTURE" ) == 0)TextureList[i]->type = 1;else if ( strcmp( stemp,"CLAMP_TEXTURE" ) == 0)TextureList[i]->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 ++ ){f scanf( fp, "%s", stemp);w hile( strcmp( stemp,"SurfaceNum" ) != 0) fscanf(fp,"%s",stemp);f scanf( fp, "%ld", &(ObjectList[i].SurfNum) );ObjectList[i].surflist = (SURFACE *)malloc( sizeof(SURFACE) * ObjectList[i].SurfNum);for(j = 0; j < ObjectList[i].SurfNum; j ++ ){/****** Read surface infor ******/fscanf( fp, "%s", stemp);while( strcmp( stemp,"TextureId" ) != 0) fscanf(fp,"%s",stemp);fscanf( fp, "%d", &(ObjectList[i].surflist[j].texId) );fscanf( fp, "%s", stemp);while( strcmp( stemp,"pointnum" ) != 0) fscanf(fp,"%s",stemp);fscanf( fp, "%d", &(ObjectList[i].surflist[j].pointn) );fscanf( fp, "%s", stemp);while( strcmp( stemp,"triangle" ) != 0) fscanf(fp,"%s",stemp);fscanf( fp, "%d", &(ObjectList[i].surflist[j].triangle) );fscanf( fp, "%s", stemp);while( strcmp( stemp,"quadrangle" ) != 0) fscanf(fp,"%s",stemp);fscanf( fp, "%d", &(ObjectList[i].surflist[j].quadric) );/****** Read point list ******/ObjectList[i].surflist[j].pointlist = (POINT3D*)malloc(sizeof(POINT3D) *ObjectList[i].surflist[j].pointn);plist = ObjectList[i].surflist[j].pointlist;for( l = 0; l < ObjectList[i].surflist[j].pointn ; l ++ )fscanf( fp, "%f%f%f%f%f%f%f%f",&(plist[l].r), &(plist[l].g), &(plist[l].b),&(plist[l].u), &(plist[l].v),&(plist[l].x), &(plist[l].y), &(plist[l].z) );/****** Read patchlist ******/nAllVertexNum = ObjectList[i].surflist[j].triangle * 3 +ObjectList[i].surflist[j].quadric *4 ;ObjectList[i].surflist[j].patchlist = (INT4U *)malloc( sizeof(INT4U) * nAllVertexNum);pchlist = ObjectList[i].surflist[j].patchlist;for( l = 0; l < nAllVertexNum; l ++ )fscanf( fp, "%ld", &(pchlist[l]) );}}fclose(fp);}void CRenderView::InitLookAt(){FILE *fp;strcpy(sLookAtFN,"room.lk");fp = fopen(sLookAtFN, "rb");if (fp == NULL){ex = ey = ez =1.0f;cx = cy = cz =0.0f;Near = 0.1f;angle = 30.0f;}else fscanf(fp, "%f%f%f%f%f%f%f%f", &angle, &Near, &ex, &ey, &ez, &cx, &cy, &cz);fclose(fp);}void C RenderView::InitRenderWin(){glShadeModel ( GL_SMOOTH );glDepthFunc ( GL_LESS );glEnable ( GL_DEPTH_TEST );glMatrixMode ( GL_PROJECTION );glLoadIdentity();glMatrixMode ( GL_MODELVIEW );glLoadIdentity();gluPerspective ( angle, (float)WinWidth/(float)WinHeigth, Near, 1000000000.0); gluLookAt( ex, ey, ez, cx, cy, cz, 0.0, 1.0, 0.0);}void C RenderView::Render(void){int i, j, k, l, m, TexIndex;POINT3D *plist;INT4U *pchlist;glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_ACCUM_BUFFER_BIT);for(i = 0; i < ObjectNum; i ++ )for(j = 0; j < ObjectList[i].SurfNum; j ++ ){TexIndex = ObjectList[i].surflist[j].texId;if( TexIndex > 0 )InitTex( TexIndex );plist = ObjectList[i].surflist[j].pointlist;pchlist = ObjectList[i].surflist[j].patchlist;l = 0;for ( k = 0; k < ObjectList[i].surflist[j].triangle; k ++){glBegin( GL_TRIANGLES );for( m = 0; m < 3; m ++ ){glColor3f ( plist[pchlist[l]].r,plist[pchlist[l]].g,plist[pchlist[l]].b );glTexCoord2f( plist[pchlist[l]].u,plist[pchlist[l]].v );glVertex3f( plist[pchlist[l]].x,plist[pchlist[l]].y,plist[pchlist[l]].z );l ++;}/* m */glEnd();}/* k */for ( k = 0; k < ObjectList[i].surflist[j].quadric; k ++){glBegin( GL_QUADS );for( m = 0; m < 4; m ++ ){glColor3f ( plist[pchlist[l]].r,plist[pchlist[l]].g,plist[pchlist[l]].b );glTexCoord2f( plist[pchlist[l]].u,plist[pchlist[l]].v );glVertex3f( plist[pchlist[l]].x,plist[pchlist[l]].y,plist[pchlist[l]].z );l ++;}/* m */glEnd();}/* k */glFlush();CloseTex();}}void CRenderView::freelist(){int i, j;for( i=0; i<ObjectNum; i++){for( j=0; j<ObjectList[i].SurfNum; j++){free(ObjectList[i].surflist[j].pointlist);free(ObjectList[i].surflist[j].patchlist);}free( ObjectList[i].surflist );}free(ObjectList);for(i=1; i<=texnum; i++)free(TextureList[i]);free(TextureList);}extern TEXTURE_2D **TextureList;/********************************//* function : OpenTexImage *//********************************/unsigned char *CRenderView::OpenTexImage( INT2U TexIndex, INT2U *rslx, INT2U *rsly ){unsigned char *image;FILE *fp;INT2U srcx, srcy;INT4U i, j;char ImageName[30];unsigned char *SImageData;int rc;int width, height;strcpy( ImageName, TextureList[TexIndex]->fname);/* load a image */fp = fopen(ImageName,"rb");if(!fp) return 0;fseek(fp,18L,0);rc=fread(&width,sizeof(long),1,fp);rc=fread(&height,sizeof(long),1,fp);*rslx=srcx=width; *rsly=srcy=height;fseek(fp,54L,0);image = (unsigned char *)malloc(width*height*3);rc=fread(image,width*height*3,1,fp);fclose(fp);SImageData = (unsigned char *)malloc(srcx*srcy*3);for(i=0; i<srcx; i++) {for(j=0; j<srcy; j++) {(unsigned char)*(SImageData+i*srcx*3+j*3+0) = (unsignedchar)*(image+i*srcx*3+j*3+2);(unsigned char)*(SImageData+i*srcx*3+j*3+1) = (unsigned char)*(image+i*srcx*3+j*3+1);(unsigned char)*(SImageData+i*srcx*3+j*3+2) = (unsigned char)*(image+i*srcx*3+j*3+0);}}free(image);printf("%s : %ld=%ld\n", ImageName, srcx*srcy*3,i*j*3);return( SImageData );}/********************************//* function : InitTex *//********************************/void C RenderView::InitTex( int TexIndex ){INT2U TextType;unsigned char *ImageData;static int OldIndex = -1;if(TexIndex<=0) return;if(TexIndex == OldIndex){glEnable(GL_TEXTURE_2D);return;}ImageData = ImageDatas[TexIndex-1];TextType = TextureList[TexIndex]->type;glPixelStorei(GL_UNPACK_ALIGNMENT, 1);glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);if( TextType == CLAMP_TEXTURE ){glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);}else{glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);}glTexImage2D( GL_TEXTURE_2D, 0, 3, rslxs[TexIndex-1], rslys[TexIndex-1], 0,GL_RGB, GL_UNSIGNED_BYTE, ImageData );glEnable(GL_TEXTURE_2D);OldIndex = TexIndex;}/********************************//* function : CloseTex *//********************************/void C RenderView::CloseTex(){glDisable(GL_TEXTURE_2D);}void CRenderView::LoadAllTexture(){int i;for (i=0; i <texnum ; i++)ImageDatas[i] = OpenTexImage( i+1, &rslxs[i], &rslys[i] );}void CRenderView::FreeAllTexture(){int i;for (i=0; i <texnum ; i++)free(ImageDatas[i]);}5、StdAfx.cpp#include "stdafx.h"6、Tool.cpp#include "stdafx.h"#include "Tool.h"#include "DlgAbout.h"#include "MainFrm.h"#include "ToolDoc.h"#include "RenderView.h"// Download by #ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif// CToolAppCToolDoc *MyDocument;BEGIN_MESSAGE_MAP(CToolApp, CWinApp)//{{AFX_MSG_MAP(CToolApp)ON_COMMAND(ID_APP_ABOUT, OnAppAbout)//}}AFX_MSG_MAP// Standard file based document commands// Standard print setup commandON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup) END_MESSAGE_MAP()// CToolApp constructionCToolApp::CToolApp(){// Optionsm_OptionColorGlBack = RGB(0,0,255);}// The one and only CToolApp objectCToolApp theApp;// CToolApp initializationBOOL CToolApp::InitInstance(){AfxEnableControlContainer();// Standard initialization// If you are not using these features and wish to reduce the size// of your final executable, you should remove from the following// the specific initialization routines you do not need.#ifdef _AFXDLLEnable3dControls(); // Call this when using MFC in a shared DLL#elseEnable3dControlsStatic(); // Call this when linking to MFC statically#endif// Change the registry key under which our settings are stored.// You should modify this string to be something appropriate// such as the name of your company or organization.SetRegistryKey(_T("3D Toolbox"));LoadStdProfileSettings(10); // Load standard INI file options (including MRU)// Register the application's document templates. Document templates// serve as the connection between documents, frame windows and views.CSingleDocTemplate* pDocTemplate;// create main SDI Frame windowCMainFrame* pMainFrame = new CMainFrame;if (!pMainFrame->LoadFrame(IDR_MAINFRAME))return FALSE;m_pMainWnd = pMainFrame;pDocTemplate = new CSingleDocTemplate(IDR_MODELTYPE,RUNTIME_CLASS(CToolDoc),RUNTIME_CLASS(CMainFrame),RUNTIME_CLASS(CRenderView));AddDocTemplate(pDocTemplate);// The main window has been initialized, so show and update it.//pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);pMainFrame->ShowWindow(SW_SHOW);pMainFrame->UpdateWindow();return TRUE;}// App command to run the dialogvoid CToolApp::OnAppAbout(){CAboutDlg aboutDlg;aboutDlg.DoModal();}6、ToolDoc.cpp#include "stdafx.h"#include "math.h"#include "Tool.h"#include "ToolDoc.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifIMPLEMENT_DYNCREATE(CToolDoc, CDocument)BEGIN_MESSAGE_MAP(CToolDoc, CDocument)//{{AFX_MSG_MAP(CToolDoc)// NOTE - the ClassWizard will add and remove mapping macros here.// DO NOT EDIT what you see in these blocks of generated code!//}}AFX_MSG_MAPEND_MESSAGE_MAP()extern CToolApp theApp;// CToolDoc construction/destruction。

相关文档
最新文档