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

用OpenInventor实现的NeHe OpenGL教程-第二十九课

2016-04-05 02:01 507 查看

用OpenInventor实现的NeHe OpenGL教程-第二十九课





NeHe教程在这节课中介绍了如何把一块纹理贴到另一块纹理上(即多重纹理)。这节课的内容不是很难,主要是如何读取raw数据格式文件,如何计算图像的透明度等。

OpenInventor对多重纹理直接提供支持,所以我们也可以利用多重纹理来实现相同的效果。具体的代码见BuildCube2

下面定义的是raw文件的数据格式,和NeHe教程完全一致
typedef struct
{
intwidth; // Width Of Image In Pixels
intheight; // Height Of Image In Pixels
intformat; // Number Of Bytes Per Pixel
unsigned char *data; // Texture Data
} TEXTURE_IMAGE;
typedef TEXTURE_IMAGE *P_TEXTURE_IMAGE; // A Pointer To The Texture Image Data Type
P_TEXTURE_IMAGE t1; // Pointer To The Texture Image Data Type
P_TEXTURE_IMAGE t2; // Pointer To The Texture Image Data Type

下面的函数用来分配纹理内存,和NeHe教程完全一致
P_TEXTURE_IMAGE AllocateTextureBuffer( int w, int h, int f)
{
…….
}

下面的函数用来从文件中读纹理数据,和NeHe教程完全一致
int ReadTextureData ( char *filename, P_TEXTURE_IMAGE buffer)
{
…..
}

下面的函数用来计算混合后的纹理数据,和NeHe教程完全一致
void Blit( P_TEXTURE_IMAGE src, P_TEXTURE_IMAGE dst, int src_xstart, int src_ystart,
int src_width, int src_height,int dst_xstart, int dst_ystart, int blend, int alpha)
{
…….
}

创建立方体场景
void BuildCube1(void)
{
t1 = AllocateTextureBuffer( 256, 256, 4 ); // Get An Image Structure
if (ReadTextureData("../Data/Monitor.raw",t1)==0) // Fill The Image Structure With Data
{ // Nothing Read?
MessageBox(NULL,"Could Not Read 'Monitor.raw' Image Data","TEXTURE ERROR",
MB_OK | MB_ICONINFORMATION);
return;
}
t2 = AllocateTextureBuffer( 256, 256, 4 ); // Second Image Structure
if (ReadTextureData("../Data/GL.raw",t2)==0) // Fill The Image Structure With Data
{ // Nothing Read?
MessageBox(NULL,"Could Not Read 'GL.raw' Image Data","TEXTURE ERROR",
MB_OK | MB_ICONINFORMATION);
return;
}
// Image To Blend In, Original Image, Src Start X & Y, Src Width & Height, Dst Location X & Y, Blend Flag, Alpha Value
Blit(t2,t1,127,127,128,128,64,64,1,127); / Call The Blitter Routine

SoTexture2 *Texture1 = new SoTexture2;
Texture1->image.setValue(SbVec2s(t1->width,t1->height),t1->format,t1->data);
Texture1->model = SoTexture2::DECAL;
g_pOivSceneRoot->addChild(Texture1);

DeallocateTexture( t1 ); // Clean Up Image Memory Because Texture Is
DeallocateTexture( t2 ); // In GL Texture Memory Now

g_pOivSceneRoot->addChild(new SoCube);
}

下面的函数是利用OpenInventor的多重纹理方法创建的场景
void BuildCube2(void)
{
t1 = AllocateTextureBuffer( 256, 256, 4 ); // Get An Image Structure
if (ReadTextureData("../Data/Monitor.raw",t1)==0) // Fill The Image Structure With Data
{ // Nothing Read?
MessageBox(NULL,"Could Not Read 'Monitor.raw' Image Data","TEXTURE ERROR",
MB_OK | MB_ICONINFORMATION);
return;
}
SoTexture2 *Texture1 = new SoTexture2;
Texture1->image.setValue(SbVec2s(t1->width,t1->height),t1->format,t1->data);
Texture1->model = SoTexture2::DECAL;
g_pOivSceneRoot->addChild(Texture1);

SoTextureUnit * tu = new SoTextureUnit;
tu->unit = 1;
g_pOivSceneRoot->addChild(tu);

SoTexture2 *Texture2 = new SoTexture2;
Texture2->filename.setValue("../Data/NeHe.png");
Texture2->model = SoTexture2::BLEND;
g_pOivSceneRoot->addChild(Texture2);
g_pOivSceneRoot->addChild(new SoCube);
DeallocateTexture( t1 ); // Clean Up Image Memory Because Texture Is
}

创建整个场景
void BuildScene(void)
{
SoRotor *XRotor = new SoRotor;
XRotor->rotation.setValue(SbVec3f(1,0,0),1);
XRotor->speed = 0.1;
g_pOivSceneRoot->addChild(XRotor);

SoRotor *YRotor = new SoRotor;
YRotor->rotation.setValue(SbVec3f(0,1,0),1);
YRotor->speed = 0.1;
g_pOivSceneRoot->addChild(YRotor);

SoRotor *ZRotor = new SoRotor;
ZRotor->rotation.setValue(SbVec3f(0,0,1),1);
ZRotor->speed = 0.1;
g_pOivSceneRoot->addChild(ZRotor);

BuildCube1();
//BuildCube2();
}

剩下的代码和以前的课程类似,我们就不在详细介绍了。

现在编译运行我们程序,屏幕会显示一个表面带有贴图的立方体。效果和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 >
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: