现在的位置: 首页 > 综合 > 正文

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

2013年08月13日 ⁄ 综合 ⁄ 共 6144字 ⁄ 字号 评论关闭
 

OpenInventor实现的NeHe OpenGL教程-第十七课

 

      

 

这节课我们将讨论使用纹理贴图的技术来显示文字。使用这种技术显示文字的好处在于程序的效果在任何机器上都是相同的。而我们前面讨论的显示文字的方法都是依赖于当前系统中所包含的字体,所以前面讨论的技术在不同的机器上有可能会有不同的显示效果。

 

使用纹理贴图来显示文字的原理很简单,首先需要制作一个包含所有需要显示的文字的图片,将这个图片作为纹理加载到内存中,然后根据要显示的文字,找到其所对应的纹理坐标,最后显示一个矩形平面,对平面的四个顶点赋值上纹理坐标,这样就完成了文字的显示。

 

 

       我们首先要仿照NeHe教程,创建一个旋转的“墙”场景。

SoSeparator* BuildWall(void)

{

     SoSeparator *pWallScene = new SoSeparator;

 

     //Z轴负方向,即屏幕向里方向移动5个单位,和NeHe教程相同。

     SoTranslation *pZTrans = new SoTranslation;

     pZTrans->translation.setValue(0.0f,0.0f,- 5.0f);

     pWallScene->addChild(pZTrans);

 

     //Z轴旋转45

     SoRotation *pZRotation = new SoRotation;

     pZRotation->rotation.setValue(SbVec3f(0,0,1),3.1415 / 4.0);

     pWallScene->addChild(pZRotation);

 

     //让物体同时绕x轴和y轴旋转,这样物体就象在一个点上旋转的钻石那样旋转

     SoRotor *pRotor = new SoRotor;

     pRotor->rotation.setValue(SbVec3f(1,1,0),1);

     pRotor->speed = 0.1;

     pWallScene->addChild(pRotor);

     //加载纹理位图

     SoTexture2 *pBumpTexture = new SoTexture2;

     pBumpTexture->filename.setValue("../Data/Bumps.png");

     pBumpTexture->model = SoTexture2::DECAL;

     pWallScene->addChild(pBumpTexture);

     //定义墙的位置坐标数据

     float vecWallCoord[][3] = { {-100.0f, 100.0f, 0.0f},

                                     {100.0f, 100.0f, 0.0f},

                                     {100.0f, -100.0f, 0.0f},

                                     {-100.0f, -100.0f, 0.0f} };

     SoCoordinate3 *pWallCoords = new SoCoordinate3;

     pWallCoords->point.setValues(0,4,vecWallCoord);

     pWallScene->addChild(pWallCoords);

 

     SoFaceSet *pWallFaceSet = new SoFaceSet;

     pWallScene->addChild(pWallFaceSet);

     //旋转坐标系

     SoRotation *pRotation = new SoRotation;

     pRotation->rotation.setValue(SbVec3f(1,1,0),3.1415 / 2.0);

     pWallScene->addChild(pRotation);

     //添加另外一个“墙”

     pWallScene->addChild(pWallFaceSet);

 

     return pWallScene;

}

 

下面的函数根据输入的字符串,使用纹理来创建字符串的场景,纹理坐标的计算方法和NeHe教程中相同。

SoSeparator* OivPrint(char *string,int set)

{

     if(lstrlen(string) <= 0)

         return NULL;

 

     float    cx,cy;

     SoCoordinate3 *pStringCoords = new SoCoordinate3;

     SoTextureCoordinate2 *pTexCoord = new SoTextureCoordinate2;

     for(int i = 0; i < lstrlen(string); i++)

     {

         pStringCoords->point.set1Value( i * 4 + 0, i * 16, 0, 0 );

         pStringCoords->point.set1Value( i * 4 + 1, (i + 1) * 16, 0, 0 );

         pStringCoords->point.set1Value( i * 4 + 2, (i + 1) * 16, 16, 0 );

         pStringCoords->point.set1Value( i * 4 + 3, i * 16, 16, 0 );

 

         cx = float((string[i] - 32 + 128 * set) % 16) / 16.0f;

         cy = float((string[i] - 32 + 128 * set) / 16) / 16.0f;

         pTexCoord->point.set1Value( i * 4 + 0,cx,1 - cy - 0.0625f);

         pTexCoord->point.set1Value( i * 4 + 1,cx + 0.0625f,1 - cy - 0.0625f);

         pTexCoord->point.set1Value( i * 4 + 2,cx + 0.0625f,1 - cy);

         pTexCoord->point.set1Value( i * 4 + 3,cx,1 - cy);

     }

 

     int iIndex = 0;

     SoIndexedFaceSet *pTextFaceSet = new SoIndexedFaceSet;

     for(int i = 0; i < lstrlen(string); i++)

     {

         pTextFaceSet->coordIndex.set1Value(iIndex,i * 4);

         pTextFaceSet->textureCoordIndex.set1Value(iIndex++,i * 4);

 

         pTextFaceSet->coordIndex.set1Value(iIndex,i * 4 + 1);

         pTextFaceSet->textureCoordIndex.set1Value(iIndex++,i * 4 + 1);

 

         pTextFaceSet->coordIndex.set1Value(iIndex,i * 4 + 2);

         pTextFaceSet->textureCoordIndex.set1Value(iIndex++,i * 4 + 2);

 

         pTextFaceSet->coordIndex.set1Value(iIndex,i * 4 + 3);

         pTextFaceSet->textureCoordIndex.set1Value(iIndex++,i * 4 + 3);

 

         pTextFaceSet->coordIndex.set1Value(iIndex,SO_END_FACE_INDEX);

         pTextFaceSet->textureCoordIndex.set1Value(iIndex++,-1);

     }

 

     SoSeparator *pStringScene = new SoSeparator;

     pStringScene->addChild(pStringCoords);

     pStringScene->addChild(pTexCoord);

     pStringScene->addChild(pTextFaceSet);

 

     return pStringScene;

}

 

下面的函数将创建三个运动的字符串,字符串运动轨迹是通过OpenInventor的数学引擎计算出来的。

SoSeparator* BuildFont(void)

{   

     SoSeparator *pFontScene = new SoSeparator;

 

     SoCallback *pGlCallback = new SoCallback();

     pGlCallback->setCallback(GlCB, 0);

     pFontScene->addChild(pGlCallback);

 

     SoTexture2 *pFontTexture = new SoTexture2;

     pFontTexture->filename.setValue("../Data/Font.png");

     pFontTexture->model = SoTexture2::MODULATE;

     pFontScene->addChild(pFontTexture);

 

     SoTimeCounter *pCnt1Counter = new SoTimeCounter;

     pCnt1Counter->max = 360;

     pCnt1Counter->step = 1;

     pCnt1Counter->frequency = .2;

     SoTimeCounter *pCnt2Counter = new SoTimeCounter;

     pCnt2Counter->max = 360;

     pCnt2Counter->step = 1;

     pCnt2Counter->frequency = .4;

 

     SoSeparator *pText1Sep = OivPrint("NeHe",0);

     if(pText1Sep != NULL)

     {

         SoTranslation *pTransl1 = new SoTranslation;

         pText1Sep->insertChild(pTransl1,0);

         SoMaterial *pText1Material = new SoMaterial;

         pText1Material->diffuseColor.setValue(1.0,0.0,0.0);

         pText1Material->transparency = 0.1;

         pText1Sep->insertChild(pText1Material,0);

 

         SoCalculator *pText1PosCalc = new SoCalculator;

         pText1PosCalc->a.connectFrom(&pCnt1Counter->output);   

         pText1PosCalc->b.connectFrom(&pCnt2Counter->output);   

         pText1PosCalc->expression.set1Value(0, "ta = a * M_PI / 180");

         pText1PosCalc->expression.set1Value(1, "tb = 28 + 120 * cos(ta)");

         pText1PosCalc->expression.set1Value(2, "td = b * M_PI / 180");

         pText1PosCalc->expression.set1Value(3, "te = 23.5 + 100 * sin(td)");

         pText1PosCalc->expression.set1Value(4, "oA = vec3f(tb,te,0)");

         pTransl1->translation.connectFrom(&pText1PosCalc->oA);

 

         SoCalculator *pText1ColorCalc = new SoCalculator;

         pText1ColorCalc->a.connectFrom(&pCnt1Counter->output);   

         pText1ColorCalc->b.connectFrom(&pCnt2Counter->output);   

         pText1ColorCalc->expression.set1Value(0, "ta = a * M_PI / 180");

         pText1ColorCalc->expression.set1Value(1, "tb = cos(ta)");

         pText1ColorCalc->expression.set1Value(2, "td = b * M_PI / 180");

         pText1ColorCalc->expression.set1Value(3, "te = sin(td)");

         pText1ColorCalc->expression.set1Value(4, "tf = 1.0 - 0.5 * cos(ta + td)"); 

         pText1ColorCalc->expression.set1Value(5, "oA = vec3f(tb,te,tf)");

         pText1Material->diffuseColor.connectFrom(&pText1ColorCalc->oA);

 

         pFontScene->addChild(pText1Sep);

     }

 

     SoSeparator *pText2Sep = OivPrint("OpenGL",1);

     if(pText2Sep != NULL)

     {

         SoTranslation *pTransl2 = new SoTranslation;

         pText2Sep->insertChild(pTransl2,0);

         SoMaterial *pText2Material = new SoMaterial;

         pText2Material->diffuseColor.setValue(1.0,0.0,0.0);

抱歉!评论已关闭.