-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathImages.cpp
More file actions
86 lines (71 loc) · 3.04 KB
/
Copy pathImages.cpp
File metadata and controls
86 lines (71 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/////////////////////////////////////////////////////////////////
/// @file Images.cpp
/// @author Chris L Baker (clb) <chris@chimail.net>
/// @date 2015.05.06
/// @brief Display an images in 3D
///
/// @attention Copyright (C) 2015
/// @attention All rights reserved
/////////////////////////////////////////////////////////////////
#include "Images.h"
#include <osg/Geometry>
#include <osg/Texture2D>
#include <osg/Geode>
#include <osg/TexEnv>
namespace d3
{
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
osg::ref_ptr<osg::Node> get(const ImageVec_t& images)
{
osg::ref_ptr<osg::Group> rv( new osg::Group() );
for ( const auto& image : images )
{
// here we create a quad that will be texture mapped with the image
osg::ref_ptr<osg::Vec3Array> quad( new osg::Vec3Array() );
quad->push_back( image.corners[0] );
quad->push_back( image.corners[1] );
quad->push_back( image.corners[2] );
quad->push_back( image.corners[3] );
osg::ref_ptr<osg::Geometry> geo( new osg::Geometry() );
geo->setVertexArray( quad );
// here we create a drawing elelment to draw on
osg::ref_ptr<osg::DrawElementsUInt>
primitiveSet( new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0) );
primitiveSet->push_back( 0 );
primitiveSet->push_back( 1 );
primitiveSet->push_back( 2 );
primitiveSet->push_back( 3 );
geo->addPrimitiveSet( primitiveSet );
// the texture mappings
osg::ref_ptr<osg::Vec2Array> texCoords( new osg::Vec2Array(4) );
(*texCoords)[3].set( 0.0f, 0.0f );
(*texCoords)[2].set( 1.0f, 0.0f );
(*texCoords)[1].set( 1.0f, 1.0f );
(*texCoords)[0].set( 0.0f, 1.0f );
geo->setTexCoordArray( 0, texCoords );
// now create the goede to hold our created geometry
osg::ref_ptr<osg::Geode> geode( new osg::Geode() );
geode->addDrawable( geo );
// create the texture for the image
osg::ref_ptr<osg::Texture2D> texture( new osg::Texture2D() );
texture->setResizeNonPowerOfTwoHint(false);
texture->setDataVariance(osg::Object::DYNAMIC);
texture->setImage( image.image );
// put this in decal mode
osg::ref_ptr<osg::TexEnv> decalTexEnv( new osg::TexEnv() );
decalTexEnv->setMode(osg::TexEnv::DECAL);
// set the state set, lighting and such, so we actually display the image
osg::ref_ptr<osg::StateSet> stateSet( geode->getOrCreateStateSet() );
stateSet->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
stateSet->setTextureAttribute(0, decalTexEnv);
stateSet->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
geo->setStateSet(stateSet);
// turn off any color binding - we want to use the texture
geo->setColorBinding(osg::Geometry::BIND_OFF);
// add this image and continue
rv->addChild(geode);
}
return rv;
};
} // namespace d3