Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <osg/MatrixTransform>
- #include <osg/Texture2D>
- #include <osg/Geode>
- #include <osg/AutoTransform>
- #include <osgDB/ReadFile>
- #include <osgViewer/Viewer>
- osg::ref_ptr<osg::Node> createFixedSizeTexture(double x, double y, osg::Image *image)
- {
- float imageWidth = image->s();
- float imageHeight = image->t();
- osg::Vec3Array* verts = new osg::Vec3Array(4);
- (*verts)[0] = osg::Vec3(x - imageWidth/2.0f, y - imageHeight/2.0, 0.0f);
- (*verts)[1] = osg::Vec3(x + imageWidth/2.0f, y - imageHeight/2.0, 0.0f);
- (*verts)[2] = osg::Vec3(x + imageWidth/2.0f, y + imageHeight/2.0, 0.0f);
- (*verts)[3] = osg::Vec3(x - imageWidth/2.0f, y + imageHeight/2.0, 0.0f);
- osg::Vec2Array* texcoords = new osg::Vec2Array(4);
- (*texcoords)[0].set(0.0f , 0.0f);
- (*texcoords)[1].set(1.0f , 0.0f);
- (*texcoords)[2].set(1.0f , 1.0f);
- (*texcoords)[3].set(0.0f , 1.0f);
- osg::Vec4Array* colors = new osg::Vec4Array(1);
- (*colors)[0].set(1, 1, 1, 1);
- osg::Geometry* geometry = new osg::Geometry;
- geometry->setVertexArray( verts );
- geometry->setTexCoordArray(0, texcoords);
- geometry->setColorArray( colors );
- geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
- geometry->addPrimitiveSet( new osg::DrawArrays(GL_QUADS, 0, 4));
- osg::Texture2D* texture = new osg::Texture2D( image );
- texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
- texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
- osg::StateSet* stateSet = geometry->getOrCreateStateSet();
- stateSet->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
- stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
- stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
- osg::Geode* geode = new osg::Geode;
- geode->addDrawable( geometry );
- return geode;
- }
- int main(int argc, char **argv)
- {
- osg::setNotifyLevel(osg::WARN);
- osg::AutoTransform *at = new osg::AutoTransform;
- at->setAutoScaleToScreen(true);
- at->setAutoRotateMode( osg::AutoTransform::ROTATE_TO_SCREEN );
- osg::ref_ptr<osg::Image> image = osgDB::readImageFile("track_8x8.png");
- at->addChild(createFixedSizeTexture(0, 0, image.get()));
- osgViewer::Viewer viewer;
- viewer.setUpViewInWindow(0, 0, 640, 480);
- viewer.setSceneData(at);
- return viewer.run();
- }
Advertisement
Add Comment
Please, Sign In to add comment