| 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 | |
|---|
| 14 | #ifndef OSG_GLBeginEndAdapter |
|---|
| 15 | #define OSG_GLBeginEndAdapter 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/ref_ptr> |
|---|
| 18 | #include <osg/Array> |
|---|
| 19 | #include <osg/Matrixd> |
|---|
| 20 | |
|---|
| 21 | #ifndef GL_TEXTURE0 |
|---|
| 22 | #define GL_TEXTURE0 0x84C0 |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | namespace osg { |
|---|
| 26 | |
|---|
| 27 | // forward declare |
|---|
| 28 | class State; |
|---|
| 29 | |
|---|
| 30 | /** A class adapting OpenGL 1.0 glBegin()/glEnd() style code to vertex array based code */ |
|---|
| 31 | class OSG_EXPORT GLBeginEndAdapter |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | |
|---|
| 35 | GLBeginEndAdapter(State* state=0); |
|---|
| 36 | |
|---|
| 37 | void setState(State* state) { _state = state; } |
|---|
| 38 | State* getState() { return _state; } |
|---|
| 39 | const State* getState() const { return _state; } |
|---|
| 40 | |
|---|
| 41 | enum MatrixMode |
|---|
| 42 | { |
|---|
| 43 | APPLY_LOCAL_MATRICES_TO_VERTICES, |
|---|
| 44 | APPLY_LOCAL_MATRICES_TO_MODELVIEW |
|---|
| 45 | }; |
|---|
| 46 | |
|---|
| 47 | void setMatrixMode(MatrixMode mode) { _mode = mode; } |
|---|
| 48 | MatrixMode setMatrixMode() const { return _mode; } |
|---|
| 49 | |
|---|
| 50 | void PushMatrix(); |
|---|
| 51 | void PopMatrix(); |
|---|
| 52 | |
|---|
| 53 | void LoadIdentity(); |
|---|
| 54 | void LoadMatrixd(const GLdouble* m); |
|---|
| 55 | void MultMatrixd(const GLdouble* m); |
|---|
| 56 | |
|---|
| 57 | void Translatef(GLfloat x, GLfloat y, GLfloat z) { Translated(x,y,z); } |
|---|
| 58 | void Scalef(GLfloat x, GLfloat y, GLfloat z) { Scaled(x,y,z); } |
|---|
| 59 | void Rotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) { Rotated(angle,x,y,z); } |
|---|
| 60 | |
|---|
| 61 | void Translated(GLdouble x, GLdouble y, GLdouble z); |
|---|
| 62 | void Scaled(GLdouble x, GLdouble y, GLdouble z); |
|---|
| 63 | void Rotated(GLdouble angle, GLdouble x, GLdouble y, GLdouble z); |
|---|
| 64 | |
|---|
| 65 | void Vertex3f(GLfloat x, GLfloat y, GLfloat z); |
|---|
| 66 | void Vertex3fv(const GLfloat* v) { Vertex3f(v[0], v[1], v[2]); } |
|---|
| 67 | |
|---|
| 68 | void Vertex3dv(GLdouble x, GLdouble y, GLdouble z) { Vertex3f(x,y,z); } |
|---|
| 69 | void Vertex3dv(const GLdouble* v) { Vertex3f(v[0], v[1], v[2]); } |
|---|
| 70 | |
|---|
| 71 | void Color4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) |
|---|
| 72 | { |
|---|
| 73 | _colorAssigned = true; |
|---|
| 74 | _color.set(red,green,blue,alpha); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | void Color4fv(const GLfloat* c) { Color4f(c[0], c[1], c[2], c[3]); } |
|---|
| 78 | void Color4ubv(const GLubyte* c) { const float div = 1.0f/255.0f; Color4f(float(c[0])*div, float(c[1])*div, float(c[2])*div, float(c[3])*div); } |
|---|
| 79 | |
|---|
| 80 | void Normal3f(GLfloat x, GLfloat y, GLfloat z) |
|---|
| 81 | { |
|---|
| 82 | _normalAssigned = true; |
|---|
| 83 | _normal.set(x,y,z); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void Normal3fv(const GLfloat* n) { Normal3f(n[0], n[1], n[2]); } |
|---|
| 87 | |
|---|
| 88 | void TexCoord1f(GLfloat x) { MultiTexCoord4f(GL_TEXTURE0, x, 0.0f, 0.0f, 1.0f); } |
|---|
| 89 | void TexCoord1fv(const GLfloat* tc) { MultiTexCoord4f(GL_TEXTURE0, tc[0], 0.0f, 0.0f, 1.0f); } |
|---|
| 90 | |
|---|
| 91 | void TexCoord2f(GLfloat x, GLfloat y) { MultiTexCoord4f(GL_TEXTURE0, x, y, 0.0f, 1.0f); } |
|---|
| 92 | void TexCoord2fv(const GLfloat* tc) { MultiTexCoord4f(GL_TEXTURE0, tc[0], tc[1], 0.0f, 1.0f); } |
|---|
| 93 | |
|---|
| 94 | void TexCoord3f(GLfloat x, GLfloat y, GLfloat z) { MultiTexCoord4f(GL_TEXTURE0, x, y, z, 1.0f); } |
|---|
| 95 | void TexCoord3fv(const GLfloat* tc) { MultiTexCoord4f(GL_TEXTURE0, tc[0], tc[1], tc[2], 1.0f); } |
|---|
| 96 | |
|---|
| 97 | void TexCoord4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w) { MultiTexCoord4f(GL_TEXTURE0, x, y, z, w); } |
|---|
| 98 | void TexCoord4fv(const GLfloat* tc) { MultiTexCoord4f(GL_TEXTURE0, tc[0], tc[1], tc[2], tc[3]); } |
|---|
| 99 | |
|---|
| 100 | void MultiTexCoord1f(GLenum target, GLfloat x) { MultiTexCoord4f(target, x, 0.0f, 0.0f, 1.0f); } |
|---|
| 101 | void MultiTexCoord1fv(GLenum target, const GLfloat* tc) { MultiTexCoord4f(target, tc[0], 0.0f, 0.0f, 1.0f); } |
|---|
| 102 | |
|---|
| 103 | void MultiTexCoord2f(GLenum target, GLfloat x, GLfloat y) { MultiTexCoord4f(target, x, y, 0.0f, 1.0f); } |
|---|
| 104 | void MultiTexCoord2fv(GLenum target, const GLfloat* tc) { MultiTexCoord4f(target, tc[0],tc[1], 0.0f, 1.0f); } |
|---|
| 105 | |
|---|
| 106 | void MultiTexCoord3f(GLenum target, GLfloat x, GLfloat y, GLfloat z) {MultiTexCoord4f(target, x, y, z, 1.0f); } |
|---|
| 107 | void MultiTexCoord3fv(GLenum target, const GLfloat* tc) { MultiTexCoord4f(target, tc[0], tc[1], tc[2], 1.0f); } |
|---|
| 108 | |
|---|
| 109 | void MultiTexCoord4f(GLenum target, GLfloat x, GLfloat y, GLfloat z, GLfloat w); |
|---|
| 110 | void MultiTexCoord4fv(GLenum target, const GLfloat* tc) { MultiTexCoord4f(target, tc[0], tc[1], tc[2], tc[3]); } |
|---|
| 111 | |
|---|
| 112 | void VertexAttrib1f(GLuint unit, GLfloat x) { VertexAttrib4f(unit, x, 0.0f, 0.0f, 0.0f); } |
|---|
| 113 | void VertexAttrib1fv(GLuint unit, const GLfloat* tc) { VertexAttrib4f(unit, tc[0], 0.0f, 0.0f, 0.0f); } |
|---|
| 114 | |
|---|
| 115 | void VertexAttrib2f(GLuint unit, GLfloat x, GLfloat y) { VertexAttrib4f(unit, x, y, 0.0f, 0.0f); } |
|---|
| 116 | void VertexAttrib2fv(GLuint unit, const GLfloat* tc) { VertexAttrib4f(unit, tc[0],tc[1], 0.0f, 0.0f); } |
|---|
| 117 | |
|---|
| 118 | void VertexAttrib3f(GLuint unit, GLfloat x, GLfloat y, GLfloat z) {VertexAttrib4f(unit, x, y, z, 0.0f); } |
|---|
| 119 | void VertexAttrib3fv(GLuint unit, const GLfloat* tc) { VertexAttrib4f(unit, tc[0], tc[1], tc[2], 0.0f); } |
|---|
| 120 | |
|---|
| 121 | void VertexAttrib4f(GLuint unit, GLfloat x, GLfloat y, GLfloat z, GLfloat w); |
|---|
| 122 | void VertexAttrib4fv(GLuint unit, const GLfloat* tc) { VertexAttrib4f(unit, tc[0], tc[1], tc[2], tc[3]); } |
|---|
| 123 | |
|---|
| 124 | void Begin(GLenum mode); |
|---|
| 125 | void End(); |
|---|
| 126 | |
|---|
| 127 | protected: |
|---|
| 128 | |
|---|
| 129 | State* _state; |
|---|
| 130 | |
|---|
| 131 | MatrixMode _mode; |
|---|
| 132 | |
|---|
| 133 | typedef std::list<Matrixd> MatrixStack; |
|---|
| 134 | MatrixStack _matrixStack; |
|---|
| 135 | |
|---|
| 136 | bool _normalAssigned; |
|---|
| 137 | osg::Vec3f _normal; |
|---|
| 138 | |
|---|
| 139 | bool _colorAssigned; |
|---|
| 140 | osg::Vec4f _color; |
|---|
| 141 | |
|---|
| 142 | osg::Vec3f _overallNormal; |
|---|
| 143 | osg::Vec4f _overallColor; |
|---|
| 144 | |
|---|
| 145 | typedef std::vector<bool> AssignedList; |
|---|
| 146 | typedef std::vector<osg::Vec4f> VertexList; |
|---|
| 147 | |
|---|
| 148 | AssignedList _texCoordAssignedList; |
|---|
| 149 | VertexList _texCoordList; |
|---|
| 150 | |
|---|
| 151 | AssignedList _vertexAttribAssignedList; |
|---|
| 152 | VertexList _vertexAttribList; |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | typedef std::vector< osg::ref_ptr<Vec4Array> > VertexArrayList; |
|---|
| 156 | |
|---|
| 157 | GLenum _primitiveMode; |
|---|
| 158 | osg::ref_ptr<osg::Vec3Array> _vertices; |
|---|
| 159 | osg::ref_ptr<osg::Vec3Array> _normals; |
|---|
| 160 | osg::ref_ptr<osg::Vec4Array> _colors; |
|---|
| 161 | VertexArrayList _texCoordsList; |
|---|
| 162 | VertexArrayList _vertexAttribsList; |
|---|
| 163 | }; |
|---|
| 164 | |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | #endif |
|---|