用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: < [email protected] > < [email protected] >
Blog: < http://blog.csdn.net/RobinHao >
Site: < http://www.openinventor.cn >