用OpenInventor实现的NeHe OpenGL教程-第二十五课

       

 

NeHe教程在这节课中向我们介绍了如何从文件加载3D模型,并且平滑的从一个模型变换为另一个模型。两个模型之间平滑过渡的算法其实是很简单的,首先我们要保证两个模型要有相同数目的顶点,然后我们分别计算每个点从一个模型移动到另外一个模型时,中间的位置数据。我们将这个过程显示出来就会产生模型平滑过渡的效果。

因为我们的程序采用NeHe提供的3D模型数据,所以我们就直接使用NeHe的代码来读取3D模型数据的代码。本节课程采用的技术都是前面的课程已经介绍过的。我们就不再详细介绍了。

下面的代码都是拷贝自NeHe教程,主要是用来读取3D模型数据,具体的解释请查阅NeHe教程。

typedef struct              // Structure For 3D Points

{

float    x, y, z;      // X, Y & Z Points

} VERTEX;                   // Called VERTEX

typedef struct              // Structure For An Object

{

int      verts;        // Number Of Vertices For The Object

VERTEX   *points;      // One Vertice (Vertex x,y & z)

} OBJECT;                   // Called OBJECT

int      key = 1;

int      maxver;            // Will Eventually Hold The Maximum Number Of Vertices

OBJECT   morph1,morph2,morph3,morph4,// Our 4 Morphable Objects (morph1,2,3 & 4)

helper,*sour,*dest;// Helper Object, Source Object, Destination Object

float    xrot = 0,yrot = 0,zrot = 0,      // X, Y & Z Rotation

xspeed = 0,yspeed = 0,zspeed = 0,// X, Y & Z Spin Speed

cx = 0,cy = 0,cz = -15;          // X, Y & Z Position

int      step = 0,steps = 100;            // Step Counter And Maximum Number Of Steps

bool morph = FALSE;                       // Default morph To False (Not Morphing)

void objallocate(OBJECT *k,int n)         // Allocate Memory For Each Object

{

// And Defines points

k->points = (VERTEX*)malloc(sizeof(VERTEX) * n);

}

// (3 Points For Each Vertice)

void objfree(OBJECT *k)                    // Frees The Object (Releasing The Memory)

{

free(k->points);                     // Frees Points

}

void readstr(FILE *f,char *string)        // Reads A String From File (f)

{

do

{

fgets(string, 255, f);           // Gets A String Of 255 Chars Max From f (File)

} while ((string[0] == '/') || (string[0] == '/n'));// Until End Of Line Is Reached

}

void objload(char *name,OBJECT *k)        // Loads Object From File (name)

{

int      ver;                        // Will Hold Vertice Count

float    rx,ry,rz;                   // Hold Vertex X, Y & Z Position

FILE *filein;                         // Filename To Open

char oneline[255];                    // Holds One Line Of Text (255 Chars Max)

filein = fopen(name, "rt");      // Opens The File For Reading Text In Translated Mode

// CTRL Z Symbolizes End Of File In Translated Mode

readstr(filein,oneline);         // Jumps To Code That Reads One Line Of Text From The File

sscanf(oneline, "Vertices: %d/n", &ver);

k->verts = ver;                  // Sets Objects verts Variable To Equal The Value Of ver

objallocate(k,ver);              // Jumps To Code That Allocates Ram To Hold The Object

for (int i = 0;i < ver;i++)      // Loops Through The Vertices

{

readstr(filein,oneline);    // Reads In The Next Line Of Text

sscanf(oneline, "%f %f %f", &rx, &ry, &rz);

k->points[i].x = rx;        // Sets Objects (k) points.x Value To rx

k->points[i].y = ry;        // Sets Objects (k) points.y Value To ry

k->points[i].z = rz;        // Sets Objects (k) points.z Value To rz

}

fclose(filein);                  // Close The File

if(ver>maxver)

maxver = ver;               // If ver Is Greater Than maxver Set maxver Equal To ver

}    // Keeps Track Of Highest Number Of Vertices Used In Any Of The

//这个函数是关键,用来计算平滑移动时,中间位置数据。

VERTEX calculate(int i)          // Calculates Movement Of Points During Morphing

{

VERTEX a;                   // Temporary Vertex Called a

// a.x Value Equals Source x - Destination x Divided By Steps

a.x = (sour->points[i].x - dest->points[i].x) / steps;

// a.y Value Equals Source y - Destination y Divided By Steps

a.y = (sour->points[i].y - dest->points[i].y) / steps;

// a.z Value Equals Source z - Destination z Divided By Steps

a.z = (sour->points[i].z - dest->points[i].z) / steps;

return a;

}

我们将在函数BuildScene中创建场景数据。

void BuildScene(void)

{

//定义事件回调节点,用来响应键盘消息

SoEventCallback* pEventCallback = new SoEventCallback;

pEventCallback->addEventCallback(SoKeyboardEvent::getClassTypeId(),

KeyboardEventCB,

g_pOivSceneRoot);

g_pOivSceneRoot->addChild(pEventCallback);

//

//读取3D模型数据,初始化顶点信息

maxver = 0;                               // Sets Max Vertices To 0 By Default

objload("../data/sphere.txt",&morph1);

objload("../data/torus.txt",&morph2);

objload("../data/tube.txt",&morph3);

objallocate(&morph4,486);   // Manually Reserver Ram For A 4th 468 Vertice Object (morph4)

for(int i = 0;i < 486;i++)  // Loop Through All 468 Vertices

{

// morph4 x Point Becomes A Random Float Value From -7 to 7

morph4.points[i].x = ((float)(rand()%14000) / 1000) - 7;

// morph4 y Point Becomes A Random Float Value From -7 to 7

morph4.points[i].y = ((float)(rand()%14000) / 1000) - 7;

// morph4 z Point Becomes A Random Float Value From -7 to 7

morph4.points[i].z = ((float)(rand()%14000) / 1000) - 7;

}

// Load sphere.txt Object Into Helper (Used As Starting Point)

objload("../data/sphere.txt",&helper);

sour = dest = &morph1; // Source & Destination Are Set To Equal First Object (morph1)

//构造位置节点

g_pPosTrans = new SoTranslation;

g_pOivSceneRoot->addChild(g_pPosTrans);

g_pPosTrans->translation.setValue(cx,cy,cz);

g_pXRotation = new SoRotation;

g_pOivSceneRoot->addChild(g_pXRotation);

g_pXRotation->rotation.setValue(SbVec3f(1,0,0),xrot);

g_pYRotation = new SoRotation;

g_pOivSceneRoot->addChild(g_pYRotation);

g_pYRotation->rotation.setValue(SbVec3f(0,1,0),yrot);

g_pZRotation = new SoRotation;

g_pOivSceneRoot->addChild(g_pZRotation);

g_pZRotation->rotation.setValue(SbVec3f(0,0,1),zrot);

g_pPointColor = new SoBaseColor;

g_pOivSceneRoot->addChild(g_pPointColor);

g_pPointColor->rgb.setValue(0,0,1);

g_pPointCoord = new SoCoordinate3;

g_pOivSceneRoot->addChild(g_pPointCoord);

g_pPointCoord->point.setValuesPointer(helper.verts,(float *)helper.points);

SoPointSet *pPointSet = new SoPointSet;

pPointSet->numPoints = helper.verts;

g_pOivSceneRoot->addChild(pPointSet);

SoTimerSensor * texttimer = new SoTimerSensor(TimerSensorCB, NULL);

texttimer->setInterval(0.001);

texttimer->schedule();

}

下面的函数是定时时间回调函数,我们在这个函数中进行过渡计算。

void TimerSensorCB(void * data, SoSensor *)

{

if(morph && step <= steps)

{

VERTEX q;              // Holds Returned Calculated Values For One Vertex

step++;

for(int i = 0;i < helper.verts; i++)

{    // The Same Amount Of Verts For Simplicity, Could Use maxver Also)

q = calculate(i); //这里开始计算中间位置

helper.points[i].x -= q.x;

helper.points[i].y -= q.y;

helper.points[i].z -= q.z;

g_pPointCoord->point.setValuesPointer(helper.verts,

(float *)helper.points);

}

}

else

{

if(step != 0)

g_pPointColor->rgb.setValue(0,0,1);

morph = FALSE;

sour = dest;

step = 0;

}

if(xspeed != 0)

{

xrot += xspeed;

g_pXRotation->rotation.setValue(SbVec3f(1,0,0),xrot);

}

if(yspeed != 0)

{

yrot += yspeed;

g_pYRotation->rotation.setValue(SbVec3f(0,1,0),yrot);

}

if(zspeed != 0)

{

zrot += zspeed;

g_pZRotation->rotation.setValue(SbVec3f(0,0,1),zrot);

}

}

现在编译运行我们程序,屏幕会显示出一个由点组成的球体。按下2键,图形变形到圆环体,按下3键,图形变形到圆柱体,按下4键,图形变成空间随机离散点。按下1键,图形重新变回球体。按下上下左右箭头可以旋转物体。PageDown/PageUP放大缩小物体。W/S/A/D键用来平移物体。效果和NeHe第二十五是相同的。

本课的完整代码下载。(VC 2003 + Coin2.5)

后记

OpenInventor是一种基于OpenGL的面向对象的三维图形软件开发包。使用这个开发包,程序员可以快速、简洁地开发出各种类型的交互式三维图形软件。这里不对OpenInventor做详细的介绍,读者如果感兴趣,可以阅读我的blog中的这篇文章《OpenInventor 简介》。

NeHe教程是目前针对初学者来说最好的OpenGL教程,它可以带领读者由浅入深,循序渐进地掌握OpenGL编程技巧。到目前为止(2007年11月),NeHe教程一共有48节。我的计划是使用OpenInventor来实现所有48节课程同样的效果。目的是复习和巩固OpenGL的知识,同时与各位读者交流OpenInventor的使用技巧。

因为篇幅的限制,我不会介绍NeHe教程中OpenGL的实现过程,因为NeHe的教程已经讲解的很清楚了,目前网络中也有NeHe的中文版本。我将使用VC 2003作为主要的编译器。程序框架采用和NeHe一样的Win32程序框架,不使用MFC。程序也可以在VC Express,VC 2005/2008中编译。我采用的OpenInventor开发环境是Coin,这是一个免费开源的OpenInventor开发库。文章 《OpenInventor-Coin3D开发环境》 介绍了如何在VC中使用Coin。我使用的Coin版本是2.5。读者可以到www.coin3d.org 中免费下载。

读者可以在遵循GNU协议的条件下自由使用、修改本文的代码。水平的原因,代码可能不是最优化的,我随时期待读者的指正和交流。转载请注明。谢谢。

我的联系方式:

E-mail: < openinventor@gmail.com > < openinventor@126.com >

Blog: < http://blog.csdn.net/RobinHao >

Site: < http://www.openinventor.cn >

用OpenInventor实现的NeHe OpenGL教程-第二十五课相关推荐

  1. NeHe OpenGL教程 第二十九课:Blt函数

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  2. NeHe OpenGL教程 第二十六课:反射

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  3. NeHe OpenGL教程 第十五课:纹理图形字

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  4. NeHe OpenGL第三十五课:播放AVI

    NeHe OpenGL第三十五课:播放AVI 在OpenGL中播放AVI: 在OpenGL中如何播放AVI呢?利用Windows的API把每一帧作为纹理绑定到OpenGL中,虽然很慢,但它的效果不错. ...

  5. PS教程第二十五课:自由选区

  6. NeHe OpenGL第二十五课:变形

    NeHe OpenGL第二十五课:变形 变形和从文件中加载3D物体: 在这一课中,你将学会如何从文件加载3D模型,并且平滑的从一个模型变换为另一个模型.   欢迎来到这激动人心的一课,在这一课里,我们 ...

  7. OpenGL教程翻译 第二十五课 天空盒

    第二十五课 天空盒 背景 天空盒是用于增强场景表现力的一个常用技术,它一般通过在相机周围包裹一个纹理来实现.这个纹理通常是一些天空.山川或者摩天大楼等等,下面是游戏 Half-Life 中使用天空盒的 ...

  8. NeHe OpenGL第三十二课:拾取游戏

    NeHe OpenGL第三十二课:拾取游戏 拾取, Alpha混合, Alpha测试, 排序: 这又是一个小游戏,交给的东西会很多,慢慢体会吧   欢迎来到32课. 这课大概是在我所写作已来最大的一课 ...

  9. NeHe OpenGL第三十九课:物理模拟

    NeHe OpenGL第三十九课:物理模拟 物理模拟简介: 还记得高中的物理吧,直线运动,自由落体运动,弹簧.在这一课里,我们将创造这一切.   物理模拟介绍 如果你很熟悉物理规律,并且想实现它,这篇 ...

最新文章

  1. FTPFileUtil_FTP文件上传 (spring boot)
  2. hihoCoder#1196 : 高斯消元·二(开关灯问题)
  3. 忽然看懂了《大话西游》
  4. linux下php远程连接mysql_Linux下PHP远程连接Oracle数据库 | 系统运维
  5. Matlab----获取一个文件夹下所有文件名
  6. 初识FPGA(搬运)
  7. samba文件共享服务详解
  8. 前端脚本API发布 | Java 开源企业信息化建设平台O2OA平台
  9. 网站跨域访问解决方法
  10. chrome访问不了go语言中文网
  11. mac nginx映射ip和端口_南京课工场IT培训:Nginx虚拟主机 (基于域名 基于端口 基于ip)...
  12. 基于Easyui框架的datagrid绑定数据,新增,修改,删除方法(一)
  13. MATLAB 2017a 下载及安装
  14. monk_notebook (交际德语教程 第二版 学生用书)
  15. 使用Feign调用服务接口
  16. 计算机固态硬盘256g,1t(后悔买256g的固态硬盘了)
  17. 2k14无法打开因为计算机,NBA2K14虚拟光驱SCSI无法开启攻略_NBA2K14提示安装SPTD_快吧单机游戏...
  18. 经济应用文写作【11】
  19. 蛋白和肽测序仪销量、收入、价格、毛利率及市场份额
  20. 苹果电脑开机慢怎么办 苹果笔记本开机特别慢的处理方法

热门文章

  1. Zookeeper C 异步 API 介绍
  2. (六) 语言模型 Language Madel 与 word2vec
  3. Java LocalDate时间加减
  4. 数据库审计产品给用户带来哪些益处?
  5. 对node工程进行压力测试与性能分析
  6. 计算机网络概念类题目汇总
  7. 数据恢复的原理(来自安易数据恢复官网)
  8. 高云FPGA实现驱动MIPI LCD屏
  9. 科学计算机计算irr,科学计算器软件 DreamCalc Professional 4.9.3
  10. 电子商务设计师教程 电子版_电子商务| 电子商务还是电子商务| 第三部分