SceneGraphBuilder 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGUTIL_SCENEGRAPHBUILDER
  14. #define OSGUTIL_SCENEGRAPHBUILDER 1
  15. #include <osg/Geode>
  16. #include <osg/Geometry>
  17. #include <osg/MatrixTransform>
  18. #include <osg/GLU>
  19. #include <osgUtil/Export>
  20. namespace osgUtil {
  21. /** A class for assisting the building a scene graph that is equivalent to OpenGL 1.0 style calls.
  22. */
  23. class OSGUTIL_EXPORT SceneGraphBuilder
  24. {
  25. public:
  26. SceneGraphBuilder();
  27. //
  28. // OpenGL 1.0 style building methods
  29. //
  30. void PushMatrix();
  31. void PopMatrix();
  32. void LoadIdentity();
  33. void LoadMatrixd(const GLdouble* m);
  34. void MultMatrixd(const GLdouble* m);
  35. void Translated(GLdouble x, GLdouble y, GLdouble z);
  36. void Scaled(GLdouble x, GLdouble y, GLdouble z);
  37. void Rotated(GLdouble angle, GLdouble x, GLdouble y, GLdouble z);
  38. void BlendFunc(GLenum srcFactor, GLenum dstFactor);
  39. void CullFace(GLenum mode);
  40. void DepthFunc(GLenum mode);
  41. void FrontFace(GLenum mode);
  42. void LineStipple(GLint factor, GLushort pattern);
  43. void LineWidth(GLfloat lineWidth);
  44. void PointSize(GLfloat pointSize);
  45. void PolygonMode(GLenum face, GLenum mode);
  46. void PolygonOffset(GLfloat factor, GLfloat units);
  47. void PolygonStipple(const GLubyte* mask);
  48. void ShadeModel(GLenum mode);
  49. void Enable(GLenum mode);
  50. void Disable(GLenum mode);
  51. void Color4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
  52. void Color4fv(GLfloat* c) { Color4f(c[0], c[1], c[2], c[3]); }
  53. void Vertex3f(GLfloat x, GLfloat y, GLfloat z);
  54. void Vertex3fv(GLfloat* v) { Vertex3f(v[0], v[1], v[2]); }
  55. void Normal3f(GLfloat x, GLfloat y, GLfloat z);
  56. void Normal3fv(GLfloat* n) { Normal3f(n[0], n[1], n[2]); }
  57. void TexCoord1f(GLfloat x);
  58. void TexCoord1fv(GLfloat* tc) { TexCoord1f(tc[0]); }
  59. void TexCoord2f(GLfloat x, GLfloat y);
  60. void TexCoord2fv(GLfloat* tc) { TexCoord2f(tc[0],tc[1]); }
  61. void TexCoord3f(GLfloat x, GLfloat y, GLfloat z);
  62. void TexCoord3fv(GLfloat* tc) { TexCoord3f(tc[0], tc[1], tc[2]); }
  63. void TexCoord4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w);
  64. void TexCoord4fv(GLfloat* tc) { TexCoord4f(tc[0], tc[1], tc[2], tc[3]); }
  65. void Begin(GLenum mode);
  66. void End();
  67. //
  68. // glu style building methods
  69. //
  70. void QuadricDrawStyle(GLenum aDrawStyle);
  71. void QuadricNormals(GLenum aNormals);
  72. void QuadricOrientation(GLenum aOrientation);
  73. void QuadricTexture(GLboolean aTexture);
  74. void Cylinder(GLfloat base,
  75. GLfloat top,
  76. GLfloat height,
  77. GLint slices,
  78. GLint stacks);
  79. void Disk(GLfloat inner,
  80. GLfloat outer,
  81. GLint slices,
  82. GLint loops);
  83. void PartialDisk(GLfloat inner,
  84. GLfloat outer,
  85. GLint slices,
  86. GLint loops,
  87. GLfloat start,
  88. GLfloat sweep);
  89. void Sphere(GLfloat radius,
  90. GLint slices,
  91. GLint stacks);
  92. //
  93. // methods for obtaining the built scene graph
  94. //
  95. osg::Node* getScene();
  96. osg::Node* takeScene();
  97. protected:
  98. typedef std::vector<osg::Matrixd> Matrices;
  99. void matrixChanged();
  100. void addAttribute(osg::StateAttribute* attribute);
  101. void addMode(GLenum mode, bool enabled);
  102. void addTextureAttribute(unsigned int unit, osg::StateAttribute* attribute);
  103. void addTextureMode(unsigned int unit, GLenum mode, bool enabled);
  104. void addShape(osg::Shape* shape);
  105. void addDrawable(osg::Drawable* drawable);
  106. void newGeometry();
  107. void allocateGeometry();
  108. void completeGeometry();
  109. void allocateStateSet();
  110. Matrices _matrixStack;
  111. osg::ref_ptr<osg::StateSet> _stateset;
  112. bool _statesetAssigned;
  113. bool _normalSet;
  114. osg::Vec3f _normal;
  115. bool _colorSet;
  116. osg::Vec4f _color;
  117. unsigned int _maxNumTexCoordComponents;
  118. osg::Vec4f _texCoord;
  119. GLenum _primitiveMode;
  120. osg::ref_ptr<osg::Vec3Array> _vertices;
  121. osg::ref_ptr<osg::Vec3Array> _normals;
  122. osg::ref_ptr<osg::Vec4Array> _colors;
  123. osg::ref_ptr<osg::Vec4Array> _texCoords;
  124. struct QuadricState
  125. {
  126. QuadricState():
  127. _drawStyle(GLU_FILL),
  128. _normals(GLU_SMOOTH),
  129. _orientation(GLU_OUTSIDE),
  130. _texture(GLU_FALSE) {}
  131. GLenum _drawStyle;
  132. GLenum _normals;
  133. GLenum _orientation;
  134. GLboolean _texture;
  135. };
  136. QuadricState _quadricState;
  137. osg::ref_ptr<osg::Geometry> _geometry;
  138. osg::ref_ptr<osg::Geode> _geode;
  139. osg::ref_ptr<osg::MatrixTransform> _transform;
  140. osg::ref_ptr<osg::Group> _group;
  141. };
  142. }
  143. #endif