您的位置:首页 > 运维架构

用OpenInventor实现的NeHe OpenGL教程-第三十四课

2010-07-29 09:32 543 查看
用OpenInventor实现的NeHe OpenGL教程-第三十四课




NeHe教程在这节课向我们介绍了如何从一个2D的灰度图创建地形。这节课的内容比较简单,只是从一个2D灰度图中读取灰度数据,然后将灰度数据转换成高度数据,最后将这些数据显示出来就可以了。


首先定义一些全局变量,这些变量的含义和NeHe教程完全一致
#define MAP_SIZE 1024 // Size Of Our .RAW Height Map (NEW)
#define STEP_SIZE 16 // Width And Height Of Each Quad (NEW)
#define HEIGHT_RATIO 1.5f // Ratio That The Y Is Scaled According To The X And Z (NEW)
float scaleValue = 0.15f;// Scale Value For The Terrain (NEW)
bool bRender = TRUE; // Polygon Flag Set To TRUE By Default (NEW)
BYTE g_HeightMap[MAP_SIZE * MAP_SIZE];// Holds The Height Map Data (NEW)

下面的函数用来读取灰度数据,计算高度,这些函数都拷贝自NeHe教程,因此这里就不再详细说明了。
void LoadRawFile(LPSTR strName, int nSize, BYTE *pHeightMap)
{
………..
}

int Height(BYTE *pHeightMap, int X, int Y)
{
……….
}

float GetVertexColor(BYTE *pHeightMap, int x, int y)
{
…………
}

下面的函数用来创建地形场景
SoSeparator* BuildHeightMapSep(BYTE pHeightMap[])
{
SoSeparator *pHeightMapSep = new SoSeparator;

int X = 0, Y = 0; // Create Some Variables To Walk The Array With.
int x, y, z; // Create Some Variables For Readability
if(!pHeightMap)
return NULL; // Make Sure Our Height Data Is Valid

SoBaseColor *pHeightMapColor = new SoBaseColor;
pHeightMapSep->addChild(pHeightMapColor);

SoCoordinate3 *pHeightMapCoord = new SoCoordinate3;
pHeightMapCoord->point.set1Value(0,-1.0f, 1.0f, 0.0f);
pHeightMapCoord->point.set1Value(1, 1.0f, 1.0f, 0.0f);
pHeightMapCoord->point.set1Value(2, 1.0f, -1.0f, 0.0f);
pHeightMapCoord->point.set1Value(3,-1.0f, -1.0f, 0.0f);
pHeightMapSep->addChild(pHeightMapCoord);

int iVecNum[63 * 63];
int iIndex = 0;
for ( X = 0; X < (MAP_SIZE - STEP_SIZE); X += STEP_SIZE )
{
for ( Y = 0; Y < (MAP_SIZE - STEP_SIZE); Y += STEP_SIZE )
{
// Get The (X, Y, Z) Value For The Bottom Left Vertex
x = X;
y = Height(pHeightMap, X, Y );
z = Y;
pHeightMapCoord->point.set1Value(iIndex,x,y,z);
pHeightMapColor->rgb.set1Value(iIndex,0,0,GetVertexColor(pHeightMap,x,y));

x = X;
y = Height(pHeightMap, X, Y + STEP_SIZE );
z = Y + STEP_SIZE ;
pHeightMapCoord->point.set1Value(iIndex + 1,x,y,z);
pHeightMapColor->rgb.set1Value(iIndex + 1,0,0,GetVertexColor(pHeightMap,x,y));

x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE );
z = Y + STEP_SIZE ;
pHeightMapCoord->point.set1Value(iIndex + 2,x,y,z);
pHeightMapColor->rgb.set1Value(iIndex + 2,0,0,GetVertexColor(pHeightMap,x,y));

x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y );
z = Y;
pHeightMapCoord->point.set1Value(iIndex + 3,x,y,z);
pHeightMapColor->rgb.set1Value(iIndex + 3,0,0,GetVertexColor(pHeightMap,x,y));

iVecNum[iIndex / 4] = 4;
iIndex += 4;
}
}
SoFaceSet *pHeightMapFaceSet = new SoFaceSet;
pHeightMapFaceSet->numVertices.setValues(0,63 * 63,iVecNum);
pHeightMapSep->addChild(pHeightMapFaceSet);

return pHeightMapSep;
}


剩下的代码和前面其他课程中的代码类似,请读者自己阅读理解。

现在编译运行我们程序,屏幕会显示一个山地形状的场景。按下上下箭头,可以放大/缩小场景。点击鼠标左键可以切换场景的显示方式。效果和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.csdn.net/RobinHao >
Site: < http://www.openinventor.cn >
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: