用OpenInventor实现的NeHe OpenGL教程-第二十三课
用OpenInventor实现的NeHe OpenGL教程-第二十三课
这节课我们将学习使用环境纹理映射。这种映射方式可以让物体表面看上去像镜子一样反射周围的环境。这节课的代码是在前面第十八节课程的基础上修改的。阅读NeHe教程我们可以发现,利用OpenGL显示环境纹理是比较容易的,同样在OpenInventor中使用环境纹理也是非常的简单。我们只需要创建一个SoTextureCoordinateEnvironment节点就可以了。
和以前的代码一样,我们将在函数BuildScene中创建场景数据。
void BuildScene(void)
{
//创建背景场景
SoSeparator *pBackgroundSep = new SoSeparator;
g_pOivSceneRoot->addChild(pBackgroundSep);
SoTranslation *pTranslation = new SoTranslation;
pBackgroundSep->addChild(pTranslation);
pTranslation->translation.setValue(0,0,-24);
SoTexture2 *pTexture = new SoTexture2;
pTexture->filename.setValue("../Data/BG.PNG");
pBackgroundSep->addChild(pTexture);
SoCoordinate3 *pCoords = new SoCoordinate3;
pBackgroundSep->addChild(pCoords);
pCoords->point.set1Value(0,-13.3f, -10.0f, 10.0f);
pCoords->point.set1Value(1, 13.3f, -10.0f, 10.0f);
pCoords->point.set1Value(2, 13.3f, 10.0f, 10.0f);
pCoords->point.set1Value(3,-13.3f, 10.0f, 10.0f);
SoTextureCoordinate2 *pTexCoord = new SoTextureCoordinate2;
pBackgroundSep->addChild(pTexCoord);
pTexCoord->point.set1Value(0,0.0f, 0.0f);
pTexCoord->point.set1Value(1,1.0f, 0.0f);
pTexCoord->point.set1Value(2,1.0f, 1.0f);
pTexCoord->point.set1Value(3,0.0f, 1.0f);
int32_t NumVertices[] = { 4 };
SoFaceSet *FaceSet = new SoFaceSet;
FaceSet->numVertices.setValues(0, 1, NumVertices);
pBackgroundSep->addChild(FaceSet);
//////////////////////////////////////////////////////////////////////
//创建几何体场景,基本上和第十八课一样
g_pZTranslation = new SoTranslation;
g_pOivSceneRoot->addChild(g_pZTranslation);
g_pXRotor = new SoRotor;
g_pXRotor->speed = 0.0f;
g_pXRotor->rotation.setValue(SbVec3f(1,0,0),0.001f);
g_pOivSceneRoot->addChild(g_pXRotor);
g_pYRotor = new SoRotor;
g_pYRotor->rotation.setValue(SbVec3f(0,1,0),0.001f);
g_pYRotor->speed = 0.0f;
g_pOivSceneRoot->addChild(g_pYRotor);
g_pPointLight = new SoPointLight;
g_pPointLight->color.setValue(1.0f, 1.0f, 1.0f);
g_pPointLight->intensity = 1.0;
g_pPointLight->location.setValue(0.0f, 0.0f, 4.0f);
g_pOivSceneRoot->addChild(g_pPointLight);
g_pTextureComplexity = new SoComplexity;
g_pTextureComplexity->value = 1.0;
g_pTextureComplexity->textureQuality = 1.0;
g_pOivSceneRoot->addChild(g_pTextureComplexity);
g_pTexture = new SoTexture2;
g_pTexture->filename.setValue("../Data/Reflect.PNG");
g_pTexture->model = SoTexture2::DECAL;
g_pOivSceneRoot->addChild(g_pTexture);
//这里是本课程的关键,定义环境纹理节点
SoTextureCoordinateEnvironment *pTexPlane = new SoTextureCoordinateEnvironment;
g_pOivSceneRoot->addChild(pTexPlane);
SoRotation *pRotation = new SoRotation;
pRotation->rotation.setValue(SbVec3f(1,0,0),3.14159 / 2.0);
g_pOivSceneRoot->addChild(pRotation);
g_pSceneSwitch = new SoSwitch;
SoSeparator *pCubeSep = new SoSeparator;
pCubeSep->addChild(new SoCube);
g_pSceneSwitch->addChild(pCubeSep);
SoSeparator *pCylinderSep = new SoSeparator;
SoCylinder *pCylinder = new SoCylinder;
pCylinder->radius = 1.0f;
pCylinder->height = 3.0f;
pCylinder->parts = SoCylinder::SIDES;
pCylinderSep->addChild(pCylinder);
g_pSceneSwitch->addChild(pCylinderSep);
SoSeparator *pSphereSep = new SoSeparator;
SoSphere *pSphere = new SoSphere;
pSphere->radius = 1.3f;
pSphereSep->addChild(pSphere);
g_pSceneSwitch->addChild(pSphereSep);
SoSeparator *pConeSep = new SoSeparator;
SoCone *pCone = new SoCone;
pCone->bottomRadius = 1.0f;
pCone->height = 3.0f;
pCone->parts = SoCone::SIDES;
pConeSep->addChild(pCone);
g_pSceneSwitch->addChild(pConeSep);
g_pSceneSwitch->whichChild = 0;
g_pOivSceneRoot->addChild(g_pSceneSwitch);
}
剩下的代码和第十八节课程都相同了。
现在编译运行我们程序,屏幕上显示一个反射周围环境立方体。按下左右方向键,立方体将绕Y轴旋转。按下上下方向键,立方体将绕X轴旋转。按下PnUp/PnDn键,立方体将放大或缩小。按下F键,立方体的纹理品质将发生变化。按下L键将打开或关闭灯光。按下空格键将显示不同的几何体。效果和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.****.net/RobinHao >
Site: < http://www.openinventor.cn >