用OpenInventor實現的NeHe OpenGL教程-第二十二課
這節課我們將討論凹凸紋理技術。使用凹凸紋理可以讓物體表面更有質感。正如NeHe教程中所說的,這是一節高級課程,請確信你對基本知識已經非常瞭解。
我們對NeHe教程閱讀後可以發現,利用OpenGL顯示凹凸紋理是比較困難的。首先需要使用OpenGL的多重紋理擴展,其次還要了解凹凸紋理的計算方法。我估計很多程序員都搞不清楚凹凸紋理的計算方法(包括我自己在內。嗚嗚!尤其是“切空間”的含義是什麼,如果那位讀者明白的話,請不吝賜教。^_^)。不過,如果我們使用OpenInventor來顯示凹凸紋理的話,那就很容易了。因爲OpenInventor提供了凹凸紋理節點。只要我們按照要求做出凹凸紋理文件,內部的複雜計算我們就不需要關心了。
下面是實現的代碼,請注意,代碼是基於Coin的OpenInventor。如果讀者使用的是TGS的OpenInventor,可能有些地方需要做些修改,而且凹凸紋理文件也可能需要修改。
首先定義一些全局變量:
SoSeparator* g_pBumpSep = NULL;
SoComplexity* g_pTextureComplexity = NULL;
SoBumpMap* g_pBumpTexture = NULL; //凹凸紋理節點
SoTexture2* g_pBaseTexture = NULL; //正常的紋理節點
SoRotor* g_pXRotor = NULL;
SoRotor* g_pYRotor = NULL;
SoTranslation* g_pZTranslation = NULL;
下面的函數用來構建右下角的“OpenGL”圖標的場景。使用了帶有Alpha通道的位圖文件
SoSeparator* BuildLogo(void)
{
SoSeparator *pLogoSep = new SoSeparator;
SoMaterial *pLogoMaterial = new SoMaterial;
pLogoMaterial->diffuseColor.setValue(1.0,1.0,1.0);
pLogoSep->addChild(pLogoMaterial);
SoCallback *pEnableGlCallback = new SoCallback();
pEnableGlCallback->setCallback(GlCB, 0);
pLogoSep->addChild(pEnableGlCallback);
AUX_RGBImageRec *pImage = auxDIBImageLoad("../Data/OpenGL_Alpha.bmp");
if(pImage != NULL)
{
// Create Memory For RGBA8-Texture
unsigned char *pImageData = new unsigned char[4 * pImage->sizeX * pImage->sizeY]; for(int i = 0; i < pImage->sizeX * pImage->sizeY; i++)
pImageData[4 * i + 3] = pImage->data[i * 3]; // Pick Only Red Value As Alpha!
if(pImage->data)
free(pImage->data);
free(pImage);
pImage = auxDIBImageLoad("../Data/OpenGL.bmp");
if(pImage != NULL)
{
for (int i = 0; i < pImage->sizeX * pImage->sizeY; i++)
{
pImageData[4 * i] = pImage->data[i * 3]; // R
pImageData[4 * i + 1] = pImage->data[i * 3 + 1]; // G
pImageData[4 * i + 2] = pImage->data[i * 3 + 2]; // B
}
}
SoTexture2 *pOpenGLTexture = new SoTexture2;
pLogoSep->addChild(pOpenGLTexture);
pOpenGLTexture->image.setValue(SbVec2s(pImage->sizeX,pImage->sizeY),4,pImageData);
if(pImage->data)
free(pImage->data);
free(pImage);
delete []pImageData;
}
SoTextureCoordinate2 *pOpenGLTexCoord = new SoTextureCoordinate2;
pOpenGLTexCoord->point.set1Value(0,0,0);
pOpenGLTexCoord->point.set1Value(1,1,0);
pOpenGLTexCoord->point.set1Value(2,1,1);
pOpenGLTexCoord->point.set1Value(3,0,1);
pLogoSep->addChild(pOpenGLTexCoord);
SoCoordinate3 *pOpenGLCoord = new SoCoordinate3;
SoPerspectiveCamera *pPerspectiveCamera = (SoPerspectiveCamera *)g_pOivView->getCamera();
SbViewVolume vv = pPerspectiveCamera->getViewVolume();
float x,y,z;
pPerspectiveCamera->position.getValue().getValue(x,y,z);
SbVec3f pt = vv.getPlanePoint(z, SbVec2f(0.8, 0.0));
pOpenGLCoord->point.set1Value(0,pt[0], pt[1], 0.0f);
pt = vv.getPlanePoint(z, SbVec2f(1.18f, 0.0));
pOpenGLCoord->point.set1Value(1,pt[0], pt[1], 0.0f);
pt = vv.getPlanePoint(z, SbVec2f(1.18f, 0.18));
pOpenGLCoord->point.set1Value(2,pt[0], pt[1], 0.0f);
pt = vv.getPlanePoint(z, SbVec2f(0.8, 0.18));
pOpenGLCoord->point.set1Value(3,pt[0], pt[1], 0.0f);
pLogoSep->addChild(pOpenGLCoord);
pLogoSep->addChild(new SoFaceSet);
SoCallback *pDisableGlCallback = new SoCallback();
pDisableGlCallback->setCallback(GlCB, (void *)1);
pLogoSep->addChild(pDisableGlCallback);
return pLogoSep;
}
開始構建場景
void BuildScene(void)
{
SoEventCallback* pEventCallback = new SoEventCallback;
pEventCallback->addEventCallback(SoKeyboardEvent::getClassTypeId(),KeyboardEventCB,g_pOivSceneRoot);
g_pOivSceneRoot->addChild(pEventCallback);
g_pBumpSep = new SoSeparator;
g_pOivSceneRoot->addChild(g_pBumpSep);
g_pZTranslation = new SoTranslation;
g_pZTranslation->translation.setValue(0,0,-5);
g_pBumpSep->addChild(g_pZTranslation);
g_pXRotor = new SoRotor;
g_pXRotor->speed = 0.0f;
g_pXRotor->rotation.setValue(SbVec3f(1,0,0),0.001f);
g_pBumpSep->addChild(g_pXRotor);
g_pYRotor = new SoRotor;
g_pYRotor->rotation.setValue(SbVec3f(0,1,0),0.001f);
g_pYRotor->speed = 0.0f;
g_pBumpSep->addChild(g_pYRotor);
g_pTextureComplexity = new SoComplexity;
g_pTextureComplexity->textureQuality = 0.1;
g_pBumpSep->addChild(g_pTextureComplexity);
//這裏是關鍵,定義一個凹凸紋理節點,指定一個紋理圖形文件。注意,這個文件必須要符合Coin的規定,具體請參考Coin的文檔。
#ifdef __COIN__
g_pBumpTexture = new SoBumpMap;
g_pBumpTexture->filename = "../Data/BumplMap.png";
g_pBumpSep->addChild(g_pBumpTexture);
g_pBumpTexture->ref();
#endif
g_pBaseTexture = new SoTexture2;
g_pBaseTexture->filename.setValue("../Data/Base.PNG");
g_pBaseTexture->model = SoTexture2::DECAL;
g_pBumpSep->addChild(g_pBaseTexture);
g_pBumpSep->addChild(new SoCube);
g_pOivSceneRoot->addChild(BuildLogo());
}
現在編譯運行我們程序,屏幕上顯示一個立方體,立方體上面有一些凹凸點。按下左右箭頭鍵,立方體將繞Y軸旋轉。按下上下箭頭鍵,立方體將繞Y軸旋轉。按下PageUp/PageDown。立方體將放大/縮小。按下B鍵,立方體將顯示/隱藏凹凸紋理。程序運行的效果和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 >