mirror of
https://github.com/baldurk/renderdoc.git
synced 2026-08-21 14:06:45 +00:00
Initial commit of existing code.
* All renderdoc code up to this point was written by me, history is available by request
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
/******************************************************************************
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014 Crytek
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "camera.h"
|
||||
#include "matrix.h"
|
||||
|
||||
void Camera::Arcball(float dist, Vec3f rot)
|
||||
{
|
||||
pos = Vec3f(0.0f, 0.0f, dist);
|
||||
|
||||
order = ORDER_ROT_TRANS;
|
||||
|
||||
angles.x = rot.x;
|
||||
angles.y = rot.y;
|
||||
}
|
||||
|
||||
void Camera::fpsLook(Vec3f p, Vec3f rot)
|
||||
{
|
||||
pos = -p;
|
||||
|
||||
angles.x = -rot.x;
|
||||
angles.y = -rot.y;
|
||||
|
||||
order = ORDER_TRANS_ROT;
|
||||
}
|
||||
|
||||
const Matrix4f Camera::GetMatrix()
|
||||
{
|
||||
Matrix4f p = Matrix4f::Translation(pos);
|
||||
Matrix4f r = Matrix4f::RotationXYZ(angles);
|
||||
|
||||
if(order == ORDER_TRANS_ROT)
|
||||
return r.Mul(p);
|
||||
|
||||
return p.Mul(r);
|
||||
}
|
||||
|
||||
const Vec3f Camera::GetPosition()
|
||||
{
|
||||
return GetMatrix().GetPosition();
|
||||
}
|
||||
|
||||
const Vec3f Camera::GetForward()
|
||||
{
|
||||
return Matrix4f::RotationZYX(-angles).GetForward();
|
||||
}
|
||||
|
||||
const Vec3f Camera::GetRight()
|
||||
{
|
||||
return Matrix4f::RotationZYX(-angles).GetRight();
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/******************************************************************************
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014 Crytek
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "vec.h"
|
||||
|
||||
class Matrix4f;
|
||||
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
Camera()
|
||||
: order(ORDER_TRANS_ROT), pos(), angles()
|
||||
{ }
|
||||
|
||||
void Arcball(float dist, Vec3f rot);
|
||||
void fpsLook(Vec3f pos, Vec3f rot);
|
||||
|
||||
void SetPosition(const Vec3f &p) { pos = p; }
|
||||
void SetAngles(const Vec3f &r) { angles = r; }
|
||||
|
||||
const Vec3f GetPosition();
|
||||
const Vec3f GetForward();
|
||||
const Vec3f GetRight();
|
||||
const Vec3f GetUp();
|
||||
const Matrix4f GetMatrix();
|
||||
|
||||
private:
|
||||
enum OperationOrder
|
||||
{
|
||||
ORDER_ROT_TRANS = 0,
|
||||
ORDER_TRANS_ROT,
|
||||
} order;
|
||||
|
||||
Vec3f pos;
|
||||
Vec3f angles;
|
||||
};
|
||||
@@ -0,0 +1,99 @@
|
||||
/******************************************************************************
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014 Crytek
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "vec.h"
|
||||
|
||||
inline Vec4f ConvertFromR10G10B10A2(uint32_t data)
|
||||
{
|
||||
return Vec4f( float((data>> 0) & 0x3ff) / 1023.0f,
|
||||
float((data>>10) & 0x3ff) / 1023.0f,
|
||||
float((data>>20) & 0x3ff) / 1023.0f,
|
||||
float((data>>30) & 0x003) / 3.0f
|
||||
);
|
||||
}
|
||||
|
||||
inline uint32_t ConvertToR10G10B10A2(Vec4f data)
|
||||
{
|
||||
float x = data.x < 1.0f ? (data.x > 0.0f ? data.x : 0.0f) : 1.0f;
|
||||
float y = data.y < 1.0f ? (data.y > 0.0f ? data.y : 0.0f) : 1.0f;
|
||||
float z = data.z < 1.0f ? (data.z > 0.0f ? data.z : 0.0f) : 1.0f;
|
||||
float w = data.w < 1.0f ? (data.w > 0.0f ? data.w : 0.0f) : 1.0f;
|
||||
|
||||
return (uint32_t(x*1023)<< 0) |
|
||||
(uint32_t(y*1023)<<10) |
|
||||
(uint32_t(z*1023)<<20) |
|
||||
(uint32_t(w*3) <<30) ;
|
||||
}
|
||||
|
||||
inline Vec3f ConvertFromR11G11B10(uint32_t data)
|
||||
{
|
||||
uint32_t xMantissa = ((data>> 0) & 0x3f);
|
||||
uint32_t xExponent = ((data>> 6) & 0x1f);
|
||||
uint32_t yMantissa = ((data>>11) & 0x3f);
|
||||
uint32_t yExponent = ((data>>17) & 0x1f);
|
||||
uint32_t zMantissa = ((data>>22) & 0x1f);
|
||||
uint32_t zExponent = ((data>>27) & 0x1f);
|
||||
|
||||
return Vec3f((float(xMantissa)/64.0f)*powf(2.0f, (float)xExponent - 15.0f),
|
||||
(float(yMantissa)/32.0f)*powf(2.0f, (float)yExponent - 15.0f),
|
||||
(float(zMantissa)/32.0f)*powf(2.0f, (float)zExponent - 15.0f));
|
||||
}
|
||||
|
||||
/*
|
||||
inline uint32_t ConvertToR11G11B10(Vec3f data)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
inline Vec4f ConvertFromB5G5R5A1(uint16_t data)
|
||||
{
|
||||
return Vec4f( (float)((data >> 0) & 0x1f) / 31.0f,
|
||||
(float)((data >> 5) & 0x1f) / 31.0f,
|
||||
(float)((data >> 10) & 0x1f) / 31.0f,
|
||||
((data & 0x8000) > 0) ? 1.0f : 0.0f );
|
||||
}
|
||||
|
||||
inline Vec3f ConvertFromB5G6R5(uint16_t data)
|
||||
{
|
||||
return Vec3f( (float)((data >> 0) & 0x1f) / 31.0f,
|
||||
(float)((data >> 5) & 0x3f) / 63.0f,
|
||||
(float)((data >> 11) & 0x1f) / 31.0f );
|
||||
}
|
||||
|
||||
inline Vec4f ConvertFromB4G4R4A4(uint16_t data)
|
||||
{
|
||||
return Vec4f( (float)((data >> 0) & 0xf) / 15.0f,
|
||||
(float)((data >> 4) & 0xf) / 15.0f,
|
||||
(float)((data >> 8) & 0xf) / 15.0f,
|
||||
(float)((data >> 12) & 0xf) / 15.0f );
|
||||
}
|
||||
|
||||
#include "half_convert.h"
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
|
||||
// Digital Ltd. LLC
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Industrial Light & Magic nor the names of
|
||||
// its contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma once
|
||||
|
||||
inline uint16_t ConvertToHalf(float comp)
|
||||
{
|
||||
int *alias = (int *)∁
|
||||
int i = *alias;
|
||||
|
||||
int sign = (i >> 16) & 0x00008000;
|
||||
int exponent = ((i >> 23) & 0x000000ff) - (127 - 15);
|
||||
int mantissa = i & 0x007fffff;
|
||||
|
||||
if(exponent <= 0)
|
||||
{
|
||||
if(exponent < -10)
|
||||
return sign&0xffff;
|
||||
|
||||
mantissa |= 0x00800000;
|
||||
|
||||
int t = 14 - exponent;
|
||||
int a = (1 << (t - 1)) - 1;
|
||||
int b = (mantissa >> t) & 1;
|
||||
|
||||
mantissa = (mantissa + a + b) >> t;
|
||||
|
||||
return (sign | mantissa)&0xffff;
|
||||
}
|
||||
else if (exponent == 0xff - (127 - 15))
|
||||
{
|
||||
if(mantissa == 0)
|
||||
return (sign | 0x7c00)&0xffff;
|
||||
|
||||
mantissa >>= 13;
|
||||
return (sign | 0x7c00 | mantissa | (mantissa == 0))&0xffff;
|
||||
}
|
||||
else
|
||||
{
|
||||
mantissa = mantissa + 0x00000fff + ((mantissa >> 13) & 1);
|
||||
|
||||
if(mantissa & 0x00800000)
|
||||
{
|
||||
mantissa = 0;
|
||||
exponent += 1;
|
||||
}
|
||||
|
||||
if(exponent > 30)
|
||||
{
|
||||
return (sign | 0x7c00)&0xffff;
|
||||
}
|
||||
|
||||
return (sign | (exponent << 10) | (mantissa >> 13))&0xffff;
|
||||
}
|
||||
}
|
||||
|
||||
inline float ConvertFromHalf(uint16_t comp)
|
||||
{
|
||||
bool sign = (comp & 0x8000) != 0;
|
||||
int exponent = (comp & 0x7C00) >> 10;
|
||||
int mantissa = comp & 0x03FF;
|
||||
|
||||
if(exponent == 0x00)
|
||||
{
|
||||
if(mantissa == 0)
|
||||
return 0.0f;
|
||||
|
||||
// subnormal
|
||||
float ret = (float)mantissa;
|
||||
int *alias = (int *)&ret;
|
||||
|
||||
// set sign bit and set exponent to 2^-24
|
||||
// (2^-14 from spec for subnormals * 2^-10 to convert (float)mantissa to 0.mantissa)
|
||||
*alias = (sign ? 0x80000000 : 0) | (*alias - (24 << 23));
|
||||
|
||||
return ret;
|
||||
}
|
||||
else if(exponent < 0x1f)
|
||||
{
|
||||
exponent -= 15;
|
||||
|
||||
float ret = 0.0f;
|
||||
int *alias = (int *)&ret;
|
||||
|
||||
// convert to float. Put sign bit in the right place, convert exponent to be
|
||||
// [-128,127] and put in the right place, then shift mantissa up.
|
||||
*alias = (sign ? 0x80000000 : 0) | (exponent+127) << 23 | (mantissa << 13);
|
||||
|
||||
return ret;
|
||||
}
|
||||
else //if(exponent = 0x1f)
|
||||
{
|
||||
int nan = 0x7F800001;
|
||||
return *(float*)&nan;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
/******************************************************************************
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014 Crytek
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
|
||||
#include "vec.h"
|
||||
#include "matrix.h"
|
||||
#include "quat.h"
|
||||
|
||||
static inline size_t matIdx(const size_t x, const size_t y) { return x+y*4; }
|
||||
|
||||
Matrix4f Matrix4f::Mul(const Matrix4f &o) const
|
||||
{
|
||||
Matrix4f m;
|
||||
for(size_t x=0; x < 4; x++)
|
||||
{
|
||||
for(size_t y=0; y < 4; y++)
|
||||
{
|
||||
m[matIdx(x,y)] = (*this)[matIdx(x,0)] * o[matIdx(0,y)] +
|
||||
(*this)[matIdx(x,1)] * o[matIdx(1,y)] +
|
||||
(*this)[matIdx(x,2)] * o[matIdx(2,y)] +
|
||||
(*this)[matIdx(x,3)] * o[matIdx(3,y)];
|
||||
}
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
Matrix4f Matrix4f::Inverse() const
|
||||
{
|
||||
float a0 = (*this)[ 0]*(*this)[ 5] - (*this)[ 1]*(*this)[ 4];
|
||||
float a1 = (*this)[ 0]*(*this)[ 6] - (*this)[ 2]*(*this)[ 4];
|
||||
float a2 = (*this)[ 0]*(*this)[ 7] - (*this)[ 3]*(*this)[ 4];
|
||||
float a3 = (*this)[ 1]*(*this)[ 6] - (*this)[ 2]*(*this)[ 5];
|
||||
float a4 = (*this)[ 1]*(*this)[ 7] - (*this)[ 3]*(*this)[ 5];
|
||||
float a5 = (*this)[ 2]*(*this)[ 7] - (*this)[ 3]*(*this)[ 6];
|
||||
float b0 = (*this)[ 8]*(*this)[13] - (*this)[ 9]*(*this)[12];
|
||||
float b1 = (*this)[ 8]*(*this)[14] - (*this)[10]*(*this)[12];
|
||||
float b2 = (*this)[ 8]*(*this)[15] - (*this)[11]*(*this)[12];
|
||||
float b3 = (*this)[ 9]*(*this)[14] - (*this)[10]*(*this)[13];
|
||||
float b4 = (*this)[ 9]*(*this)[15] - (*this)[11]*(*this)[13];
|
||||
float b5 = (*this)[10]*(*this)[15] - (*this)[11]*(*this)[14];
|
||||
|
||||
float det = a0*b5 - a1*b4 + a2*b3 + a3*b2 - a4*b1 + a5*b0;
|
||||
if (fabs(det) > FLT_EPSILON)
|
||||
{
|
||||
Matrix4f inverse;
|
||||
inverse[ 0] = + (*this)[ 5]*b5 - (*this)[ 6]*b4 + (*this)[ 7]*b3;
|
||||
inverse[ 4] = - (*this)[ 4]*b5 + (*this)[ 6]*b2 - (*this)[ 7]*b1;
|
||||
inverse[ 8] = + (*this)[ 4]*b4 - (*this)[ 5]*b2 + (*this)[ 7]*b0;
|
||||
inverse[12] = - (*this)[ 4]*b3 + (*this)[ 5]*b1 - (*this)[ 6]*b0;
|
||||
inverse[ 1] = - (*this)[ 1]*b5 + (*this)[ 2]*b4 - (*this)[ 3]*b3;
|
||||
inverse[ 5] = + (*this)[ 0]*b5 - (*this)[ 2]*b2 + (*this)[ 3]*b1;
|
||||
inverse[ 9] = - (*this)[ 0]*b4 + (*this)[ 1]*b2 - (*this)[ 3]*b0;
|
||||
inverse[13] = + (*this)[ 0]*b3 - (*this)[ 1]*b1 + (*this)[ 2]*b0;
|
||||
inverse[ 2] = + (*this)[13]*a5 - (*this)[14]*a4 + (*this)[15]*a3;
|
||||
inverse[ 6] = - (*this)[12]*a5 + (*this)[14]*a2 - (*this)[15]*a1;
|
||||
inverse[10] = + (*this)[12]*a4 - (*this)[13]*a2 + (*this)[15]*a0;
|
||||
inverse[14] = - (*this)[12]*a3 + (*this)[13]*a1 - (*this)[14]*a0;
|
||||
inverse[ 3] = - (*this)[ 9]*a5 + (*this)[10]*a4 - (*this)[11]*a3;
|
||||
inverse[ 7] = + (*this)[ 8]*a5 - (*this)[10]*a2 + (*this)[11]*a1;
|
||||
inverse[11] = - (*this)[ 8]*a4 + (*this)[ 9]*a2 - (*this)[11]*a0;
|
||||
inverse[15] = + (*this)[ 8]*a3 - (*this)[ 9]*a1 + (*this)[10]*a0;
|
||||
|
||||
float invDet = 1.0f/det;
|
||||
inverse[ 0] *= invDet;
|
||||
inverse[ 1] *= invDet;
|
||||
inverse[ 2] *= invDet;
|
||||
inverse[ 3] *= invDet;
|
||||
inverse[ 4] *= invDet;
|
||||
inverse[ 5] *= invDet;
|
||||
inverse[ 6] *= invDet;
|
||||
inverse[ 7] *= invDet;
|
||||
inverse[ 8] *= invDet;
|
||||
inverse[ 9] *= invDet;
|
||||
inverse[10] *= invDet;
|
||||
inverse[11] *= invDet;
|
||||
inverse[12] *= invDet;
|
||||
inverse[13] *= invDet;
|
||||
inverse[14] *= invDet;
|
||||
inverse[15] *= invDet;
|
||||
|
||||
return inverse;
|
||||
}
|
||||
|
||||
// no inverse
|
||||
return Matrix4f::Identity();
|
||||
}
|
||||
|
||||
Vec3f Matrix4f::Transform(const Vec3f &v, const float w) const
|
||||
{
|
||||
Vec3f vout = Vec3f((*this)[matIdx(0,0)] * v.x + (*this)[matIdx(0,1)] * v.y + (*this)[matIdx(0,2)] * v.z + (*this)[matIdx(0,3)] * w,
|
||||
(*this)[matIdx(1,0)] * v.x + (*this)[matIdx(1,1)] * v.y + (*this)[matIdx(1,2)] * v.z + (*this)[matIdx(1,3)] * w,
|
||||
(*this)[matIdx(2,0)] * v.x + (*this)[matIdx(2,1)] * v.y + (*this)[matIdx(2,2)] * v.z + (*this)[matIdx(2,3)] * w);
|
||||
float wout = (*this)[matIdx(3,0)] * v.x + (*this)[matIdx(3,1)] * v.y + (*this)[matIdx(3,2)] * v.z + (*this)[matIdx(3,3)] * w;
|
||||
|
||||
return vout*(1.0f/wout);
|
||||
}
|
||||
|
||||
const Vec3f Matrix4f::GetPosition() const
|
||||
{
|
||||
return Vec3f(f[12], f[13], f[14]);
|
||||
}
|
||||
|
||||
const Vec3f Matrix4f::GetForward() const
|
||||
{
|
||||
return Vec3f(f[8], f[9], f[10]);
|
||||
}
|
||||
|
||||
const Vec3f Matrix4f::GetRight() const
|
||||
{
|
||||
return Vec3f(f[0], f[1], f[2]);
|
||||
}
|
||||
|
||||
const Vec3f Matrix4f::GetUp() const
|
||||
{
|
||||
return Vec3f(f[4], f[5], f[6]);
|
||||
}
|
||||
|
||||
Matrix4f Matrix4f::Translation(const Vec3f &t)
|
||||
{
|
||||
Matrix4f trans = Matrix4f::Identity();
|
||||
|
||||
trans[12] = t.x;
|
||||
trans[13] = t.y;
|
||||
trans[14] = t.z;
|
||||
|
||||
return trans;
|
||||
}
|
||||
|
||||
Matrix4f Matrix4f::RotationX(const float r)
|
||||
{
|
||||
float m[16] = { 1.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, cosf(r), -sinf(r), 0.0f,
|
||||
0.0f, sinf(r), cosf(r), 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f,
|
||||
};
|
||||
|
||||
return Matrix4f(m);
|
||||
}
|
||||
|
||||
Matrix4f Matrix4f::RotationY(const float r)
|
||||
{
|
||||
float m[16] = { cosf(r), 0.0f, sinf(r), 0.0f,
|
||||
0.0f, 1.0f, 0.0f, 0.0f,
|
||||
-sinf(r), 0.0f, cosf(r), 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f,
|
||||
};
|
||||
|
||||
return Matrix4f(m);
|
||||
}
|
||||
|
||||
Matrix4f Matrix4f::RotationZ(const float r)
|
||||
{
|
||||
float m[16] = { cosf(r), -sinf(r), 0.0f, 0.0f,
|
||||
sinf(r), cosf(r), 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f,
|
||||
};
|
||||
|
||||
return Matrix4f(m);
|
||||
}
|
||||
|
||||
Matrix4f Matrix4f::RotationZYX(const Vec3f &rot)
|
||||
{
|
||||
Quatf Qx = Quatf::AxisAngle(Vec3f(1.0f, 0.0f, 0.0f), rot.x);
|
||||
Quatf Qy = Quatf::AxisAngle(Vec3f(0.0f, 1.0f, 0.0f), rot.y);
|
||||
Quatf Qz = Quatf::AxisAngle(Vec3f(0.0f, 0.0f, 1.0f), rot.z);
|
||||
|
||||
Quatf R = Qx * Qy * Qz;
|
||||
|
||||
return R.GetMatrix();
|
||||
}
|
||||
|
||||
Matrix4f Matrix4f::RotationXYZ(const Vec3f &rot)
|
||||
{
|
||||
Quatf Qx = Quatf::AxisAngle(Vec3f(1.0f, 0.0f, 0.0f), rot.x);
|
||||
Quatf Qy = Quatf::AxisAngle(Vec3f(0.0f, 1.0f, 0.0f), rot.y);
|
||||
Quatf Qz = Quatf::AxisAngle(Vec3f(0.0f, 0.0f, 1.0f), rot.z);
|
||||
|
||||
Quatf R = Qz * Qy * Qx;
|
||||
|
||||
return R.GetMatrix();
|
||||
}
|
||||
|
||||
Matrix4f Matrix4f::Orthographic(const float near, const float far)
|
||||
{
|
||||
float L = -10.0f;
|
||||
float R = 10.0f;
|
||||
|
||||
float T = 10.0f;
|
||||
float B = -10.0f;
|
||||
|
||||
float N = -abs(far-near)*0.5f;
|
||||
float F = abs(far-near)*0.5f;
|
||||
|
||||
if(far < near)
|
||||
{
|
||||
float tmp = F;
|
||||
F = N;
|
||||
N = tmp;
|
||||
}
|
||||
|
||||
float ortho[16] = { 2.0f/(R-L), 0.0f, 0.0f, (L+R)/(L-R),
|
||||
0.0f, 2.0f/(T-B), 0.0f, (T+B)/(B-T),
|
||||
0.0f, 0.0f, 1.0f/(F-N), (F+N)/(N-F),
|
||||
0.0f, 0.0f, 0.0f, 1.0f,
|
||||
};
|
||||
|
||||
return Matrix4f(ortho);
|
||||
}
|
||||
|
||||
Matrix4f Matrix4f::Perspective(const float degfov, const float N, const float F, const float A)
|
||||
{
|
||||
const float radfov = degfov * (3.1415926535f/180.0f);
|
||||
float S = 1/tanf(radfov * 0.5f);
|
||||
|
||||
float persp[16] = { S/A, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, S, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, F/(F-N), 1.0f,
|
||||
0.0f, 0.0f, -(F*N)/(F-N), 0.0f,
|
||||
};
|
||||
|
||||
return Matrix4f(persp);
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/******************************************************************************
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014 Crytek
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
class Vec3f;
|
||||
class Quatf;
|
||||
|
||||
#include <string.h>
|
||||
|
||||
class Matrix4f
|
||||
{
|
||||
public:
|
||||
Matrix4f() {}
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
// Matrix generation functions
|
||||
|
||||
inline static Matrix4f Zero()
|
||||
{
|
||||
Matrix4f m;
|
||||
memset(&m, 0, sizeof(Matrix4f));
|
||||
return m;
|
||||
}
|
||||
|
||||
inline static Matrix4f Identity()
|
||||
{
|
||||
Matrix4f m = Zero();
|
||||
m[0] = m[5] = m[10] = m[15] = 1.0f;
|
||||
return m;
|
||||
}
|
||||
|
||||
static Matrix4f Translation(const Vec3f &t);
|
||||
static Matrix4f RotationX(const float r);
|
||||
static Matrix4f RotationY(const float r);
|
||||
static Matrix4f RotationZ(const float r);
|
||||
static Matrix4f RotationXYZ(const Vec3f &rot);
|
||||
static Matrix4f RotationZYX(const Vec3f &rot);
|
||||
static Matrix4f Orthographic(const float nearplane, const float farplane);
|
||||
static Matrix4f Perspective(const float degfov, const float nearplane, const float farplane, const float aspect);
|
||||
|
||||
inline float operator[](const size_t i) const
|
||||
{
|
||||
return f[i];
|
||||
}
|
||||
|
||||
inline float &operator[](const size_t i)
|
||||
{
|
||||
return f[i];
|
||||
}
|
||||
|
||||
Matrix4f Inverse() const;
|
||||
Matrix4f Mul(const Matrix4f &o) const;
|
||||
|
||||
Vec3f Transform(const Vec3f &v, const float w=1.0f) const;
|
||||
|
||||
const float * const Data() const { return &f[0]; }
|
||||
|
||||
const Vec3f GetPosition() const;
|
||||
const Vec3f GetForward() const;
|
||||
const Vec3f GetRight() const;
|
||||
const Vec3f GetUp() const;
|
||||
|
||||
private:
|
||||
Matrix4f(const float *d) { memcpy(f, d, sizeof(Matrix4f)); }
|
||||
|
||||
friend class Quatf;
|
||||
|
||||
float f[16];
|
||||
};
|
||||
@@ -0,0 +1,75 @@
|
||||
/******************************************************************************
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014 Crytek
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <math.h>
|
||||
#include "vec.h"
|
||||
#include "matrix.h"
|
||||
|
||||
class Quatf
|
||||
{
|
||||
public:
|
||||
float w;
|
||||
Vec3f v;
|
||||
|
||||
static Quatf AxisAngle(Vec3f axis, float angle)
|
||||
{
|
||||
Quatf q;
|
||||
|
||||
q.w = cosf(angle/2.0f);
|
||||
q.v.x = axis.x * sinf(angle/2.0f);
|
||||
q.v.y = axis.y * sinf(angle/2.0f);
|
||||
q.v.z = axis.z * sinf(angle/2.0f);
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
Matrix4f GetMatrix()
|
||||
{
|
||||
float q0 = w;
|
||||
float q1 = v.x;
|
||||
float q2 = v.y;
|
||||
float q3 = v.z;
|
||||
|
||||
float m[16] = { 1.0f - 2*(q2*q2 + q3*q3), 2.0f*(q1*q2 - q0*q3), 2.0f*(q0*q2 + q1*q3), 0.0f,
|
||||
2.0f*(q1*q2 + q0*q3), 1.0f - 2*(q1*q1 + q3*q3), 2.0f*(q2*q3 - q0*q1), 0.0f,
|
||||
2.0f*(q1*q3 - q0*q2), 2.0f*(q0*q1 + q2*q3), 1.0f - 2*(q1*q1 + q2*q2), 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f,
|
||||
};
|
||||
|
||||
return Matrix4f(m);
|
||||
}
|
||||
};
|
||||
|
||||
inline Quatf operator *(const Quatf &a, const Quatf &b)
|
||||
{
|
||||
Quatf r;
|
||||
|
||||
r.w = a.w * b.w - a.v.Dot(b.v);
|
||||
r.v = b.v * a.w + a.v * b.w + a.v.Cross(b.v);
|
||||
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/******************************************************************************
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2014 Crytek
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <math.h>
|
||||
|
||||
struct Vec2f
|
||||
{
|
||||
Vec2f(float X = 0.0f, float Y = 0.0f) { x = X; y = Y; }
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
class Vec3f
|
||||
{
|
||||
public:
|
||||
Vec3f(const float X=0.0f, const float Y=0.0f, const float Z=0.0f)
|
||||
: x(X), y(Y), z(Z)
|
||||
{ }
|
||||
|
||||
inline float Dot(const Vec3f &o) const
|
||||
{
|
||||
return x*o.x + y*o.y + z*o.z;
|
||||
}
|
||||
|
||||
inline Vec3f Cross(const Vec3f &o) const
|
||||
{
|
||||
return Vec3f(y*o.z - z*o.y,
|
||||
z*o.x - x*o.z,
|
||||
x*o.y - y*o.x);
|
||||
}
|
||||
|
||||
inline float Length() const
|
||||
{
|
||||
return sqrt(Dot(*this));
|
||||
}
|
||||
|
||||
inline void Normalise()
|
||||
{
|
||||
float l = Length();
|
||||
x /= l;
|
||||
y /= l;
|
||||
z /= l;
|
||||
}
|
||||
|
||||
float x, y, z;
|
||||
};
|
||||
|
||||
struct Vec4f
|
||||
{
|
||||
Vec4f(float X = 0.0f, float Y = 0.0f, float Z = 0.0f, float W = 0.0f) { x = X; y = Y; z = Z; w = W; }
|
||||
float x, y, z, w;
|
||||
};
|
||||
|
||||
inline Vec3f operator *(const Vec3f &a, const float b)
|
||||
{
|
||||
return Vec3f(a.x*b, a.y*b, a.z*b);
|
||||
}
|
||||
|
||||
inline Vec3f operator +(const Vec3f &a, const Vec3f &b)
|
||||
{
|
||||
return Vec3f(a.x+b.x, a.y+b.y, a.z+b.z);
|
||||
}
|
||||
|
||||
inline Vec3f operator -(const Vec3f &a)
|
||||
{
|
||||
return Vec3f(-a.x, -a.y, -a.z);
|
||||
}
|
||||
|
||||
inline Vec3f operator -(const Vec3f &a, const Vec3f &b)
|
||||
{
|
||||
return a + (-b);
|
||||
}
|
||||
|
||||
inline Vec3f operator -=(Vec3f &a, const Vec3f &b)
|
||||
{
|
||||
a = a-b;
|
||||
return a;
|
||||
}
|
||||
|
||||
inline Vec3f operator +=(Vec3f &a, const Vec3f &b)
|
||||
{
|
||||
a = a+b;
|
||||
return a;
|
||||
}
|
||||
Reference in New Issue
Block a user