OSG(美国海军NPS)教程学习加实践(2)

来源:互联网 发布:exe软件修改工具 编辑:程序博客网 时间:2024/05/16 07:50
在编程开始前要认识一下*.tga后缀的文件:

TGA格式(Tagged Graphics)是由美国Truevision公司为其显示卡开发的一种图像文件格式,文件后缀为“.tga”,已被国际上的图形、图像工业所接受。

  TGA的结构比较简单,属于一种图形、图像数据的通用格式,在多媒体领域有很大影响,是计算机生成图像向电视转换的一种首选格式。

  TGA图像格式最大的特点是可以做出不规则形状的图形、图像文件,一般图形、图像文件都为四方形,若需要有圆形、菱形甚至是缕空的图像文件时,TGA可就派上用场了!

  TGA格式支持压缩,使用不失真的压缩算法。

  在工业设计领域,使用三维软件制作出来的图像可以利用TGA格式的优势,在图像内部生成一个Alpha(通道),这个功能方便了在平面软件中的工作。

==================================================================================

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/ref_ptr>
#include <osg/Geometry>
#include <osg/PositionAttitudeTransform>
#include <osg/Geode>
#include <osg/Group>
#include <osg/Texture2D>
#include <iostream>
using namespace std;


osg::Geode* createPyramid()
{
   osg::Geode* pyramidGeode = new osg::Geode();
   osg::Geometry* pyramidGeometry = new osg::Geometry();
   pyramidGeode->addDrawable(pyramidGeometry);

   // 指定顶点
   osg::Vec3Array* pyramidVertices = new osg::Vec3Array;
   pyramidVertices->push_back( osg::Vec3(0, 0, 0) ); // 左前 
   pyramidVertices->push_back( osg::Vec3(2, 0, 0) ); // 右前 
   pyramidVertices->push_back( osg::Vec3(2, 2, 0) ); // 右后 
   pyramidVertices->push_back( osg::Vec3( 0,2, 0) ); // 左后 
   pyramidVertices->push_back( osg::Vec3( 1, 1,2) ); // 塔尖

   // 将顶点数组关联给几何体
   pyramidGeometry->setVertexArray( pyramidVertices );

   // 根据底面的四个顶点创建底面四边形(QUAD)
   osg::DrawElementsUInt* pyramidBase = 
      new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
   pyramidBase->push_back(3);
   pyramidBase->push_back(2);
   pyramidBase->push_back(1);
   pyramidBase->push_back(0);

   // 创建其他面的代码从略
   osg::Vec4Array* colors = new osg::Vec4Array;
   colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) ); //索引0 红色
   colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) ); //索引1 绿色
   colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) ); //索引2 蓝色
   colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); //索引3 白色

   osg::TemplateIndexArray
      <unsigned int, osg::Array::UIntArrayType,4,4> *colorIndexArray;
   colorIndexArray = 
      new osg::TemplateIndexArray<unsigned int, osg::Array::UIntArrayType,4,4>;
   colorIndexArray->push_back(0); // 顶点0对应颜色元素0
   colorIndexArray->push_back(1); // 顶点1对应颜色元素1
   colorIndexArray->push_back(2); // 顶点2对应颜色元素2
   colorIndexArray->push_back(3); // 顶点3对应颜色元素3
   colorIndexArray->push_back(0); // 顶点4对应颜色元素0

   pyramidGeometry->setColorArray(colors);
   pyramidGeometry->setColorIndices(colorIndexArray);
   pyramidGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);

   osg::Vec2Array* texcoords = new osg::Vec2Array(5);
   (*texcoords)[0].set(0.00f,0.0f); // 顶点0的纹理坐标
   (*texcoords)[1].set(0.25f,0.0f); // 顶点1的纹理坐标
   (*texcoords)[2].set(0.50f,0.0f); // 顶点2的纹理坐标
   (*texcoords)[3].set(0.75f,0.0f); // 顶点3的纹理坐标
   (*texcoords)[4].set(0.50f,1.0f); // 顶点4的纹理坐标
   pyramidGeometry->setTexCoordArray(0,texcoords);

   return pyramidGeode;
}

int main()
{
 // 声明场景的根节点
   osg::Group* root = new osg::Group();
   osg::Geode* pyramidGeode = createPyramid();
   root->addChild(pyramidGeode);

   osg::Texture2D* KLN89FaceTexture = new osg::Texture2D;

   //避免在优化过程中出错
   KLN89FaceTexture->setDataVariance(osg::Object::DYNAMIC);

   // 从文件读取图片
   osg::Image* klnFace = osgDB::readImageFile("KLN89FaceB.tga");
   
   if (!klnFace)
   {
       printf(" couldn't find texture, quiting.");//输出错误
    system("pause");
       return -1;
   }

   //  将图片关联到纹理
   KLN89FaceTexture->setImage(klnFace);


   // 创建StateSet
   osg::StateSet* stateOne = new osg::StateSet();

   // 将纹理关联给StateSet的纹理单元0
   stateOne->setTextureAttributeAndModes
      (0,KLN89FaceTexture,osg::StateAttribute::ON);
   //  将渲染状态关联给金字塔节点
   pyramidGeode->setStateSet(stateOne);

   osgViewer::Viewer viewer;

   //最后我们进入仿真循环:
   viewer.setSceneData( root );

   return viewer.run();

}
0 0
原创粉丝点击