用OpenInventor实现的NeHe OpenGL教程-第二十八课
NeHe教程在这节课中向我们介绍了贝塞尔曲面,贝塞尔曲面是一种可以只使用很少的参数就可以描述出复杂曲面的一种数学工具。对于使用OpenInventor的程序员来说,创建贝塞尔曲面是非常容易的事情,OpenInventor已经对贝塞尔曲面,Nurbs曲面等都做了相应的支持。
首先定义一些程序中使用的全局变量:
SoRotation* g_pZRotation = NULL;//旋转贝塞尔曲面
SoDrawStyle* g_pDrawStyle = NULL;
const float pts[16][3] = {
{-0.75, -0.75, -0.5},
{-0.25, -0.75, 0.0},
……….
} //定义贝塞尔曲面的控制点位置
创建贝塞尔曲面场景节点
SoSeparator* BuildSurface(void)
{
SoSeparator *surfSep = new SoSeparator();
SoComplexity *complexity = new SoComplexity;
complexity->value = 1.0f;
surfSep->addChild(complexity);
SoTexture2 *pTexture = new SoTexture2;
pTexture->filename.setValue("../Data/NeHe.png");
surfSep->addChild(pTexture);
SoCoordinate3 *controlPts = new SoCoordinate3;//加载控制点参数
controlPts->point.setValues(0, 16, pts);
surfSep->addChild(controlPts);
// The knot vector
float knots[8] = {0, 0, 0, 0, 1, 1, 1, 1};
SoNurbsSurface *surface = new SoNurbsSurface;//定义贝塞尔曲面节点
surface->numUControlPoints = 4;
surface->numVControlPoints = 4;
surface->uKnotVector.setValues(0, 8, knots);
surface->vKnotVector.setValues(0, 8, knots);
surfSep->addChild(surface);
return surfSep;
}
创建控制点场景
SoSeparator* BuildCtrlPt(void)
{
SoSeparator *pCtrlPtSep = new SoSeparator();
g_pDrawStyle = new SoDrawStyle;
g_pDrawStyle->style = SoDrawStyleElement::INVISIBLE;
pCtrlPtSep->addChild(g_pDrawStyle);
SoBaseColor *pCtrlPtColor = new SoBaseColor;
pCtrlPtColor->rgb.setValue(1.0,0.0,0.0);
pCtrlPtSep->addChild(pCtrlPtColor);
SoCoordinate3 *controlPts = new SoCoordinate3;
controlPts->point.setValues(0, 16, pts);
pCtrlPtSep->addChild(controlPts);
int32_t coordIndex[ 8 * 5],iIndex = 0;
for(int i = 0;i < 4; i++)
{
for(int j = 0;j < 4; j++)
coordIndex[iIndex++] = i * 4 + j;
coordIndex[iIndex++] = SO_END_LINE_INDEX;
}
for(int i = 0;i < 4; i++)
{
for(int j = 0;j < 4; j++)
coordIndex[iIndex++] = j * 4 + i;
coordIndex[iIndex++] = SO_END_LINE_INDEX;
}
SoIndexedLineSet *pLineSet = new SoIndexedLineSet;
pLineSet->coordIndex.setValues(0,8 * 5,coordIndex);
pCtrlPtSep->addChild(pLineSet);
return pCtrlPtSep;
}
创建整个场景
void BuildScene(void)
{
SoEventCallback* pEventCallback = new SoEventCallback;
pEventCallback->addEventCallback(SoKeyboardEvent::getClassTypeId(),KeyboardEventCB,g_pOivSceneRoot);
g_pOivSceneRoot->addChild(pEventCallback);
///
SoRotation *pFixRot = new SoRotation;
pFixRot->rotation.setValue(SbVec3f(1,0,0),-75.0 / 180.0 * 3.1415);
g_pOivSceneRoot->addChild(pFixRot);
g_pZRotation = new SoRotation;
g_pOivSceneRoot->addChild(g_pZRotation);
g_pOivSceneRoot->addChild(BuildSurface());
g_pOivSceneRoot->addChild(BuildCtrlPt());
}
剩下的代码和以前的课程类似,我们就不在详细介绍了。
现在编译运行我们程序,屏幕会显示一个表面带有贴图的贝塞尔曲面。按左右箭头可以旋转曲面。按下空格键,可以显示/隐藏贝塞尔曲面的控制点数据。效果和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.youkuaiyun.com/RobinHao >
Site: < http://www.openinventor.cn >