OpenSceneGraph实现的NeHe OpenGL教程 - 第六课

  • 简介

本节课要实现的内容是为立方体的六个面添加纹理

纹理映射(Texture Mapping)是OpenGL程序开发中的一个重要概念。纹理是一个数组的概念,其中每一个数据(RGB颜色以及Alpha值,或者其他系统及用户定义的类型)称为一个纹素(texel)。在描绘具有真实感物体时,使用一幅真实拍摄的照片作为纹理贴到几何体的表面,可以大大丰富物体的表现效果。

OSG使用派生自StateAttribute的Texture类来定义一个纹理对象,其6个子类对象(Texture1D Texture2D等)分别表示一维纹理、二维纹理等

  • 实现

首先我们先要将纹理坐标加入到几何体Geometry的纹理数组之中

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. float textureVertices[][2] = {  
  2.     //Front Face  
  3.     {0.0f, 0.0f},  
  4.     {1.0f, 0.0f},  
  5.     {1.0f, 1.0f},  
  6.     {0.0f, 1.0f},  
  7.     // Back Face  
  8.     {1.0f, 0.0f},  
  9.     {1.0f, 1.0f},  
  10.     {0.0f, 1.0f},  
  11.     {0.0f, 0.0f},  
  12.     // Top Face  
  13.     {0.0f, 1.0f},  
  14.     {0.0f, 0.0f},  
  15.     {1.0f, 0.0f},  
  16.     {1.0f, 1.0f},  
  17.     // Bottom Face  
  18.     {1.0f, 1.0f},  
  19.     {0.0f, 1.0f},  
  20.     {0.0f, 0.0f},  
  21.     {1.0f, 0.0f},  
  22.     // Right face  
  23.     {1.0f, 0.0f},  
  24.     {1.0f, 1.0f},  
  25.     {0.0f, 1.0f},  
  26.     {0.0f, 0.0f},  
  27.     // Left Face  
  28.     {0.0f, 0.0f},  
  29.     {1.0f, 0.0f},  
  30.     {1.0f, 1.0f},  
  31.     {0.0f, 1.0f}  
  32. };  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. osg::Vec2Array* texcoords = new osg::Vec2Array;  
  2. for (unsigned i = 0; i < sizeof(textureVertices); ++i)  
  3. {  
  4.     texcoords->push_back(osg::Vec2(textureVertices[i][0], textureVertices[i][1]));  
  5. }  
  6. quadGeometry->setTexCoordArray(0,texcoords);  
接着我们声明一个二维纹理对象Texture2D,并按NeHe第六课的要求对二维纹理作一些设置

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. osg::Image *textureImage = osgDB::readImageFile("Data/NeHe.bmp");  
  2. osg::Texture2D *texture2D = new osg::Texture2D;  
  3. texture2D->setImage(textureImage);  
  4. texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);  
  5. 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图片作为纹理使用

最后将纹理设置给节点

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. quadGeode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2D);  

编译运行程序

附:本课源码(源码中可能存在错误和不足,仅供参考)

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "../osgNeHe.h"  
  2.   
  3. #include <QtCore/QTimer>  
  4. #include <QtGui/QApplication>  
  5. #include <QtGui/QVBoxLayout>  
  6.   
  7. #include <osgViewer/Viewer>  
  8. #include <osgDB/ReadFile>  
  9. #include <osgQt/GraphicsWindowQt>  
  10.   
  11. #include <osg/MatrixTransform>  
  12. #include <osg/NodeVisitor>  
  13. #include <osg/AnimationPath>  
  14.   
  15. #include <osg/Texture2D>  
  16.   
  17. float textureVertices[][2] = {  
  18.     //Front Face  
  19.     {0.0f, 0.0f},  
  20.     {1.0f, 0.0f},  
  21.     {1.0f, 1.0f},  
  22.     {0.0f, 1.0f},  
  23.     // Back Face  
  24.     {1.0f, 0.0f},  
  25.     {1.0f, 1.0f},  
  26.     {0.0f, 1.0f},  
  27.     {0.0f, 0.0f},  
  28.     // Top Face  
  29.     {0.0f, 1.0f},  
  30.     {0.0f, 0.0f},  
  31.     {1.0f, 0.0f},  
  32.     {1.0f, 1.0f},  
  33.     // Bottom Face  
  34.     {1.0f, 1.0f},  
  35.     {0.0f, 1.0f},  
  36.     {0.0f, 0.0f},  
  37.     {1.0f, 0.0f},  
  38.     // Right face  
  39.     {1.0f, 0.0f},  
  40.     {1.0f, 1.0f},  
  41.     {0.0f, 1.0f},  
  42.     {0.0f, 0.0f},  
  43.     // Left Face  
  44.     {0.0f, 0.0f},  
  45.     {1.0f, 0.0f},  
  46.     {1.0f, 1.0f},  
  47.     {0.0f, 1.0f}  
  48. };  
  49.   
  50. float QuadVertices[][3] = {  
  51.   
  52.     {-1.0f, -1.0f,  1.0f},  
  53.     { 1.0f, -1.0f,  1.0f},  
  54.     { 1.0f,  1.0f,  1.0f},  
  55.     {-1.0f,  1.0f,  1.0f},  
  56.   
  57.     {-1.0f, -1.0f, -1.0f},  
  58.     {-1.0f,  1.0f, -1.0f},  
  59.     { 1.0f,  1.0f, -1.0f},  
  60.     { 1.0f, -1.0f, -1.0f},  
  61.   
  62.     {-1.0f,  1.0f, -1.0f},  
  63.     {-1.0f,  1.0f,  1.0f},  
  64.     { 1.0f,  1.0f,  1.0f},  
  65.     { 1.0f,  1.0f, -1.0f},  
  66.   
  67.     {-1.0f, -1.0f, -1.0f},  
  68.     { 1.0f, -1.0f, -1.0f},  
  69.     { 1.0f, -1.0f,  1.0f},  
  70.     {-1.0f, -1.0f,  1.0f},  
  71.   
  72.     { 1.0f, -1.0f, -1.0f},  
  73.     { 1.0f,  1.0f, -1.0f},  
  74.     { 1.0f,  1.0f,  1.0f},  
  75.     { 1.0f, -1.0f,  1.0f},  
  76.   
  77.     {-1.0f, -1.0f, -1.0f},  
  78.     {-1.0f, -1.0f,  1.0f},  
  79.     {-1.0f,  1.0f,  1.0f},  
  80.     {-1.0f,  1.0f, -1.0f}  
  81.   
  82. };  
  83.   
  84.   
  85. class ViewerWidget : public QWidget, public osgViewer::Viewer  
  86. {  
  87. public:  
  88.     ViewerWidget(osg::Node *scene = NULL)  
  89.     {  
  90.         QWidget* renderWidget = getRenderWidget( createGraphicsWindow(0,0,100,100), scene);  
  91.   
  92.         QVBoxLayout* layout = new QVBoxLayout;  
  93.         layout->addWidget(renderWidget);  
  94.         layout->setContentsMargins(0, 0, 0, 1);  
  95.         setLayout( layout );  
  96.   
  97.         connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );  
  98.         _timer.start( 10 );  
  99.     }  
  100.   
  101.     QWidget* getRenderWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene )  
  102.     {  
  103.         osg::Camera* camera = this->getCamera();  
  104.         camera->setGraphicsContext( gw );  
  105.   
  106.         const osg::GraphicsContext::Traits* traits = gw->getTraits();  
  107.   
  108.         camera->setClearColor( osg::Vec4(0.0, 0.0, 0.0, 1.0) );  
  109.         camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );  
  110.         camera->setProjectionMatrixAsPerspective(45.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 0.1f, 100.0f );  
  111.         camera->setViewMatrixAsLookAt(osg::Vec3d(0, 0, 1), osg::Vec3d(0, 0, 0), osg::Vec3d(0, 1, 0));  
  112.   
  113.         this->setSceneData( scene );  
  114.   
  115.         return gw->getGLWidget();  
  116.     }  
  117.   
  118.     osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name=""bool windowDecoration=false )  
  119.     {  
  120.         osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();  
  121.         osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;  
  122.         traits->windowName = name;  
  123.         traits->windowDecoration = windowDecoration;  
  124.         traits->x = x;  
  125.         traits->y = y;  
  126.         traits->width = w;  
  127.         traits->height = h;  
  128.         traits->doubleBuffer = true;  
  129.         traits->alpha = ds->getMinimumNumAlphaBits();  
  130.         traits->stencil = ds->getMinimumNumStencilBits();  
  131.         traits->sampleBuffers = ds->getMultiSamples();  
  132.         traits->samples = ds->getNumMultiSamples();  
  133.   
  134.         return new osgQt::GraphicsWindowQt(traits.get());  
  135.     }  
  136.   
  137.     virtual void paintEvent( QPaintEvent* event )  
  138.     {   
  139.         frame();   
  140.     }  
  141.   
  142. protected:  
  143.   
  144.     QTimer _timer;  
  145. };  
  146.   
  147.   
  148.   
  149. //  
  150.   
  151.   
  152.   
  153. osg::Node*  buildScene()  
  154. {  
  155.     osg::Group *root = new osg::Group;  
  156.   
  157.     osg::MatrixTransform *quadMT = new osg::MatrixTransform;  
  158.     quadMT->setMatrix(osg::Matrix::translate(0.0, 0.0, -5.0));  
  159.   
  160.     osg::AnimationPathCallback *quadAnimationCallback =   
  161.         new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Vec3(1, 1, 1), 2.5);  
  162.     osg::MatrixTransform *quadRotMT = new osg::MatrixTransform;  
  163.     quadRotMT->setUpdateCallback(quadAnimationCallback);  
  164.   
  165.     osg::Geometry *quadGeometry = new osg::Geometry;  
  166.     osg::Vec3Array *quadVertexArray = new osg::Vec3Array;  
  167.     for (unsigned i = 0; i < sizeof(QuadVertices); ++i)  
  168.     {  
  169.         quadVertexArray->push_back(osg::Vec3(QuadVertices[i][0], QuadVertices[i][1], QuadVertices[i][2]));  
  170.     }  
  171.     quadGeometry->setVertexArray(quadVertexArray);  
  172.   
  173.     osg::Vec2Array* texcoords = new osg::Vec2Array;  
  174.     for (unsigned i = 0; i < sizeof(textureVertices); ++i)  
  175.     {  
  176.         texcoords->push_back(osg::Vec2(textureVertices[i][0], textureVertices[i][1]));  
  177.     }  
  178.     quadGeometry->setTexCoordArray(0,texcoords);  
  179.   
  180.     int first = 0;  
  181.     for (unsigned i = 0; i < 6; ++i)  
  182.     {  
  183.         osg::DrawArrays *vertexIndices = new osg::DrawArrays(osg::PrimitiveSet::QUADS, first, 4);  
  184.         first += 4;  
  185.         quadGeometry->addPrimitiveSet(vertexIndices);  
  186.     }  
  187.   
  188.     osg::Image *textureImage = osgDB::readImageFile("Data/NeHe.bmp");  
  189.     osg::Texture2D *texture2D = new osg::Texture2D;  
  190.     texture2D->setImage(textureImage);  
  191.     texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);  
  192.     texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);  
  193.   
  194.     osg::Geode *quadGeode = new osg::Geode;  
  195.     quadGeode->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture2D);  
  196.     quadGeode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);  
  197.     quadGeode->addDrawable(quadGeometry);  
  198.     quadRotMT->addChild(quadGeode);  
  199.     quadMT->addChild(quadRotMT);  
  200.   
  201.     root->addChild(quadMT);  
  202.   
  203.     return root;  
  204. }  
  205.   
  206.   
  207.   
  208. int main( int argc, char** argv )  
  209. {  
  210.     QApplication app(argc, argv);  
  211.     ViewerWidget* viewWidget = new ViewerWidget(buildScene());  
  212.     viewWidget->setGeometry( 100, 100, 640, 480 );  
  213.     viewWidget->show();  
  214.     return app.exec();  
  215. }  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值