| [6941] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| [1697] | 19 | #include <osg/Node> |
|---|
| 20 | #include <osg/Geometry> |
|---|
| 21 | #include <osg/Notify> |
|---|
| 22 | #include <osg/MatrixTransform> |
|---|
| 23 | #include <osg/Texture2D> |
|---|
| 24 | #include <osg/Billboard> |
|---|
| 25 | #include <osg/LineWidth> |
|---|
| 26 | |
|---|
| 27 | #include <osgGA/TrackballManipulator> |
|---|
| 28 | #include <osgGA/FlightManipulator> |
|---|
| 29 | #include <osgGA/DriveManipulator> |
|---|
| 30 | |
|---|
| 31 | #include <osgDB/Registry> |
|---|
| 32 | #include <osgDB/ReadFile> |
|---|
| 33 | |
|---|
| [5911] | 34 | #include <osgViewer/Viewer> |
|---|
| [1697] | 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | typedef std::vector< osg::ref_ptr<osg::Image> > ImageList; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | osg::Drawable* createSquare(const osg::Vec3& corner,const osg::Vec3& width,const osg::Vec3& height, osg::Image* image=NULL) |
|---|
| 46 | { |
|---|
| 47 | |
|---|
| 48 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 49 | |
|---|
| 50 | osg::Vec3Array* coords = new osg::Vec3Array(4); |
|---|
| 51 | (*coords)[0] = corner; |
|---|
| 52 | (*coords)[1] = corner+width; |
|---|
| 53 | (*coords)[2] = corner+width+height; |
|---|
| 54 | (*coords)[3] = corner+height; |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | geom->setVertexArray(coords); |
|---|
| 58 | |
|---|
| 59 | osg::Vec3Array* norms = new osg::Vec3Array(1); |
|---|
| 60 | (*norms)[0] = width^height; |
|---|
| 61 | (*norms)[0].normalize(); |
|---|
| 62 | |
|---|
| 63 | geom->setNormalArray(norms); |
|---|
| 64 | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 65 | |
|---|
| 66 | osg::Vec2Array* tcoords = new osg::Vec2Array(4); |
|---|
| 67 | (*tcoords)[0].set(0.0f,0.0f); |
|---|
| 68 | (*tcoords)[1].set(1.0f,0.0f); |
|---|
| 69 | (*tcoords)[2].set(1.0f,1.0f); |
|---|
| 70 | (*tcoords)[3].set(0.0f,1.0f); |
|---|
| 71 | geom->setTexCoordArray(0,tcoords); |
|---|
| 72 | |
|---|
| 73 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); |
|---|
| 74 | |
|---|
| 75 | if (image) |
|---|
| 76 | { |
|---|
| 77 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 78 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 79 | texture->setImage(image); |
|---|
| 80 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 81 | geom->setStateSet(stateset); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | return geom; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | osg::Drawable* createAxis(const osg::Vec3& corner,const osg::Vec3& xdir,const osg::Vec3& ydir,const osg::Vec3& zdir) |
|---|
| 88 | { |
|---|
| 89 | |
|---|
| 90 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 91 | |
|---|
| 92 | osg::Vec3Array* coords = new osg::Vec3Array(6); |
|---|
| 93 | (*coords)[0] = corner; |
|---|
| 94 | (*coords)[1] = corner+xdir; |
|---|
| 95 | (*coords)[2] = corner; |
|---|
| 96 | (*coords)[3] = corner+ydir; |
|---|
| 97 | (*coords)[4] = corner; |
|---|
| 98 | (*coords)[5] = corner+zdir; |
|---|
| 99 | |
|---|
| 100 | geom->setVertexArray(coords); |
|---|
| 101 | |
|---|
| 102 | osg::Vec4 x_color(0.0f,1.0f,1.0f,1.0f); |
|---|
| 103 | osg::Vec4 y_color(0.0f,1.0f,1.0f,1.0f); |
|---|
| 104 | osg::Vec4 z_color(1.0f,0.0f,0.0f,1.0f); |
|---|
| 105 | |
|---|
| 106 | osg::Vec4Array* color = new osg::Vec4Array(6); |
|---|
| 107 | (*color)[0] = x_color; |
|---|
| 108 | (*color)[1] = x_color; |
|---|
| 109 | (*color)[2] = y_color; |
|---|
| 110 | (*color)[3] = y_color; |
|---|
| 111 | (*color)[4] = z_color; |
|---|
| 112 | (*color)[5] = z_color; |
|---|
| 113 | |
|---|
| 114 | geom->setColorArray(color); |
|---|
| 115 | geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 116 | |
|---|
| 117 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6)); |
|---|
| 118 | |
|---|
| 119 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 120 | osg::LineWidth* linewidth = new osg::LineWidth(); |
|---|
| 121 | linewidth->setWidth(4.0f); |
|---|
| 122 | stateset->setAttributeAndModes(linewidth,osg::StateAttribute::ON); |
|---|
| 123 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 124 | geom->setStateSet(stateset); |
|---|
| 125 | |
|---|
| 126 | return geom; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | osg::Node* createModel() |
|---|
| 130 | { |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | osg::Group* root = new osg::Group(); |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | osg::Billboard* center = new osg::Billboard(); |
|---|
| 137 | center->setMode(osg::Billboard::POINT_ROT_EYE); |
|---|
| 138 | center->addDrawable( |
|---|
| 139 | createSquare(osg::Vec3(-0.5f,0.0f,-0.5f),osg::Vec3(1.0f,0.0f,0.0f),osg::Vec3(0.0f,0.0f,1.0f),osgDB::readImageFile("Images/reflect.rgb")), |
|---|
| 140 | osg::Vec3(0.0f,0.0f,0.0f)); |
|---|
| 141 | |
|---|
| 142 | osg::Billboard* x_arrow = new osg::Billboard(); |
|---|
| 143 | x_arrow->setMode(osg::Billboard::AXIAL_ROT); |
|---|
| 144 | x_arrow->setAxis(osg::Vec3(1.0f,0.0f,0.0f)); |
|---|
| 145 | x_arrow->setNormal(osg::Vec3(0.0f,-1.0f,0.0f)); |
|---|
| 146 | x_arrow->addDrawable( |
|---|
| 147 | createSquare(osg::Vec3(-0.5f,0.0f,-0.5f),osg::Vec3(1.0f,0.0f,0.0f),osg::Vec3(0.0f,0.0f,1.0f),osgDB::readImageFile("Cubemap_axis/posx.png")), |
|---|
| 148 | osg::Vec3(5.0f,0.0f,0.0f)); |
|---|
| 149 | |
|---|
| 150 | osg::Billboard* y_arrow = new osg::Billboard(); |
|---|
| 151 | y_arrow->setMode(osg::Billboard::AXIAL_ROT); |
|---|
| 152 | y_arrow->setAxis(osg::Vec3(0.0f,1.0f,0.0f)); |
|---|
| 153 | y_arrow->setNormal(osg::Vec3(1.0f,0.0f,0.0f)); |
|---|
| 154 | y_arrow->addDrawable( |
|---|
| 155 | createSquare(osg::Vec3(0.0f,-0.5f,-0.5f),osg::Vec3(0.0f,1.0f,0.0f),osg::Vec3(0.0f,0.0f,1.0f),osgDB::readImageFile("Cubemap_axis/posy.png")), |
|---|
| 156 | osg::Vec3(0.0f,5.0f,0.0f)); |
|---|
| 157 | |
|---|
| 158 | osg::Billboard* z_arrow = new osg::Billboard(); |
|---|
| 159 | z_arrow->setMode(osg::Billboard::AXIAL_ROT); |
|---|
| 160 | z_arrow->setAxis(osg::Vec3(0.0f,0.0f,1.0f)); |
|---|
| 161 | z_arrow->setNormal(osg::Vec3(0.0f,-1.0f,0.0f)); |
|---|
| 162 | z_arrow->addDrawable( |
|---|
| 163 | createSquare(osg::Vec3(-0.5f,0.0f,-0.5f),osg::Vec3(1.0f,0.0f,0.0f),osg::Vec3(0.0f,0.0f,1.0f),osgDB::readImageFile("Cubemap_axis/posz.png")), |
|---|
| 164 | osg::Vec3(0.0f,0.0f,5.0f)); |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | osg::Geode* axis = new osg::Geode(); |
|---|
| 169 | axis->addDrawable(createAxis(osg::Vec3(0.0f,0.0f,0.0f),osg::Vec3(5.0f,0.0f,0.0f),osg::Vec3(0.0f,5.0f,0.0f),osg::Vec3(0.0f,0.0f,5.0f))); |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | root->addChild(center); |
|---|
| 173 | root->addChild(x_arrow); |
|---|
| 174 | root->addChild(y_arrow); |
|---|
| 175 | root->addChild(z_arrow); |
|---|
| 176 | root->addChild(axis); |
|---|
| 177 | |
|---|
| 178 | return root; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| [5911] | 181 | int main(int, char**) |
|---|
| [1697] | 182 | { |
|---|
| [5911] | 183 | |
|---|
| 184 | osgViewer::Viewer viewer; |
|---|
| [1815] | 185 | |
|---|
| [1697] | 186 | |
|---|
| [5911] | 187 | viewer.setSceneData(createModel()); |
|---|
| [1697] | 188 | |
|---|
| [5911] | 189 | |
|---|
| 190 | return viewer.run(); |
|---|
| [1697] | 191 | } |
|---|