Browse Source

Revert "Caching light position relative to camera."

* The light didn't catch camera transformation changes, so it was
   returning wrong position for most of the time.
 * The multiplication was in wrong order, it should be multiplied with
   camera matrix from the left.

I need to find an solution for this, because now it is one redundant
matrix*vector multiplication per object per frame again.

This reverts commit 0443bbe286.

Conflicts:
	src/Light.cpp
	src/Test/LightTest.cpp
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
a894c434b2
  1. 15
      src/Light.cpp
  2. 15
      src/Light.h
  3. 1
      src/Test/CMakeLists.txt
  4. 63
      src/Test/LightTest.cpp
  5. 32
      src/Test/LightTest.h

15
src/Light.cpp

@ -14,26 +14,13 @@
*/ */
#include "Light.h" #include "Light.h"
#include "Camera.h"
namespace Magnum { namespace Magnum {
Vector3 Light::position(Camera* camera) {
CORRADE_ASSERT(scene() && camera->scene() == scene(), "Light: camera and light aren't in the same scene!", Vector3())
if(camera != _camera) {
_camera = camera;
setDirty();
}
setClean();
return _position;
}
void Light::clean(const Matrix4& absoluteTransformation) { void Light::clean(const Matrix4& absoluteTransformation) {
Object::clean(absoluteTransformation); Object::clean(absoluteTransformation);
_position = (absoluteTransformation*_camera->cameraMatrix())[3].xyz(); _position = absoluteTransformation[3];
} }
} }

15
src/Light.h

@ -34,15 +34,15 @@ class Light: public Object {
* @brief Constructor * @brief Constructor
* @param parent Parent object * @param parent Parent object
*/ */
inline Light(Object* parent = nullptr): Object(parent), _camera(nullptr) {} inline Light(Object* parent = nullptr): Object(parent) {}
/** /**
* @brief Light position relative to given camera * @brief Light position relative to root object (scene)
*
* The position is cached until the camera is changed to another or
* the light dirty bit is set.
*/ */
Vector3 position(Camera* camera); inline Vector4 position() {
setClean();
return _position;
}
protected: protected:
/** /**
@ -51,8 +51,7 @@ class Light: public Object {
void clean(const Matrix4& absoluteTransformation); void clean(const Matrix4& absoluteTransformation);
private: private:
Camera* _camera; Vector4 _position;
Vector3 _position;
}; };
} }

1
src/Test/CMakeLists.txt

@ -1,4 +1,3 @@
corrade_add_test(ObjectTest ObjectTest.h ObjectTest.cpp MagnumTestLib) corrade_add_test(ObjectTest ObjectTest.h ObjectTest.cpp MagnumTestLib)
corrade_add_test(CameraTest CameraTest.h CameraTest.cpp Magnum) corrade_add_test(CameraTest CameraTest.h CameraTest.cpp Magnum)
corrade_add_test(LightTest LightTest.h LightTest.cpp MagnumTestLib)
corrade_add_test(SceneTest SceneTest.h SceneTest.cpp Magnum) corrade_add_test(SceneTest SceneTest.h SceneTest.cpp Magnum)

63
src/Test/LightTest.cpp

@ -1,63 +0,0 @@
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include "LightTest.h"
#include <sstream>
#include <QtTest/QTest>
#include "Camera.h"
#include "Light.h"
#include "Scene.h"
QTEST_APPLESS_MAIN(Magnum::Test::LightTest)
using namespace std;
using namespace Corrade::Utility;
namespace Magnum { namespace Test {
void LightTest::positionWrongCamera() {
stringstream ss;
Error::setOutput(&ss);
Camera c;
Light l;
QVERIFY(l.position(&c) == Vector3());
QVERIFY(ss.str() == "Light: camera and light aren't in the same scene!\n");
}
void LightTest::position() {
Scene s;
Object lightParent(&s);
lightParent.translate(Vector3::zAxis(3));
Light light(&lightParent);
Object cameraParent(&s);
cameraParent.rotate(deg(90.0f), Vector3::xAxis());
Camera camera(&cameraParent);
QVERIFY(light.position(&camera) == (Matrix4::translation(Vector3::zAxis(3))*
Matrix4::rotation(deg(90.0f), Vector3::xAxis()).inverted())[3].xyz());
/* Set another camera */
Camera another(&cameraParent);
another.scale(Vector3(3.0f));
QVERIFY(light.position(&another) == (Matrix4::translation(Vector3::zAxis(3))*
(Matrix4::scaling(Vector3(3.0f))*Matrix4::rotation(deg(90.0f), Vector3::xAxis())).inverted())[3].xyz());
}
}}

32
src/Test/LightTest.h

@ -1,32 +0,0 @@
#ifndef Magnum_Test_LightTest_h
#define Magnum_Test_LightTest_h
/*
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz>
This file is part of Magnum.
Magnum is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 3
only, as published by the Free Software Foundation.
Magnum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License version 3 for more details.
*/
#include <QtCore/QObject>
namespace Magnum { namespace Test {
class LightTest: public QObject {
Q_OBJECT
private slots:
void positionWrongCamera();
void position();
};
}}
#endif
Loading…
Cancel
Save