source: trunk/VUT/work/ogre_changes/OgreMain/src/OgreRenderSystem.cpp @ 150

Revision 150, 15.4 KB checked in by mattausch, 19 years ago (diff)

added item buffer functionality

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#include "OgreStableHeaders.h"
26// RenderSystem implementation
27// Note that most of this class is abstract since
28//  we cannot know how to implement the behaviour without
29//  being aware of the 3D API. However there are a few
30//  simple functions which can have a base implementation
31
32#include "OgreRenderSystem.h"
33
34#include "OgreRoot.h"
35#include "OgreViewport.h"
36#include "OgreException.h"
37#include "OgreRenderTarget.h"
38#include "OgreRenderWindow.h"
39#include "OgreMeshManager.h"
40#include "OgreMaterial.h"
41#include "OgreTimer.h"
42
43namespace Ogre {
44
45    const PlaneList Renderable::msDummyPlaneList; // FIX ME: temporary
46
47    //-----------------------------------------------------------------------
48    RenderSystem::RenderSystem()
49    {
50        mActiveViewport = 0;
51                mActiveRenderTarget = NULL;
52        mTextureManager = 0;
53        mCapabilities = 0;
54        mVSync = true;
55                mWBuffer = false;
56
57
58        // This means CULL clockwise vertices, i.e. front of poly is counter-clockwise
59        // This makes it the same as OpenGL and other right-handed systems
60        mCullingMode = CULL_CLOCKWISE;
61        mInvertVertexWinding = false;
62
63        // instanciate RenderSystemCapabilities
64        mCapabilities = new RenderSystemCapabilities();
65
66#ifdef GTP_VISIBILITY_MODIFIED_OGRE
67                mColour[0] = mColour[1] = mColour[2] = mColour[3] = 255;
68#endif // GTP_VISIBILITY_MODIFIED_OGRE
69    }
70
71    //-----------------------------------------------------------------------
72    RenderSystem::~RenderSystem()
73    {
74        shutdown();
75    }
76    //-----------------------------------------------------------------------
77    void RenderSystem::_initRenderTargets(void)
78    {
79
80        // Init stats
81        for(
82            RenderTargetMap::iterator it = mRenderTargets.begin();
83            it != mRenderTargets.end();
84            ++it )
85        {
86            it->second->resetStatistics();
87        }
88
89    }
90    //-----------------------------------------------------------------------
91    void RenderSystem::_updateAllRenderTargets(void)
92    {
93        // Update all in order of priority
94        // This ensures render-to-texture targets get updated before render windows
95                RenderTargetPriorityMap::iterator itarg, itargend;
96                itargend = mPrioritisedRenderTargets.end();
97                for( itarg = mPrioritisedRenderTargets.begin(); itarg != itargend; ++itarg )
98                {
99                        if( itarg->second->isActive() && itarg->second->isAutoUpdated())
100                                itarg->second->update();
101                }
102    }
103    //-----------------------------------------------------------------------
104    RenderWindow* RenderSystem::initialise(bool autoCreateWindow, const String& windowTitle)
105    {
106        // Have I been registered by call to Root::setRenderSystem?
107                /** Don't do this anymore, just allow via Root
108        RenderSystem* regPtr = Root::getSingleton().getRenderSystem();
109        if (!regPtr || regPtr != this)
110            // Register self - library user has come to me direct
111            Root::getSingleton().setRenderSystem(this);
112                */
113
114
115        // Subclasses should take it from here
116        // They should ALL call this superclass method from
117        //   their own initialise() implementations.
118
119        return 0;
120    }
121    //---------------------------------------------------------------------------------------------
122    void RenderSystem::destroyRenderWindow(const String& name)
123    {
124        destroyRenderTarget(name);
125    }
126    //---------------------------------------------------------------------------------------------
127    void RenderSystem::destroyRenderTexture(const String& name)
128    {
129        destroyRenderTarget(name);
130    }
131    //---------------------------------------------------------------------------------------------
132    void RenderSystem::destroyRenderTarget(const String& name)
133    {
134        RenderTarget* rt = detachRenderTarget(name);
135        delete rt;
136    }
137    //---------------------------------------------------------------------------------------------
138    void RenderSystem::attachRenderTarget( RenderTarget &target )
139    {
140                assert( target.getPriority() < OGRE_NUM_RENDERTARGET_GROUPS );
141
142        mRenderTargets.insert( RenderTargetMap::value_type( target.getName(), &target ) );
143        mPrioritisedRenderTargets.insert(
144            RenderTargetPriorityMap::value_type(target.getPriority(), &target ));
145    }
146
147    //---------------------------------------------------------------------------------------------
148    RenderTarget * RenderSystem::getRenderTarget( const String &name )
149    {
150        RenderTargetMap::iterator it = mRenderTargets.find( name );
151        RenderTarget *ret = NULL;
152
153        if( it != mRenderTargets.end() )
154        {
155            ret = it->second;
156        }
157
158        return ret;
159    }
160
161    //---------------------------------------------------------------------------------------------
162    RenderTarget * RenderSystem::detachRenderTarget( const String &name )
163    {
164        RenderTargetMap::iterator it = mRenderTargets.find( name );
165        RenderTarget *ret = NULL;
166
167        if( it != mRenderTargets.end() )
168        {
169            ret = it->second;
170                       
171                        /* Remove the render target from the priority groups. */
172            RenderTargetPriorityMap::iterator itarg, itargend;
173            itargend = mPrioritisedRenderTargets.end();
174                        for( itarg = mPrioritisedRenderTargets.begin(); itarg != itargend; ++itarg )
175            {
176                                if( itarg->second == ret ) {
177                                        mPrioritisedRenderTargets.erase( itarg );
178                                        break;
179                                }
180            }
181
182            mRenderTargets.erase( it );
183        }
184
185        return ret;
186    }
187    //-----------------------------------------------------------------------
188    Viewport* RenderSystem::_getViewport(void)
189    {
190        return mActiveViewport;
191    }
192    //-----------------------------------------------------------------------
193    void RenderSystem::_setTextureUnitSettings(size_t texUnit, TextureUnitState& tl)
194    {
195        // This method is only ever called to set a texture unit to valid details
196        // The method _disableTextureUnit is called to turn a unit off
197
198        // Texture name
199                if (tl.isBlank())
200                {
201                        _setTexture(texUnit, true, StringUtil::BLANK);
202                }
203                else
204                {
205                        _setTexture(texUnit, true, tl.getTextureName());
206                }
207
208        // Set texture coordinate set
209        _setTextureCoordSet(texUnit, tl.getTextureCoordSet());
210
211        // Set texture layer filtering
212        _setTextureUnitFiltering(texUnit,
213            tl.getTextureFiltering(FT_MIN),
214            tl.getTextureFiltering(FT_MAG),
215            tl.getTextureFiltering(FT_MIP));
216
217        // Set texture layer filtering
218        _setTextureLayerAnisotropy(texUnit, tl.getTextureAnisotropy());
219
220                // Set blend modes
221                // Note, colour before alpha is important
222        _setTextureBlendMode(texUnit, tl.getColourBlendMode());
223        _setTextureBlendMode(texUnit, tl.getAlphaBlendMode());
224
225        // Texture addressing mode
226        _setTextureAddressingMode(texUnit, tl.getTextureAddressingMode() );
227
228        // Set texture effects
229        TextureUnitState::EffectMap::iterator effi;
230        // Iterate over new effects
231        bool anyCalcs = false;
232        for (effi = tl.mEffects.begin(); effi != tl.mEffects.end(); ++effi)
233        {
234            switch (effi->second.type)
235            {
236            case TextureUnitState::ET_ENVIRONMENT_MAP:
237                if (effi->second.subtype == TextureUnitState::ENV_CURVED)
238                {
239                    _setTextureCoordCalculation(texUnit, TEXCALC_ENVIRONMENT_MAP);
240                    anyCalcs = true;
241                }
242                else if (effi->second.subtype == TextureUnitState::ENV_PLANAR)
243                {
244                    _setTextureCoordCalculation(texUnit, TEXCALC_ENVIRONMENT_MAP_PLANAR);
245                    anyCalcs = true;
246                }
247                else if (effi->second.subtype == TextureUnitState::ENV_REFLECTION)
248                {
249                    _setTextureCoordCalculation(texUnit, TEXCALC_ENVIRONMENT_MAP_REFLECTION);
250                    anyCalcs = true;
251                }
252                else if (effi->second.subtype == TextureUnitState::ENV_NORMAL)
253                {
254                    _setTextureCoordCalculation(texUnit, TEXCALC_ENVIRONMENT_MAP_NORMAL);
255                    anyCalcs = true;
256                }
257                break;
258            case TextureUnitState::ET_SCROLL:
259            case TextureUnitState::ET_ROTATE:
260            case TextureUnitState::ET_TRANSFORM:
261                break;
262            case TextureUnitState::ET_PROJECTIVE_TEXTURE:
263                _setTextureCoordCalculation(texUnit, TEXCALC_PROJECTIVE_TEXTURE,
264                    effi->second.frustum);
265                anyCalcs = true;
266                break;
267            }
268        }
269        // Ensure any previous texcoord calc settings are reset if there are now none
270        if (!anyCalcs)
271        {
272            _setTextureCoordCalculation(texUnit, TEXCALC_NONE);
273            _setTextureCoordSet(texUnit, tl.getTextureCoordSet());
274        }
275
276        // Change tetxure matrix
277        _setTextureMatrix(texUnit, tl.getTextureTransform());
278
279
280    }
281    //-----------------------------------------------------------------------
282    void RenderSystem::_disableTextureUnit(size_t texUnit)
283    {
284        _setTexture(texUnit, false, "");
285        _setTextureMatrix(texUnit, Matrix4::IDENTITY);
286    }
287    //---------------------------------------------------------------------
288    void RenderSystem::_disableTextureUnitsFrom(size_t texUnit)
289    {
290        for (size_t i = texUnit; i < mCapabilities->getNumTextureUnits(); ++i)
291        {
292            _disableTextureUnit(i);
293        }
294    }
295    //-----------------------------------------------------------------------
296    void RenderSystem::_setTextureUnitFiltering(size_t unit, FilterOptions minFilter,
297            FilterOptions magFilter, FilterOptions mipFilter)
298    {
299        _setTextureUnitFiltering(unit, FT_MIN, minFilter);
300        _setTextureUnitFiltering(unit, FT_MAG, magFilter);
301        _setTextureUnitFiltering(unit, FT_MIP, mipFilter);
302    }
303    //-----------------------------------------------------------------------
304    CullingMode RenderSystem::_getCullingMode(void) const
305    {
306        return mCullingMode;
307    }
308    //-----------------------------------------------------------------------
309    bool RenderSystem::getWaitForVerticalBlank(void) const
310    {
311        return mVSync;
312    }
313    //-----------------------------------------------------------------------
314    void RenderSystem::setWaitForVerticalBlank(bool enabled)
315    {
316        mVSync = enabled;
317    }
318    bool RenderSystem::getWBufferEnabled(void) const
319    {
320        return mWBuffer;
321    }
322    //-----------------------------------------------------------------------
323    void RenderSystem::setWBufferEnabled(bool enabled)
324    {
325        mWBuffer = enabled;
326    }
327    //-----------------------------------------------------------------------
328    void RenderSystem::shutdown(void)
329    {
330        // Remove all the render targets.
331                // (destroy primary target last since others may depend on it)
332                RenderTarget* primary = 0;
333                for (RenderTargetMap::iterator it = mRenderTargets.begin(); it != mRenderTargets.end(); ++it)
334                {
335                        if (!primary && it->second->isPrimary())
336                                primary = it->second;
337                        else
338                                delete it->second;
339                }
340                delete primary;
341                mRenderTargets.clear();
342
343                mPrioritisedRenderTargets.clear();
344    }
345    //-----------------------------------------------------------------------
346    void RenderSystem::_beginGeometryCount(void)
347    {
348        mFaceCount = mVertexCount = 0;
349
350    }
351    //-----------------------------------------------------------------------
352    unsigned int RenderSystem::_getFaceCount(void) const
353    {
354        return static_cast< unsigned int >( mFaceCount );
355    }
356    //-----------------------------------------------------------------------
357    unsigned int RenderSystem::_getVertexCount(void) const
358    {
359        return static_cast< unsigned int >( mVertexCount );
360    }
361    //-----------------------------------------------------------------------
362    void RenderSystem::_setWorldMatrices(const Matrix4* m, unsigned short count)
363    {
364        // Save these matrices for software blending later
365        for (unsigned short i = 0; i < count; ++i)
366        {
367            mWorldMatrices[i] = m[i];
368        }
369        // Set hardware matrix to nothing
370        _setWorldMatrix(Matrix4::IDENTITY);
371    }
372    //-----------------------------------------------------------------------
373    void RenderSystem::_render(const RenderOperation& op)
374    {
375        // Update stats
376        size_t val;
377
378        if (op.useIndexes)
379            val = op.indexData->indexCount;
380        else
381            val = op.vertexData->vertexCount;
382
383        switch(op.operationType)
384        {
385                case RenderOperation::OT_TRIANGLE_LIST:
386            mFaceCount += val / 3;
387            break;
388        case RenderOperation::OT_TRIANGLE_STRIP:
389        case RenderOperation::OT_TRIANGLE_FAN:
390            mFaceCount += val - 2;
391            break;
392            case RenderOperation::OT_POINT_LIST:
393            case RenderOperation::OT_LINE_LIST:
394            case RenderOperation::OT_LINE_STRIP:
395                break;
396            }
397
398        mVertexCount += op.vertexData->vertexCount;
399
400    }
401    //-----------------------------------------------------------------------
402    void RenderSystem::setInvertVertexWinding(bool invert)
403    {
404        mInvertVertexWinding = invert;
405    }
406    //-----------------------------------------------------------------------
407    void RenderSystem::setClipPlane (ushort index, const Plane &p)
408    {
409        setClipPlane (index, p.normal.x, p.normal.y, p.normal.z, p.d);
410    }
411    //-----------------------------------------------------------------------
412    void RenderSystem::_notifyCameraRemoved(const Camera* cam)
413    {
414        RenderTargetMap::iterator i, iend;
415        iend = mRenderTargets.end();
416        for (i = mRenderTargets.begin(); i != iend; ++i)
417        {
418            RenderTarget* target = i->second;
419            target->_notifyCameraRemoved(cam);
420        }
421    }
422#ifdef GTP_VISIBILITY_MODIFIED_OGRE
423        // ------------------------------------------------------------------
424        void RenderSystem::setColour(int r, int g, int b, int a)
425        {
426                mColour[0] = r;
427                mColour[1] = g;
428                mColour[2] = b;
429                mColour[3] = a;
430        }
431#endif //  GTP_VISIBILITY_MODIFIED_OGRE
432}
433
Note: See TracBrowser for help on using the repository browser.