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

来源:互联网 发布:贵州广电网络上不了网 编辑:程序博客网 时间:2024/05/11 17:40

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编程技巧。到目前为止(200711月),NeHe教程一共有48节。我的计划是使用OpenInventor来实现所有48节课程同样的效果。目的是复习和巩固OpenGL的知识,同时与各位读者交流OpenInventor的使用技巧。

 

       因为篇幅的限制,我不会介绍NeHe教程中OpenGL的实现过程,因为NeHe的教程已经讲解的很清楚了,目前网络中也有NeHe的中文版本。我将使用VC 2003作为主要的编译器。程序框架采用和NeHe一样的Win32程序框架,不使用MFC。程序也可以在VC ExpressVC 2005/2008中编译。我采用的OpenInventor开发环境是Coin,这是一个免费开源的OpenInventor开发库。文章 OpenInventorCoin3D开发环境 介绍了如何在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 >

原创粉丝点击