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

Revision 657, 21.3 KB checked in by mattausch, 18 years ago (diff)

added ogre dependencies and patched ogre sources

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