-
简介
这节课主要是介绍了怎样读取TGA格式的文件,TGA格式分为压缩和未压缩两种。在OSG中这两种方式的TGA格式都是支持的,因此没有必要像NeHe课程那样去解析。
-
实现
首先创建两种类型的Group节点用来保存使用压缩格式纹理的节点以及未使用压缩格式纹理的节点,实现如下:
osg::Group* createCompressedGroup()
osg::Group* createUnCompressedGroup()
对每一个节点定义它们的更新回调实现动画的效果:
class RotCompressedUpdateCallback : public osg::NodeCallback
{
public:
RotCompressedUpdateCallback(int index) : _index(index){}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osg::MatrixTransform *mt = dynamic_cast<osg::MatrixTransform*>(node);
if (!mt)
return;
mt->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(spin+_index*36.0f),osg::Y_AXIS));
traverse(node, nv);
}
int _index;
};
class RotUnCompressedUpdateCallback : public osg::NodeCallback
{
public:
RotUnCompressedUpdateCallback(int index) : _index(index){}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osg::MatrixTransform *mt = dynamic_cast<osg::MatrixTransform*>(node);
if (!mt)
return;
mt->setMatrix(osg::Matrix::rotate( osg::DegreesToRadians(spin+_index*18.0f) , osg::X_AXIS));
spin += 0.05;
traverse(node, nv);
}
int _index;
};
对于OpenGL中花费大量篇幅解析的过程,我们只需要下面几行代码就可以完成:
osg::Texture2D* createTexture(const char *fileName)
{
osg::Image *textureImage = osgDB::readImageFile(fileName);
osg::Texture2D *texture2D = new osg::Texture2D;
texture2D->setImage(textureImage);
texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
return texture2D;
}
osg::Texture2D* createCompressedTexture()
{
return createTexture("Data/Compressed.tga");
}
osg::Texture2D* createUnCompressedTexture()
{
return createTexture("Data/Uncompressed.tga");
}
最后把所有部分添加到场景根节点中。编译运行程序:
附:本课源码(源码中可能存在错误和不足,仅供参考)
#include "../osgNeHe.h"
#include <QtCore/QTimer>
#include <QtGui/QApplication>
#include <QtGui/QVBoxLayout>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgQt/GraphicsWindowQt>
#include <osg/MatrixTransform>
#include <osg/Texture2D>
float spin;
//
osg::Texture2D* createTexture(const char *fileName)
{
osg::Image *textureImage = osgDB::readImageFile(fileName);
osg::Texture2D *texture2D = new osg::Texture2D;
texture2D->setImage(textureImage);
texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
return texture2D;
}
osg::Texture2D* createCompressedTexture()
{
return createTexture("Data/Compressed.tga");
}
osg::Texture2D* createUnCompressedTexture()
{
return createTexture("Data/Uncompressed.tga");
}
class RotCompressedUpdateCallback : public osg::NodeCallback
{
public:
RotCompressedUpdateCallback(int index) : _index(index){}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osg::MatrixTransform *mt = dynamic_cast<osg::MatrixTransform*>(node);
if (!mt)
return;
mt->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(spin+_index*36.0f),osg::Y_AXIS));
traverse(node, nv);
}
int _index;
};
class RotUnCompressedUpdateCallback : public osg::NodeCallback
{
public:
RotUnCompressedUpdateCallback(int index) : _index(index){}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
osg::MatrixTransform *mt = dynamic_cast<osg::MatrixTransform*>(node);
if (!mt)
return;
mt->setMatrix(osg::Matrix::rotate( osg::DegreesToRadians(spin+_index*18.0f) , osg::X_AXIS));
spin += 0.05;
traverse(node, nv);
}
int _index;
};
osg::Group* createCompressedGroup()
{
osg::Group *groupNode = new osg::Group;
for (int loop=0; loop<20; loop++)
{
osg::MatrixTransform *trans1MT = new osg::MatrixTransform;
trans1MT->setMatrix(osg::Matrix::translate(2, 0, 0));
osg::MatrixTransform *rotMT = new osg::MatrixTransform;
rotMT->addUpdateCallback(new RotCompressedUpdateCallback(loop));
osg::MatrixTransform *trans2MT = new osg::MatrixTransform;
trans2MT->setMatrix(osg::Matrix::translate(1,0,0));
osg::Geode *quadGeode = new osg::Geode;
osg::Geometry *quadGeometry = new osg::Geometry;
osg::Vec3Array *quadVertexArray = new osg::Vec3Array;
osg::Vec2Array *textureArray = new osg::Vec2Array;
quadVertexArray->push_back(osg::Vec3(-1.0f, 1.0f, 0.0f));
quadVertexArray->push_back(osg::Vec3(1.0f, 1.0f, 0.0f));
quadVertexArray->push_back(osg::Vec3(1.0f, -1.0f, 0.0f));
quadVertexArray->push_back(osg::Vec3(-1.0f, -1.0f, 0.0f));
textureArray->push_back(osg::Vec2(0.0f, 0.0f));
textureArray->push_back(osg::Vec2(1.0f, 0.0f));
textureArray->push_back(osg::Vec2(1.0f, 1.0f));
textureArray->push_back(osg::Vec2(0.0f, 1.0f));
quadGeometry->setVertexArray(quadVertexArray);
quadGeometry->setTexCoordArray(0, textureArray);
quadGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));
quadGeometry->getOrCreateStateSet()->setTextureAttributeAndModes(0, createCompressedTexture());
quadGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
quadGeode->addDrawable(quadGeometry);
trans1MT->addChild(rotMT);
rotMT->addChild(trans2MT);
trans2MT->addChild(quadGeode);
groupNode->addChild(trans1MT);
}
return groupNode;
}
osg::Group* createUnCompressedGroup()
{
osg::Group *groupNode = new osg::Group;
for (int loop=0; loop<20; loop++)
{
osg::MatrixTransform *rotMT = new osg::MatrixTransform;
rotMT->addUpdateCallback(new RotUnCompressedUpdateCallback(loop));
osg::MatrixTransform *trans2MT = new osg::MatrixTransform;
trans2MT->setMatrix(osg::Matrix::translate(-2.0f,2.0f,0.0f));
osg::Geode *quadGeode = new osg::Geode;
osg::Geometry *quadGeometry = new osg::Geometry;
osg::Vec3Array *quadVertexArray = new osg::Vec3Array;
osg::Vec2Array *textureArray = new osg::Vec2Array;
quadVertexArray->push_back(osg::Vec3(-1.0f, 1.0f, 0.0f));
quadVertexArray->push_back(osg::Vec3(1.0f, 1.0f, 0.0f));
quadVertexArray->push_back(osg::Vec3(1.0f, -1.0f, 0.0f));
quadVertexArray->push_back(osg::Vec3(-1.0f, -1.0f, 0.0f));
textureArray->push_back(osg::Vec2(0.0f, 0.0f));
textureArray->push_back(osg::Vec2(1.0f, 0.0f));
textureArray->push_back(osg::Vec2(1.0f, 1.0f));
textureArray->push_back(osg::Vec2(0.0f, 1.0f));
quadGeometry->setVertexArray(quadVertexArray);
quadGeometry->setTexCoordArray(0, textureArray);
quadGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));
quadGeometry->getOrCreateStateSet()->setTextureAttributeAndModes(0, createUnCompressedTexture());
quadGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
quadGeode->addDrawable(quadGeometry);
rotMT->addChild(trans2MT);
trans2MT->addChild(quadGeode);
groupNode->addChild(rotMT);
}
return groupNode;
}
class ViewerWidget : public QWidget, public osgViewer::Viewer
{
public:
ViewerWidget(osg::Node *scene = NULL)
{
QWidget* renderWidget = getRenderWidget( createGraphicsWindow(0,0,100,100), scene);
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(renderWidget);
layout->setContentsMargins(0, 0, 0, 1);
setLayout( layout );
connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
_timer.start( 10 );
}
QWidget* getRenderWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene )
{
osg::Camera* camera = this->getCamera();
camera->setGraphicsContext( gw );
const osg::GraphicsContext::Traits* traits = gw->getTraits();
camera->setClearColor( osg::Vec4(0.0, 0.0, 0.0, 1.0) );
camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
camera->setProjectionMatrixAsPerspective(45.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 0.1f, 100.0f );
camera->setViewMatrixAsLookAt(osg::Vec3d(0, 0, 1), osg::Vec3d(0, 0, 0), osg::Vec3d(0, 1, 0));
this->setSceneData( scene );
return gw->getGLWidget();
}
osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false )
{
osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->windowName = name;
traits->windowDecoration = windowDecoration;
traits->x = x;
traits->y = y;
traits->width = w;
traits->height = h;
traits->doubleBuffer = true;
traits->alpha = ds->getMinimumNumAlphaBits();
traits->stencil = ds->getMinimumNumStencilBits();
traits->sampleBuffers = ds->getMultiSamples();
traits->samples = ds->getNumMultiSamples();
return new osgQt::GraphicsWindowQt(traits.get());
}
virtual void paintEvent( QPaintEvent* event )
{
frame();
}
protected:
QTimer _timer;
};
osg::Node* buildScene()
{
osg::Group *root = new osg::Group;
osg::MatrixTransform *zoomMT = new osg::MatrixTransform;
zoomMT->setMatrix(osg::Matrix::translate(0, 0, -10));
zoomMT->addChild(createUnCompressedGroup());
zoomMT->addChild(createCompressedGroup());
root->addChild(zoomMT);
return root;
}
int main( int argc, char** argv )
{
QApplication app(argc, argv);
ViewerWidget* viewWidget = new ViewerWidget(buildScene());
viewWidget->setGeometry( 100, 100, 640, 480 );
viewWidget->show();
return app.exec();
}