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
- OSG(美国海军NPS)教程学习加实践(2)
- 美国海军(NPS)的OSG教程链接与资源下载
- 美国海军(NPS)的OSG教程——示例代码一
- 美国海军研究生学校(NPS)OSG教程学习笔记——第一课
- 美国海军研究生学校(NPS)OSG教程学习笔记——第二课
- 美国海军研究生学校(NPS)OSG教程学习笔记——第三课
- 美国海军osg教程
- 美国海军osg教程
- 《NPS Training》学习分享
- OSG学习:裁剪变换(2)
- OSG学习笔记(一)
- osg学习笔记(一)
- 个人网站学习实践(wordpress教程)
- OSG OpenThreads 线程学习(1)
- osg学习第二篇:矩阵(二)
- OSG学习:裁剪变换(1)
- OSG 网上学习教程 较全
- osg for android 编译(Windows)完整图文教程
- Wakelock
- 欢迎使用CSDN-markdown编辑器
- 设计模式——观察者模式
- UML中几种类间关系:继承、实现、依赖、关联、聚合、组合的联系与区别
- Learn C Pointers
- OSG(美国海军NPS)教程学习加实践(2)
- html 5视频和音频
- 数据结构实验之队列一:排队买饭
- 剑指offer55--之字形式打印二叉树
- 新瓶子装老酒!!!
- Activity的生命周期和启动模式
- PHP比较常见的五大运行模式详解
- Django开发博客(十四)—用户登录和站点统计
- Bag of Tricks for Efficient Text Classification