NeHe这节课主要讨论OpenGL怎样给文字赋予纹理贴图。OpenGL使用glTexGen函数给文字自动生成纹理坐标。在OSG中,使用osg::TexGen这个继承自StateAttribute的类来实现相同的效果
首先将创建3D的字体,代码如下:
osg::Geode *fontGeode = new osg::Geode; osg::Image *textureImage = osgDB::readImageFile("Data/Lights.bmp"); osg::Texture2D *texture2D = new osg::Texture2D; texture2D->setImage(textureImage); texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST); texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST); osg::TexGen *texGen = new osg::TexGen; texGen->setMode(osg::TexGen::OBJECT_LINEAR); osgText::Text3D *text = new osgText::Text3D; text->setCharacterSize(3.0f); text->setCharacterDepth(0.5f); text->setText("N"); text->setFont("fonts/Wingding.ttf"); text->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2D); fontGeode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texGen); fontGeode->addDrawable(text);设置字体为系统中的Wingding.tff字体,就可以实现骷髅头的效果。
可以看到我们按照NeHe教程中的实现方式,将texGen状态属性添加到字体Drawable之中,实现了纹理的自动计算
编译运行程序:
附:本课源码(源码中可能存在错误和不足,仅供参考)
#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 <osgDB/ReadFile> #include <osgText/Text> #include <osgText/Text3D> #include <osg/AnimationPath> #include <iostream> #include <sstream> #include <osg/TexGen> float rot = 0.0; class FontColorCallback : public osg::Drawable::UpdateCallback { public: virtual void update(osg::NodeVisitor*, osg::Drawable* drawable) { if (dynamic_cast<osgText::Text3D*>(drawable)) { osgText::Text3D *text = dynamic_cast<osgText::Text3D*>(drawable); text->setText("N"); rot+=0.5f; } } }; 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 *moveMT = new osg::MatrixTransform; moveMT->setMatrix(osg::Matrix::translate(0, 0, -10)); root->addChild(moveMT); osg::MatrixTransform *rotX = new osg::MatrixTransform; rotX->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::X_AXIS, 0.5)); osg::MatrixTransform *rotY = new osg::MatrixTransform; rotY->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Y_AXIS, 0.4)); osg::MatrixTransform *rotZ = new osg::MatrixTransform; rotZ->setUpdateCallback(new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Z_AXIS, 0.3)); moveMT->addChild(rotX); rotX->addChild(rotY); rotY->addChild(rotZ); osg::Geode *fontGeode = new osg::Geode; osg::Image *textureImage = osgDB::readImageFile("Data/Lights.bmp"); osg::Texture2D *texture2D = new osg::Texture2D; texture2D->setImage(textureImage); texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST); texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST); osg::TexGen *texGen = new osg::TexGen; texGen->setMode(osg::TexGen::OBJECT_LINEAR); osgText::Text3D *text = new osgText::Text3D; text->setCharacterSize(3.0f); text->setCharacterDepth(0.5f); text->setText("N"); text->setFont("fonts/Wingding.ttf"); text->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2D); fontGeode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texGen); fontGeode->addDrawable(text); rotZ->addChild(fontGeode); 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(); }