本节课要实现的内容是为立方体的六个面添加纹理
纹理映射(Texture Mapping)是OpenGL程序开发中的一个重要概念。纹理是一个数组的概念,其中每一个数据(RGB颜色以及Alpha值,或者其他系统及用户定义的类型)称为一个纹素(texel)。在描绘具有真实感物体时,使用一幅真实拍摄的照片作为纹理贴到几何体的表面,可以大大丰富物体的表现效果。
OSG使用派生自StateAttribute的Texture类来定义一个纹理对象,其6个子类对象(Texture1D Texture2D等)分别表示一维纹理、二维纹理等
首先我们先要将纹理坐标加入到几何体Geometry的纹理数组之中
float textureVertices[][2] = { //Front Face {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}, // Back Face {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}, {0.0f, 0.0f}, // Top Face {0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, // Bottom Face {1.0f, 1.0f}, {0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 0.0f}, // Right face {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}, {0.0f, 0.0f}, // Left Face {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f} };
osg::Vec2Array* texcoords = new osg::Vec2Array; for (unsigned i = 0; i < sizeof(textureVertices); ++i) { texcoords->push_back(osg::Vec2(textureVertices[i][0], textureVertices[i][1])); } quadGeometry->setTexCoordArray(0,texcoords);接着我们声明一个二维纹理对象Texture2D,并按NeHe第六课的要求对二维纹理作一些设置
osg::Image *textureImage = osgDB::readImageFile("Data/NeHe.bmp"); 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);在NeHe第六课中用到了auxDIBImageLoad函数来读取一个bmp类型的图片,在OSG中我们不需要这么做,OSG实现了强大的文件读写机制,通过插件的方式提供了各种模型,图片和其他类型文件的解析和转换支持,并可以很方便地进行扩展。OSG目前支持的图片格式包括bmp、dds、jpg、jpg2、rgb、svg、tga、tiff等等,在这里我们直接使用osgDB库中的readImageFile来加载bmp图片作为纹理使用
最后将纹理设置给节点
quadGeode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2D);
附:本课源码(源码中可能存在错误和不足,仅供参考)
#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/NodeVisitor> #include <osg/AnimationPath> #include <osg/Texture2D> float textureVertices[][2] = { //Front Face {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}, // Back Face {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}, {0.0f, 0.0f}, // Top Face {0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, // Bottom Face {1.0f, 1.0f}, {0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 0.0f}, // Right face {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}, {0.0f, 0.0f}, // Left Face {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f} }; float QuadVertices[][3] = { {-1.0f, -1.0f, 1.0f}, { 1.0f, -1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f}, {-1.0f, 1.0f, 1.0f}, {-1.0f, -1.0f, -1.0f}, {-1.0f, 1.0f, -1.0f}, { 1.0f, 1.0f, -1.0f}, { 1.0f, -1.0f, -1.0f}, {-1.0f, 1.0f, -1.0f}, {-1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f}, { 1.0f, 1.0f, -1.0f}, {-1.0f, -1.0f, -1.0f}, { 1.0f, -1.0f, -1.0f}, { 1.0f, -1.0f, 1.0f}, {-1.0f, -1.0f, 1.0f}, { 1.0f, -1.0f, -1.0f}, { 1.0f, 1.0f, -1.0f}, { 1.0f, 1.0f, 1.0f}, { 1.0f, -1.0f, 1.0f}, {-1.0f, -1.0f, -1.0f}, {-1.0f, -1.0f, 1.0f}, {-1.0f, 1.0f, 1.0f}, {-1.0f, 1.0f, -1.0f} }; 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 *quadMT = new osg::MatrixTransform; quadMT->setMatrix(osg::Matrix::translate(0.0, 0.0, -5.0)); osg::AnimationPathCallback *quadAnimationCallback = new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Vec3(1, 1, 1), 2.5); osg::MatrixTransform *quadRotMT = new osg::MatrixTransform; quadRotMT->setUpdateCallback(quadAnimationCallback); osg::Geometry *quadGeometry = new osg::Geometry; osg::Vec3Array *quadVertexArray = new osg::Vec3Array; for (unsigned i = 0; i < sizeof(QuadVertices); ++i) { quadVertexArray->push_back(osg::Vec3(QuadVertices[i][0], QuadVertices[i][1], QuadVertices[i][2])); } quadGeometry->setVertexArray(quadVertexArray); osg::Vec2Array* texcoords = new osg::Vec2Array; for (unsigned i = 0; i < sizeof(textureVertices); ++i) { texcoords->push_back(osg::Vec2(textureVertices[i][0], textureVertices[i][1])); } quadGeometry->setTexCoordArray(0,texcoords); int first = 0; for (unsigned i = 0; i < 6; ++i) { osg::DrawArrays *vertexIndices = new osg::DrawArrays(osg::PrimitiveSet::QUADS, first, 4); first += 4; quadGeometry->addPrimitiveSet(vertexIndices); } osg::Image *textureImage = osgDB::readImageFile("Data/NeHe.bmp"); 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); osg::Geode *quadGeode = new osg::Geode; quadGeode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2D); quadGeode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); quadGeode->addDrawable(quadGeometry); quadRotMT->addChild(quadGeode); quadMT->addChild(quadRotMT); root->addChild(quadMT); 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(); }