Time for action—loading and applying 2D textures

The most common texture mapping technique is 2D texture mapping. This accepts a 2D image as the texture and maps it onto one or more geometry surfaces. The osg::Texture2D class is used here as a texture attribute of a specific texture mapping unit.

  1. Include the necessary headers:
    #include <osg/Texture2D>
    #include <osg/Geometry>
    #include <osgDB/ReadFile>
    #include <osgViewer/Viewer>
    
  2. We will quickly create a quad and call the setTexCoordArray() method to bind texture coordinates per vertex. The texture coordinate array only affects the texture unit 0 in this example, but it is always possible to share arrays among units:
    osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array; vertices->push_back( osg::Vec3(-0.5f, ...

Get OpenSceneGraph 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.