root/OpenSceneGraph/trunk/examples/osgtexturerectangle/osgtexturerectangle.cpp @ 6051

Revision 6051, 7.1 kB (checked in by robert, 7 years ago)

Added osg::FrameStamp::set/getSimulationTime().

Added setting of osg_SimulationTime and osg_DeltaSimulationTime to the uniforms set by SceneView?

Added frame(double simulationTime) and advance(double simulationTime) parameters to
osgViewer::SimpleViewer?, Vewer and CompositeViewer?.

Updated various examples and Nodes to use SimulationTime? where appropriate.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// -*-c++-*-
2
3/*
4 * demonstrates usage of osg::TextureRectangle.
5 *
6 * Actually there isn't much difference to the rest of the osg::Texture*
7 * bunch only this:
8 * - texture coordinates for texture rectangles must be in image
9 *   coordinates instead of normalized coordinates (0-1). So for a 500x250
10 *   image the coordinates for the entire image would be
11 *   0,250 0,0 500,0 500,250 instead of 0,1 0,0 1,0 1,1
12 * - only the following wrap modes are supported (but not enforced)
13 *   CLAMP, CLAMP_TO_EDGE, CLAMP_TO_BORDER
14 * - a border is not supported
15 * - mipmap is not supported
16 */
17
18#include <osg/Notify>
19#include <osg/TextureRectangle>
20#include <osg/Geometry>
21#include <osg/Geode>
22#include <osg/TexMat>
23
24#include <osg/Group>
25#include <osg/Projection>
26#include <osg/MatrixTransform>
27#include <osgText/Text>
28
29#include <osgDB/Registry>
30#include <osgDB/ReadFile>
31
32#include <osgViewer/Viewer>
33
34
35/**********************************************************************
36 *
37 * Texture pan animation callback
38 *
39 **********************************************************************/
40
41class TexturePanCallback : public osg::NodeCallback
42{
43public:
44    TexturePanCallback(osg::TexMat* texmat,
45                       double delay = 0.05) :
46        _texmat(texmat),
47        _phaseS(35.0f),
48        _phaseT(18.0f),
49        _phaseScale(5.0f),
50        _delay(delay),
51        _prevTime(0.0)
52    {
53    }
54
55    virtual void operator()(osg::Node*, osg::NodeVisitor* nv)
56    {
57        if (!_texmat)
58            return;
59
60        if (nv->getFrameStamp()) {
61            double currTime = nv->getFrameStamp()->getSimulationTime();
62            if (currTime - _prevTime > _delay) {
63
64                float rad = osg::DegreesToRadians(currTime);
65
66                // zoom scale (0.2 - 1.0)
67                float scale = sin(rad * _phaseScale) * 0.4f + 0.6f;
68                float scaleR = 1.0f - scale;
69
70                // calculate new texture coordinates
71                float s, t;
72                s = ((sin(rad * _phaseS) + 1) * 0.5f) * (scaleR);
73                t = ((sin(rad * _phaseT) + 1) * 0.5f) * (scaleR);
74
75
76                _texmat->setMatrix(osg::Matrix::translate(s,t,1.0)*osg::Matrix::scale(scale,scale,1.0));
77
78                // record time
79                _prevTime = currTime;
80            }
81        }
82    }
83
84private:
85    osg::TexMat* _texmat;
86
87    float _phaseS, _phaseT, _phaseScale;
88
89    double _delay;
90    double _prevTime;
91};
92
93
94osg::Node* createRectangle(osg::BoundingBox& bb,
95                           const std::string& filename)
96{
97    osg::Vec3 top_left(bb.xMin(),bb.yMax(),bb.zMax());
98    osg::Vec3 bottom_left(bb.xMin(),bb.yMax(),bb.zMin());
99    osg::Vec3 bottom_right(bb.xMax(),bb.yMax(),bb.zMin());
100    osg::Vec3 top_right(bb.xMax(),bb.yMax(),bb.zMax());
101   
102    // create geometry
103    osg::Geometry* geom = new osg::Geometry;
104
105    osg::Vec3Array* vertices = new osg::Vec3Array(4);
106    (*vertices)[0] = top_left;
107    (*vertices)[1] = bottom_left;
108    (*vertices)[2] = bottom_right;
109    (*vertices)[3] = top_right;
110    geom->setVertexArray(vertices);
111   
112    osg::Vec2Array* texcoords = new osg::Vec2Array(4);
113    (*texcoords)[0].set(0.0f, 0.0f);
114    (*texcoords)[1].set(1.0f, 0.0f);
115    (*texcoords)[2].set(1.0f, 1.0f);
116    (*texcoords)[3].set(0.0f, 1.0f);
117    geom->setTexCoordArray(0,texcoords);
118
119    osg::Vec3Array* normals = new osg::Vec3Array(1);
120    (*normals)[0].set(0.0f,-1.0f,0.0f);
121    geom->setNormalArray(normals);
122    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
123
124    osg::Vec4Array* colors = new osg::Vec4Array(1);
125    (*colors)[0].set(1.0f,1.0f,1.0f,1.0f);
126    geom->setColorArray(colors);
127    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
128
129    geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));
130
131    // disable display list so our modified tex coordinates show up
132    geom->setUseDisplayList(false);
133
134    // load image
135    osg::Image* img = osgDB::readImageFile(filename);
136
137    // setup texture
138    osg::TextureRectangle* texture = new osg::TextureRectangle(img);
139
140    osg::TexMat* texmat = new osg::TexMat;
141    texmat->setScaleByTextureRectangleSize(true);
142
143    // setup state
144    osg::StateSet* state = geom->getOrCreateStateSet();
145    state->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
146    state->setTextureAttributeAndModes(0, texmat, osg::StateAttribute::ON);
147
148    // turn off lighting
149    state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
150
151    // install 'update' callback
152    osg::Geode* geode = new osg::Geode;
153    geode->addDrawable(geom);
154    geode->setUpdateCallback(new TexturePanCallback(texmat));
155   
156    return geode;
157}
158
159
160osg::Geode* createText(const std::string& str,
161                       const osg::Vec3& pos)
162{
163    static std::string font("fonts/arial.ttf");
164
165    osg::Geode* geode = new osg::Geode;
166
167    osgText::Text* text = new osgText::Text;
168    geode->addDrawable(text);
169
170    text->setFont(font);
171    text->setPosition(pos);
172    text->setText(str);
173
174    return geode;
175}
176
177
178osg::Node* createHUD()
179{
180    osg::Group* group = new osg::Group;
181
182    // turn off lighting and depth test
183    osg::StateSet* state = group->getOrCreateStateSet();
184    state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
185    state->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
186
187    // add text
188    osg::Vec3 pos(120.0f, 800.0f, 0.0f);
189    const osg::Vec3 delta(0.0f, -80.0f, 0.0f);
190
191    const char* text[] = {
192        "TextureRectangle Mini-HOWTO",
193        "- essentially behaves like Texture2D, *except* that:",
194        "- tex coords must be non-normalized (0..pixel) instead of (0..1),\nalternatively you can use osg::TexMat to scale normal non dimensional texcoords.",
195        "- wrap modes must be CLAMP, CLAMP_TO_EDGE, or CLAMP_TO_BORDER\n  repeating wrap modes are not supported",
196        "- filter modes must be NEAREST or LINEAR since\n  mipmaps are not supported",
197        "- texture borders are not supported",
198        "- defaults should be fine",
199        NULL
200    };
201    const char** t = text;
202    while (*t) {
203        group->addChild(createText(*t++, pos));
204        pos += delta;
205    }
206
207    // create HUD
208    osg::MatrixTransform* modelview_abs = new osg::MatrixTransform;
209    modelview_abs->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
210    modelview_abs->setMatrix(osg::Matrix::identity());
211    modelview_abs->addChild(group);
212
213    osg::Projection* projection = new osg::Projection;
214    projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
215    projection->addChild(modelview_abs);
216
217    return projection;
218}
219
220
221osg::Node* createModel(const std::string& filename)
222{
223    osg::Group* root = new osg::Group;
224
225    if (filename != "X") {
226        osg::BoundingBox bb(0.0f,0.0f,0.0f,1.0f,1.0f,1.0f);
227        root->addChild(createRectangle(bb, filename)); // XXX
228    }
229
230    root->addChild(createHUD());
231
232    return root;
233}
234
235
236int main(int argc, char** argv)
237{
238    // use an ArgumentParser object to manage the program arguments.
239    osg::ArgumentParser arguments(&argc,argv);
240
241    // construct the viewer.
242    osgViewer::Viewer viewer;
243
244    // create a model from the images.
245    osg::Node* rootNode = createModel((arguments.argc() > 1 ? arguments[1] : "Images/lz.rgb"));
246
247    // add model to viewer.
248    viewer.setSceneData(rootNode);
249   
250    return viewer.run();
251}
Note: See TracBrowser for help on using the browser.