main.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <osg/Referenced>
  2. #include <osg/Geometry>
  3. #include <osg/Geode>
  4. #include <osg/Array>
  5. #include <osgViewer/Viewer>
  6. int main(int argc, char** argv)
  7. {
  8. // Create the geometry which will define the triangle
  9. osg::ref_ptr<osg::Geometry> myTriangleGeometry = new osg::Geometry;
  10. // Define the triangle's 3 vertices
  11. osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
  12. vertices->push_back(osg::Vec3(0, 0, 0));
  13. vertices->push_back(osg::Vec3(100, 0, 0));
  14. vertices->push_back(osg::Vec3(0, 0, 100));
  15. myTriangleGeometry->setVertexArray(vertices);
  16. // You can give each vertex its own color, but let's just make it green for now
  17. osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
  18. colors->push_back(osg::Vec4(0, 1.0, 0, 1.0)); // RGBA for green
  19. myTriangleGeometry->setColorArray(colors);
  20. myTriangleGeometry->setColorBinding(osg::Geometry::BIND_OVERALL);
  21. // Turn off lighting
  22. myTriangleGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
  23. // Turn on blending
  24. myTriangleGeometry->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
  25. // Define the geometry type as 'triangles'
  26. myTriangleGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, vertices->size()));
  27. // Finally, let's add our triangle to a geode
  28. osg::ref_ptr<osg::Geode> myGeode = new osg::Geode;
  29. myGeode->addDrawable(myTriangleGeometry);
  30. // And now we can create a viewer to look at our geode
  31. osgViewer::Viewer viewer;
  32. viewer.setSceneData(myGeode);
  33. return viewer.run();
  34. }