| 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 | #include <osg/Notify> |
|---|
| 35 | #include <osg/TextureRectangle> |
|---|
| 36 | #include <osg/Geometry> |
|---|
| 37 | #include <osg/Geode> |
|---|
| 38 | #include <osg/TexMat> |
|---|
| 39 | |
|---|
| 40 | #include <osg/Group> |
|---|
| 41 | #include <osg/Projection> |
|---|
| 42 | #include <osg/MatrixTransform> |
|---|
| 43 | #include <osgText/Text> |
|---|
| 44 | |
|---|
| 45 | #include <osgDB/Registry> |
|---|
| 46 | #include <osgDB/ReadFile> |
|---|
| 47 | |
|---|
| 48 | #include <osgViewer/Viewer> |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | class TexturePanCallback : public osg::NodeCallback |
|---|
| 58 | { |
|---|
| 59 | public: |
|---|
| 60 | TexturePanCallback(osg::TexMat* texmat, |
|---|
| 61 | double delay = 0.05) : |
|---|
| 62 | _texmat(texmat), |
|---|
| 63 | _phaseS(35.0f), |
|---|
| 64 | _phaseT(18.0f), |
|---|
| 65 | _phaseScale(5.0f), |
|---|
| 66 | _delay(delay), |
|---|
| 67 | _prevTime(0.0) |
|---|
| 68 | { |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | virtual void operator()(osg::Node*, osg::NodeVisitor* nv) |
|---|
| 72 | { |
|---|
| 73 | if (!_texmat) |
|---|
| 74 | return; |
|---|
| 75 | |
|---|
| 76 | if (nv->getFrameStamp()) { |
|---|
| 77 | double currTime = nv->getFrameStamp()->getSimulationTime(); |
|---|
| 78 | if (currTime - _prevTime > _delay) { |
|---|
| 79 | |
|---|
| 80 | float rad = osg::DegreesToRadians(currTime); |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | float scale = sin(rad * _phaseScale) * 0.4f + 0.6f; |
|---|
| 84 | float scaleR = 1.0f - scale; |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | float s, t; |
|---|
| 88 | s = ((sin(rad * _phaseS) + 1) * 0.5f) * (scaleR); |
|---|
| 89 | t = ((sin(rad * _phaseT) + 1) * 0.5f) * (scaleR); |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | _texmat->setMatrix(osg::Matrix::translate(s,t,1.0)*osg::Matrix::scale(scale,scale,1.0)); |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | _prevTime = currTime; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | private: |
|---|
| 101 | osg::TexMat* _texmat; |
|---|
| 102 | |
|---|
| 103 | float _phaseS, _phaseT, _phaseScale; |
|---|
| 104 | |
|---|
| 105 | double _delay; |
|---|
| 106 | double _prevTime; |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | osg::Node* createRectangle(osg::BoundingBox& bb, |
|---|
| 111 | const std::string& filename) |
|---|
| 112 | { |
|---|
| 113 | osg::Vec3 top_left(bb.xMin(),bb.yMax(),bb.zMax()); |
|---|
| 114 | osg::Vec3 bottom_left(bb.xMin(),bb.yMax(),bb.zMin()); |
|---|
| 115 | osg::Vec3 bottom_right(bb.xMax(),bb.yMax(),bb.zMin()); |
|---|
| 116 | osg::Vec3 top_right(bb.xMax(),bb.yMax(),bb.zMax()); |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 120 | |
|---|
| 121 | osg::Vec3Array* vertices = new osg::Vec3Array(4); |
|---|
| 122 | (*vertices)[0] = top_left; |
|---|
| 123 | (*vertices)[1] = bottom_left; |
|---|
| 124 | (*vertices)[2] = bottom_right; |
|---|
| 125 | (*vertices)[3] = top_right; |
|---|
| 126 | geom->setVertexArray(vertices); |
|---|
| 127 | |
|---|
| 128 | osg::Vec2Array* texcoords = new osg::Vec2Array(4); |
|---|
| 129 | (*texcoords)[0].set(0.0f, 0.0f); |
|---|
| 130 | (*texcoords)[1].set(1.0f, 0.0f); |
|---|
| 131 | (*texcoords)[2].set(1.0f, 1.0f); |
|---|
| 132 | (*texcoords)[3].set(0.0f, 1.0f); |
|---|
| 133 | geom->setTexCoordArray(0,texcoords); |
|---|
| 134 | |
|---|
| 135 | osg::Vec3Array* normals = new osg::Vec3Array(1); |
|---|
| 136 | (*normals)[0].set(0.0f,-1.0f,0.0f); |
|---|
| 137 | geom->setNormalArray(normals); |
|---|
| 138 | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 139 | |
|---|
| 140 | osg::Vec4Array* colors = new osg::Vec4Array(1); |
|---|
| 141 | (*colors)[0].set(1.0f,1.0f,1.0f,1.0f); |
|---|
| 142 | geom->setColorArray(colors); |
|---|
| 143 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 144 | |
|---|
| 145 | geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4)); |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | geom->setUseDisplayList(false); |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | osg::Image* img = osgDB::readImageFile(filename); |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | osg::TextureRectangle* texture = new osg::TextureRectangle(img); |
|---|
| 155 | |
|---|
| 156 | osg::TexMat* texmat = new osg::TexMat; |
|---|
| 157 | texmat->setScaleByTextureRectangleSize(true); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | osg::StateSet* state = geom->getOrCreateStateSet(); |
|---|
| 161 | state->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON); |
|---|
| 162 | state->setTextureAttributeAndModes(0, texmat, osg::StateAttribute::ON); |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | state->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | osg::Geode* geode = new osg::Geode; |
|---|
| 169 | geode->addDrawable(geom); |
|---|
| 170 | geode->setUpdateCallback(new TexturePanCallback(texmat)); |
|---|
| 171 | |
|---|
| 172 | return geode; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | osg::Geode* createText(const std::string& str, |
|---|
| 177 | const osg::Vec3& pos) |
|---|
| 178 | { |
|---|
| 179 | static std::string font("fonts/arial.ttf"); |
|---|
| 180 | |
|---|
| 181 | osg::Geode* geode = new osg::Geode; |
|---|
| 182 | |
|---|
| 183 | osgText::Text* text = new osgText::Text; |
|---|
| 184 | geode->addDrawable(text); |
|---|
| 185 | |
|---|
| 186 | text->setFont(font); |
|---|
| 187 | text->setPosition(pos); |
|---|
| 188 | text->setText(str); |
|---|
| 189 | |
|---|
| 190 | return geode; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | osg::Node* createHUD() |
|---|
| 195 | { |
|---|
| 196 | osg::Group* group = new osg::Group; |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | osg::StateSet* state = group->getOrCreateStateSet(); |
|---|
| 200 | state->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
|---|
| 201 | state->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | osg::Vec3 pos(120.0f, 800.0f, 0.0f); |
|---|
| 205 | const osg::Vec3 delta(0.0f, -80.0f, 0.0f); |
|---|
| 206 | |
|---|
| 207 | const char* text[] = { |
|---|
| 208 | "TextureRectangle Mini-HOWTO", |
|---|
| 209 | "- essentially behaves like Texture2D, *except* that:", |
|---|
| 210 | "- tex coords must be non-normalized (0..pixel) instead of (0..1),\nalternatively you can use osg::TexMat to scale normal non dimensional texcoords.", |
|---|
| 211 | "- wrap modes must be CLAMP, CLAMP_TO_EDGE, or CLAMP_TO_BORDER\n repeating wrap modes are not supported", |
|---|
| 212 | "- filter modes must be NEAREST or LINEAR since\n mipmaps are not supported", |
|---|
| 213 | "- texture borders are not supported", |
|---|
| 214 | "- defaults should be fine", |
|---|
| 215 | NULL |
|---|
| 216 | }; |
|---|
| 217 | const char** t = text; |
|---|
| 218 | while (*t) { |
|---|
| 219 | group->addChild(createText(*t++, pos)); |
|---|
| 220 | pos += delta; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | osg::MatrixTransform* modelview_abs = new osg::MatrixTransform; |
|---|
| 225 | modelview_abs->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 226 | modelview_abs->setMatrix(osg::Matrix::identity()); |
|---|
| 227 | modelview_abs->addChild(group); |
|---|
| 228 | |
|---|
| 229 | osg::Projection* projection = new osg::Projection; |
|---|
| 230 | projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); |
|---|
| 231 | projection->addChild(modelview_abs); |
|---|
| 232 | |
|---|
| 233 | return projection; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | osg::Node* createModel(const std::string& filename) |
|---|
| 238 | { |
|---|
| 239 | osg::Group* root = new osg::Group; |
|---|
| 240 | |
|---|
| 241 | if (filename != "X") { |
|---|
| 242 | osg::BoundingBox bb(0.0f,0.0f,0.0f,1.0f,1.0f,1.0f); |
|---|
| 243 | root->addChild(createRectangle(bb, filename)); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | root->addChild(createHUD()); |
|---|
| 247 | |
|---|
| 248 | return root; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | int main(int argc, char** argv) |
|---|
| 253 | { |
|---|
| 254 | |
|---|
| 255 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | osgViewer::Viewer viewer; |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | osg::Node* rootNode = createModel((arguments.argc() > 1 ? arguments[1] : "Images/lz.rgb")); |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | viewer.setSceneData(rootNode); |
|---|
| 265 | |
|---|
| 266 | return viewer.run(); |
|---|
| 267 | } |
|---|