root/OpenSceneGraph/trunk/src/osg/GLBeginEndAdapter.cpp @ 13041

Revision 13041, 8.9 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
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#include <osg/GLBeginEndAdapter>
14#include <osg/State>
15
16#include <osg/Notify>
17#include <osg/io_utils>
18
19using namespace osg;
20
21GLBeginEndAdapter::GLBeginEndAdapter(State* state):
22    _state(state),
23    _mode(APPLY_LOCAL_MATRICES_TO_VERTICES),
24    _normalAssigned(false),
25    _normal(0.0f,0.0f,1.0f),
26    _colorAssigned(false),
27    _color(1.0f,1.0f,1.0f,1.0f),
28    _primitiveMode(0)
29{
30}
31
32void GLBeginEndAdapter::PushMatrix()
33{
34    if (_matrixStack.empty())
35    {
36        if (_mode==APPLY_LOCAL_MATRICES_TO_VERTICES) _matrixStack.push_back(Matrixd());
37        else _matrixStack.push_back(_state->getModelViewMatrix());
38    }
39    else _matrixStack.push_back(_matrixStack.back());
40}
41
42void GLBeginEndAdapter::PopMatrix()
43{
44    if (!_matrixStack.empty()) _matrixStack.pop_back();
45}
46
47
48void GLBeginEndAdapter::LoadIdentity()
49{
50    if (_matrixStack.empty()) _matrixStack.push_back(Matrixd::identity());
51    else _matrixStack.back().makeIdentity();
52}
53
54void GLBeginEndAdapter::LoadMatrixd(const GLdouble* m)
55{
56    if (_matrixStack.empty()) _matrixStack.push_back(Matrixd(m));
57    else _matrixStack.back().set(m);
58}
59
60void GLBeginEndAdapter::MultMatrixd(const GLdouble* m)
61{
62    if (_matrixStack.empty())
63    {
64        if (_mode==APPLY_LOCAL_MATRICES_TO_VERTICES) _matrixStack.push_back(Matrixd());
65        else _matrixStack.push_back(_state->getModelViewMatrix());
66    }
67    _matrixStack.back().preMult(Matrixd(m));
68}
69
70
71void GLBeginEndAdapter::Translated(GLdouble x, GLdouble y, GLdouble z)
72{
73    if (_matrixStack.empty())
74    {
75        if (_mode==APPLY_LOCAL_MATRICES_TO_VERTICES) _matrixStack.push_back(Matrixd());
76        else _matrixStack.push_back(_state->getModelViewMatrix());
77    }
78    _matrixStack.back().preMultTranslate(Vec3d(x,y,z));
79}
80
81void GLBeginEndAdapter::Scaled(GLdouble x, GLdouble y, GLdouble z)
82{
83    if (_matrixStack.empty())
84    {
85        if (_mode==APPLY_LOCAL_MATRICES_TO_VERTICES) _matrixStack.push_back(Matrixd());
86        else _matrixStack.push_back(_state->getModelViewMatrix());
87    }
88    _matrixStack.back().preMultScale(Vec3d(x,y,z));
89}
90
91void GLBeginEndAdapter::Rotated(GLdouble angle, GLdouble x, GLdouble y, GLdouble z)
92{
93    if (_matrixStack.empty())
94    {
95        if (_mode==APPLY_LOCAL_MATRICES_TO_VERTICES) _matrixStack.push_back(Matrixd());
96        else _matrixStack.push_back(_state->getModelViewMatrix());
97    }
98    _matrixStack.back().preMultRotate(Quat(DegreesToRadians(angle), Vec3d(x,y,z)));
99}
100
101void GLBeginEndAdapter::MultiTexCoord4f(GLenum target, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
102{
103    unsigned int unit = static_cast<unsigned int>(target-GL_TEXTURE0);
104
105    if (unit>=_texCoordAssignedList.size()) _texCoordAssignedList.resize(unit+1, false);
106    if (unit>=_texCoordList.size()) _texCoordList.resize(unit+1, osg::Vec4(0.0f,0.0f,0.0f,0.0f));
107
108    _texCoordAssignedList[unit] = true;
109    _texCoordList[unit].set(x,y,z,w);
110}
111
112void GLBeginEndAdapter::VertexAttrib4f(GLuint unit, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
113{
114    if (unit>=_vertexAttribAssignedList.size()) _vertexAttribAssignedList.resize(unit+1, false);
115    if (unit>=_vertexAttribList.size()) _vertexAttribList.resize(unit+1, osg::Vec4(0.0f,0.0f,0.0f,0.0f));
116
117    _vertexAttribAssignedList[unit] = true;
118    _vertexAttribList[unit].set(x,y,z,w);
119}
120
121void GLBeginEndAdapter::Vertex3f(GLfloat x, GLfloat y, GLfloat z)
122{
123    osg::Vec3 vertex(x,y,z);
124
125    if (!_vertices) _vertices = new osg::Vec3Array;
126
127    if (_normalAssigned)
128    {
129        if (!_normals) _normals = new osg::Vec3Array;
130        if (_normals->size()<_vertices->size()) _normals->resize(_vertices->size(), _overallNormal);
131
132        _normals->push_back(_normal);
133    }
134
135    if (_colorAssigned)
136    {
137        if (!_colors) _colors = new osg::Vec4Array;
138        if (_colors->size()<_vertices->size()) _colors->resize(_vertices->size(), _overallColor);
139
140        _colors->push_back(_color);
141    }
142
143    if (!_texCoordAssignedList.empty())
144    {
145        for(unsigned int unit=0; unit<_texCoordAssignedList.size(); ++unit)
146        {
147            if (_texCoordAssignedList[unit])
148            {
149                if (unit>=_texCoordsList.size()) _texCoordsList.resize(unit+1);
150                if (!_texCoordsList[unit]) _texCoordsList[unit] = new osg::Vec4Array;
151                if (_texCoordsList[unit]->size()<_vertices->size()) _texCoordsList[unit]->resize(_vertices->size(), osg::Vec4(0.0,0.0f,0.0f,0.0f));
152
153                _texCoordsList[unit]->push_back(_texCoordList[unit]);
154            }
155        }
156    }
157
158
159    if (!_vertexAttribAssignedList.empty())
160    {
161        for(unsigned int unit=0; unit<_vertexAttribAssignedList.size(); ++unit)
162        {
163            if (_vertexAttribAssignedList[unit])
164            {
165                if (unit>=_vertexAttribsList.size()) _vertexAttribsList.resize(unit+1);
166                if (!_vertexAttribsList[unit]) _vertexAttribsList[unit] = new osg::Vec4Array;
167                if (_vertexAttribsList[unit]->size()<_vertices->size()) _vertexAttribsList[unit]->resize(_vertices->size(), osg::Vec4(0.0,0.0f,0.0f,0.0f));
168
169                _vertexAttribsList[unit]->push_back(_vertexAttribList[unit]);
170            }
171        }
172    }
173
174    _vertices->push_back(vertex);
175}
176
177void GLBeginEndAdapter::Begin(GLenum mode)
178{
179    _overallNormal = _normal;
180    _overallColor = _color;
181
182    // reset geometry
183    _primitiveMode = mode;
184    if (_vertices.valid()) _vertices->clear();
185
186    _normalAssigned = false;
187    if (_normals.valid()) _normals->clear();
188
189    _colorAssigned = false;
190    if (_colors.valid()) _colors->clear();
191
192    _texCoordAssignedList.clear();
193    _texCoordList.clear();
194    for(VertexArrayList::iterator itr = _texCoordsList.begin();
195        itr != _texCoordsList.end();
196        ++itr)
197    {
198        if (itr->valid()) (*itr)->clear();
199    }
200
201    _vertexAttribAssignedList.clear();
202    _vertexAttribList.clear();
203}
204
205void GLBeginEndAdapter::End()
206{
207    if (!_vertices || _vertices->empty()) return;
208
209    if (!_matrixStack.empty())
210    {
211        const osg::Matrixd& matrix = _matrixStack.back();
212        if (_mode==APPLY_LOCAL_MATRICES_TO_VERTICES)
213        {
214            osg::Matrix inverse;
215            inverse.invert(matrix);
216
217            for(Vec3Array::iterator itr = _vertices->begin();
218                itr != _vertices->end();
219                ++itr)
220            {
221                *itr = *itr * matrix;
222            }
223
224            if (_normalAssigned && _normals.valid())
225            {
226                for(Vec3Array::iterator itr = _normals->begin();
227                    itr != _normals->end();
228                    ++itr)
229                {
230                    *itr = osg::Matrixd::transform3x3(inverse, *itr);
231                    (*itr).normalize();
232                }
233            }
234            else
235            {
236                _overallNormal = osg::Matrixd::transform3x3(inverse, _overallNormal);
237                _overallNormal.normalize();
238            }
239        }
240        else
241        {
242            _state->applyModelViewMatrix(new RefMatrix(matrix));
243        }
244    }
245
246    _state->lazyDisablingOfVertexAttributes();
247
248    if (_colorAssigned)
249    {
250        _state->setColorPointer(_colors.get());
251    }
252    else
253    {
254        _state->Color(_overallColor.r(), _overallColor.g(), _overallColor.b(), _overallColor.a());
255    }
256
257    if (_normalAssigned)
258    {
259         _state->setNormalPointer(_normals.get());
260    }
261    else
262    {
263        _state->Normal(_overallNormal.x(), _overallNormal.y(), _overallNormal.z());
264    }
265
266    for(unsigned int unit=0; unit<_texCoordAssignedList.size(); ++unit)
267    {
268        if (_texCoordAssignedList[unit] && _texCoordsList[unit].valid())
269        {
270            _state->setTexCoordPointer(unit, _texCoordsList[unit].get());
271        }
272    }
273
274
275    for(unsigned int unit=0; unit<_vertexAttribAssignedList.size(); ++unit)
276    {
277        if (_vertexAttribAssignedList[unit] && _vertexAttribsList[unit].valid())
278        {
279            _state->setVertexAttribPointer(unit, _vertexAttribsList[unit].get(), false);
280        }
281    }
282
283    _state->setVertexPointer(_vertices.get());
284
285    _state->applyDisablingOfVertexAttributes();
286
287    if (_primitiveMode==GL_QUADS)
288    {
289        _state->drawQuads(0, _vertices->size());
290    }
291    else if (_primitiveMode==GL_QUAD_STRIP)
292    {
293        // will the winding be wrong? Do we need to swap it?
294        glDrawArrays(GL_TRIANGLE_STRIP, 0, _vertices->size());
295    }
296    else if (_primitiveMode==GL_POLYGON) glDrawArrays(GL_TRIANGLE_FAN, 0, _vertices->size());
297    else glDrawArrays(_primitiveMode, 0, _vertices->size());
298}
Note: See TracBrowser for help on using the browser.