source: OGRE/trunk/ogrenew/OgreMain/src/OgreAutoParamDataSource.cpp @ 692

Revision 692, 24.2 KB checked in by mattausch, 18 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://ogre.sourceforge.net/
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
27#include "OgreAutoParamDataSource.h"
28#include "OgreRenderable.h"
29#include "OgreCamera.h"
30#include "OgreRenderTarget.h"
31#include "OgreControllerManager.h"
32#include "OgreMath.h"
33#include "OgreRoot.h"
34#include "OgreRenderSystem.h"
35
36namespace Ogre {
37    const Matrix4 PROJECTIONCLIPSPACE2DTOIMAGESPACE_PERSPECTIVE(
38        0.5,    0,    0,  0.5,
39        0,   -0.5,    0,  0.5,
40        0,      0,  0.5,  0.5,
41        0,      0,    0,    1);
42
43    //-----------------------------------------------------------------------------
44    AutoParamDataSource::AutoParamDataSource()
45        : mWorldMatrixDirty(true),
46         mViewMatrixDirty(true),
47         mProjMatrixDirty(true),
48         mWorldViewMatrixDirty(true),
49         mViewProjMatrixDirty(true),
50         mWorldViewProjMatrixDirty(true),
51         mInverseWorldMatrixDirty(true),
52         mInverseWorldViewMatrixDirty(true),
53         mInverseViewMatrixDirty(true),
54         mInverseTransposeWorldMatrixDirty(true),
55         mInverseTransposeWorldViewMatrixDirty(true),
56         mCameraPositionObjectSpaceDirty(true),
57         mCameraPositionDirty(true),
58         mTextureViewProjMatrixDirty(true),
59         mCurrentRenderable(NULL),
60         mCurrentCamera(NULL),
61         mCurrentTextureProjector(NULL),
62         mCurrentRenderTarget(NULL),
63         mCurrentViewport(NULL)
64    {
65        mBlankLight.setDiffuseColour(ColourValue::Black);
66        mBlankLight.setSpecularColour(ColourValue::Black);
67        mBlankLight.setAttenuation(0,0,0,0);
68    }
69    //-----------------------------------------------------------------------------
70    AutoParamDataSource::~AutoParamDataSource()
71    {
72    }
73    //-----------------------------------------------------------------------------
74    void AutoParamDataSource::setCurrentRenderable(const Renderable* rend)
75    {
76                mCurrentRenderable = rend;
77                mWorldMatrixDirty = true;
78        mViewMatrixDirty = true;
79        mProjMatrixDirty = true;
80                mWorldViewMatrixDirty = true;
81        mViewProjMatrixDirty = true;
82                mWorldViewProjMatrixDirty = true;
83                mInverseWorldMatrixDirty = true;
84        mInverseViewMatrixDirty = true;
85                mInverseWorldViewMatrixDirty = true;
86                mInverseTransposeWorldMatrixDirty = true;
87                mInverseTransposeWorldViewMatrixDirty = true;
88                mCameraPositionObjectSpaceDirty = true;
89    }
90    //-----------------------------------------------------------------------------
91    void AutoParamDataSource::setCurrentCamera(const Camera* cam)
92    {
93        mCurrentCamera = cam;
94        mViewMatrixDirty = true;
95        mProjMatrixDirty = true;
96        mWorldViewMatrixDirty = true;
97        mViewProjMatrixDirty = true;
98        mWorldViewProjMatrixDirty = true;
99        mInverseViewMatrixDirty = true;
100        mInverseWorldViewMatrixDirty = true;
101        mInverseTransposeWorldViewMatrixDirty = true;
102        mCameraPositionObjectSpaceDirty = true;
103        mCameraPositionDirty = true;
104    }
105    //-----------------------------------------------------------------------------
106    void AutoParamDataSource::setCurrentLightList(const LightList* ll)
107    {
108        mCurrentLightList = ll;
109    }
110    //-----------------------------------------------------------------------------
111    const Matrix4& AutoParamDataSource::getWorldMatrix(void) const
112    {
113        if (mWorldMatrixDirty)
114        {
115            mCurrentRenderable->getWorldTransforms(mWorldMatrix);
116            mWorldMatrixCount = mCurrentRenderable->getNumWorldTransforms();
117            mWorldMatrixDirty = false;
118        }
119        return mWorldMatrix[0];
120    }
121    //-----------------------------------------------------------------------------
122    size_t AutoParamDataSource::getWorldMatrixCount(void) const
123    {
124        if (mWorldMatrixDirty)
125        {
126            mCurrentRenderable->getWorldTransforms(mWorldMatrix);
127            mWorldMatrixCount = mCurrentRenderable->getNumWorldTransforms();
128            mWorldMatrixDirty = false;
129        }
130        return mWorldMatrixCount;
131    }
132    //-----------------------------------------------------------------------------
133    const Matrix4* AutoParamDataSource::getWorldMatrixArray(void) const
134    {
135        if (mWorldMatrixDirty)
136        {
137            mCurrentRenderable->getWorldTransforms(mWorldMatrix);
138            mWorldMatrixCount = mCurrentRenderable->getNumWorldTransforms();
139            mWorldMatrixDirty = false;
140        }
141        return mWorldMatrix;
142    }
143    //-----------------------------------------------------------------------------
144    const Matrix4& AutoParamDataSource::getViewMatrix(void) const
145    {
146        if (mViewMatrixDirty)
147        {
148            if (mCurrentRenderable && mCurrentRenderable->useIdentityView())
149                mViewMatrix = Matrix4::IDENTITY;
150            else
151                mViewMatrix = mCurrentCamera->getViewMatrix(true);
152            mViewMatrixDirty = false;
153        }
154        return mViewMatrix;
155    }
156    //-----------------------------------------------------------------------------
157    const Matrix4& AutoParamDataSource::getViewProjectionMatrix(void) const
158    {
159        if (mViewProjMatrixDirty)
160        {
161            mViewProjMatrix = getProjectionMatrix() * getViewMatrix();
162            mViewProjMatrixDirty = false;
163        }
164        return mViewProjMatrix;
165    }
166    //-----------------------------------------------------------------------------
167    const Matrix4& AutoParamDataSource::getProjectionMatrix(void) const
168    {
169        if (mProjMatrixDirty)
170        {
171            // NB use API-independent projection matrix since GPU programs
172            // bypass the API-specific handedness and use right-handed coords
173            if (mCurrentRenderable && mCurrentRenderable->useIdentityProjection())
174            {
175                // Use identity projection matrix, still need to take RS depth into account.
176                RenderSystem* rs = Root::getSingleton().getRenderSystem();
177                rs->_convertProjectionMatrix(Matrix4::IDENTITY, mProjectionMatrix, true);
178            }
179            else
180            {
181                mProjectionMatrix = mCurrentCamera->getProjectionMatrixWithRSDepth();
182            }
183            if (mCurrentRenderTarget && mCurrentRenderTarget->requiresTextureFlipping())
184            {
185                // Because we're not using setProjectionMatrix, this needs to be done here
186                // Invert transformed y
187                mProjectionMatrix[1][0] = -mProjectionMatrix[1][0];
188                mProjectionMatrix[1][1] = -mProjectionMatrix[1][1];
189                mProjectionMatrix[1][2] = -mProjectionMatrix[1][2];
190                mProjectionMatrix[1][3] = -mProjectionMatrix[1][3];
191            }
192            mProjMatrixDirty = false;
193        }
194        return mProjectionMatrix;
195    }
196    //-----------------------------------------------------------------------------
197    const Matrix4& AutoParamDataSource::getWorldViewMatrix(void) const
198    {
199        if (mWorldViewMatrixDirty)
200        {
201            mWorldViewMatrix = getViewMatrix() * getWorldMatrix();
202            mWorldViewMatrixDirty = false;
203        }
204        return mWorldViewMatrix;
205    }
206    //-----------------------------------------------------------------------------
207    const Matrix4& AutoParamDataSource::getWorldViewProjMatrix(void) const
208    {
209        if (mWorldViewProjMatrixDirty)
210        {
211            mWorldViewProjMatrix = getProjectionMatrix() * getWorldViewMatrix();
212            mWorldViewProjMatrixDirty = false;
213        }
214        return mWorldViewProjMatrix;
215    }
216    //-----------------------------------------------------------------------------
217    const Matrix4& AutoParamDataSource::getInverseWorldMatrix(void) const
218    {
219        if (mInverseWorldMatrixDirty)
220        {
221            mInverseWorldMatrix = getWorldMatrix().inverse();
222            mInverseWorldMatrixDirty = false;
223        }
224        return mInverseWorldMatrix;
225    }
226    //-----------------------------------------------------------------------------
227    const Matrix4& AutoParamDataSource::getInverseWorldViewMatrix(void) const
228    {
229        if (mInverseWorldViewMatrixDirty)
230        {
231            mInverseWorldViewMatrix = getWorldViewMatrix().inverse();
232            mInverseWorldViewMatrixDirty = false;
233        }
234        return mInverseWorldViewMatrix;
235    }
236    //-----------------------------------------------------------------------------
237    const Matrix4& AutoParamDataSource::getInverseViewMatrix(void) const
238    {
239        if (mInverseViewMatrixDirty)
240        {
241            mInverseViewMatrix = getViewMatrix().inverse();
242            mInverseViewMatrixDirty = false;
243        }
244        return mInverseViewMatrix;
245    }
246    //-----------------------------------------------------------------------------
247    const Matrix4& AutoParamDataSource::getInverseTransposeWorldMatrix(void) const
248    {
249        if (mInverseTransposeWorldMatrixDirty)
250        {
251            mInverseTransposeWorldMatrix = getInverseWorldMatrix().transpose();
252            mInverseTransposeWorldMatrixDirty = false;
253        }
254        return mInverseWorldMatrix;
255    }
256    //-----------------------------------------------------------------------------
257    const Matrix4& AutoParamDataSource::getInverseTransposeWorldViewMatrix(void) const
258    {
259        if (mInverseTransposeWorldViewMatrixDirty)
260        {
261            mInverseTransposeWorldViewMatrix = getInverseWorldViewMatrix().transpose();
262            mInverseTransposeWorldViewMatrixDirty = false;
263        }
264        return mInverseWorldViewMatrix;
265    }
266    //-----------------------------------------------------------------------------
267    const Vector4& AutoParamDataSource::getCameraPosition(void) const
268    {
269        if(mCameraPositionDirty)
270        {
271            Vector3 vec3 = mCurrentCamera->getDerivedPosition();
272            mCameraPosition[0] = vec3[0];
273            mCameraPosition[1] = vec3[1];
274            mCameraPosition[2] = vec3[2];
275            mCameraPosition[3] = 1.0;
276            mCameraPositionDirty = false;
277        }
278        return mCameraPosition;
279    }   
280    //-----------------------------------------------------------------------------
281    const Vector4& AutoParamDataSource::getCameraPositionObjectSpace(void) const
282    {
283        if (mCameraPositionObjectSpaceDirty)
284        {
285            mCameraPositionObjectSpace =
286                getInverseWorldMatrix() * mCurrentCamera->getDerivedPosition();
287            mCameraPositionObjectSpaceDirty = false;
288        }
289        return mCameraPositionObjectSpace;
290    }
291    //-----------------------------------------------------------------------------
292    const Light& AutoParamDataSource::getLight(size_t index) const
293    {
294        // If outside light range, return a blank light to ensure zeroised for program
295        if (mCurrentLightList->size() <= index)
296        {
297            return mBlankLight;
298        }
299        else
300        {
301            return *((*mCurrentLightList)[index]);
302        }
303    }
304    //-----------------------------------------------------------------------------
305        void AutoParamDataSource::setAmbientLightColour(const ColourValue& ambient)
306        {
307                mAmbientLight = ambient;
308        }
309    //-----------------------------------------------------------------------------
310        const ColourValue& AutoParamDataSource::getAmbientLightColour(void) const
311        {
312                return mAmbientLight;
313               
314        }
315    //-----------------------------------------------------------------------------
316    void AutoParamDataSource::setFog(FogMode mode, const ColourValue& colour,
317        Real expDensity, Real linearStart, Real linearEnd)
318    {
319        (void)mode; // ignored
320        mFogColour = colour;
321        mFogParams.x = expDensity;
322        mFogParams.y = linearStart;
323        mFogParams.z = linearEnd;
324        mFogParams.w = 1 / (linearEnd - linearStart);
325    }
326    //-----------------------------------------------------------------------------
327    const ColourValue& AutoParamDataSource::getFogColour(void) const
328    {
329        return mFogColour;
330    }
331    //-----------------------------------------------------------------------------
332    const Vector4& AutoParamDataSource::getFogParams(void) const
333    {
334        return mFogParams;
335    }
336    //-----------------------------------------------------------------------------
337    void AutoParamDataSource::setTextureProjector(const Frustum* frust)
338    {
339        mCurrentTextureProjector = frust;
340        mTextureViewProjMatrixDirty = true;
341
342    }
343    //-----------------------------------------------------------------------------
344    const Matrix4& AutoParamDataSource::getTextureViewProjMatrix(void) const
345    {
346        if (mTextureViewProjMatrixDirty)
347        {
348            mTextureViewProjMatrix =
349                PROJECTIONCLIPSPACE2DTOIMAGESPACE_PERSPECTIVE *
350                mCurrentTextureProjector->getProjectionMatrixWithRSDepth() *
351                                mCurrentTextureProjector->getViewMatrix();
352            mTextureViewProjMatrixDirty = false;
353        }
354        return mTextureViewProjMatrix;
355    }
356    //-----------------------------------------------------------------------------
357    void AutoParamDataSource::setCurrentRenderTarget(const RenderTarget* target)
358    {
359        mCurrentRenderTarget = target;
360    }
361    //-----------------------------------------------------------------------------
362    const RenderTarget* AutoParamDataSource::getCurrentRenderTarget(void) const
363    {
364        return mCurrentRenderTarget;
365    }
366    //-----------------------------------------------------------------------------
367    void AutoParamDataSource::setCurrentViewport(const Viewport* viewport)
368    {
369        mCurrentViewport = viewport;
370    }
371    //-----------------------------------------------------------------------------
372        void AutoParamDataSource::setShadowDirLightExtrusionDistance(Real dist)
373        {
374                mDirLightExtrusionDistance = dist;
375        }
376    //-----------------------------------------------------------------------------
377        Real AutoParamDataSource::getShadowExtrusionDistance(void) const
378        {
379                const Light& l = getLight(0); // only ever applies to one light at once
380                if (l.getType() == Light::LT_DIRECTIONAL)
381                {
382                        // use constant
383                        return mDirLightExtrusionDistance;
384                }
385                else
386                {
387                        // Calculate based on object space light distance
388                        // compared to light attenuation range
389                        Vector3 objPos = getInverseWorldMatrix() *
390                                l.getDerivedPosition();
391                        return l.getAttenuationRange() - objPos.length();
392                }
393        }
394    //-----------------------------------------------------------------------------
395    const Renderable* AutoParamDataSource::getCurrentRenderable(void) const
396    {
397        return mCurrentRenderable;
398    }
399        //-----------------------------------------------------------------------------
400        Matrix4 AutoParamDataSource::getInverseViewProjMatrix(void) const
401        {
402                return this->getViewProjectionMatrix().inverse();
403        }
404        //-----------------------------------------------------------------------------
405        Matrix4 AutoParamDataSource::getInverseTransposeViewProjMatrix(void) const
406        {
407                return this->getInverseViewProjMatrix().transpose();
408        }
409        //-----------------------------------------------------------------------------
410        Matrix4 AutoParamDataSource::getTransposeViewProjMatrix(void) const
411        {
412                return this->getViewProjectionMatrix().transpose();
413        }
414        //-----------------------------------------------------------------------------
415        Matrix4 AutoParamDataSource::getTransposeViewMatrix(void) const
416        {
417                return this->getViewMatrix().transpose();
418        }
419        //-----------------------------------------------------------------------------
420        Matrix4 AutoParamDataSource::getTransposeProjectionMatrix(void) const
421        {
422                return this->getProjectionMatrix().transpose();
423        }
424        //-----------------------------------------------------------------------------
425        Matrix4 AutoParamDataSource::getInverseProjectionMatrix(void) const
426        {
427                return this->getProjectionMatrix().inverse();
428        }
429        //-----------------------------------------------------------------------------
430        Matrix4 AutoParamDataSource::getInverseTransposeProjectionMatrix(void) const
431        {
432                return this->getInverseProjectionMatrix().transpose();
433        }
434        //-----------------------------------------------------------------------------
435        Matrix4 AutoParamDataSource::getTransposeWorldViewProjMatrix(void) const
436        {
437                return this->getWorldViewProjMatrix().transpose();
438        }
439        //-----------------------------------------------------------------------------
440        Matrix4 AutoParamDataSource::getInverseWorldViewProjMatrix(void) const
441        {
442                return this->getWorldViewProjMatrix().inverse();
443        }
444        //-----------------------------------------------------------------------------
445        Matrix4 AutoParamDataSource::getInverseTransposeWorldViewProjMatrix(void) const
446        {
447                return this->getInverseWorldViewProjMatrix().transpose();
448        }
449        //-----------------------------------------------------------------------------
450        Matrix4 AutoParamDataSource::getTransposeWorldViewMatrix(void) const
451        {
452                return this->getWorldViewMatrix().transpose();
453        }
454        //-----------------------------------------------------------------------------
455        Matrix4 AutoParamDataSource::getTransposeWorldMatrix(void) const
456        {
457                return this->getWorldMatrix().transpose();
458        }
459    //-----------------------------------------------------------------------------
460    Real AutoParamDataSource::getTime(void) const
461    {
462        return ControllerManager::getSingleton().getElapsedTime();
463    }
464        //-----------------------------------------------------------------------------
465        Real AutoParamDataSource::getTime_0_X(Real x) const
466        {
467                return fmod(this->getTime(), x);
468        }
469        //-----------------------------------------------------------------------------
470        Real AutoParamDataSource::getCosTime_0_X(Real x) const
471        {
472                return cos(this->getTime_0_X(x));
473        }
474        //-----------------------------------------------------------------------------
475        Real AutoParamDataSource::getSinTime_0_X(Real x) const
476        {
477                return sin(this->getTime_0_X(x));
478        }
479        //-----------------------------------------------------------------------------
480        Real AutoParamDataSource::getTanTime_0_X(Real x) const
481        {
482                return tan(this->getTime_0_X(x));
483        }
484        //-----------------------------------------------------------------------------
485        Vector4 AutoParamDataSource::getTime_0_X_packed(Real x) const
486        {
487                Real t = this->getTime_0_X(x);
488                return Vector4(t, sin(t), cos(t), tan(t));
489        }
490        //-----------------------------------------------------------------------------
491        Real AutoParamDataSource::getTime_0_1(Real x) const
492        {
493                return this->getTime_0_X(x)/x;
494        }
495        //-----------------------------------------------------------------------------
496        Real AutoParamDataSource::getCosTime_0_1(Real x) const
497        {
498                return cos(this->getTime_0_1(x));
499        }
500        //-----------------------------------------------------------------------------
501        Real AutoParamDataSource::getSinTime_0_1(Real x) const
502        {
503                return sin(this->getTime_0_1(x));
504        }
505        //-----------------------------------------------------------------------------
506        Real AutoParamDataSource::getTanTime_0_1(Real x) const
507        {
508                return tan(this->getTime_0_1(x));
509        }
510        //-----------------------------------------------------------------------------
511        Vector4 AutoParamDataSource::getTime_0_1_packed(Real x) const
512        {
513                Real t = this->getTime_0_1(x);
514                return Vector4(t, sin(t), cos(t), tan(t));
515        }
516        //-----------------------------------------------------------------------------
517        Real AutoParamDataSource::getTime_0_2Pi(Real x) const
518        {
519                return this->getTime_0_X(x)/x*2*Math::PI;
520        }
521        //-----------------------------------------------------------------------------
522        Real AutoParamDataSource::getCosTime_0_2Pi(Real x) const
523        {
524                return cos(this->getTime_0_2Pi(x));
525        }
526        //-----------------------------------------------------------------------------
527        Real AutoParamDataSource::getSinTime_0_2Pi(Real x) const
528        {
529                return sin(this->getTime_0_2Pi(x));
530        }
531        //-----------------------------------------------------------------------------
532        Real AutoParamDataSource::getTanTime_0_2Pi(Real x) const
533        {
534                return tan(this->getTime_0_2Pi(x));
535        }
536        //-----------------------------------------------------------------------------
537        Vector4 AutoParamDataSource::getTime_0_2Pi_packed(Real x) const
538        {
539                Real t = this->getTime_0_2Pi(x);
540                return Vector4(t, sin(t), cos(t), tan(t));
541        }
542        //-----------------------------------------------------------------------------
543        Real AutoParamDataSource::getFrameTime(void) const
544        {
545                return ControllerManager::getSingleton().getFrameTimeSource()->getValue();
546        }
547        //-----------------------------------------------------------------------------
548        Real AutoParamDataSource::getFPS() const
549        {
550                return mCurrentRenderTarget->getLastFPS();
551        }
552        //-----------------------------------------------------------------------------
553        Real AutoParamDataSource::getViewportWidth() const
554        {
555                return mCurrentViewport->getActualWidth();
556        }
557        //-----------------------------------------------------------------------------
558        Real AutoParamDataSource::getViewportHeight() const
559        {
560                return mCurrentViewport->getActualHeight();
561        }
562        //-----------------------------------------------------------------------------
563        Real AutoParamDataSource::getInverseViewportWidth() const
564        {
565                return 1.0f/mCurrentViewport->getActualWidth();
566        }
567        //-----------------------------------------------------------------------------
568        Real AutoParamDataSource::getInverseViewportHeight() const
569        {
570                return 1.0f/mCurrentViewport->getActualHeight();
571        }
572        //-----------------------------------------------------------------------------
573        Vector3 AutoParamDataSource::getViewDirection() const
574        {
575                return mCurrentCamera->getDerivedDirection();
576        }
577        //-----------------------------------------------------------------------------
578        Vector3 AutoParamDataSource::getViewSideVector() const
579        {
580                return mCurrentCamera->getDerivedRight();
581        }
582        //-----------------------------------------------------------------------------
583        Vector3 AutoParamDataSource::getViewUpVector() const
584        {
585                return mCurrentCamera->getDerivedUp();
586        }
587        //-----------------------------------------------------------------------------
588        Real AutoParamDataSource::getFOV() const
589        {
590                return mCurrentCamera->getFOVy().valueRadians();
591        }
592        //-----------------------------------------------------------------------------
593        Real AutoParamDataSource::getNearClipDistance() const
594        {
595                return mCurrentCamera->getNearClipDistance();
596        }
597        //-----------------------------------------------------------------------------
598        Real AutoParamDataSource::getFarClipDistance() const
599        {
600                return mCurrentCamera->getFarClipDistance();
601        }
602        //-----------------------------------------------------------------------------
603    int AutoParamDataSource::getPassNumber(void) const
604    {
605        return mPassNumber;
606    }
607        //-----------------------------------------------------------------------------
608    void AutoParamDataSource::setPassNumber(const int passNumber)
609    {
610        mPassNumber = passNumber;
611    }
612        //-----------------------------------------------------------------------------
613    void AutoParamDataSource::incPassNumber(void)
614    {
615        ++mPassNumber;
616    }
617        //-----------------------------------------------------------------------------
618
619}
620
Note: See TracBrowser for help on using the repository browser.