diff --git a/engine/Application.cpp b/engine/Application.cpp new file mode 100644 index 0000000..862fd7d --- /dev/null +++ b/engine/Application.cpp @@ -0,0 +1,220 @@ +#include "Application.h" + +#include "Utilities/CfgParser.h" +#include "Utilities/Log.h" + +#include "ScriptSystem_Application.h" +#include "ScriptSystem_Camera.h" +#include "ScriptSystem_Font.h" +#include "ScriptSystem_Image.h" +#include "ScriptSystem_Math.h" +#include "ScriptSystem_RigidBody.h" + +namespace BlueCore +{ + +void Application::KeySlot(int key, int action) +{ + if (key == GLFW_KEY_ESC && action == GLFW_RELEASE) + quit(); +} + +void Application::MouseMoveSlot(int x, int y) +{ + _CameraPhi += (x - _LastMouseX) / 200.0; + _LastMouseX = x; + + _CameraTheta += (y - _LastMouseY) / 200.0; + _LastMouseY = y; +} + +void Application::MouseWheelSlot(int z) +{ + if ((z - _LastWheel) < 0) + _CameraRadius *= 2.0; + else + _CameraRadius *= 0.5; + + if (_CameraRadius < 1.0) + _CameraRadius = 1.0; + + _LastWheel = z; +} + +bool Application::initialize() +{ + CfgParser cfg; + cfg.parseFile("options.cfg"); + + int width = cfg.get("width", 640); + int height = cfg.get("height", 480); + bool fullscreen = cfg.get("fullscreen", false); + + _Window = new RenderWindow(); + if (_Window->create(width, height, 0, 0, 0, fullscreen) == false) + return false; + + _Device = new RenderDevice(_Window); + _FontManager = new FontManager(_Device); + _MeshManager = new MeshManager(_Device); + _TextureManager = new TextureManager(); + _ScriptSystem = new ScriptSystem(); + _ShaderManager = new ShaderManager(_Window); + _Simulation = new RigidBodySimulation(_ScriptSystem); + _ModelManager = new ModelManager (_TextureManager, _ShaderManager, _MeshManager); + _RenderQueue = new RenderQueue(); + + setupScriptSystem_Application(_ScriptSystem, this); + setupScriptSystem_Camera(_ScriptSystem); + setupScriptSystem_Font(_ScriptSystem, _FontManager); + setupScriptSystem_Image(_ScriptSystem, _TextureManager, _Device); + setupScriptSystem_Math(_ScriptSystem); + setupScriptSystem_RigidBody(_ScriptSystem, _Simulation); + + if (_ScriptSystem->loadScript("main") == false) + return false; + + _ScriptSystem->callFunction("Initialize"); + + _Window->KeySignal.connect(this, &Application::KeySlot); + _Window->MouseMoveSignal.connect(this, &Application::MouseMoveSlot); + _Window->MouseWheelSignal.connect(this, &Application::MouseWheelSlot); + + _Running = true; + + return true; +} + +void Application::shutdown() +{ + _ScriptSystem->callFunction("Shutdown"); +} + +void Application::quit() +{ + _Running = false; +} + +void Application::run() +{ + clog << "--- starting main loop..."<< endlog; + + _SimulationTime = 0.0; + _StartTime = glfwGetTime(); + _LastTime = _StartTime; + + double delta = 0.0; + + _Paused = false; + _Running = true; + + while (_Window->isOpen() && _Running) + { + double now = glfwGetTime(); + delta = now - _LastTime; + _LastTime = now; + + _ScriptSystem->callFunction("OnFrame", delta, now - _StartTime); + + _Device->clear(); + + //Quaternion q(-_CameraPhi, 0.0, -_CameraTheta); + //camera->setPosition(q.apply(Vector3(0.0, 0.0, _CameraRadius))); + //camera->setRotation(q); + //pos += 5 * _DeltaTime; + if (_SceneGraph.valid()) + { + _SceneGraph->queue(_RenderQueue); + _RenderQueue->render(_Device); + } + /* + { + _Device->begin3D(_Camera); + _Device->setAmbientLight(1.0, 1.0, 1.0); + _RenderQueue->clear(); + _RenderQueue->addOpaqueItem(model, Vector3(0.0, 0.0, 0.0), + Quaternion()); + _RenderQueue->render(_Device); + _Device->end3D(); + } + */ + /* + device->pushAbsoluteTransformation(Vector3(), camera->getRotation()); + + class RenderState + { + bool _Blending; + GLint _BlendFuncSrc; + GLint _BlendFuncDest; + + bool _DepthTest; + bool _DepthMask; + + bool _Lighting; + }; + GLfloat mat_specular[] = + { 1.0, 1.0, 1.0, 1.0 }; + GLfloat mat_shininess[] = + { 2.0 }; + glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_specular); + glMaterialfv(GL_FRONT, GL_AMBIENT, mat_specular); + glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); + glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); + + glEnable(GL_BLEND); + glBlendFunc(GL_ONE, GL_ONE); + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_FALSE); + glDisable(GL_LIGHTING); + glColor4d( 1.0f, 1.0f, 1.0f, 1.0f); + + device->setTexture(0, starTexture, true); + starMesh->render(); + + glDisable(GL_BLEND); + device->popTransformation(); + */ + // device->useShader (program); + // device->setTexture (stage, name, texture) + // device->clearTextures (stage+1); + //glEnable (GL_TEXTURE_2D); + //glEnable (GL_LIGHTING); + //glBindTexture (GL_TEXTURE_2D, texture->getId() ); + + // device-> + //device-> + //model->render(); + if (_Paused == false) + { + _Simulation->saveStates(); + _Simulation->updateSteps(delta); + while (_Simulation->getSteps() > 0) + { + _ScriptSystem->callFunction("OnStep", + _Simulation->getStepSize(), _SimulationTime); + _SimulationTime += _Simulation->getStepSize(); + _Simulation->step(); + } + } + + _Device->begin2D(); + _ScriptSystem->callFunction("OnOverlay", delta, now - _StartTime); + _Device->end2D(); + + _Device->swap(); + } + + clog << "--- main loop finished..."<< endlog; +} + +void Application::setSceneGraph(SceneGraph *graph) +{ + _SceneGraph = graph; +} + +void Application::togglePause() +{ + _Paused = !_Paused; +} +} + diff --git a/engine/Application.h b/engine/Application.h new file mode 100644 index 0000000..591b461 --- /dev/null +++ b/engine/Application.h @@ -0,0 +1,60 @@ +#ifndef BLUECORE_APPLICATION_H +#define BLUECORE_APPLICATION_H + +#include "RenderWindow.h" +#include "RenderDevice.h" +#include "FontManager.h" +#include "MeshManager.h" +#include "TextureManager.h" +#include "ShaderManager.h" +#include "ModelManager.h" +#include "TextureImage.h" +#include "ScriptSystem.h" +#include "RigidBodySimulation.h" +#include "SceneGraph.h" + +namespace BlueCore +{ +class Application : public Referenced, public sigslot::has_slots<> +{ + bool _Running; + bool _Paused; + + double _SimulationTime; + double _StartTime; + double _LastTime; + + double _CameraPhi, _CameraTheta, _CameraRadius; + int _LastMouseX, _LastMouseY, _LastWheel; + + ref_ptr _Window; + ref_ptr _Device; + ref_ptr _FontManager; + ref_ptr _MeshManager; + ref_ptr _TextureManager; + ref_ptr _ScriptSystem; + ref_ptr _ShaderManager; + ref_ptr _Simulation; + ref_ptr _ModelManager; + ref_ptr _RenderQueue; + ref_ptr _SceneGraph; + + + void KeySlot(int key, int action); + void MouseMoveSlot(int x, int y); + void MouseWheelSlot(int z); + +public: + + bool initialize(); + void shutdown(); + void quit(); + void run(); + void togglePause(); + + void setSceneGraph (SceneGraph *scenegraph); +}; + +} + +#endif /*APPLICATION_H_*/ diff --git a/engine/Camera.cpp b/engine/Camera.cpp index 5daaa46..fe453b8 100644 --- a/engine/Camera.cpp +++ b/engine/Camera.cpp @@ -7,44 +7,36 @@ namespace BlueCore { - -Camera::Camera() +Camera::Camera() : + _LookAt (false), _FoV(45.0), _NearPlane(1.0), _FarPlane(15000.0) { } - Camera::~Camera() { } - void Camera::setFoV(Scalar fov) { _FoV = fov; } - -void Camera::setAspectRatio(Scalar aspect) -{ - _AspectRatio = aspect; -} - - void Camera::setNearPlane(Scalar near) { _NearPlane = near; } - void Camera::setFarPlane(Scalar far) { _FarPlane = far; } - void Camera::setPosition(const Vector3& position) { _Position = position; + + if (_LookAt) + updateLookAtRotation(); } const Vector3& Camera::getPosition() @@ -52,7 +44,6 @@ const Vector3& Camera::getPosition() return _Position; } - void Camera::setRotation(const Quaternion& rotation) { _Rotation = rotation; @@ -63,69 +54,6 @@ const Quaternion& Camera::getRotation() return _Rotation; } -#if 0 - -void Camera::setupProjectionMatrix() -{ - Scalar fW, fH; - - fH = tan ( (_FoV / 2) / 180* Pi ) * _NearPlane; - fW = fH * _AspectRatio; - - // setup projectiom matrix - glMatrixMode (GL_PROJECTION ); - glLoadIdentity(); - glFrustum ( -fW, fW, -fH, fH, _NearPlane, _FarPlane ); - - // save variables for frustum culling - /* - _near = nearZ; - _far = farZ; - _hNear = tan ( ( fov / 2 ) / 180 * Pi ) * nearZ; - _wNear = _hNear * aspect; - _hFar = tan ( ( fov / 2 ) / 180 * Pi ) * farZ; - _wFar = _hFar * aspect; - */ -} - - -void Camera::setupViewMatrix() -{ - // set the view matrix - glMatrixMode (GL_MODELVIEW ); - glLoadIdentity(); - Matrix4x4 m(_Rotation, _Rotation.applyInversed(_Position * -1.0) ); - glMultMatrixd ( ( GLdouble * ) &m.m ); - /* - // calculate frustum planes - Vector3 up = q.apply ( Vector3 ( 0.0, 1.0, 0.0 ) ); - Vector3 right = q.apply ( Vector3 ( 1.0, 0.0, 0.0 ) ); - Vector3 d = q.apply ( Vector3 ( 0.0, 0.0, -1.0 ) ); - - Vector3 fc = p + d * _far; - - Vector3 ftl = fc + ( up * _hFar ) - ( right * _wFar ); - Vector3 ftr = fc + ( up * _hFar ) + ( right * _wFar ); - Vector3 fbl = fc - ( up * _hFar ) - ( right * _wFar ); - Vector3 fbr = fc - ( up * _hFar ) + ( right * _wFar ); - - Vector3 nc = p + d * _near; - - Vector3 ntl = nc + ( up * _hNear ) - ( right * _wNear ); - Vector3 ntr = nc + ( up * _hNear ) + ( right * _wNear ); - Vector3 nbl = nc - ( up * _hNear ) - ( right * _wNear ); - Vector3 nbr = nc - ( up * _hNear ) + ( right * _wNear ); - - _frustumPlanes[RightPlane] = Plane ( nbr, fbr, ntr ); - _frustumPlanes[LeftPlane] = Plane ( ntl, fbl, nbl ); - _frustumPlanes[BottomPlane] = Plane ( nbl, fbr, nbr ); - _frustumPlanes[TopPlane] = Plane ( ntr, ftl, ntl ); - _frustumPlanes[FarPlane] = Plane ( ftl, ftr, fbl ); - _frustumPlanes[NearPlane] = Plane ( ntl, nbl, ntr ); - */ -} -#endif - Scalar Camera::getNearPlane() const { return _NearPlane; @@ -141,5 +69,19 @@ Scalar Camera::getFov() const return _FoV; } -} // namespace BlueCore +void Camera::lookAt(const Vector3 &point) +{ + _LookAt = true; + _LookAtPoint = point; + _LookAtUp = Vector3(0.0, 1.0, 0.0); + updateLookAtRotation(); +} + +void Camera::updateLookAtRotation() +{ + Matrix3x3 m (_LookAtPoint - _Position, _LookAtUp); + _Rotation = m.toQuaternion().inversed(); +} + +} // namespace BlueCore diff --git a/engine/Camera.h b/engine/Camera.h index d3df420..0ea6e4e 100644 --- a/engine/Camera.h +++ b/engine/Camera.h @@ -19,12 +19,14 @@ private: Vector3 _LookAtPoint; Vector3 _LookAtUp; bool _LookAt; - Scalar _NearPlane, _FarPlane, _FoV, _AspectRatio; + Scalar _FoV, _NearPlane, _FarPlane; //Frustum _Frustum; Vector3 _Position; Quaternion _Rotation; + void updateLookAtRotation(); + public: Camera(); @@ -35,7 +37,6 @@ public: void lookStraight(); void setFoV(Scalar fov); - void setAspectRatio(Scalar aspect); void setNearPlane(Scalar near); void setFarPlane(Scalar far); @@ -48,7 +49,7 @@ public: Scalar getNearPlane() const; Scalar getFarPlane() const; Scalar getFov() const; - + //const Frustum &getFrustum() const; }; diff --git a/engine/Math/Matrix.h b/engine/Math/Matrix.h index 3d7c6db..26b38dd 100644 --- a/engine/Math/Matrix.h +++ b/engine/Math/Matrix.h @@ -12,855 +12,682 @@ namespace BlueCore { - template - class Matrix3x3Template +template class Matrix3x3Template +{ +public: + T m[9]; + + inline Matrix3x3Template() { - public: - T m[9]; + identity(); + } - /** - * default constructor - */ - inline Matrix3x3Template() - { - identity(); - } - - - /** - * constructor from array - */ - template - inline Matrix3x3Template( const S a[9] ) - { - m[0] = static_cast(a[0]); - m[1] = static_cast(a[1]); - m[2] = static_cast(a[2]); - - m[3] = static_cast(a[3]); - m[4] = static_cast(a[4]); - m[5] = static_cast(a[5]); - - m[6] = static_cast(a[6]); - m[7] = static_cast(a[7]); - m[8] = static_cast(a[8]); - } - - - /** - * copy constructor - */ - template - inline Matrix3x3Template( const Matrix3x3Template &a ) - { - m[0] = static_cast(a.m[0]); - m[1] = static_cast(a.m[1]); - m[2] = static_cast(a.m[2]); - - m[3] = static_cast(a.m[3]); - m[4] = static_cast(a.m[4]); - m[5] = static_cast(a.m[5]); - - m[6] = static_cast(a.m[6]); - m[7] = static_cast(a.m[7]); - m[8] = static_cast(a.m[8]); - } - - - /** - * constructor from heading and up vectors - */ - inline Matrix3x3Template( - const Vector3Template &h, - const Vector3Template &u ) - { - Vector3Template a, b, c; - c = h.unit(); - a = h.crossProduct( u ).unit(); - b = c.crossProduct( a ).unit(); - - m[0] = a.x; - m[1] = b.x; - m[2] = c.x; - - m[3] = a.y; - m[4] = b.y; - m[5] = c.y; - - m[6] = a.z; - m[7] = b.z; - m[8] = c.z; - } - - - /** - * contructor from euler angles - */ - inline Matrix3x3Template( T a, T e, T t ) - { - T ch = cos( a ); - T sh = sin( a ); - T ca = cos( e ); - T sa = sin( e ); - T cb = cos( t ); - T sb = sin( t ); - - m[0] = ch * ca; - m[1] = sh*sb - ch*sa*cb; - m[2] = ch*sa*sb + sh*cb; - - m[3] = sa; - m[4] = ca*cb; - m[5] = -ca*sb; - - m[6] = -sh*ca; - m[7] = sh*sa*cb + ch*sb; - m[8] = -sh*sa*sb + ch*cb; - } - - - /** - * contructor from quaternion - */ - template - inline Matrix3x3Template( const QuaternionTemplate &q ) - { - T xx = static_cast(q.x*q.x), - xy = static_cast(q.x*q.y), - xz = static_cast(q.x*q.z), - xw = static_cast(q.x*q.w), - - yy = static_cast(q.y*q.y), - yz = static_cast(q.y*q.z), - yw = static_cast(q.y*q.w), - - zz = static_cast(q.z*q.z), - zw = static_cast(q.z*q.w); - - m[0] = 1 - yy - yy - zz - zz; - m[1] = xy + xy + zw + zw; - m[2] = xz + xz - yw - yw; - - m[3] = xy + xy - zw - zw; - m[4] = 1 - xx - xx - zz - zz; - m[5] = yz + yz + xw + xw; - - m[6] = xz + xz + yw + yw; - m[7] = yz + yz - xw - xw; - m[8] = 1 - xx - xx - yy - yy; - } - - - /** - * set matrix to identity matrix - */ - inline void identity() - { - m[0] = static_cast(1); - m[1] = static_cast(0); - m[2] = static_cast(0); - - m[3] = static_cast(0); - m[4] = static_cast(1); - m[5] = static_cast(0); - - m[6] = static_cast(0); - m[7] = static_cast(0); - m[8] = static_cast(1); - } - - - /** - * get transposed matrix - */ - inline Matrix3x3Template transposed() const - { - Matrix3x3Template a; - - a.m[0] = m[0]; a.m[3] = m[1]; a.m[6] = m[2]; - a.m[1] = m[3]; a.m[4] = m[4]; a.m[7] = m[5]; - a.m[2] = m[6]; a.m[5] = m[7]; a.m[8] = m[8]; - - return a; - } - - - /** - * transpose - */ - inline void transpose() - { - swap( m[3], m[1] ); - swap( m[6], m[2] ); - swap( m[7], m[5] ); - } - - - /** - * determinate - */ - inline T determinant() const - { - return ( m[0]*m[4]*m[8] + m[1]*m[5]*m[6] + m[2]*m[3]*m[7] ) - - ( m[6]*m[4]*m[2] + m[7]*m[5]*m[0] + m[8]*m[3]*m[1] ); - } - - inline Matrix3x3Template inverted() - { - T det = determinant(); - T one_over_det = 1.0f / det; - - Matrix3x3Template result; - result.m[0] = +(m[4] * m[8] - m[5] * m[7]) * one_over_det; - result.m[1] = -(m[1] * m[8] - m[2] * m[7]) * one_over_det; - result.m[2] = +(m[1] * m[5] - m[2] * m[4]) * one_over_det; - result.m[3] = -(m[3] * m[8] - m[5] * m[6]) * one_over_det; - result.m[4] = +(m[0] * m[8] - m[2] * m[6]) * one_over_det; - result.m[5] = -(m[0] * m[5] - m[2] * m[3]) * one_over_det; - result.m[6] = +(m[3] * m[7] - m[4] * m[6]) * one_over_det; - result.m[7] = -(m[0] * m[7] - m[1] * m[6]) * one_over_det; - result.m[8] = +(m[0] * m[4] - m[1] * m[3]) * one_over_det; - - return result; - } - - - /** - * add/assign operator - */ - template - inline Matrix3x3Template &operator += ( - const Matrix3x3Template &a ) - { - m[0] += static_cast(a.m[0]); - m[1] += static_cast(a.m[1]); - m[2] += static_cast(a.m[2]); - - m[3] += static_cast(a.m[3]); - m[4] += static_cast(a.m[4]); - m[5] += static_cast(a.m[5]); - - m[6] += static_cast(a.m[6]); - m[7] += static_cast(a.m[7]); - m[8] += static_cast(a.m[8]); - - return *this; - } - - - /** - * matrix multiplication - */ - template - inline Matrix3x3Template operator * ( const Matrix3x3Template &a ) - { - Matrix3x3Template result; - - result.m[0] = m[0] * static_cast(a.m[0]) - + m[3] * static_cast(a.m[1]) - + m[6] * static_cast(a.m[2]); - - result.m[1] = m[1] * static_cast(a.m[0]) - + m[4] * static_cast(a.m[1]) - + m[7] * static_cast(a.m[2]); - - result.m[2] = m[2] * static_cast(a.m[0]) - + m[5] * static_cast(a.m[1]) - + m[8] * static_cast(a.m[2]); - - - result.m[3] = m[0] * static_cast(a.m[3]) - + m[3] * static_cast(a.m[4]) - + m[6] * static_cast(a.m[5]); - - result.m[4] = m[1] * static_cast(a.m[3]) - + m[4] * static_cast(a.m[4]) - + m[7] * static_cast(a.m[5]); - - result.m[5] = m[2] * static_cast(a.m[3]) - + m[5] * static_cast(a.m[4]) - + m[8] * static_cast(a.m[5]); - - - result.m[6] = m[0] * static_cast(a.m[6]) - + m[3] * static_cast(a.m[7]) - + m[6] * static_cast(a.m[8]); - - result.m[7] = m[1] * static_cast(a.m[6]) - + m[4] * static_cast(a.m[7]) - + m[7] * static_cast(a.m[8]); - - result.m[8] = m[2] * static_cast(a.m[6]) - + m[5] * static_cast(a.m[7]) - + m[8] * static_cast(a.m[8]); - - return result; - } - - - /** - * matrix vector multiplication - */ - template - Vector3Template operator * ( const Vector3Template& a ) - { - return Vector3Template( - m[0] * static_cast(a.x) - + m[3] * static_cast(a.y) - + m[6] * static_cast(a.z), - m[1] * static_cast(a.x) - + m[4] * static_cast(a.y) - + m[7] * static_cast(a.z), - m[2] * static_cast(a.x) - + m[5] * static_cast(a.y) - + m[8] * static_cast(a.z) ); - } - - /** - * matrix scalar multiplication - */ - template - Matrix3x3Template operator * ( const S a ) - { - Matrix3x3Template result; - - result.m[0] = m[0] * static_cast(a); - result.m[1] = m[1] * static_cast(a); - result.m[2] = m[2] * static_cast(a); - result.m[3] = m[3] * static_cast(a); - result.m[4] = m[4] * static_cast(a); - result.m[5] = m[5] * static_cast(a); - result.m[6] = m[6] * static_cast(a); - result.m[7] = m[7] * static_cast(a); - result.m[8] = m[8] * static_cast(a); - - return result; - } - - friend std::ostream &operator << ( std::ostream& os, Matrix3x3Template m ) - { - os << "( " << m.m[0] << ", " << m.m[1] << ", " << m.m[2] << " )" << std::endl; - os << "( " << m.m[3] << ", " << m.m[4] << ", " << m.m[5] << " )" << std::endl; - os << "( " << m.m[6] << ", " << m.m[7] << ", " << m.m[8] << " )" << std::endl; - return os; - } - - }; - - typedef Matrix3x3Template Matrix3x3Float; - typedef Matrix3x3Template Matrix3x3Double; - typedef Matrix3x3Template Matrix3x3; - - - template - class Matrix4x4Template + template inline Matrix3x3Template(const S a[9]) { + m[0] = static_cast(a[0]); + m[1] = static_cast(a[1]); + m[2] = static_cast(a[2]); - public: + m[3] = static_cast(a[3]); + m[4] = static_cast(a[4]); + m[5] = static_cast(a[5]); - T m[16]; + m[6] = static_cast(a[6]); + m[7] = static_cast(a[7]); + m[8] = static_cast(a[8]); + } - /** - * default constructor - */ - inline Matrix4x4Template() + template inline Matrix3x3Template(const Matrix3x3Template &a) + { + m[0] = static_cast(a.m[0]); + m[1] = static_cast(a.m[1]); + m[2] = static_cast(a.m[2]); + + m[3] = static_cast(a.m[3]); + m[4] = static_cast(a.m[4]); + m[5] = static_cast(a.m[5]); + + m[6] = static_cast(a.m[6]); + m[7] = static_cast(a.m[7]); + m[8] = static_cast(a.m[8]); + } + + inline Matrix3x3Template(const Vector3Template &h, + const Vector3Template &u) + { + Vector3Template a, b, c; + c = h.normalized(); + a = h.cross( u ).normalized(); + b = c.cross( a ).normalized(); + + m[0] = a.x; + m[1] = b.x; + m[2] = c.x; + + m[3] = a.y; + m[4] = b.y; + m[5] = c.y; + + m[6] = a.z; + m[7] = b.z; + m[8] = c.z; + } + + inline Matrix3x3Template(T a, T e, T t) + { + T ch = cos(a); + T sh = sin(a); + T ca = cos(e); + T sa = sin(e); + T cb = cos(t); + T sb = sin(t); + + m[0] = ch * ca; + m[1] = sh*sb - ch*sa*cb; + m[2] = ch*sa*sb + sh*cb; + + m[3] = sa; + m[4] = ca*cb; + m[5] = -ca*sb; + + m[6] = -sh*ca; + m[7] = sh*sa*cb + ch*sb; + m[8] = -sh*sa*sb + ch*cb; + } + + template inline Matrix3x3Template(const QuaternionTemplate &q) + { + T xx = static_cast(q.x*q.x), xy = static_cast(q.x*q.y), xz = + static_cast(q.x*q.z), xw = static_cast(q.x*q.w), + + yy = static_cast(q.y*q.y), yz = static_cast(q.y*q.z), yw = + static_cast(q.y*q.w), + + zz = static_cast(q.z*q.z), zw = static_cast(q.z*q.w); + + m[0] = 1 - yy - yy - zz - zz; + m[1] = xy + xy + zw + zw; + m[2] = xz + xz - yw - yw; + + m[3] = xy + xy - zw - zw; + m[4] = 1 - xx - xx - zz - zz; + m[5] = yz + yz + xw + xw; + + m[6] = xz + xz + yw + yw; + m[7] = yz + yz - xw - xw; + m[8] = 1 - xx - xx - yy - yy; + } + + void set(size_t i, Vector3Template& v) + { + m[i] = v.x; + m[i+3] = v.y; + m[i+6] = v.z; + } + + inline void identity() + { + m[0] = static_cast(1); + m[1] = static_cast(0); + m[2] = static_cast(0); + + m[3] = static_cast(0); + m[4] = static_cast(1); + m[5] = static_cast(0); + + m[6] = static_cast(0); + m[7] = static_cast(0); + m[8] = static_cast(1); + } + + inline Matrix3x3Template transposed() const + { + Matrix3x3Template a; + + a.m[0] = m[0]; + a.m[3] = m[1]; + a.m[6] = m[2]; + a.m[1] = m[3]; + a.m[4] = m[4]; + a.m[7] = m[5]; + a.m[2] = m[6]; + a.m[5] = m[7]; + a.m[8] = m[8]; + + return a; + } + + inline void transpose() + { + swap(m[3], m[1]); + swap(m[6], m[2]); + swap(m[7], m[5]); + } + + inline T determinant() const + { + return (m[0]*m[4]*m[8] + m[1]*m[5]*m[6] + m[2]*m[3]*m[7] ) - (m[6]*m[4] + *m[2] + m[7]*m[5]*m[0] + m[8]*m[3]*m[1] ); + } + + inline Matrix3x3Template inverted() + { + T det = determinant(); + T one_over_det = 1.0f / det; + + Matrix3x3Template result; + result.m[0] = +(m[4] * m[8] - m[5] * m[7]) * one_over_det; + result.m[1] = -(m[1] * m[8] - m[2] * m[7]) * one_over_det; + result.m[2] = +(m[1] * m[5] - m[2] * m[4]) * one_over_det; + result.m[3] = -(m[3] * m[8] - m[5] * m[6]) * one_over_det; + result.m[4] = +(m[0] * m[8] - m[2] * m[6]) * one_over_det; + result.m[5] = -(m[0] * m[5] - m[2] * m[3]) * one_over_det; + result.m[6] = +(m[3] * m[7] - m[4] * m[6]) * one_over_det; + result.m[7] = -(m[0] * m[7] - m[1] * m[6]) * one_over_det; + result.m[8] = +(m[0] * m[4] - m[1] * m[3]) * one_over_det; + + return result; + } + + template inline Matrix3x3Template &operator +=( + const Matrix3x3Template &a) + { + m[0] += static_cast(a.m[0]); + m[1] += static_cast(a.m[1]); + m[2] += static_cast(a.m[2]); + + m[3] += static_cast(a.m[3]); + m[4] += static_cast(a.m[4]); + m[5] += static_cast(a.m[5]); + + m[6] += static_cast(a.m[6]); + m[7] += static_cast(a.m[7]); + m[8] += static_cast(a.m[8]); + + return *this; + } + + template inline Matrix3x3Template operator *( + const Matrix3x3Template &a) + { + Matrix3x3Template result; + + result.m[0] = m[0] * static_cast(a.m[0]) + m[3] + * static_cast(a.m[1]) + m[6] * static_cast(a.m[2]); + + result.m[1] = m[1] * static_cast(a.m[0]) + m[4] + * static_cast(a.m[1]) + m[7] * static_cast(a.m[2]); + + result.m[2] = m[2] * static_cast(a.m[0]) + m[5] + * static_cast(a.m[1]) + m[8] * static_cast(a.m[2]); + + result.m[3] = m[0] * static_cast(a.m[3]) + m[3] + * static_cast(a.m[4]) + m[6] * static_cast(a.m[5]); + + result.m[4] = m[1] * static_cast(a.m[3]) + m[4] + * static_cast(a.m[4]) + m[7] * static_cast(a.m[5]); + + result.m[5] = m[2] * static_cast(a.m[3]) + m[5] + * static_cast(a.m[4]) + m[8] * static_cast(a.m[5]); + + result.m[6] = m[0] * static_cast(a.m[6]) + m[3] + * static_cast(a.m[7]) + m[6] * static_cast(a.m[8]); + + result.m[7] = m[1] * static_cast(a.m[6]) + m[4] + * static_cast(a.m[7]) + m[7] * static_cast(a.m[8]); + + result.m[8] = m[2] * static_cast(a.m[6]) + m[5] + * static_cast(a.m[7]) + m[8] * static_cast(a.m[8]); + + return result; + } + + template Vector3Template operator *(const Vector3Template& a) + { + return Vector3Template(m[0] * static_cast(a.x) + m[3] + * static_cast(a.y) + m[6] * static_cast(a.z), m[1] + * static_cast(a.x) + m[4] * static_cast(a.y) + m[7] + * static_cast(a.z), m[2] * static_cast(a.x) + m[5] + * static_cast(a.y) + m[8] * static_cast(a.z) ); + } + + template Matrix3x3Template operator *(const S a) + { + Matrix3x3Template result; + + result.m[0] = m[0] * static_cast(a); + result.m[1] = m[1] * static_cast(a); + result.m[2] = m[2] * static_cast(a); + result.m[3] = m[3] * static_cast(a); + result.m[4] = m[4] * static_cast(a); + result.m[5] = m[5] * static_cast(a); + result.m[6] = m[6] * static_cast(a); + result.m[7] = m[7] * static_cast(a); + result.m[8] = m[8] * static_cast(a); + + return result; + } + + friend std::ostream &operator <<(std::ostream& os, Matrix3x3Template m) + { + os << "( " << m.m[0] << ", " << m.m[1] << ", " << m.m[2] << " )" + << std::endl; + os << "( " << m.m[3] << ", " << m.m[4] << ", " << m.m[5] << " )" + << std::endl; + os << "( " << m.m[6] << ", " << m.m[7] << ", " << m.m[8] << " )" + << std::endl; + return os; + } + + QuaternionTemplate toQuaternion() const + { + // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes + // article "Quaternion Calculus and Fast Animation". + + T fTrace = m[0]+m[4]+m[8]; + T fRoot; + QuaternionTemplate q; + + if (fTrace > 0.0) { - identity(); + // |w| > 1/2, may as well choose w > 1/2 + fRoot = std::sqrt(fTrace + 1.0); // 2w + q.w = 0.5*fRoot; + fRoot = 0.5/fRoot; // 1/(4w) + q.x = (m[7]-m[5])*fRoot; + q.y = (m[2]-m[6])*fRoot; + q.z = (m[3]-m[1])*fRoot; } - - inline void identity() + else { - m[0] = static_cast(1); - m[1] = static_cast(0); - m[2] = static_cast(0); - m[3] = static_cast(0); - - m[4] = static_cast(0); - m[5] = static_cast(1); - m[6] = static_cast(0); - m[7] = static_cast(0); - - m[8] = static_cast(0); - m[9] = static_cast(0); - m[10] = static_cast(1); - m[11] = static_cast(0); - - m[12] = static_cast(0); - m[13] = static_cast(0); - m[14] = static_cast(0); - m[15] = static_cast(1); - } - - - /** - * constructor from array - */ - inline Matrix4x4Template( const T a[16] ) - { - m[0] = a[0]; m[1] = a[1]; m[2] = a[2]; m[3] = a[3]; - m[4] = a[4]; m[5] = a[5]; m[6] = a[6]; m[7] = a[7]; - m[8] = a[8]; m[9] = a[9]; m[10] = a[10]; m[11] = a[11]; - m[12] = a[12]; m[13] = a[13]; m[14] = a[14]; m[15] = a[15]; + // |w| <= 1/2 + static size_t s_iNext[3] = + { 1, 2, 0 }; + size_t i = 0; + if (m[4] > m[0]) + i = 1; + if (m[8] > m[i*3+i]) + i = 2; + size_t j = s_iNext[i]; + size_t k = s_iNext[j]; + + fRoot = std::sqrt(m[i*3+i]-m[j*3+j]-m[k*k+1] + 1.0); + T* apkQuat[3] = + { &q.x, &q.y, &q.z }; + *apkQuat[i] = 0.5*fRoot; + fRoot = 0.5/fRoot; + q.w = (m[k*3+j]-m[j*3+k])*fRoot; + *apkQuat[j] = (m[j*3+i]+m[i*3+j])*fRoot; + *apkQuat[k] = (m[k*3+i]+m[i*3+k])*fRoot; } - /** - * constructor from 3x3 matrix, add. element are set to zero - */ - inline Matrix4x4Template( const Matrix3x3Template &a ) - { - m[0] = a[0]; - m[1] = a[1]; - m[2] = a[2]; - m[3] = static_cast(0); - - m[4] = a[3]; - m[5] = a[4]; - m[6] = a[5]; - m[7] = static_cast(0); - - m[8] = a[6]; - m[9] = a[7]; - m[10] = a[8]; - m[11] = static_cast(0); - - m[12] = static_cast(0); - m[13] = static_cast(0); - m[14] = static_cast(0); - m[15] = static_cast(1); - } - - /** - * constructor from 3x3 rotation matrix and translation vector - */ - inline Matrix4x4Template( - const Matrix3x3Template &a, - const Vector3Template &b ) - { - m[0] = a[0]; - m[1] = a[1]; - m[2] = a[2]; - m[3] = static_cast(0); - - m[4] = a[3]; - m[5] = a[4]; - m[6] = a[5]; - m[7] = static_cast(0); - - m[8] = a[6]; - m[9] = a[7]; - m[10] = a[8]; - m[11] = static_cast(0); - - m[12] = b.x; - m[13] = b.y; - m[14] = b.z; - m[15] = static_cast(1); - } - + return q; + } +}; - /** - * copy constructor - */ - inline Matrix4x4Template( const Matrix4x4Template &a ) - { - m[0] = a.m[0]; - m[1] = a.m[1]; - m[2] = a.m[2]; - m[3] = a.m[3]; +typedef Matrix3x3Template Matrix3x3Float; +typedef Matrix3x3Template Matrix3x3Double; +typedef Matrix3x3Template Matrix3x3; - m[4] = a.m[4]; - m[5] = a.m[5]; - m[6] = a.m[6]; - m[7] = a.m[7]; +template class Matrix4x4Template +{ - m[8] = a.m[8]; - m[9] = a.m[9]; - m[10] = a.m[10]; - m[11] = a.m[11]; +public: - m[12] = a.m[12]; - m[13] = a.m[13]; - m[14] = a.m[14]; - m[15] = a.m[15]; - } + T m[16]; + inline Matrix4x4Template() + { + identity(); + } - /** - * constructor from quaternion - */ - inline Matrix4x4Template( const QuaternionTemplate &q ) - { - m[0] = 1 - 2*q.y*q.y - 2*q.z*q.z; - m[1] = 2*q.x*q.y - 2*q.z*q.w; - m[2] = 2*q.x*q.z + 2*q.y*q.w; - m[3] = static_cast(0); - - m[4] = 2*q.x*q.y + 2*q.z*q.w; - m[5] = 1 - 2*q.x*q.x - 2*q.z*q.z; - m[6] = 2*q.y*q.z - 2*q.x*q.w; - m[7] = static_cast(0); - - m[8] = 2*q.x*q.z - 2*q.y*q.w; - m[9] = 2*q.y*q.z + 2*q.x*q.w; - m[10] = 1 - 2*q.x*q.x - 2*q.y*q.y; - m[11] = static_cast(0); + inline void identity() + { + m[0] = static_cast(1); + m[1] = static_cast(0); + m[2] = static_cast(0); + m[3] = static_cast(0); - m[12] = static_cast(0); - m[13] = static_cast(0); - m[14] = static_cast(0); - m[15] = static_cast(1); - } + m[4] = static_cast(0); + m[5] = static_cast(1); + m[6] = static_cast(0); + m[7] = static_cast(0); + m[8] = static_cast(0); + m[9] = static_cast(0); + m[10] = static_cast(1); + m[11] = static_cast(0); - /** - * constructor from quaternion and translation vector - */ - inline Matrix4x4Template( - const QuaternionTemplate &q, - const Vector3Template &a ) - { - m[0] = 1 - 2*q.y*q.y - 2*q.z*q.z; - m[1] = 2*q.x*q.y - 2*q.z*q.w; - m[2] = 2*q.x*q.z + 2*q.y*q.w; - m[3] = static_cast(0); - - m[4] = 2*q.x*q.y + 2*q.z*q.w; - m[5] = 1 - 2*q.x*q.x - 2*q.z*q.z; - m[6] = 2*q.y*q.z - 2*q.x*q.w; - m[7] = static_cast(0); - - m[8] = 2*q.x*q.z - 2*q.y*q.w; - m[9] = 2*q.y*q.z + 2*q.x*q.w; - m[10] = 1 - 2*q.x*q.x - 2*q.y*q.y; - m[11] = static_cast(0); + m[12] = static_cast(0); + m[13] = static_cast(0); + m[14] = static_cast(0); + m[15] = static_cast(1); + } - m[12] = a.x; - m[13] = a.y; - m[14] = a.z; - m[15] = static_cast(1); - } + inline Matrix4x4Template(const T a[16]) + { + m[0] = a[0]; + m[1] = a[1]; + m[2] = a[2]; + m[3] = a[3]; + m[4] = a[4]; + m[5] = a[5]; + m[6] = a[6]; + m[7] = a[7]; + m[8] = a[8]; + m[9] = a[9]; + m[10] = a[10]; + m[11] = a[11]; + m[12] = a[12]; + m[13] = a[13]; + m[14] = a[14]; + m[15] = a[15]; + } - - /** - * constructor from heading, up and translation vectors - */ - inline Matrix4x4Template( - const Vector3Template &h, - const Vector3Template &u, - const Vector3Template &t ) - { - Vector3Template a, b, c; - c = h.unit(); - a = h.crossProduct( u ).unit(); - b = c.crossProduct( a ).unit(); - - m[0] = a.x; - m[1] = b.x; - m[2] = c.x; - m[3] = static_cast(0); - - m[4] = a.y; - m[5] = b.y; - m[6] = c.y; - m[7] = static_cast(0); - - m[8] = a.z; - m[9] = b.z; - m[10] = c.z; - m[11] = static_cast(0); - - m[12] = t.x; - m[13] = t.y; - m[14] = t.z; - m[15] = static_cast(1); - } + inline Matrix4x4Template(const Matrix3x3Template &a) + { + m[0] = a[0]; + m[1] = a[1]; + m[2] = a[2]; + m[3] = static_cast(0); + m[4] = a[3]; + m[5] = a[4]; + m[6] = a[5]; + m[7] = static_cast(0); - /** - * contructor from euler angles - */ - inline Matrix4x4Template( - T a, - T e, - T t, - const Vector3Template &tr ) - { - T ch = cos( a ); - T sh = sin( a ); - T ca = cos( e ); - T sa = sin( e ); - T cb = cos( t ); - T sb = sin( t ); - - m[0] = ch * ca; - m[1] = sh*sb - ch*sa*cb; - m[2] = ch*sa*sb + sh*cb; - m[3] = static_cast(0); - - m[4] = sa; - m[5] = ca*cb; - m[6] = -ca*sb; - m[7] = static_cast(0); - - - m[8] = -sh*ca; - m[9] = sh*sa*cb + ch*sb; - m[10] = -sh*sa*sb + ch*cb; - m[11] = static_cast(0); - - m[12] = tr.x; - m[13] = tr.y; - m[14] = tr.z; - m[15] = static_cast(1); - } + m[8] = a[6]; + m[9] = a[7]; + m[10] = a[8]; + m[11] = static_cast(0); + m[12] = static_cast(0); + m[13] = static_cast(0); + m[14] = static_cast(0); + m[15] = static_cast(1); + } - /** - * transpose this matrix - */ - inline void transpose() - { - std::swap( m[4], m[1] ); - std::swap( m[8], m[2] ); - std::swap( m[12], m[3] ); + inline Matrix4x4Template(const Matrix3x3Template &a, + const Vector3Template &b) + { + m[0] = a[0]; + m[1] = a[1]; + m[2] = a[2]; + m[3] = static_cast(0); - std::swap( m[9], m[6] ); - std::swap( m[13], m[7] ); - std::swap( m[14], m[11] ); - } - + m[4] = a[3]; + m[5] = a[4]; + m[6] = a[5]; + m[7] = static_cast(0); - /** - * get transposed matrix - */ - inline Matrix4x4Template transposed() - { - Matrix4x4Template a; - - a.m[0] = m[0]; a.m[4] = m[1]; a.m[8] = m[2]; a.m[12] = m[3]; - a.m[1] = m[4]; a.m[5] = m[5]; a.m[9] = m[6]; a.m[13] = m[7]; - a.m[2] = m[8]; a.m[6] = m[9]; a.m[10] = m[10]; a.m[14] = m[11]; - a.m[3] = m[12]; a.m[7] = m[13]; a.m[11] = m[14]; a.m[15] = m[15]; - - return a; - } + m[8] = a[6]; + m[9] = a[7]; + m[10] = a[8]; + m[11] = static_cast(0); + m[12] = b.x; + m[13] = b.y; + m[14] = b.z; + m[15] = static_cast(1); + } - /** - * matrix multiplication - */ - template - inline Matrix4x4Template operator * ( const Matrix4x4Template &a ) - { - Matrix4x4Template result; - - result.m[0] = m[0] * static_cast(a.m[0]) - + m[4] * static_cast(a.m[1]) - + m[8] * static_cast(a.m[2]) + inline Matrix4x4Template(const Matrix4x4Template &a) + { + m[0] = a.m[0]; + m[1] = a.m[1]; + m[2] = a.m[2]; + m[3] = a.m[3]; + + m[4] = a.m[4]; + m[5] = a.m[5]; + m[6] = a.m[6]; + m[7] = a.m[7]; + + m[8] = a.m[8]; + m[9] = a.m[9]; + m[10] = a.m[10]; + m[11] = a.m[11]; + + m[12] = a.m[12]; + m[13] = a.m[13]; + m[14] = a.m[14]; + m[15] = a.m[15]; + } + + inline Matrix4x4Template(const QuaternionTemplate &q) + { + m[0] = 1 - 2*q.y*q.y - 2*q.z*q.z; + m[1] = 2*q.x*q.y - 2*q.z*q.w; + m[2] = 2*q.x*q.z + 2*q.y*q.w; + m[3] = static_cast(0); + + m[4] = 2*q.x*q.y + 2*q.z*q.w; + m[5] = 1 - 2*q.x*q.x - 2*q.z*q.z; + m[6] = 2*q.y*q.z - 2*q.x*q.w; + m[7] = static_cast(0); + + m[8] = 2*q.x*q.z - 2*q.y*q.w; + m[9] = 2*q.y*q.z + 2*q.x*q.w; + m[10] = 1 - 2*q.x*q.x - 2*q.y*q.y; + m[11] = static_cast(0); + + m[12] = static_cast(0); + m[13] = static_cast(0); + m[14] = static_cast(0); + m[15] = static_cast(1); + } + + inline Matrix4x4Template(const QuaternionTemplate &q, + const Vector3Template &a) + { + m[0] = 1 - 2*q.y*q.y - 2*q.z*q.z; + m[1] = 2*q.x*q.y - 2*q.z*q.w; + m[2] = 2*q.x*q.z + 2*q.y*q.w; + m[3] = static_cast(0); + + m[4] = 2*q.x*q.y + 2*q.z*q.w; + m[5] = 1 - 2*q.x*q.x - 2*q.z*q.z; + m[6] = 2*q.y*q.z - 2*q.x*q.w; + m[7] = static_cast(0); + + m[8] = 2*q.x*q.z - 2*q.y*q.w; + m[9] = 2*q.y*q.z + 2*q.x*q.w; + m[10] = 1 - 2*q.x*q.x - 2*q.y*q.y; + m[11] = static_cast(0); + + m[12] = a.x; + m[13] = a.y; + m[14] = a.z; + m[15] = static_cast(1); + } + + inline Matrix4x4Template(const Vector3Template &h, + const Vector3Template &u, const Vector3Template &t) + { + Vector3Template a, b, c; + c = h.unit(); + a = h.crossProduct( u ).unit(); + b = c.crossProduct( a ).unit(); + + m[0] = a.x; + m[1] = b.x; + m[2] = c.x; + m[3] = static_cast(0); + + m[4] = a.y; + m[5] = b.y; + m[6] = c.y; + m[7] = static_cast(0); + + m[8] = a.z; + m[9] = b.z; + m[10] = c.z; + m[11] = static_cast(0); + + m[12] = t.x; + m[13] = t.y; + m[14] = t.z; + m[15] = static_cast(1); + } + + inline Matrix4x4Template(T a, T e, T t, const Vector3Template &tr) + { + T ch = cos(a); + T sh = sin(a); + T ca = cos(e); + T sa = sin(e); + T cb = cos(t); + T sb = sin(t); + + m[0] = ch * ca; + m[1] = sh*sb - ch*sa*cb; + m[2] = ch*sa*sb + sh*cb; + m[3] = static_cast(0); + + m[4] = sa; + m[5] = ca*cb; + m[6] = -ca*sb; + m[7] = static_cast(0); + + m[8] = -sh*ca; + m[9] = sh*sa*cb + ch*sb; + m[10] = -sh*sa*sb + ch*cb; + m[11] = static_cast(0); + + m[12] = tr.x; + m[13] = tr.y; + m[14] = tr.z; + m[15] = static_cast(1); + } + + inline void transpose() + { + std::swap(m[4], m[1]); + std::swap(m[8], m[2]); + std::swap(m[12], m[3]); + + std::swap(m[9], m[6]); + std::swap(m[13], m[7]); + std::swap(m[14], m[11]); + } + + inline Matrix4x4Template transposed() + { + Matrix4x4Template a; + + a.m[0] = m[0]; + a.m[4] = m[1]; + a.m[8] = m[2]; + a.m[12] = m[3]; + a.m[1] = m[4]; + a.m[5] = m[5]; + a.m[9] = m[6]; + a.m[13] = m[7]; + a.m[2] = m[8]; + a.m[6] = m[9]; + a.m[10] = m[10]; + a.m[14] = m[11]; + a.m[3] = m[12]; + a.m[7] = m[13]; + a.m[11] = m[14]; + a.m[15] = m[15]; + + return a; + } + + template inline Matrix4x4Template operator *( + const Matrix4x4Template &a) + { + Matrix4x4Template result; + + result.m[0] = m[0] * static_cast(a.m[0]) + m[4] + * static_cast(a.m[1]) + m[8] * static_cast(a.m[2]) + m[12] * static_cast(a.m[3]); - - result.m[1] = m[1] * static_cast(a.m[0]) - + m[5] * static_cast(a.m[1]) - + m[9] * static_cast(a.m[2]) + + result.m[1] = m[1] * static_cast(a.m[0]) + m[5] + * static_cast(a.m[1]) + m[9] * static_cast(a.m[2]) + m[13] * static_cast(a.m[3]); - - result.m[2] = m[2] * static_cast(a.m[0]) - + m[6] * static_cast(a.m[1]) - + m[10] * static_cast(a.m[2]) + + result.m[2] = m[2] * static_cast(a.m[0]) + m[6] + * static_cast(a.m[1]) + m[10] * static_cast(a.m[2]) + m[14] * static_cast(a.m[3]); - result.m[3] = m[3] * static_cast(a.m[0]) - + m[7] * static_cast(a.m[1]) - + m[11] * static_cast(a.m[2]) + result.m[3] = m[3] * static_cast(a.m[0]) + m[7] + * static_cast(a.m[1]) + m[11] * static_cast(a.m[2]) + m[15] * static_cast(a.m[3]); - - result.m[4] = m[0] * static_cast(a.m[4]) - + m[4] * static_cast(a.m[5]) - + m[8] * static_cast(a.m[6]) + result.m[4] = m[0] * static_cast(a.m[4]) + m[4] + * static_cast(a.m[5]) + m[8] * static_cast(a.m[6]) + m[12] * static_cast(a.m[7]); - - result.m[5] = m[1] * static_cast(a.m[4]) - + m[5] * static_cast(a.m[5]) - + m[9] * static_cast(a.m[6]) + + result.m[5] = m[1] * static_cast(a.m[4]) + m[5] + * static_cast(a.m[5]) + m[9] * static_cast(a.m[6]) + m[13] * static_cast(a.m[7]); - - result.m[6] = m[2] * static_cast(a.m[4]) - + m[6] * static_cast(a.m[5]) - + m[10] * static_cast(a.m[6]) + + result.m[6] = m[2] * static_cast(a.m[4]) + m[6] + * static_cast(a.m[5]) + m[10] * static_cast(a.m[6]) + m[14] * static_cast(a.m[7]); - result.m[7] = m[3] * static_cast(a.m[4]) - + m[7] * static_cast(a.m[5]) - + m[11] * static_cast(a.m[6]) + result.m[7] = m[3] * static_cast(a.m[4]) + m[7] + * static_cast(a.m[5]) + m[11] * static_cast(a.m[6]) + m[15] * static_cast(a.m[7]); - - result.m[8] = m[0] * static_cast(a.m[8]) - + m[4] * static_cast(a.m[9]) - + m[8] * static_cast(a.m[10]) + result.m[8] = m[0] * static_cast(a.m[8]) + m[4] + * static_cast(a.m[9]) + m[8] * static_cast(a.m[10]) + m[12] * static_cast(a.m[11]); - - result.m[9] = m[1] * static_cast(a.m[8]) - + m[5] * static_cast(a.m[9]) - + m[9] * static_cast(a.m[10]) + + result.m[9] = m[1] * static_cast(a.m[8]) + m[5] + * static_cast(a.m[9]) + m[9] * static_cast(a.m[10]) + m[13] * static_cast(a.m[11]); - - result.m[10] = m[2] * static_cast(a.m[8]) - + m[6] * static_cast(a.m[9]) - + m[10] * static_cast(a.m[10]) + + result.m[10] = m[2] * static_cast(a.m[8]) + m[6] + * static_cast(a.m[9]) + m[10] * static_cast(a.m[10]) + m[14] * static_cast(a.m[11]); - result.m[11] = m[3] * static_cast(a.m[8]) - + m[7] * static_cast(a.m[9]) - + m[11] * static_cast(a.m[10]) + result.m[11] = m[3] * static_cast(a.m[8]) + m[7] + * static_cast(a.m[9]) + m[11] * static_cast(a.m[10]) + m[15] * static_cast(a.m[11]); - - result.m[12] = m[0] * static_cast(a.m[12]) - + m[4] * static_cast(a.m[13]) - + m[8] * static_cast(a.m[14]) + result.m[12] = m[0] * static_cast(a.m[12]) + m[4] + * static_cast(a.m[13]) + m[8] * static_cast(a.m[14]) + m[12] * static_cast(a.m[15]); - - result.m[13] = m[1] * static_cast(a.m[12]) - + m[5] * static_cast(a.m[13]) - + m[9] * static_cast(a.m[14]) + + result.m[13] = m[1] * static_cast(a.m[12]) + m[5] + * static_cast(a.m[13]) + m[9] * static_cast(a.m[14]) + m[13] * static_cast(a.m[15]); - - result.m[14] = m[2] * static_cast(a.m[12]) - + m[6] * static_cast(a.m[13]) - + m[10] * static_cast(a.m[14]) + + result.m[14] = m[2] * static_cast(a.m[12]) + m[6] + * static_cast(a.m[13]) + m[10] * static_cast(a.m[14]) + m[14] * static_cast(a.m[15]); - result.m[15] = m[3] * static_cast(a.m[12]) - + m[7] * static_cast(a.m[13]) - + m[11] * static_cast(a.m[14]) + result.m[15] = m[3] * static_cast(a.m[12]) + m[7] + * static_cast(a.m[13]) + m[11] * static_cast(a.m[14]) + m[15] * static_cast(a.m[15]); - - return result; - } - - /** - * matrix multiplication - */ -/* - template - inline Matrix4x4Template operator *= ( const Matrix4x4Template &a ) - { - Matrix4x4Template result; - - result.m[0] = m[0] * static_cast(a.m[0]) - + m[4] * static_cast(a.m[1]) - + m[8] * static_cast(a.m[2]); - + m[12] * static_cast(a.m[3]); - - result.m[1] = m[1] * static_cast(a.m[0]) - + m[5] * static_cast(a.m[1]) - + m[9] * static_cast(a.m[2]); - + m[13] * static_cast(a.m[3]); - - result.m[2] = m[2] * static_cast(a.m[0]) - + m[6] * static_cast(a.m[1]) - + m[10] * static_cast(a.m[2]); - + m[14] * static_cast(a.m[3]); - result.m[3] = m[3] * static_cast(a.m[0]) - + m[7] * static_cast(a.m[1]) - + m[11] * static_cast(a.m[2]); - + m[15] * static_cast(a.m[3]); + return result; + } + Vector3Template operator *(const Vector3Template &a) + { + Vector3Template result; - result.m[4] = m[0] * static_cast(a.m[4]) - + m[4] * static_cast(a.m[5]) - + m[8] * static_cast(a.m[6]); - + m[12] * static_cast(a.m[7]); - - result.m[5] = m[1] * static_cast(a.m[4]) - + m[5] * static_cast(a.m[5]) - + m[9] * static_cast(a.m[6]); - + m[13] * static_cast(a.m[7]); - - result.m[6] = m[2] * static_cast(a.m[4]) - + m[6] * static_cast(a.m[5]) - + m[10] * static_cast(a.m[6]); - + m[14] * static_cast(a.m[7]); + result.x = m[0] * a.x + m[4] * a.y + m[8] * a.z + m[12]; + result.y = m[1] * a.x + m[5] * a.y + m[9] * a.z + m[13]; + result.z = m[2] * a.x + m[6] * a.y + m[11] * a.z + m[14]; - result.m[7] = m[3] * static_cast(a.m[4]) - + m[7] * static_cast(a.m[5]) - + m[11] * static_cast(a.m[6]); - + m[15] * static_cast(a.m[7]); + return result; + } + T *data() + { + return &m[0]; + } +}; - result.m[8] = m[0] * static_cast(a.m[8]) - + m[4] * static_cast(a.m[9]) - + m[8] * static_cast(a.m[10]); - + m[12] * static_cast(a.m[11]); - - result.m[9] = m[1] * static_cast(a.m[8]) - + m[5] * static_cast(a.m[9]) - + m[9] * static_cast(a.m[10]); - + m[13] * static_cast(a.m[11]); - - result.m[10] = m[2] * static_cast(a.m[8]) - + m[6] * static_cast(a.m[9]) - + m[10] * static_cast(a.m[10]); - + m[14] * static_cast(a.m[11]); +typedef Matrix4x4Template Matrix4x4Float; +typedef Matrix4x4Template Matrix4x4Double; +typedef Matrix4x4Template Matrix4x4; - result.m[11] = m[3] * static_cast(a.m[8]) - + m[7] * static_cast(a.m[9]) - + m[11] * static_cast(a.m[10]); - + m[15] * static_cast(a.m[11]); - - - result.m[12] = m[0] * static_cast(a.m[12]) - + m[4] * static_cast(a.m[13]) - + m[8] * static_cast(a.m[14]); - + m[12] * static_cast(a.m[15]); - - result.m[13] = m[1] * static_cast(a.m[12]) - + m[5] * static_cast(a.m[13]) - + m[9] * static_cast(a.m[14]); - + m[13] * static_cast(a.m[15]); - - result.m[14] = m[2] * static_cast(a.m[12]) - + m[6] * static_cast(a.m[13]) - + m[10] * static_cast(a.m[14]); - + m[14] * static_cast(a.m[15]); - - result.m[15] = m[3] * static_cast(a.m[12]) - + m[7] * static_cast(a.m[13]) - + m[11] * static_cast(a.m[14]); - + m[15] * static_cast(a.m[15]); - - m = result.m; - - return *this; - } - */ - /** - * matrix vector multiplication - */ - Vector3Template operator * ( const Vector3Template &a ) - { - Vector3Template result; - - result.x = m[0] * a.x + m[4] * a.y + m[8] * a.z + m[12]; - result.y = m[1] * a.x + m[5] * a.y + m[9] * a.z + m[13]; - result.z = m[2] * a.x + m[6] * a.y + m[11] * a.z + m[14]; - - return result; - } - - T *data() - { - return &m[0]; - } - }; - - typedef Matrix4x4Template Matrix4x4Float; - typedef Matrix4x4Template Matrix4x4Double; - typedef Matrix4x4Template Matrix4x4; - } // namespace bc #endif // BLUECORE_MATRIX_H diff --git a/engine/Math/Quaternion.h b/engine/Math/Quaternion.h index b02e3f5..896450a 100644 --- a/engine/Math/Quaternion.h +++ b/engine/Math/Quaternion.h @@ -5,362 +5,234 @@ namespace BlueCore { - template - class QuaternionTemplate +template class QuaternionTemplate +{ +public: + T w, x, y, z; + + inline QuaternionTemplate() { - public: - T w, x, y, z; + w = static_cast(1.0); + x = static_cast(0.0); + y = static_cast(0.0); + z = static_cast(0.0); + } + template inline QuaternionTemplate(S w, S x, S y, S z) + { + this->w = static_cast(w); + this->x = static_cast(x); + this->y = static_cast(y); + this->z = static_cast(z); + } - /** - * constructor - */ - inline QuaternionTemplate() - { - w = static_cast(1.0); - x = static_cast(0.0); - y = static_cast(0.0); - z = static_cast(0.0); - } - + template inline QuaternionTemplate(Vector3Template axis, S angle) + { + T half_angle = static_cast(angle)/static_cast(2.0); + T sin_half_angle = static_cast(sin(half_angle) ); - /** - * contructor - */ - template - inline QuaternionTemplate( S w, S x, S y, S z ) - { - this->w = static_cast(w); - this->x = static_cast(x); - this->y = static_cast(y); - this->z = static_cast(z); - } + w = static_cast(cos(half_angle) ); + x = sin_half_angle * static_cast(axis.x); + y = sin_half_angle * static_cast(axis.y); + z = sin_half_angle * static_cast(axis.z); + } + template inline QuaternionTemplate(S h, S a, S b) + { + T c1 = static_cast(cos(h / 2.0) ); + T c2 = static_cast(cos(a / 2.0) ); + T c3 = static_cast(cos(b / 2.0) ); + T s1 = static_cast(sin(h / 2.0) ); + T s2 = static_cast(sin(a / 2.0) ); + T s3 = static_cast(sin(b / 2.0) ); - /** - * contructor - */ - template - inline QuaternionTemplate( Vector3Template axis, S angle ) - { - T half_angle = static_cast(angle)/static_cast(2.0); - T sin_half_angle = static_cast( sin( half_angle ) ); - - w = static_cast( cos( half_angle ) ); - x = sin_half_angle * static_cast(axis.x); - y = sin_half_angle * static_cast(axis.y); - z = sin_half_angle * static_cast(axis.z); - } + w = c1 * c2 * c3 - s1 * s2 * s3; + x = s1 * s2 * c3 + c1 * c2 * s3; + y = s1 * c2 * c3 + c1 * s2 * s3; + z = c1 * s2 * c3 - s1 * c2 * s3; + } + inline void identity() + { + w = static_cast(1.0); + x = static_cast(0.0); + y = static_cast(0.0); + z = static_cast(0.0); + } - /** - * contructor - */ - template - inline QuaternionTemplate( S h, S a, S b ) - { - T c1 = static_cast( cos(h / 2.0) ); - T c2 = static_cast( cos(a / 2.0) ); - T c3 = static_cast( cos(b / 2.0) ); - T s1 = static_cast( sin(h / 2.0) ); - T s2 = static_cast( sin(a / 2.0) ); - T s3 = static_cast( sin(b / 2.0) ); - - w = c1 * c2 * c3 - s1 * s2 * s3; - x = s1 * s2 * c3 + c1 * c2 * s3; - y = s1 * c2 * c3 + c1 * s2 * s3; - z = c1 * s2 * c3 - s1 * c2 * s3; - } + template inline QuaternionTemplate operator +( + const QuaternionTemplate &a) const + { + return QuaternionTemplate(w + static_cast(a.w), x + + static_cast(a.x), y + static_cast(a.y), z + + static_cast(a.z) ); + } + template inline QuaternionTemplate &operator +=( + const QuaternionTemplate &a) + { + w += static_cast(a.w); + x += static_cast(a.x); + y += static_cast(a.y); + z += static_cast(a.z); - /** - * identity - */ - inline void identity() - { - w = static_cast(1.0); - x = static_cast(0.0); - y = static_cast(0.0); - z = static_cast(0.0); - } - + return *this; + } - /** - * operator + - */ - template - inline QuaternionTemplate operator + ( - const QuaternionTemplate &a ) const - { - return QuaternionTemplate( - w + static_cast(a.w), - x + static_cast(a.x), - y + static_cast(a.y), - z + static_cast(a.z) ); - } + template inline QuaternionTemplate operator -( + const QuaternionTemplate &a) const + { + return QuaternionTemplate(w - static_cast(a.w), x + - static_cast(a.x), y - static_cast(a.y), z + - static_cast(a.z) ); + } + template inline QuaternionTemplate operator *( + const QuaternionTemplate &a) const + { + return QuaternionTemplate(w * static_cast(a.w) - x + * static_cast(a.x) - y * static_cast(a.y) - z + * static_cast(a.z), w * static_cast(a.x) + x + * static_cast(a.w) + y * static_cast(a.z) - z + * static_cast(a.y), w * static_cast(a.y) - x + * static_cast(a.z) + y * static_cast(a.w) + z + * static_cast(a.x), w * static_cast(a.z) + x + * static_cast(a.y) - y * static_cast(a.x) + z + * static_cast(a.w) ); + } - /** - * operator += - */ - template - inline QuaternionTemplate &operator += ( - const QuaternionTemplate &a ) - { - w += static_cast(a.w); - x += static_cast(a.x); - y += static_cast(a.y); - z += static_cast(a.z); - - return *this; - } + template inline QuaternionTemplate &operator *=( + const QuaternionTemplate &a) + { + w = w * static_cast(a.w) - x * static_cast(a.x) - y + * static_cast(a.y) - z * static_cast(a.z); + x = w * static_cast(a.x) + x * static_cast(a.w) + y + * static_cast(a.z) - z * static_cast(a.y); + y = w * static_cast(a.y) - x * static_cast(a.z) + y + * static_cast(a.w) + z * static_cast(a.x); + z = w * static_cast(a.z) + x * static_cast(a.y) - y + * static_cast(a.x) + z * static_cast(a.w); + return *this; + } - /** - * operator - - */ - template - inline QuaternionTemplate operator - ( - const QuaternionTemplate &a ) const - { - return QuaternionTemplate( - w - static_cast(a.w), - x - static_cast(a.x), - y - static_cast(a.y), - z - static_cast(a.z) ); - } - + template inline QuaternionTemplate &operator -=( + const QuaternionTemplate &a) + { + w -= static_cast(a.w); + x -= static_cast(a.x); + y -= static_cast(a.y); + z -= static_cast(a.z); - /** - * operator * - */ - template - inline QuaternionTemplate operator * ( - const QuaternionTemplate &a ) const - { - return QuaternionTemplate( - w * static_cast(a.w) - - x * static_cast(a.x) - - y * static_cast(a.y) - - z * static_cast(a.z), - w * static_cast(a.x) + - x * static_cast(a.w) + - y * static_cast(a.z) - - z * static_cast(a.y), - w * static_cast(a.y) - - x * static_cast(a.z) + - y * static_cast(a.w) + - z * static_cast(a.x), - w * static_cast(a.z) + - x * static_cast(a.y) - - y * static_cast(a.x) + - z * static_cast(a.w) ); - } - + return *this; + } - /** - * operator *= - */ - template - inline QuaternionTemplate &operator *=( - const QuaternionTemplate &a ) - { - w = w * static_cast(a.w) - x * static_cast(a.x) - - y * static_cast(a.y) - z * static_cast(a.z); - x = w * static_cast(a.x) + x * static_cast(a.w) + - y * static_cast(a.z) - z * static_cast(a.y); - y = w * static_cast(a.y) - x * static_cast(a.z) + - y * static_cast(a.w) + z * static_cast(a.x); - z = w * static_cast(a.z) + x * static_cast(a.y) - - y * static_cast(a.x) + z * static_cast(a.w); - - return *this; - } + template inline QuaternionTemplate &operator =( + const QuaternionTemplate &a) + { + w = static_cast(a.w); + x = static_cast(a.x); + y = static_cast(a.y); + z = static_cast(a.z); + return *this; + } - /** - * operator -= - */ - template - inline QuaternionTemplate &operator -= ( - const QuaternionTemplate &a ) - { - w -= static_cast(a.w); - x -= static_cast(a.x); - y -= static_cast(a.y); - z -= static_cast(a.z); - - return *this; - } + inline QuaternionTemplate unit() const + { + T d = 1/sqrt(w*w + x*x + y*y + z*z); + return QuaternionTemplate(w * d, x * d, y * d, z * d); + } + inline QuaternionTemplate inversed() const + { + return QuaternionTemplate(w, -x, -y, -z); + } - /** - * operator = - */ - template - inline QuaternionTemplate &operator = ( - const QuaternionTemplate &a ) - { - w = static_cast(a.w); - x = static_cast(a.x); - y = static_cast(a.y); - z = static_cast(a.z); - - return *this; - } + inline void inverse() + { + x = -x; + y = -y; + z = -z; + } + inline QuaternionTemplate &normalize() + { + T d = 1/sqrt(w*w + x*x + y*y + z*z); + w *= d; + x *= d; + y *= d; + z *= d; - /** - * unit - */ - inline QuaternionTemplate unit() const - { - T d = 1/sqrt( w*w + x*x + y*y + z*z ); - return QuaternionTemplate( w * d, x * d, y * d, z * d ); - } + return *this; + } - - /** - * inversed - */ - inline QuaternionTemplate inversed() const - { - return QuaternionTemplate( w, -x, -y, -z ); - } - - /** - * inversed - */ - inline void inverse() - { - x = -x; - y = -y; - z = -z; - } - - - /** - * normalize - */ - inline QuaternionTemplate &normalize() - { - T d = 1/sqrt( w*w + x*x + y*y + z*z ); - w *= d; - x *= d; - y *= d; - z *= d; - - return *this; - } - - - /** - * apply - */ - template - inline const Vector3Template apply( - const Vector3Template &a ) const - { - T xx = x*x, xy = x*y, xz = x*z, xw = x*w, - yy = y*y, yz = y*z, yw = y*w, + template inline const Vector3Template apply( + const Vector3Template &a) const + { + T xx = x*x, xy = x*y, xz = x*z, xw = x*w, yy = y*y, yz = y*z, yw = y*w, zz = z*z, zw = z*w; - - return Vector3Template( - 2.0 * ( static_cast(a.x) * ( 0.5 - yy - zz ) + - static_cast(a.y) * ( xy - zw ) + - static_cast(a.z) * ( xz + yw ) ), - - 2.0 * ( static_cast(a.x) * ( xy + zw ) + - static_cast(a.y) * ( 0.5 - xx - zz ) + - static_cast(a.z) * ( yz - xw ) ), - - 2.0 * ( static_cast(a.x) * ( xz - yw ) + - static_cast(a.y) * ( yz + xw ) + - static_cast(a.z) * ( 0.5 - xx - yy ) ) ); - } + return Vector3Template( 2.0 * ( static_cast(a.x) * ( 0.5 - yy + - zz ) + static_cast(a.y) * (xy - zw ) + static_cast(a.z) + * (xz + yw ) ), - /** - * apply - */ - template - inline const Vector3Template operator * ( - const Vector3Template &a ) const - { - T xx = x*x, xy = x*y, xz = x*z, xw = x*w, - yy = y*y, yz = y*z, yw = y*w, + 2.0 * ( static_cast(a.x) * (xy + zw ) + static_cast(a.y) * ( 0.5 + - xx - zz ) + static_cast(a.z) * (yz - xw ) ), + + 2.0 * ( static_cast(a.x) * (xz - yw ) + static_cast(a.y) * (yz + + xw ) + static_cast(a.z) * ( 0.5 - xx - yy ) )); + } + + template inline const Vector3Template operator *( + const Vector3Template &a) const + { + T xx = x*x, xy = x*y, xz = x*z, xw = x*w, yy = y*y, yz = y*z, yw = y*w, zz = z*z, zw = z*w; - - return Vector3Template( - 2.0 * ( static_cast(a.x) * ( 0.5 - yy - zz ) + - static_cast(a.y) * ( xy - zw ) + - static_cast(a.z) * ( xz + yw ) ), - - 2.0 * ( static_cast(a.x) * ( xy + zw ) + - static_cast(a.y) * ( 0.5 - xx - zz ) + - static_cast(a.z) * ( yz - xw ) ), - - 2.0 * ( static_cast(a.x) * ( xz - yw ) + - static_cast(a.y) * ( yz + xw ) + - static_cast(a.z) * ( 0.5 - xx - yy ) ) ); - } + return Vector3Template( 2.0 * ( static_cast(a.x) * ( 0.5 - yy + - zz ) + static_cast(a.y) * (xy - zw ) + static_cast(a.z) + * (xz + yw ) ), - /** - * applyInversed - */ - template - inline Vector3Template applyInversed( - const Vector3Template &a ) const - { - T xx = x*x, xy = x*y, xz = x*z, xw = -x*w, - yy = y*y, yz = y*z, yw = -y*w, - zz = z*z, zw = -z*w; - - return Vector3Template( - 2.0 * ( static_cast(a.x) * ( 0.5 - yy - zz ) + - static_cast(a.y) * ( xy - zw ) + - static_cast(a.z) * ( xz + yw ) ), - - 2.0 * ( static_cast(a.x) * ( xy + zw ) + - static_cast(a.y) * ( 0.5 - xx - zz ) + - static_cast(a.z) * ( yz - xw ) ), - - 2.0 * ( static_cast(a.x) * ( xz - yw ) + - static_cast(a.y) * ( yz + xw ) + - static_cast(a.z) * ( 0.5 - xx - yy ) ) ); - } - - /** - * transform from ode to gl coodinates - */ - inline QuaternionTemplate toGl() const - { - return QuaternionTemplate( w, x, z, -y ); - } + 2.0 * ( static_cast(a.x) * (xy + zw ) + static_cast(a.y) * ( 0.5 + - xx - zz ) + static_cast(a.z) * (yz - xw ) ), - /** - * transform from gl to ode coodinates - */ - inline QuaternionTemplate toOde() const - { - return QuaternionTemplate( w, x, -z, y ); - } - - inline QuaternionTemplate slerp( const QuaternionTemplate &q, const Scalar &t ) - { - Scalar phi = acos(w*q.w + x*q.x + y*q.y + z*q.z); - Scalar s = 1 / sin(phi); - Scalar a = sin(phi*(1-t)) * s; - Scalar b = sin(phi*t) * s; + 2.0 * ( static_cast(a.x) * (xz - yw ) + static_cast(a.y) * (yz + + xw ) + static_cast(a.z) * ( 0.5 - xx - yy ) )); + } - return QuaternionTemplate( a*w+b*q.w, a*x+b*q.x, a*y+b*q.y, a*z+b*q.z ); - } - }; - - typedef QuaternionTemplate QuaternionFloat; - typedef QuaternionTemplate QuaternionDouble; - typedef QuaternionTemplate Quaternion; + template inline Vector3Template applyInversed( + const Vector3Template &a) const + { + T xx = x*x, xy = x*y, xz = x*z, xw = -x*w, yy = y*y, yz = y*z, yw = -y + *w, zz = z*z, zw = -z*w; + + return Vector3Template( 2.0 * ( static_cast(a.x) * ( 0.5 - yy + - zz ) + static_cast(a.y) * (xy - zw ) + static_cast(a.z) + * (xz + yw ) ), + + 2.0 * ( static_cast(a.x) * (xy + zw ) + static_cast(a.y) * ( 0.5 + - xx - zz ) + static_cast(a.z) * (yz - xw ) ), + + 2.0 * ( static_cast(a.x) * (xz - yw ) + static_cast(a.y) * (yz + + xw ) + static_cast(a.z) * ( 0.5 - xx - yy ) )); + } + + inline QuaternionTemplate slerp(const QuaternionTemplate &q, + const Scalar &t) + { + T phi = acos(w*q.w + x*q.x + y*q.y + z*q.z); + T s = 1 / sin(phi); + T a = sin(phi*(1-t)) * s; + T b = sin(phi*t) * s; + + return QuaternionTemplate(a*w+b*q.w, a*x+b*q.x, a*y+b*q.y, a*z+b*q.z); + } +}; + +typedef QuaternionTemplate QuaternionFloat; +typedef QuaternionTemplate QuaternionDouble; +typedef QuaternionTemplate Quaternion; } #endif diff --git a/engine/ModelManager.cpp b/engine/ModelManager.cpp index f3f1767..8715fa2 100644 --- a/engine/ModelManager.cpp +++ b/engine/ModelManager.cpp @@ -20,25 +20,21 @@ namespace BlueCore void Model::render(RenderDevice *device) const { ModelMesh->render(); - //glEnable(GL_LIGHTING); - //glDepthFunc ( GL_LEQUAL ); - //glEnable ( GL_DEPTH_TEST ); - //glDepthMask ( GL_TRUE ); - +/* + glEnable(GL_LIGHTING); + glDepthFunc ( GL_LEQUAL ); + glEnable ( GL_DEPTH_TEST ); + glDepthMask ( GL_TRUE ); +*/ /* glEnableClientState (GL_VERTEX_ARRAY ); glEnableClientState (GL_TEXTURE_COORD_ARRAY ); glEnableClientState (GL_NORMAL_ARRAY ); - glMatrixMode (GL_MODELVIEW ); - glPushMatrix(); - Matrix4x4 m(_AbsoluteRotation, _AbsoluteTranslation); - glMultMatrixd ( ( GLdouble * ) &m.m ); - mesh->vertex_buffer.bind(); mesh->index_buffer.bind(); */ - /* +/* glMaterialfv (GL_FRONT, GL_SHININESS, &pass.Shininess); glMaterialfv (GL_FRONT, GL_SPECULAR, ( GLfloat * ) &pass.Specular ); diff --git a/engine/RenderDevice.cpp b/engine/RenderDevice.cpp index 88287c1..1380633 100644 --- a/engine/RenderDevice.cpp +++ b/engine/RenderDevice.cpp @@ -137,10 +137,12 @@ void RenderDevice::setupProjectionMatrix() void RenderDevice::setupViewMatrix() { if (_Camera.valid()) + { glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - Matrix4x4 m(_Camera->getRotation(), _Camera->getRotation().applyInversed(_Camera->getPosition() * -1.0) ); - glMultMatrixd( ( GLdouble * ) &m.m ); + glLoadIdentity(); + Matrix4x4 m(_Camera->getRotation(), _Camera->getRotation().applyInversed(_Camera->getPosition() * -1.0) ); + glMultMatrixd( ( GLdouble * ) &m.m ); + } } void RenderDevice::pushTransformation(const Vector3& position, diff --git a/engine/RigidBodySimulation.cpp b/engine/RigidBodySimulation.cpp index cc71105..8330adb 100644 --- a/engine/RigidBodySimulation.cpp +++ b/engine/RigidBodySimulation.cpp @@ -545,7 +545,6 @@ bool RigidBodySimulation::step() { if (_steps > 0) { - _ScriptSystem->callFunction("OnStep", _stepSize); dSpaceCollide(_space, 0, &nearCallback); dWorldQuickStep(_world, _stepSize); StepSignal(_stepSize); diff --git a/engine/RigidBodySimulation.h b/engine/RigidBodySimulation.h index bc2e223..15d17bd 100644 --- a/engine/RigidBodySimulation.h +++ b/engine/RigidBodySimulation.h @@ -25,7 +25,7 @@ namespace BlueCore { class RigidBodySimulation; -class RigidBody +class RigidBody : public Referenced { Vector3 _Position; Quaternion _Orientation; diff --git a/engine/SceneGraph.cpp b/engine/SceneGraph.cpp new file mode 100644 index 0000000..0b99715 --- /dev/null +++ b/engine/SceneGraph.cpp @@ -0,0 +1,35 @@ +#include "SceneGraph.h" + +namespace BlueCore +{ + +SceneGraph::SceneGraph() +{ + _RootNode = new SceneNode(); +} +SceneGraph::~SceneGraph() +{ + +} + +SceneNode* SceneGraph::getRootNode() +{ + return _RootNode.pointer(); +} + +void SceneGraph::setCamera(Camera *camera) +{ + _Camera = camera; +} + +void SceneGraph::update(Scalar time) +{ + _RootNode->update (time); +} + +void SceneGraph::queue (RenderQueue *queue) +{ + _RootNode->queue (queue, _Camera); +} + +} diff --git a/engine/SceneGraph.h b/engine/SceneGraph.h new file mode 100644 index 0000000..0bc8d46 --- /dev/null +++ b/engine/SceneGraph.h @@ -0,0 +1,29 @@ +#ifndef BLUECORE_SCENEGRAPH_H +#define BLUECORE_SCENEGRAPH_H + +#include "SceneNode.h" + +namespace BlueCore +{ + +class SceneGraph : public Referenced +{ + ref_ptr _RootNode; + ref_ptr _Camera; + +public: + SceneGraph(); + ~SceneGraph(); + + SceneNode* getRootNode(); + + void setCamera(Camera *camera); + + void update(Scalar time); + void queue (RenderQueue *queue); + +}; + +} + +#endif /*SCENEGRAPH_H_*/ diff --git a/engine/SceneNode.cpp b/engine/SceneNode.cpp index 7b1cb51..d1fa48f 100644 --- a/engine/SceneNode.cpp +++ b/engine/SceneNode.cpp @@ -1,162 +1,150 @@ #include "SceneNode.h" -namespace BlueCore { +namespace BlueCore +{ #define DEBUG_SCENEGRAPH -//------------------------------------------------------------------------------ SceneNode::SceneNode() : - Named("unnamed SceneNode"), _Parent(0) + Named("unnamed SceneNode"), _Parent(0) { #ifdef DEBUG_SCENEGRAPH - clog << "SceneNode 'Unnamed SceneNode' created." << endline; + clog << "SceneNode 'Unnamed SceneNode' created." << endline; #endif } -//------------------------------------------------------------------------------ SceneNode::SceneNode(const std::string &name) : - Named(name), _Parent(0) + Named(name), _Parent(0) { #ifdef DEBUG_SCENEGRAPH - clog << "SceneNode '" << name << "' created." << endline; + clog << "SceneNode '" << name << "' created." << endline; #endif } -//------------------------------------------------------------------------------ SceneNode::~SceneNode() { - detachAll(); + detachAll(); - #ifdef DEBUG_SCENEGRAPH - clog << "SceneNode '" << getName() << "' deleted." << endline; - #endif +#ifdef DEBUG_SCENEGRAPH + clog << "SceneNode '" << getName() << "' deleted." << endline; +#endif } -//------------------------------------------------------------------------------ void SceneNode::attach(SceneNode *node) { - if (node == 0) - return; + if (node == 0) + return; - _Children.push_back(node ); + _Children.push_back(node); - node->_Parent = this; + node->_Parent = this; - node->addReference(); + node->addReference(); #ifdef DEBUG_SCENEGRAPH - clog << "SceneNode '" << node->getName() << "' attached to '" << this->getName() << "'" << endline; + clog << "SceneNode '" << node->getName() << "' attached to '" + << this->getName() << "'" << endline; #endif } -//------------------------------------------------------------------------------ void SceneNode::detach(SceneNode *node) { - node->_Parent = 0; - node->removeReference(); + node->_Parent = 0; + node->removeReference(); - _Children.remove(node ); + _Children.remove(node); #ifdef DEBUG_SCENEGRAPH - clog << "SceneNode '" << node->getName() << "' detach from '" << this->getName() << "'" << endline; + clog << "SceneNode '" << node->getName() << "' detach from '" + << this->getName() << "'" << endline; #endif } -//------------------------------------------------------------------------------ void SceneNode::detachAll() { - SceneNodeList::iterator i; + SceneNodeList::iterator i; - for (i = _Children.begin(); i != _Children.end(); i++) - { - ( *i )->_Parent = 0; - ( *i )->removeReference(); - } + for (i = _Children.begin(); i != _Children.end(); i++) + { + ( *i )->_Parent = 0; + ( *i )->removeReference(); + } - _Children.clear(); + _Children.clear(); } -//------------------------------------------------------------------------------ SceneNode *SceneNode::getParent() const { - return _Parent; + return _Parent; } -//------------------------------------------------------------------------------ void SceneNode::detachFromParent() { - if (_Parent ) - _Parent->detach( this); + if (_Parent) + _Parent->detach( this); } -//------------------------------------------------------------------------------ -const SceneNode::SceneNodeList& SceneNode::getChildren () const +const SceneNode::SceneNodeList& SceneNode::getChildren() const { - return _Children; + return _Children; } -//------------------------------------------------------------------------------ void SceneNode::update(Scalar time) { - updateAbsoluteTransformation(); + updateAbsoluteTransformation(); -// if (isActive() ) - { - SceneNodeList::iterator i; + // if (isActive() ) + { + SceneNodeList::iterator i; - for (i = _Children.begin(); i != _Children.end(); i++) - ( *i )->update(time ); - } + for (i = _Children.begin(); i != _Children.end(); i++) + ( *i )->update(time); + } } -//------------------------------------------------------------------------------ -void SceneNode::render(RenderDevice *device, Camera *camera) +void SceneNode::queue(RenderQueue *queue, Camera *camera) { - if (isActive() ) - { - SceneNodeList::iterator i; + if (isActive()) + { + SceneNodeList::iterator i; - for (i = _Children.begin(); i != _Children.end(); i++) - { - ( *i )->render(device, camera); - } - } + for (i = _Children.begin(); i != _Children.end(); i++) + { + ( *i )->queue(queue, camera); + } + } } -//------------------------------------------------------------------------------ const Transformation& SceneNode::getRelativeTransformation() { - return _RelativeTransformation; + return _RelativeTransformation; } const Transformation& SceneNode::getAbsoluteTransformation() { - return _AbsoluteTransformation; + return _AbsoluteTransformation; } -//------------------------------------------------------------------------------ void SceneNode::setRelativeTranslation(const Vector3 &translation) { - _RelativeTransformation.translation = translation; + _RelativeTransformation.translation = translation; } -//------------------------------------------------------------------------------ -void SceneNode::setRelativeRotation (const Quaternion &rotation) +void SceneNode::setRelativeRotation(const Quaternion &rotation) { - _RelativeTransformation.rotation = rotation; + _RelativeTransformation.rotation = rotation; } -//------------------------------------------------------------------------------ void SceneNode::updateAbsoluteTransformation() { -/* - if (_Parent ) - _AbsoluteTranslation = _Parent->getAbsoluteTranslation() - +_Parent->getAbsoluteRotation().inversed() * _RelativeTranslation; - else - _AbsoluteTranslation = _RelativeTranslation; - */ + /* + if (_Parent ) + _AbsoluteTranslation = _Parent->getAbsoluteTranslation() + +_Parent->getAbsoluteRotation().inversed() * _RelativeTranslation; + else + _AbsoluteTranslation = _RelativeTranslation; + */ } } // namespace BlueCore diff --git a/engine/SceneNode.h b/engine/SceneNode.h index eae7653..2a78f6b 100644 --- a/engine/SceneNode.h +++ b/engine/SceneNode.h @@ -2,7 +2,9 @@ #define BLUECORE_SCENE_NODE_H #include "Camera.h" -#include "RenderDevice.h" +#include "RenderQueue.h" +#include "RigidBodySimulation.h" +#include "ModelManager.h" #include "Utilities/Referenced.h" #include "Utilities/Named.h" @@ -30,6 +32,9 @@ class SceneNode : public Referenced, public Named, public Activated Transformation _RelativeTransformation; Transformation _AbsoluteTransformation; + weak_ptr _RigidBody; + weak_ptr _Model; + public: SceneNode(); @@ -43,9 +48,9 @@ class SceneNode : public Referenced, public Named, public Activated void detach(SceneNode *node); void detachAll(); void detachFromParent(); - + virtual void update(Scalar time); - virtual void render (RenderDevice *device, Camera *camera); + virtual void queue (RenderQueue *queue, Camera *camera); const Transformation& getRelativeTransformation(); const Transformation& getAbsoluteTransformation(); diff --git a/engine/ScriptSystem.cpp b/engine/ScriptSystem.cpp index 39ff2f6..64b21ad 100644 --- a/engine/ScriptSystem.cpp +++ b/engine/ScriptSystem.cpp @@ -200,4 +200,20 @@ void ScriptSystem::callFunction(const std::string &name, double value) } sq_pop(_VM, 1); } + +void ScriptSystem::callFunction(const std::string &name, double value1, double value2) +{ + sq_pushroottable(_VM); + sq_pushstring(_VM, name.c_str(), -1); + if (SQ_SUCCEEDED(sq_get(_VM, -2)) ) + { + sq_pushroottable(_VM); + sq_pushfloat(_VM, value1); + sq_pushfloat(_VM, value2); + sq_call(_VM, 3, SQFalse, SQTrue); + sq_pop(_VM, 1); + } + sq_pop(_VM, 1); +} + } diff --git a/engine/ScriptSystem.h b/engine/ScriptSystem.h index aa97a69..cbd5a8e 100644 --- a/engine/ScriptSystem.h +++ b/engine/ScriptSystem.h @@ -31,6 +31,8 @@ class ScriptSystem : public Referenced void callFunction(const std::string &name); void callFunction(const std::string &name, double value); + void callFunction(const std::string &name, double value1, double value2); + void callFunction(const std::string &name, double value1, double value2, double value3); }; } // namespace BlueCore diff --git a/engine/ScriptSystem_Application.cpp b/engine/ScriptSystem_Application.cpp new file mode 100644 index 0000000..5a247e6 --- /dev/null +++ b/engine/ScriptSystem_Application.cpp @@ -0,0 +1,59 @@ +#include "Application.h" + +namespace BlueCore +{ + +static weak_ptr gApplication; + +static SQInteger _quit(HSQUIRRELVM v) +{ + if (gApplication.valid()) + gApplication->quit(); + return 0; +} + +static SQInteger _toggle_pause(HSQUIRRELVM v) +{ + if (gApplication.valid()) + gApplication->togglePause(); + return 0; +} + +static SQInteger _setscenegraph(HSQUIRRELVM v) +{ + SceneGraph *scenegraph = 0; + + sq_getinstanceup(v, -1, ( void ** ) &scenegraph, 0); + + gApplication->setSceneGraph(scenegraph); + + return 0; +} + +void setupScriptSystem_Application(ScriptSystem* scriptsystem, + Application* application) +{ + if (application == 0) + return; + + gApplication = application; + + HSQUIRRELVM vm = scriptsystem->getVM(); + sq_pushroottable(vm); + + sq_pushstring(vm, "quit", -1); + sq_newclosure(vm, _quit, 0); + sq_newslot(vm, -3, false); + + sq_pushstring(vm, "togglePause", -1); + sq_newclosure(vm, _toggle_pause, 0); + sq_newslot(vm, -3, false); + + sq_pushstring(vm, "setSceneGraph", -1); + sq_newclosure(vm, _setscenegraph, 0); + sq_newslot(vm, -3, false); + + sq_pop(vm, 1); +} + +} diff --git a/engine/ScriptSystem_Application.h b/engine/ScriptSystem_Application.h new file mode 100644 index 0000000..d28ca2d --- /dev/null +++ b/engine/ScriptSystem_Application.h @@ -0,0 +1,11 @@ +#ifndef BLUECORE_SCRIPTING_APPLICATION_H +#define BLUECORE_SCRIPTING_APPLICATION_H + +#include "Application.h" + +namespace BlueCore +{ + void setupScriptSystem_Application(ScriptSystem* scriptsystem, Application* application); +} + +#endif diff --git a/engine/ScriptSystem_Camera.cpp b/engine/ScriptSystem_Camera.cpp new file mode 100644 index 0000000..b819139 --- /dev/null +++ b/engine/ScriptSystem_Camera.cpp @@ -0,0 +1,125 @@ +#include "ScriptSystem_Camera.h" +#include "ScriptSystem_Math.h" + +#include "Camera.h" + +namespace BlueCore +{ + +static SQInteger _camera_releasehook(SQUserPointer p, SQInteger size) +{ + Camera *camera = (Camera *)p; + + if (camera) + camera->removeReference(); + + return 1; +} + +static SQInteger _camera_constructor(HSQUIRRELVM vm) +{ + SQInteger argc = sq_gettop(vm); + + if (argc < 1) + return 0; + + Camera *camera = new Camera(); + camera->addReference(); + + sq_setinstanceup(vm, 1, (void *)camera ); + sq_setreleasehook(vm, 1, _camera_releasehook); + + return 0; +} + +static SQInteger _camera_setposition(HSQUIRRELVM v) +{ + Camera *camera = 0; + + sq_getinstanceup(v, 1, ( void ** ) &camera, 0); + + if (camera) + { + Vector3 position; + int argc = sq_gettop(v); + + if (argc == 2) + { + _getvectorvalues(v, 2, position.x, position.y, position.z); + } + else if (argc == 4) + { + sq_getfloat(v, 2, &position.x); + sq_getfloat(v, 3, &position.y); + sq_getfloat(v, 4, &position.z); + } + + camera->setPosition(position); + } + + return 0; +} + +static SQInteger _camera_lookat(HSQUIRRELVM v) +{ + Camera *camera = 0; + + sq_getinstanceup(v, 1, ( void ** ) &camera, 0); + + if (camera) + { + Vector3 position; + int argc = sq_gettop(v); + + if (argc == 2) + { + _getvectorvalues(v, 2, position.x, position.y, position.z); + } + else if (argc == 4) + { + sq_getfloat(v, 2, &position.x); + sq_getfloat(v, 3, &position.y); + sq_getfloat(v, 4, &position.z); + } + + camera->lookAt(position); + } + + return 0; +} + +void setupScriptSystem_Camera(ScriptSystem* scriptsystem) +{ + if (scriptsystem == 0) + return; + + HSQUIRRELVM vm = scriptsystem->getVM(); + + sq_pushroottable(vm); + + // push class + sq_pushstring(vm, "Camera", -1); + + if (SQ_SUCCEEDED(sq_newclass(vm, SQFalse) ) ) + { + // register constructor + sq_pushstring(vm, "constructor", -1); + sq_newclosure(vm, _camera_constructor, 0); + sq_newslot(vm, -3, false); + + sq_pushstring(vm, "setPosition", -1); + sq_newclosure(vm, _camera_setposition, 0); + sq_newslot(vm, -3, false); + + sq_pushstring(vm, "lookAt", -1); + sq_newclosure(vm, _camera_lookat, 0); + sq_newslot(vm, -3, false); + + // create class + sq_newslot(vm, -3, false); + } + + sq_poptop(vm); +} + +} diff --git a/engine/ScriptSystem_Camera.h b/engine/ScriptSystem_Camera.h new file mode 100644 index 0000000..e1cc22c --- /dev/null +++ b/engine/ScriptSystem_Camera.h @@ -0,0 +1,11 @@ +#ifndef BLUECORE_SCRIPTING_CAMERA_H +#define BLUECORE_SCRIPTING_CAMERA_H + +#include "ScriptSystem.h" + +namespace BlueCore +{ + void setupScriptSystem_Camera(ScriptSystem* scriptsystem); +} + +#endif diff --git a/engine/ScriptSystem_Font.cpp b/engine/ScriptSystem_Font.cpp index 2a80c8d..4f9153e 100644 --- a/engine/ScriptSystem_Font.cpp +++ b/engine/ScriptSystem_Font.cpp @@ -20,107 +20,107 @@ static SQInteger _font_releasehook(SQUserPointer p, SQInteger size) //------------------------------------------------------------------------------ static SQInteger _font_constructor(HSQUIRRELVM vm) { - SQInteger argc = sq_gettop (vm ); + SQInteger argc = sq_gettop(vm); - Font *font = 0; + Font *font = 0; - if (argc < 3) - { - if (gFontManager.valid()) - font = gFontManager->getDefaultFont(); - } - else - { - const SQChar *name = 0; - SQInteger size = 1; - SQBool hinting = SQFalse; + if (argc < 3) + { + if (gFontManager.valid()) + font = gFontManager->getDefaultFont(); + } + else + { + const SQChar *name = 0; + SQInteger size = 1; + SQBool hinting = SQFalse; - sq_getstring (vm, 2, &name ); - sq_getinteger (vm, 3, &size ); - if (argc > 3) - sq_getbool(vm, 4, &hinting ); + sq_getstring(vm, 2, &name); + sq_getinteger(vm, 3, &size); + if (argc > 3) + sq_getbool(vm, 4, &hinting); - if (gFontManager.valid()) - { - font = gFontManager->loadFont(name, size, hinting == SQTrue ); - if (font) - font->addReference(); - } - } + if (gFontManager.valid()) + { + font = gFontManager->loadFont(name, size, hinting == SQTrue); + if (font) + font->addReference(); + } + } - sq_setinstanceup(vm, 1, (void *)font ); - sq_setreleasehook (vm, 1, _font_releasehook ); - - return 0; + sq_setinstanceup(vm, 1, (void *)font ); + sq_setreleasehook(vm, 1, _font_releasehook); + + return 0; } //------------------------------------------------------------------------------ static SQInteger _font_print(HSQUIRRELVM vm) { - int argc = sq_gettop (vm ); + int argc = sq_gettop(vm); - if (argc < 4) - return 0; + if (argc < 4) + return 0; - const Font *font = 0; - sq_getinstanceup (vm, 1, ( void ** ) &font, 0); + const Font *font = 0; + sq_getinstanceup(vm, 1, ( void ** ) &font, 0); - if (font) - { - SQFloat x, y; - const char *text; - SQInteger halign = 0, valign = 0; + if (font) + { + SQFloat x, y; + const char *text; + SQInteger halign = 0, valign = 0; - sq_getfloat (vm, 2, &x ); - sq_getfloat (vm, 3, &y ); - sq_getstring(vm, 4, &text); + sq_getfloat(vm, 2, &x); + sq_getfloat(vm, 3, &y); + sq_getstring(vm, 4, &text); - if (argc > 4) - { - sq_getinteger(vm, 5, &halign ); - } + if (argc > 4) + { + sq_getinteger(vm, 5, &halign); + } - if (argc > 5) - { - sq_getinteger(vm, 6, &valign ); - } + if (argc > 5) + { + sq_getinteger(vm, 6, &valign); + } - font->print( (float)x, (float)y, text, halign, valign ); - } + font->print( (float)x, (float)y, text, halign, valign); + } - return 0; + return 0; } //------------------------------------------------------------------------------ -void setupScriptSystem_Font (ScriptSystem* scriptsystem, FontManager* fontmanager) +void setupScriptSystem_Font(ScriptSystem* scriptsystem, FontManager* fontmanager) { - if (scriptsystem && fontmanager) - { - HSQUIRRELVM vm = scriptsystem->getVM(); + if (scriptsystem && fontmanager) + { + HSQUIRRELVM vm = scriptsystem->getVM(); - gFontManager = fontmanager; + gFontManager = fontmanager; - sq_pushroottable (vm ); + sq_pushroottable(vm); - // push class - sq_pushstring (vm, "Font", -1); + // push class + sq_pushstring(vm, "Font", -1); - if (SQ_SUCCEEDED (sq_newclass (vm, SQFalse ) ) ) - { - // register constructor - sq_pushstring (vm, "constructor", -1); - sq_newclosure (vm, _font_constructor, 0); - sq_newslot (vm, -3, false); + if (SQ_SUCCEEDED(sq_newclass(vm, SQFalse) ) ) + { + // register constructor + sq_pushstring(vm, "constructor", -1); + sq_newclosure(vm, _font_constructor, 0); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "print", -1); - sq_newclosure (vm, _font_print, 0); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "print", -1); + sq_newclosure(vm, _font_print, 0); + sq_newslot(vm, -3, false); - // create class - sq_newslot (vm, -3, false); - } + // create class + sq_newslot(vm, -3, false); + } - sq_poptop (vm ); - } + sq_poptop(vm); + } } } diff --git a/engine/ScriptSystem_RigidBody.cpp b/engine/ScriptSystem_RigidBody.cpp index 3bc5a76..778f404 100644 --- a/engine/ScriptSystem_RigidBody.cpp +++ b/engine/ScriptSystem_RigidBody.cpp @@ -11,532 +11,532 @@ static weak_ptr gRigidBodySimulation; //------------------------------------------------------------------------------ static SQInteger _rigidbody_getposition(HSQUIRRELVM v) { - RigidBody *body = 0; - Vector3 position; + RigidBody *body = 0; + Vector3 position; - if (sq_gettop (v ) == 1) - { - sq_getinstanceup (v, 1, ( void ** ) &body, 0); - } + if (sq_gettop(v) == 1) + { + sq_getinstanceup(v, 1, ( void ** ) &body, 0); + } - if (body ) - { - position = body->getPosition(); - _pushvector (v, position.x, position.y, position.z); - return 1; - } + if (body) + { + position = body->getPosition(); + _pushvector(v, position.x, position.y, position.z); + return 1; + } - return 0; + return 0; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_getrotation(HSQUIRRELVM v) { - RigidBody *body = 0; - Quaternion q; + RigidBody *body = 0; + Quaternion q; - if (sq_gettop (v ) == 1) - { - sq_getinstanceup (v, 1, ( void ** ) &body, 0); - } + if (sq_gettop(v) == 1) + { + sq_getinstanceup(v, 1, ( void ** ) &body, 0); + } - if (body ) - { - q = body->getOrientation(); - _pushquaternion (v, q.w, q.x, q.y, q.z); - return 1; - } + if (body) + { + q = body->getOrientation(); + _pushquaternion(v, q.w, q.x, q.y, q.z); + return 1; + } - return 0; + return 0; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_getangularvelocity(HSQUIRRELVM v) { - RigidBody *body = 0; - Vector3 velocity; + RigidBody *body = 0; + Vector3 velocity; - if (sq_gettop (v ) == 1) - { - sq_getinstanceup (v, 1, ( void ** ) &body, 0); - } + if (sq_gettop(v) == 1) + { + sq_getinstanceup(v, 1, ( void ** ) &body, 0); + } - if (body ) - { - velocity = body->getAngularVelocity(); + if (body) + { + velocity = body->getAngularVelocity(); - _pushvector (v, velocity.x, velocity.y, velocity.z); + _pushvector(v, velocity.x, velocity.y, velocity.z); - return 1; - } + return 1; + } - return 0; + return 0; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_getlinearvelocity(HSQUIRRELVM v) { - RigidBody *body = 0; - Vector3 velocity; + RigidBody *body = 0; + Vector3 velocity; - if (sq_gettop (v ) == 1) - { - sq_getinstanceup (v, 1, ( void ** ) &body, 0); - } + if (sq_gettop(v) == 1) + { + sq_getinstanceup(v, 1, ( void ** ) &body, 0); + } - if (body ) - { - velocity = body->getLinearVelocity(); + if (body) + { + velocity = body->getLinearVelocity(); - _pushvector (v, velocity.x, velocity.y, velocity.z); + _pushvector(v, velocity.x, velocity.y, velocity.z); - return 1; - } + return 1; + } - return 0; + return 0; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_setposition(HSQUIRRELVM v) { - RigidBody *body = 0; + RigidBody *body = 0; - sq_getinstanceup (v, 1, ( void ** ) &body, 0); + sq_getinstanceup(v, 1, ( void ** ) &body, 0); - if (body ) - { - Vector3 position; - int argc = sq_gettop (v ); + if (body) + { + Vector3 position; + int argc = sq_gettop(v); - if (argc == 2) - { - _getvectorvalues (v, 2, position.x, position.y, position.z); - } - else if (argc == 4) - { - sq_getfloat (v, 2, &position.x); - sq_getfloat (v, 3, &position.y); - sq_getfloat (v, 4, &position.z); - } + if (argc == 2) + { + _getvectorvalues(v, 2, position.x, position.y, position.z); + } + else if (argc == 4) + { + sq_getfloat(v, 2, &position.x); + sq_getfloat(v, 3, &position.y); + sq_getfloat(v, 4, &position.z); + } - body->setPosition(position ); - } + body->setPosition(position); + } - return 0; + return 0; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_setlinearvelocity(HSQUIRRELVM v) { - RigidBody *body = 0; + RigidBody *body = 0; - sq_getinstanceup (v, 1, ( void ** ) &body, 0); + sq_getinstanceup(v, 1, ( void ** ) &body, 0); - if (body ) - { - Vector3 velocity; - int argc = sq_gettop (v ); + if (body) + { + Vector3 velocity; + int argc = sq_gettop(v); - if (argc == 2) - { - _getvectorvalues (v, 2, velocity.x, velocity.y, velocity.z); - } - else if (argc == 4) - { - sq_getfloat (v, 2, &velocity.x); - sq_getfloat (v, 3, &velocity.y); - sq_getfloat (v, 4, &velocity.z); - } + if (argc == 2) + { + _getvectorvalues(v, 2, velocity.x, velocity.y, velocity.z); + } + else if (argc == 4) + { + sq_getfloat(v, 2, &velocity.x); + sq_getfloat(v, 3, &velocity.y); + sq_getfloat(v, 4, &velocity.z); + } - body->setLinearVelocity(velocity ); - } + body->setLinearVelocity(velocity); + } - return 0; + return 0; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_setrotation(HSQUIRRELVM v) { - RigidBody *body = 0; + RigidBody *body = 0; - sq_getinstanceup (v, 1, ( void ** ) &body, 0); + sq_getinstanceup(v, 1, ( void ** ) &body, 0); - if (body ) - { - Quaternion q; - int argc = sq_gettop (v ); + if (body) + { + Quaternion q; + int argc = sq_gettop(v); - if (argc == 2) - { - _getquaternionvalues (v, 2, q.w, q.x, q.y, q.z); - } - else if (argc == 5) - { - sq_getfloat (v, 2, &q.w); - sq_getfloat (v, 3, &q.x); - sq_getfloat (v, 4, &q.y); - sq_getfloat (v, 5, &q.z); - } + if (argc == 2) + { + _getquaternionvalues(v, 2, q.w, q.x, q.y, q.z); + } + else if (argc == 5) + { + sq_getfloat(v, 2, &q.w); + sq_getfloat(v, 3, &q.x); + sq_getfloat(v, 4, &q.y); + sq_getfloat(v, 5, &q.z); + } - body->setOrientation(q ); - } + body->setOrientation(q); + } - return 0; + return 0; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_applylocalforce(HSQUIRRELVM v) { - RigidBody *body = 0; + RigidBody *body = 0; - sq_getinstanceup (v, 1, ( void ** ) &body, 0); + sq_getinstanceup(v, 1, ( void ** ) &body, 0); - if (body ) - { - int argc = sq_gettop (v ); + if (body) + { + int argc = sq_gettop(v); - if (argc == 2) - { - Vector3 force; - _getvectorvalues (v, 2, force.x, force.y, force.z); - body->applyLocalForce(force ); - } - else if (argc == 3) - { - Vector3 force, point; - _getvectorvalues (v, 2, force.x, force.y, force.z); - _getvectorvalues (v, 3, point.x, point.y, point.z); - body->applyLocalForce(force, point ); - } - } + if (argc == 2) + { + Vector3 force; + _getvectorvalues(v, 2, force.x, force.y, force.z); + body->applyLocalForce(force); + } + else if (argc == 3) + { + Vector3 force, point; + _getvectorvalues(v, 2, force.x, force.y, force.z); + _getvectorvalues(v, 3, point.x, point.y, point.z); + body->applyLocalForce(force, point); + } + } - return 0; + return 0; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_applyglobalforce(HSQUIRRELVM v) { - RigidBody *body = 0; + RigidBody *body = 0; - sq_getinstanceup (v, 1, ( void ** ) &body, 0); + sq_getinstanceup(v, 1, ( void ** ) &body, 0); - if (body ) - { - int argc = sq_gettop (v ); + if (body) + { + int argc = sq_gettop(v); - if (argc == 2) - { - Vector3 force; - _getvectorvalues (v, 2, force.x, force.y, force.z); - body->applyGlobalForce(force ); - } - else if (argc == 3) - { - Vector3 force, point; - _getvectorvalues (v, 2, force.x, force.y, force.z); - _getvectorvalues (v, 3, point.x, point.y, point.z); - body->applyGlobalForce(force, point ); - } - } + if (argc == 2) + { + Vector3 force; + _getvectorvalues(v, 2, force.x, force.y, force.z); + body->applyGlobalForce(force); + } + else if (argc == 3) + { + Vector3 force, point; + _getvectorvalues(v, 2, force.x, force.y, force.z); + _getvectorvalues(v, 3, point.x, point.y, point.z); + body->applyGlobalForce(force, point); + } + } - return 0; + return 0; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_addcollisionmesh(HSQUIRRELVM v) { - SQInteger argc = sq_gettop (v ); + SQInteger argc = sq_gettop(v); - if (argc < 3) - { - sq_pushinteger (v, 0); - return 1; - } + if (argc < 3) + { + sq_pushinteger(v, 0); + return 1; + } - RigidBody *body = 0; + RigidBody *body = 0; - const SQChar *meshname = 0; + const SQChar *meshname = 0; - SQFloat density = 1.0; + SQFloat density = 1.0; - sq_getinstanceup (v, 1, ( void ** ) &body, 0); + sq_getinstanceup(v, 1, ( void ** ) &body, 0); - sq_getstring (v, 2, &meshname ); + sq_getstring(v, 2, &meshname); - sq_getfloat (v, 3, &density ); + sq_getfloat(v, 3, &density); - if (body ) - { - sq_pushinteger (v, body->addCollisionMesh(meshname, density ) ); - } - else - sq_pushinteger (v, 0); + if (body) + { + sq_pushinteger(v, body->addCollisionMesh(meshname, density) ); + } + else + sq_pushinteger(v, 0); - return 1; + return 1; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_setcollisionmeshposition(HSQUIRRELVM v) { - int argc = sq_gettop (v ); + int argc = sq_gettop(v); - // need at least geom + vector + // need at least geom + vector - if (argc < 3) - return 0; + if (argc < 3) + return 0; - RigidBody *body = 0; + RigidBody *body = 0; - sq_getinstanceup (v, 1, ( void ** ) &body, 0); + sq_getinstanceup(v, 1, ( void ** ) &body, 0); - if (body ) - { - SQInteger geom = 0; - sq_getinteger (v, 2, &geom ); + if (body) + { + SQInteger geom = 0; + sq_getinteger(v, 2, &geom); - Vector3 position; + Vector3 position; - if (argc == 3) - { - _getvectorvalues (v, 3, position.x, position.y, position.z); - } - else if (argc == 5) - { - sq_getfloat (v, 3, &position.x); - sq_getfloat (v, 4, &position.y); - sq_getfloat (v, 5, &position.z); - } + if (argc == 3) + { + _getvectorvalues(v, 3, position.x, position.y, position.z); + } + else if (argc == 5) + { + sq_getfloat(v, 3, &position.x); + sq_getfloat(v, 4, &position.y); + sq_getfloat(v, 5, &position.z); + } - body->setCollisionMeshPosition(geom, position ); - } + body->setCollisionMeshPosition(geom, position); + } - return 0; + return 0; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_disablecollisionmesh(HSQUIRRELVM v) { - int argc = sq_gettop (v ); + int argc = sq_gettop(v); - // need at least geom + // need at least geom - if (argc < 2) - return 0; + if (argc < 2) + return 0; - RigidBody *body = 0; + RigidBody *body = 0; - sq_getinstanceup (v, 1, ( void ** ) &body, 0); + sq_getinstanceup(v, 1, ( void ** ) &body, 0); - if (body ) - { - SQInteger geom = 0; - sq_getinteger (v, 2, &geom ); + if (body) + { + SQInteger geom = 0; + sq_getinteger(v, 2, &geom); - body->disableCollisionMesh(geom ); - } + body->disableCollisionMesh(geom); + } - return 0; + return 0; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_enablecollisionmesh(HSQUIRRELVM v) { - int argc = sq_gettop (v ); + int argc = sq_gettop(v); - // need at least geom + // need at least geom - if (argc < 2) - return 0; + if (argc < 2) + return 0; - RigidBody *body = 0; + RigidBody *body = 0; - sq_getinstanceup (v, 1, ( void ** ) &body, 0); + sq_getinstanceup(v, 1, ( void ** ) &body, 0); - if (body ) - { - SQInteger geom = 0; - sq_getinteger (v, 2, &geom ); + if (body) + { + SQInteger geom = 0; + sq_getinteger(v, 2, &geom); - body->enableCollisionMesh(geom ); - } + body->enableCollisionMesh(geom); + } - return 0; + return 0; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_setcollisionmeshrotation(HSQUIRRELVM v) { - int argc = sq_gettop (v ); + int argc = sq_gettop(v); - // need at least geom + quaternion + // need at least geom + quaternion - if (argc < 3) - return 0; + if (argc < 3) + return 0; - RigidBody *body = 0; + RigidBody *body = 0; - sq_getinstanceup (v, 1, ( void ** ) &body, 0); + sq_getinstanceup(v, 1, ( void ** ) &body, 0); - if (body ) - { - SQInteger geom = 0; - sq_getinteger (v, 2, &geom ); + if (body) + { + SQInteger geom = 0; + sq_getinteger(v, 2, &geom); - Quaternion rotation; + Quaternion rotation; - if (argc == 3) - { - _getquaternionvalues (v, 3, rotation.w, rotation.x, rotation.y, - rotation.z); - } - else if (argc == 4) - { - Vector3 axis; - SQFloat angle; - _getvectorvalues (v, 3, axis.x, axis.y, axis.z); - sq_getfloat (v, 5, &angle ); - rotation = Quaternion (axis, angle ); - } - else if (argc == 5) - { - SQFloat h, a, b; - sq_getfloat (v, 3, &h ); - sq_getfloat (v, 4, &a ); - sq_getfloat (v, 5, &b ); - rotation = Quaternion (h, a, b ); - } + if (argc == 3) + { + _getquaternionvalues(v, 3, rotation.w, rotation.x, rotation.y, + rotation.z); + } + else if (argc == 4) + { + Vector3 axis; + SQFloat angle; + _getvectorvalues(v, 3, axis.x, axis.y, axis.z); + sq_getfloat(v, 5, &angle); + rotation = Quaternion(axis, angle); + } + else if (argc == 5) + { + SQFloat h, a, b; + sq_getfloat(v, 3, &h); + sq_getfloat(v, 4, &a); + sq_getfloat(v, 5, &b); + rotation = Quaternion(h, a, b); + } - body->setCollisionMeshRotation(geom, rotation ); - } + body->setCollisionMeshRotation(geom, rotation); + } - return 0; + return 0; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_releasehook(SQUserPointer p, SQInteger size) { - if (gRigidBodySimulation.valid()) - { - RigidBody *self = ( RigidBody* ) p; - gRigidBodySimulation->deleteRigidBody(self ); - } - - return 1; + if (gRigidBodySimulation.valid()) + { + RigidBody *self = ( RigidBody* ) p; + gRigidBodySimulation->deleteRigidBody(self); + } + + return 1; } //------------------------------------------------------------------------------ static SQInteger _rigidbody_constructor(HSQUIRRELVM v) { - RigidBody *body = gRigidBodySimulation->createRigidBody(); + RigidBody *body = gRigidBodySimulation->createRigidBody(); - if (SQ_FAILED (sq_setinstanceup (v, 1, body ) ) ) - { - gRigidBodySimulation->deleteRigidBody(body ); - return 0; - } + if (SQ_FAILED(sq_setinstanceup(v, 1, body) ) ) + { + gRigidBodySimulation->deleteRigidBody(body); + return 0; + } - HSQOBJECT obj; + HSQOBJECT obj; - sq_getstackobj (v, 2, &obj ); - body->setCollisionHandler(obj ); - sq_setreleasehook (v, 1, _rigidbody_releasehook ); - return 0; + sq_getstackobj(v, 2, &obj); + body->setCollisionHandler(obj); + sq_setreleasehook(v, 1, _rigidbody_releasehook); + return 0; } //------------------------------------------------------------------------------ void setupScriptSystem_RigidBody(ScriptSystem *scriptsystem, - RigidBodySimulation *simulation) + RigidBodySimulation *simulation) { - gRigidBodySimulation = simulation; + gRigidBodySimulation = simulation; - HSQUIRRELVM vm = scriptsystem->getVM(); - sq_pushroottable(vm ); + HSQUIRRELVM vm = scriptsystem->getVM(); + sq_pushroottable(vm); - sq_pushstring (vm, "RigidBody", -1); - if (SQ_SUCCEEDED (sq_newclass (vm, SQFalse ) ) ) - { - // register rigidbody functions - sq_pushstring (vm, "constructor", -1); - sq_newclosure (vm, _rigidbody_constructor, 0); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "RigidBody", -1); + if (SQ_SUCCEEDED(sq_newclass(vm, SQFalse) ) ) + { + // register rigidbody functions + sq_pushstring(vm, "constructor", -1); + sq_newclosure(vm, _rigidbody_constructor, 0); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "setPosition", -1); - sq_newclosure (vm, _rigidbody_setposition, 0); - //sq_setparamscheck( vm, 2, "xx" ); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "setPosition", -1); + sq_newclosure(vm, _rigidbody_setposition, 0); + //sq_setparamscheck( vm, 2, "xx" ); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "getPosition", -1); - sq_newclosure (vm, _rigidbody_getposition, 0); - sq_setparamscheck (vm, 1, "x"); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "getPosition", -1); + sq_newclosure(vm, _rigidbody_getposition, 0); + sq_setparamscheck(vm, 1, "x"); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "setRotation", -1); - sq_newclosure (vm, _rigidbody_setrotation, 0); - //sq_setparamscheck( vm, 2, "xx" ); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "setRotation", -1); + sq_newclosure(vm, _rigidbody_setrotation, 0); + //sq_setparamscheck( vm, 2, "xx" ); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "getRotation", -1); - sq_newclosure (vm, _rigidbody_getrotation, 0); - sq_setparamscheck (vm, 1, "x"); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "getRotation", -1); + sq_newclosure(vm, _rigidbody_getrotation, 0); + sq_setparamscheck(vm, 1, "x"); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "getLinearVelocity", -1); - sq_newclosure (vm, _rigidbody_getlinearvelocity, 0); - sq_setparamscheck (vm, 1, "x"); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "getLinearVelocity", -1); + sq_newclosure(vm, _rigidbody_getlinearvelocity, 0); + sq_setparamscheck(vm, 1, "x"); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "setLinearVelocity", -1); - sq_newclosure (vm, _rigidbody_setlinearvelocity, 0); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "setLinearVelocity", -1); + sq_newclosure(vm, _rigidbody_setlinearvelocity, 0); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "getAngularVelocity", -1); - sq_newclosure (vm, _rigidbody_getangularvelocity, 0); - sq_setparamscheck (vm, 1, "x"); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "getAngularVelocity", -1); + sq_newclosure(vm, _rigidbody_getangularvelocity, 0); + sq_setparamscheck(vm, 1, "x"); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "applyLocalForce", -1); - sq_newclosure (vm, _rigidbody_applylocalforce, 0); - //sq_setparamscheck (vm, 3, "xxx"); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "applyLocalForce", -1); + sq_newclosure(vm, _rigidbody_applylocalforce, 0); + //sq_setparamscheck (vm, 3, "xxx"); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "applyGlobalForce", -1); - sq_newclosure (vm, _rigidbody_applyglobalforce, 0); - sq_setparamscheck (vm, 3, "xxx"); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "applyGlobalForce", -1); + sq_newclosure(vm, _rigidbody_applyglobalforce, 0); + sq_setparamscheck(vm, 3, "xxx"); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "addCollisionMesh", -1); - sq_newclosure (vm, _rigidbody_addcollisionmesh, 0); - sq_setparamscheck (vm, 3, "xsn"); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "addCollisionMesh", -1); + sq_newclosure(vm, _rigidbody_addcollisionmesh, 0); + sq_setparamscheck(vm, 3, "xsn"); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "setCollisionMeshPosition", -1); - sq_newclosure (vm, _rigidbody_setcollisionmeshposition, 0); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "setCollisionMeshPosition", -1); + sq_newclosure(vm, _rigidbody_setcollisionmeshposition, 0); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "setCollisionMeshRotation", -1); - sq_newclosure (vm, _rigidbody_setcollisionmeshrotation, 0); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "setCollisionMeshRotation", -1); + sq_newclosure(vm, _rigidbody_setcollisionmeshrotation, 0); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "getCollisionMeshPosition", -1); - sq_newclosure (vm, _rigidbody_setcollisionmeshposition, 0); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "getCollisionMeshPosition", -1); + sq_newclosure(vm, _rigidbody_setcollisionmeshposition, 0); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "getCollisionMeshRotation", -1); - sq_newclosure (vm, _rigidbody_setcollisionmeshrotation, 0); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "getCollisionMeshRotation", -1); + sq_newclosure(vm, _rigidbody_setcollisionmeshrotation, 0); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "disableCollisionMesh", -1); - sq_newclosure (vm, _rigidbody_disablecollisionmesh, 0); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "disableCollisionMesh", -1); + sq_newclosure(vm, _rigidbody_disablecollisionmesh, 0); + sq_newslot(vm, -3, false); - sq_pushstring (vm, "enableCollisionMesh", -1); - sq_newclosure (vm, _rigidbody_enablecollisionmesh, 0); - sq_newslot (vm, -3, false); + sq_pushstring(vm, "enableCollisionMesh", -1); + sq_newclosure(vm, _rigidbody_enablecollisionmesh, 0); + sq_newslot(vm, -3, false); - sq_newslot (vm, -3, false); - } + sq_newslot(vm, -3, false); + } - sq_poptop (vm ); + sq_poptop(vm); } } // namespace BlueCore diff --git a/engine/ScriptSystem_SceneGraph.cpp b/engine/ScriptSystem_SceneGraph.cpp new file mode 100644 index 0000000..29c9e81 --- /dev/null +++ b/engine/ScriptSystem_SceneGraph.cpp @@ -0,0 +1,81 @@ +#include "ScriptSystem_Camera.h" +#include "ScriptSystem_Math.h" + +#include "SceneGraph.h" + +namespace BlueCore +{ + +static SQInteger _scenegraph_releasehook(SQUserPointer p, SQInteger size) +{ + SceneGraph *scenegraph = (SceneGraph *)p; + + if (scenegraph) + scenegraph->removeReference(); + + return 1; +} + +static SQInteger _scenegraph_constructor(HSQUIRRELVM vm) +{ + SQInteger argc = sq_gettop(vm); + + if (argc < 1) + return 0; + + SceneGraph *scenegraph = new SceneGraph(); + scenegraph->addReference(); + + sq_setinstanceup(vm, 1, (void *)scenegraph ); + sq_setreleasehook(vm, 1, _scenegraph_releasehook); + + return 0; +} + +static SQInteger _set_camera(HSQUIRRELVM v) +{ + SceneGraph *scenegraph = 0; + + sq_getinstanceup(v, 1, ( void ** ) &scenegraph, 0); + + if (scenegraph) + { + Camera *camera; + sq_getinstanceup (v, 2, ( void ** ) &camera, 0); + scenegraph->setCamera(camera); + } + + return 0; +} + +void setupScriptSystem_SceneGraph(ScriptSystem* scriptsystem) +{ + if (scriptsystem == 0) + return; + + HSQUIRRELVM vm = scriptsystem->getVM(); + + sq_pushroottable(vm); + + // push class + sq_pushstring(vm, "SceneGraph", -1); + + if (SQ_SUCCEEDED(sq_newclass(vm, SQFalse) ) ) + { + // register constructor + sq_pushstring(vm, "constructor", -1); + sq_newclosure(vm, _scenegraph_constructor, 0); + sq_newslot(vm, -3, false); + + sq_pushstring(vm, "setCamera", -1); + sq_newclosure(vm, _set_camera, 0); + sq_newslot(vm, -3, false); + + // create class + sq_newslot(vm, -3, false); + } + + sq_poptop(vm); +} + +} diff --git a/engine/ScriptSystem_SceneGraph.h b/engine/ScriptSystem_SceneGraph.h new file mode 100644 index 0000000..59c0c93 --- /dev/null +++ b/engine/ScriptSystem_SceneGraph.h @@ -0,0 +1,11 @@ +#ifndef BLUECORE_SCRIPTING_CAMERA_H +#define BLUECORE_SCRIPTING_CAMERA_H + +#include "ScriptSystem.h" + +namespace BlueCore +{ + void setupScriptSystem_SceneGraph(ScriptSystem* scriptsystem); +} + +#endif diff --git a/engine/Utilities/CfgParser.cpp b/engine/Utilities/CfgParser.cpp index f9757aa..86b245a 100644 --- a/engine/Utilities/CfgParser.cpp +++ b/engine/Utilities/CfgParser.cpp @@ -5,194 +5,191 @@ using namespace std; -namespace BlueCore { - -void CfgParser::parseFile (const std::string& filename) +namespace BlueCore { - ifstream file (filename.c_str(), ios::in); - std::string line; - while (file.good()) - { - getline (file, line); - parseLine (line); - } +void CfgParser::parseFile(const std::string& filename) +{ + ifstream file(filename.c_str(), ios::in); + std::string line; + + while (file.good()) + { + getline(file, line); + parseLine(line); + } } - - -void CfgParser::parseLine (const std::string& line) +void CfgParser::parseLine(const std::string& line) { - std::string::size_type i (line.find_first_of ('=')); + std::string::size_type i(line.find_first_of('=')); if (i == std::string::npos) return; - std::string key = trim (string (line, 0, i-1)); - std::string value = trim (string (line, i+1)); + std::string key = trim(string(line, 0, i-1)); + std::string value = trim(string(line, i+1)); _Pairs[key] = value; } - - -void CfgParser::parse (const char* buffer, unsigned int length) +void CfgParser::parse(const char* buffer, unsigned int length) { - const char* ptr = buffer; - const char* end = buffer + length; - unsigned int l = 0; - - while (ptr < end) - { - // find end of line - while (ptr[l] != '\n') - { - l++; - if (ptr + l >= end) - break; - } - - parseLine (string(ptr, l)); - ptr += l+1; - l = 0; - } -} + const char* ptr = buffer; + const char* end = buffer + length; + unsigned int l = 0; - - -double CfgParser::get (const std::string& key, double defaultValue) -{ - double value; - - if (getDouble (key, value) == false) - return defaultValue; - else - return value; -} - - - -bool CfgParser::getDouble (const std::string& key, double& value) -{ - std::map::const_iterator result; - result = _Pairs.find (key); - - if (result != _Pairs.end()) - { - value = atof (result->second.c_str()); - return true; - } - else - return false; -} - - - -int CfgParser::get (const std::string& key, int defaultValue) -{ - int value; - - if (getInteger (key, value) == false) - return defaultValue; - else - return value; -} - - - -bool CfgParser::getInteger (const std::string& key, int& value) -{ - std::map::const_iterator result; - result = _Pairs.find (key); - - if (result != _Pairs.end()) - { - value = atoi (result->second.c_str()); - return true; - } - else - return false; -} - - -bool CfgParser::get (const std::string& key, bool defaultValue) -{ - bool value; - - if (getBoolean (key, value) == false) - return defaultValue; - else - return value; -} - - - -bool CfgParser::getBoolean (const std::string& key, bool& value) -{ - std::map::const_iterator result; - result = _Pairs.find (key); - - if (result != _Pairs.end()) + while (ptr < end) { - value = false; + // find end of line + while (ptr[l] != '\n') + { + l++; + if (ptr + l >= end) + break; + } - if (result->second == "true") - value = true; - else if (result->second == "1") - value = true; - else if (result->second == "yes") - value = true; - - return true; + parseLine(string(ptr, l)); + ptr += l+1; + l = 0; } - else - return false; } - - -std::string CfgParser::get (const std::string& key, std::string defaultValue) +double CfgParser::get(const std::string& key, double defaultValue) { - std::string value; + double value; - if (getString (key, value) == false) - return defaultValue; - else - return value; + if (getDouble(key, value) == false) + return defaultValue; + else + return value; } - - -bool CfgParser::getString (const std::string& key, std::string& value) +bool CfgParser::getDouble(const std::string& key, double& value) { - std::map::const_iterator result; - result = _Pairs.find (key); - - if (result != _Pairs.end()) - { + std::map::const_iterator result; + result = _Pairs.find(key); + + if (result != _Pairs.end()) + { + value = atof(result->second.c_str()); + return true; + } + else + return false; +} + +int CfgParser::get(const std::string& key, int defaultValue) +{ + int value; + + if (getInteger(key, value) == false) + return defaultValue; + else + return value; +} + +bool CfgParser::getInteger(const std::string& key, int& value) +{ + std::map::const_iterator result; + result = _Pairs.find(key); + + if (result != _Pairs.end()) + { + value = atoi(result->second.c_str()); + return true; + } + else + return false; +} + +bool CfgParser::get(const std::string& key, bool defaultValue) +{ + bool value; + + if (getBoolean(key, value) == false) + return defaultValue; + else + return value; +} + +bool CfgParser::getBoolean(const std::string& key, bool& value) +{ + std::map::const_iterator result; + result = _Pairs.find(key); + + if (result != _Pairs.end()) + { + value = false; + + if (result->second == "true") + value = true; + else if (result->second == "1") + value = true; + else if (result->second == "yes") + value = true; + + return true; + } + else + return false; +} + +std::string CfgParser::get(const std::string& key, std::string defaultValue) +{ + std::string value; + + if (getString(key, value) == false) + return defaultValue; + else + return value; +} + +bool CfgParser::getString(const std::string& key, std::string& value) +{ + std::map::const_iterator result; + result = _Pairs.find(key); + + if (result != _Pairs.end()) + { value = result->second; - return true; + return true; } - else - return false; + else + return false; } - - -bool CfgParser::getStrings (const std::string& key, std::vector& strings) +bool CfgParser::getStrings(const std::string& key, std::vector& strings) { - std::map::const_iterator result; - result = _Pairs.find (key); + std::map::const_iterator result; + result = _Pairs.find(key); - if (result != _Pairs.end()) - { - explode (result->second, strings, true, ","); - return true; - } - else - return false; + if (result != _Pairs.end()) + { + explode(result->second, strings, true, ","); + return true; + } + else + return false; } +bool CfgParser::getDoubles(const std::string& key, std::vector& doubles) +{ + std::map::const_iterator result; + result = _Pairs.find(key); + + if (result != _Pairs.end()) + { + std::vector strings; + explode(result->second, strings, true, ","); + std::vector::iterator i; + for (i = strings.begin(); i != strings.end(); i++) + doubles.push_back(atof((*i).c_str())); + return true; + } + else + return false; +} } // namespace BlueCore diff --git a/engine/Utilities/CfgParser.h b/engine/Utilities/CfgParser.h index dcf35df..0bd2ab0 100644 --- a/engine/Utilities/CfgParser.h +++ b/engine/Utilities/CfgParser.h @@ -27,7 +27,9 @@ public: bool getString (const std::string& key, std::string& defaultValue); bool getStrings (const std::string& key, std::vector& strings); - + + bool getDoubles (const std::string& key, std::vector& doubles); + private: std::map _Pairs; diff --git a/engine/data/main.nut b/engine/data/main.nut index 0b066fd..efebbec 100644 --- a/engine/data/main.nut +++ b/engine/data/main.nut @@ -71,16 +71,26 @@ lastFPS <- 0; frameCount <- 0; FPS <- 1; body <- null; +camera <- null; +mainSceneGraph <- null; function Initialize() { ::font = Font();//Font ("DejaVuSans.ttf", 24, 1 ); ::logo = Image ("image.png", 0.0, 0.0, 1.0, 1.0); ::body = RigidBody(); - + ::camera = Camera(); + ::mainSceneGraph = SceneGraph(); + ::camera.lookAt (0.0, 0.0, 0.0); + ::mainSceneGraph.setCamera (::camera); } -function OnFrame( delta ) +function OnFrame (delta, total) +{ + ::camera.setPosition (::body.getPosition()); +} + +function OnOverlay (delta, total) { ::frameCount += 1 ::lastFPS += delta; @@ -91,22 +101,32 @@ function OnFrame( delta ) ::frameCount = 0; ::lastFPS -= 0.1; } - - if (::FPS > 0) + + if (total < 2.0) + ::logo.draw (0.5, 0.5); + else { - local fps = "FPS: " + FPS + " / " + (1.0/::FPS)*1000.0 + " ms"; - ::font.print( 10, 10, fps, 1, 1 ); + v <- ::body.getPosition(); + ::font.print(10, 40, "Position: " + v.x + ", " + v.y + ", " + v.z, 1, 1 ); + + local seconds = floor(total % 60); + if (seconds < 10) + ::font.print(10, 70, "Time: " + floor(total / 60.0) + ":0" + seconds, 1, 1 ); + else + ::font.print(10, 70, "Time: " + floor(total / 60.0) + ":" + seconds, 1, 1 ); + + if (::FPS > 0) + { + local fps = "FPS: " + FPS + " / " + (1.0/::FPS)*1000.0 + " ms"; + ::font.print( 10, 10, fps, 1, 1 ); + } } - // ::logo.draw (0.5, 0.5); - - v <- ::body.getPosition(); - ::font.print(10, 40, "Position: " + v.x + ", " + v.y + ", " + v.z, 1, 1 ); } -function OnStep( delte ) +function OnStep (delta, total) { - ::body.applyLocalForce( Vector(0.0, 0.0, 1.0) ); - } + ::body.applyLocalForce( Vector(1.0, 0.0, 1.0) ); +} function Shutdown() { diff --git a/engine/main.cpp b/engine/main.cpp index e966a57..2021ae0 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -1,26 +1,5 @@ -#include "GL/glew.h" - -#include "RenderWindow.h" -#include "RenderDevice.h" -#include "FontManager.h" -#include "MeshManager.h" -#include "TextureManager.h" -#include "ShaderManager.h" -#include "ModelManager.h" -#include "TextureImage.h" -#include "ScriptSystem.h" -#include "ScriptSystem_Font.h" -#include "ScriptSystem_Image.h" -#include "ScriptSystem_Math.h" -#include "ScriptSystem_RigidBody.h" -#include "RigidBodySimulation.h" - -#include "Camera.h" -#include "SceneNode.h" - -#include "Utilities/CfgParser.h" +#include "Application.h" #include "Utilities/Log.h" -#include "Utilities/Kernel.h" #include "physfs.h" @@ -65,193 +44,17 @@ void shutdownPhysfs() PHYSFS_deinit(); } -class Application : public sigslot::has_slots<> -{ - bool _Running; - double _DeltaTime; - double _LastTime; - - void KeySlot(int key, int action) - { - if (key == GLFW_KEY_ESC && action == GLFW_RELEASE) - quit(); - } - - ref_ptr _Window; - ref_ptr _Device; - ref_ptr _FontManager; - ref_ptr _MeshManager; - ref_ptr _TextureManager; - ref_ptr _ScriptSystem; - ref_ptr _ShaderManager; - ref_ptr _Simulation; - ref_ptr _ModelManager; - ref_ptr _Camera; - ref_ptr _RenderQueue; - -public: - - bool initialize() - { - CfgParser cfg; - cfg.parseFile("options.cfg"); - - int width = cfg.get("width", 640); - int height = cfg.get("height", 480); - bool fullscreen = cfg.get("fullscreen", false); - - _Window = new RenderWindow(); - if (_Window->create(width, height, 0, 0, 0, fullscreen) == false) - return false; - - _Device = new RenderDevice(_Window); - _FontManager = new FontManager(_Device); - _MeshManager = new MeshManager(_Device); - _TextureManager = new TextureManager(); - _ScriptSystem = new ScriptSystem(); - _ShaderManager = new ShaderManager(_Window); - _Simulation = new RigidBodySimulation(_ScriptSystem); - _ModelManager = new ModelManager (_TextureManager, _ShaderManager, _MeshManager); - _Camera = new Camera(); - _RenderQueue = new RenderQueue(); - - setupScriptSystem_Font(_ScriptSystem, _FontManager); - setupScriptSystem_Image(_ScriptSystem, _TextureManager, _Device); - setupScriptSystem_Math(_ScriptSystem); - setupScriptSystem_RigidBody(_ScriptSystem, _Simulation); - - _ScriptSystem->loadScript("main"); - _ScriptSystem->callFunction("Initialize"); - - _Window->KeySignal.connect(this, &Application::KeySlot); - - _Running = true; - - return true; - } - /* - if (window.valid() && device.valid()) - { - - camera->setFoV(45.0); - camera->setAspectRatio((double)width/(double)height); - camera->setNearPlane(1.0); - camera->setFarPlane(15000.0); - camera->setPosition(Vector3(0.0, 0.0, 20.0)); - - device->setAmbientLight(1.0, 1.0, 1.0); - - //ref_ptr rootnode(new SceneNode("root node")); - - - ref_ptr model = modelmanager->loadModel("combat"); - - } - */ - - void shutdown() - { - _ScriptSystem->callFunction("Shutdown"); - } - - void quit() - { - _Running = false; - } - - void run() - { - clog << "--- starting main loop..."<< endlog; - - _DeltaTime = 0; - _LastTime = glfwGetTime(); - - while (_Window->isOpen() && _Running) - { - double time = glfwGetTime(); - _DeltaTime = time - _LastTime; - _LastTime = time; - - _Device->clear(); - /* - camera->setRotation(Quaternion(Vector3(0.0, 1.0, 0.0), fmod(time - /5.0, 6.2))); - device->begin3D(camera); - queue->clear(); - queue->addOpaqueItem(model, Vector3(10.0, 0.0, 0.0), Quaternion()); - queue->render(device); - - device->pushAbsoluteTransformation(Vector3(), camera->getRotation()); - - class RenderState - { - bool _Blending; - GLint _BlendFuncSrc; - GLint _BlendFuncDest; - - bool _DepthTest; - bool _DepthMask; - - bool _Lighting; - }; - GLfloat mat_specular[] = - { 1.0, 1.0, 1.0, 1.0 }; - GLfloat mat_shininess[] = - { 2.0 }; - glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_specular); - glMaterialfv(GL_FRONT, GL_AMBIENT, mat_specular); - glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); - glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); - - glEnable(GL_BLEND); - glBlendFunc(GL_ONE, GL_ONE); - glEnable(GL_DEPTH_TEST); - glDepthMask(GL_FALSE); - glDisable(GL_LIGHTING); - glColor4d( 1.0f, 1.0f, 1.0f, 1.0f); - - device->setTexture(0, starTexture, true); - starMesh->render(); - - glDisable(GL_BLEND); - device->popTransformation(); - */ - // device->useShader (program); - // device->setTexture (stage, name, texture) - // device->clearTextures (stage+1); - //glEnable (GL_TEXTURE_2D); - //glEnable (GL_LIGHTING); - //glBindTexture (GL_TEXTURE_2D, texture->getId() ); - - // device-> - //device-> - //model->render(); - - _Simulation->saveStates(); - _Simulation->updateSteps(_DeltaTime); - while (_Simulation->step()) - ; - - _ScriptSystem->callFunction("OnFrame", _DeltaTime); - - _Device->swap(); - } - - clog << "--- main loop finished..."<< endlog; - } -}; - int main(int argc, char **argv) { initializePhysfs(argv[0]); - Application app; + ref_ptr app = new Application(); - if (app.initialize()) + if (app->initialize()) { - app.run(); + app->run(); } - app.shutdown(); - + app->shutdown(); + shutdownPhysfs(); } diff --git a/engine/todo.txt b/engine/todo.txt index 8c8550e..1a4c523 100644 --- a/engine/todo.txt +++ b/engine/todo.txt @@ -7,10 +7,16 @@ Scripting --------- * make event based input available + * make font height available + +SceneGraph +---------- + + * create scene graph * make ShaderProgram a class * create RenderSet AND/OR RenderQueue - +* RenderQueueVisitor #if 0 /*