Remove softpoly specific matrix and vector classes

This commit is contained in:
Magnus Norddahl 2020-01-18 13:56:37 +01:00
parent 43562c94d0
commit e43894213e
10 changed files with 87 additions and 483 deletions

View File

@ -713,7 +713,6 @@ set( POLYRENDER_SOURCES
rendering/polyrenderer/drawers/screen_scanline_setup.cpp
rendering/polyrenderer/drawers/screen_shader.cpp
rendering/polyrenderer/drawers/screen_blend.cpp
rendering/polyrenderer/math/gpu_types.cpp
)
# These files will be flagged as "headers" so that they appear in project files

View File

@ -136,7 +136,7 @@ void PolyVertexInputAssembly::Load(PolyTriangleThreadData *thread, const void *v
if ((UseVertexData & 2) == 0)
{
const auto &n = thread->mainVertexShader.Data.uVertexNormal;
thread->mainVertexShader.aNormal = Vec4f(n.X, n.Y, n.Z, 1.0);
thread->mainVertexShader.aNormal = FVector4(n.X, n.Y, n.Z, 1.0);
thread->mainVertexShader.aNormal2 = thread->mainVertexShader.aNormal;
}
else
@ -149,8 +149,8 @@ void PolyVertexInputAssembly::Load(PolyTriangleThreadData *thread, const void *v
float x2 = ((n2 << 22) >> 22) / 512.0f;
float y2 = ((n2 << 12) >> 22) / 512.0f;
float z2 = ((n2 << 2) >> 22) / 512.0f;
thread->mainVertexShader.aNormal = Vec4f(x, y, z, 0.0f);
thread->mainVertexShader.aNormal2 = Vec4f(x2, y2, z2, 0.0f);
thread->mainVertexShader.aNormal = FVector4(x, y, z, 0.0f);
thread->mainVertexShader.aNormal2 = FVector4(x2, y2, z2, 0.0f);
}
}

View File

@ -25,7 +25,6 @@
#include "swrenderer/drawers/r_draw.h"
#include "swrenderer/drawers/r_thread.h"
#include "polyrenderer/drawers/screen_triangle.h"
#include "polyrenderer/math/gpu_types.h"
#include "polyrenderer/drawers/poly_vertex_shader.h"
class DCanvas;
@ -101,7 +100,7 @@ struct PolyPushConstants
{
int uTextureMode;
float uAlphaThreshold;
Vec2f uClipSplit;
FVector2 uClipSplit;
// Lighting + Fog
float uLightLevel;

View File

@ -1,7 +1,6 @@
#pragma once
#include "polyrenderer/math/gpu_types.h"
#include "hwrenderer/scene/hw_viewpointuniforms.h"
#include "hwrenderer/scene/hw_renderstate.h"
@ -12,27 +11,27 @@
class ShadedTriVertex
{
public:
Vec4f gl_Position;
FVector4 gl_Position;
float gl_ClipDistance[5];
Vec4f vTexCoord;
Vec4f vColor;
Vec4f pixelpos;
//Vec3f glowdist;
Vec3f gradientdist;
//Vec4f vEyeNormal;
Vec4f vWorldNormal;
FVector4 vTexCoord;
FVector4 vColor;
FVector4 pixelpos;
//FVector3 glowdist;
FVector3 gradientdist;
//FVector4 vEyeNormal;
FVector4 vWorldNormal;
};
class PolyMainVertexShader : public ShadedTriVertex
{
public:
// Input
Vec4f aPosition;
Vec2f aTexCoord;
Vec4f aColor;
Vec4f aVertex2;
Vec4f aNormal;
Vec4f aNormal2;
FVector4 aPosition;
FVector2 aTexCoord;
FVector4 aColor;
FVector4 aVertex2;
FVector4 aNormal;
FVector4 aNormal2;
// Defines
bool SIMPLE = false;
@ -43,21 +42,21 @@ public:
VSMatrix NormalModelMatrix;
VSMatrix TextureMatrix;
StreamData Data;
Vec2f uClipSplit;
FVector2 uClipSplit;
const HWViewpointUniforms *Viewpoint = nullptr;
void main()
{
Vec2f parmTexCoord = aTexCoord;
Vec4f parmPosition = aPosition;
FVector2 parmTexCoord = aTexCoord;
FVector4 parmPosition = aPosition;
Vec4f worldcoord;
FVector4 worldcoord;
if (SIMPLE)
worldcoord = mul(ModelMatrix, mix(parmPosition, aVertex2, Data.uInterpolationFactor));
else
worldcoord = mul(ModelMatrix, parmPosition);
Vec4f eyeCoordPos = mul(Viewpoint->mViewMatrix, worldcoord);
FVector4 eyeCoordPos = mul(Viewpoint->mViewMatrix, worldcoord);
vColor = aColor;
@ -92,19 +91,19 @@ public:
gl_ClipDistance[4] = worldcoord.Y - ((Data.uSplitBottomPlane.W + Data.uSplitBottomPlane.X * worldcoord.X + Data.uSplitBottomPlane.Y * worldcoord.Z) * Data.uSplitBottomPlane.Z);
}
vWorldNormal = mul(NormalModelMatrix, Vec4f(normalize(mix3(aNormal, aNormal2, Data.uInterpolationFactor)), 1.0f));
vWorldNormal = mul(NormalModelMatrix, FVector4(normalize(mix3(aNormal, aNormal2, Data.uInterpolationFactor)), 1.0f));
//vEyeNormal = mul(Viewpoint->mNormalViewMatrix, vWorldNormal);
}
if (!SPHEREMAP)
{
vTexCoord = mul(TextureMatrix, Vec4f(parmTexCoord, 0.0f, 1.0f));
vTexCoord = mul(TextureMatrix, FVector4(parmTexCoord.X, parmTexCoord.Y, 0.0f, 1.0f));
}
else
{
Vec3f u = normalize3(eyeCoordPos);
Vec3f n = normalize3(mul(Viewpoint->mNormalViewMatrix, Vec4f(parmTexCoord.X, 0.0f, parmTexCoord.Y, 0.0f)));
Vec3f r = reflect(u, n);
FVector3 u = normalize3(eyeCoordPos);
FVector3 n = normalize3(mul(Viewpoint->mNormalViewMatrix, FVector4(parmTexCoord.X, 0.0f, parmTexCoord.Y, 0.0f)));
FVector3 r = reflect(u, n);
float m = 2.0f * sqrt(r.X*r.X + r.Y*r.Y + (r.Z + 1.0f)*(r.Z + 1.0f));
vTexCoord.X = r.X / m + 0.5f;
vTexCoord.Y = r.Y / m + 0.5f;
@ -137,41 +136,41 @@ public:
}
private:
static Vec3f normalize(const Vec3f &a)
static FVector3 normalize(const FVector3 &a)
{
float rcplen = 1.0f / sqrt(a.X * a.X + a.Y * a.Y + a.Z * a.Z);
return Vec3f(a.X * rcplen, a.Y * rcplen, a.Z * rcplen);
return FVector3(a.X * rcplen, a.Y * rcplen, a.Z * rcplen);
}
static Vec3f normalize3(const Vec4f &a)
static FVector3 normalize3(const FVector4 &a)
{
float rcplen = 1.0f / sqrt(a.X * a.X + a.Y * a.Y + a.Z * a.Z);
return Vec3f(a.X * rcplen, a.Y * rcplen, a.Z * rcplen);
return FVector3(a.X * rcplen, a.Y * rcplen, a.Z * rcplen);
}
static Vec4f mix(const Vec4f &a, const Vec4f &b, float t)
static FVector4 mix(const FVector4 &a, const FVector4 &b, float t)
{
float invt = 1.0f - t;
return Vec4f(a.X * invt + b.X * t, a.Y * invt + b.Y * t, a.Z * invt + b.Z * t, a.W * invt + b.W * t);
return FVector4(a.X * invt + b.X * t, a.Y * invt + b.Y * t, a.Z * invt + b.Z * t, a.W * invt + b.W * t);
}
static Vec3f mix3(const Vec4f &a, const Vec4f &b, float t)
static FVector3 mix3(const FVector4 &a, const FVector4 &b, float t)
{
float invt = 1.0f - t;
return Vec3f(a.X * invt + b.X * t, a.Y * invt + b.Y * t, a.Z * invt + b.Z * t);
return FVector3(a.X * invt + b.X * t, a.Y * invt + b.Y * t, a.Z * invt + b.Z * t);
}
static Vec3f reflect(const Vec3f &u, const Vec3f &n)
static FVector3 reflect(const FVector3 &u, const FVector3 &n)
{
float d = 2.0f * (n.X * u.X + n.Y * u.Y + n.Z * u.Z);
return Vec3f(u.X - d * n.X, u.Y - d * n.Y, u.Z - d * n.Z);
return FVector3(u.X - d * n.X, u.Y - d * n.Y, u.Z - d * n.Z);
}
static Vec4f mul(const VSMatrix &mat, const Vec4f &v)
static FVector4 mul(const VSMatrix &mat, const FVector4 &v)
{
const float *m = mat.get();
Vec4f result;
FVector4 result;
#ifdef NO_SSE
result.X = m[0 * 4 + 0] * v.X + m[1 * 4 + 0] * v.Y + m[2 * 4 + 0] * v.Z + m[3 * 4 + 0] * v.W;
result.Y = m[0 * 4 + 1] * v.X + m[1 * 4 + 1] * v.Y + m[2 * 4 + 1] * v.Z + m[3 * 4 + 1] * v.W;

View File

@ -1,271 +0,0 @@
/*
** GPU math utility classes
** Copyright (c) 2016-2020 Magnus Norddahl
**
** This software is provided 'as-is', without any express or implied
** warranty. In no event will the authors be held liable for any damages
** arising from the use of this software.
**
** Permission is granted to anyone to use this software for any purpose,
** including commercial applications, and to alter it and redistribute it
** freely, subject to the following restrictions:
**
** 1. The origin of this software must not be misrepresented; you must not
** claim that you wrote the original software. If you use this software
** in a product, an acknowledgment in the product documentation would be
** appreciated but is not required.
** 2. Altered source versions must be plainly marked as such, and must not be
** misrepresented as being the original software.
** 3. This notice may not be removed or altered from any source distribution.
**
*/
#include <stdlib.h>
#include "gpu_types.h"
#include "doomtype.h"
#include <cmath>
Mat4f Mat4f::Null()
{
Mat4f m;
memset(m.Matrix, 0, sizeof(m.Matrix));
return m;
}
Mat4f Mat4f::Identity()
{
Mat4f m = Null();
m.Matrix[0] = 1.0f;
m.Matrix[5] = 1.0f;
m.Matrix[10] = 1.0f;
m.Matrix[15] = 1.0f;
return m;
}
Mat4f Mat4f::FromValues(const float *matrix)
{
Mat4f m;
memcpy(m.Matrix, matrix, sizeof(m.Matrix));
return m;
}
Mat4f Mat4f::Transpose(const Mat4f &matrix)
{
Mat4f m;
for (int y = 0; y < 4; y++)
for (int x = 0; x < 4; x++)
m.Matrix[x + y * 4] = matrix.Matrix[y + x * 4];
return m;
}
Mat4f Mat4f::Translate(float x, float y, float z)
{
Mat4f m = Identity();
m.Matrix[0 + 3 * 4] = x;
m.Matrix[1 + 3 * 4] = y;
m.Matrix[2 + 3 * 4] = z;
return m;
}
Mat4f Mat4f::Scale(float x, float y, float z)
{
Mat4f m = Null();
m.Matrix[0 + 0 * 4] = x;
m.Matrix[1 + 1 * 4] = y;
m.Matrix[2 + 2 * 4] = z;
m.Matrix[3 + 3 * 4] = 1;
return m;
}
Mat4f Mat4f::Rotate(float angle, float x, float y, float z)
{
float c = cosf(angle);
float s = sinf(angle);
Mat4f m = Null();
m.Matrix[0 + 0 * 4] = (x*x*(1.0f - c) + c);
m.Matrix[0 + 1 * 4] = (x*y*(1.0f - c) - z*s);
m.Matrix[0 + 2 * 4] = (x*z*(1.0f - c) + y*s);
m.Matrix[1 + 0 * 4] = (y*x*(1.0f - c) + z*s);
m.Matrix[1 + 1 * 4] = (y*y*(1.0f - c) + c);
m.Matrix[1 + 2 * 4] = (y*z*(1.0f - c) - x*s);
m.Matrix[2 + 0 * 4] = (x*z*(1.0f - c) - y*s);
m.Matrix[2 + 1 * 4] = (y*z*(1.0f - c) + x*s);
m.Matrix[2 + 2 * 4] = (z*z*(1.0f - c) + c);
m.Matrix[3 + 3 * 4] = 1.0f;
return m;
}
Mat4f Mat4f::SwapYZ()
{
Mat4f m = Null();
m.Matrix[0 + 0 * 4] = 1.0f;
m.Matrix[1 + 2 * 4] = 1.0f;
m.Matrix[2 + 1 * 4] = -1.0f;
m.Matrix[3 + 3 * 4] = 1.0f;
return m;
}
Mat4f Mat4f::Perspective(float fovy, float aspect, float z_near, float z_far, Handedness handedness, ClipZRange clipZ)
{
float f = (float)(1.0 / tan(fovy * M_PI / 360.0));
Mat4f m = Null();
m.Matrix[0 + 0 * 4] = f / aspect;
m.Matrix[1 + 1 * 4] = f;
m.Matrix[2 + 2 * 4] = (z_far + z_near) / (z_near - z_far);
m.Matrix[2 + 3 * 4] = (2.0f * z_far * z_near) / (z_near - z_far);
m.Matrix[3 + 2 * 4] = -1.0f;
if (handedness == Handedness::Left)
{
m = m * Mat4f::Scale(1.0f, 1.0f, -1.0f);
}
if (clipZ == ClipZRange::ZeroPositiveW)
{
Mat4f scale_translate = Identity();
scale_translate.Matrix[2 + 2 * 4] = 0.5f;
scale_translate.Matrix[2 + 3 * 4] = 0.5f;
m = scale_translate * m;
}
return m;
}
Mat4f Mat4f::Frustum(float left, float right, float bottom, float top, float near, float far, Handedness handedness, ClipZRange clipZ)
{
float a = (right + left) / (right - left);
float b = (top + bottom) / (top - bottom);
float c = -(far + near) / (far - near);
float d = -(2.0f * far) / (far - near);
Mat4f m = Null();
m.Matrix[0 + 0 * 4] = 2.0f * near / (right - left);
m.Matrix[1 + 1 * 4] = 2.0f * near / (top - bottom);
m.Matrix[0 + 2 * 4] = a;
m.Matrix[1 + 2 * 4] = b;
m.Matrix[2 + 2 * 4] = c;
m.Matrix[2 + 3 * 4] = d;
m.Matrix[3 + 2 * 4] = -1;
if (handedness == Handedness::Left)
{
m = m * Mat4f::Scale(1.0f, 1.0f, -1.0f);
}
if (clipZ == ClipZRange::ZeroPositiveW)
{
Mat4f scale_translate = Identity();
scale_translate.Matrix[2 + 2 * 4] = 0.5f;
scale_translate.Matrix[2 + 3 * 4] = 0.5f;
m = scale_translate * m;
}
return m;
}
Mat4f Mat4f::operator*(const Mat4f &mult) const
{
Mat4f result;
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 4; y++)
{
result.Matrix[x + y * 4] =
Matrix[0 * 4 + x] * mult.Matrix[y * 4 + 0] +
Matrix[1 * 4 + x] * mult.Matrix[y * 4 + 1] +
Matrix[2 * 4 + x] * mult.Matrix[y * 4 + 2] +
Matrix[3 * 4 + x] * mult.Matrix[y * 4 + 3];
}
}
return result;
}
Vec4f Mat4f::operator*(const Vec4f &v) const
{
Vec4f result;
#ifdef NO_SSE
result.X = Matrix[0 * 4 + 0] * v.X + Matrix[1 * 4 + 0] * v.Y + Matrix[2 * 4 + 0] * v.Z + Matrix[3 * 4 + 0] * v.W;
result.Y = Matrix[0 * 4 + 1] * v.X + Matrix[1 * 4 + 1] * v.Y + Matrix[2 * 4 + 1] * v.Z + Matrix[3 * 4 + 1] * v.W;
result.Z = Matrix[0 * 4 + 2] * v.X + Matrix[1 * 4 + 2] * v.Y + Matrix[2 * 4 + 2] * v.Z + Matrix[3 * 4 + 2] * v.W;
result.W = Matrix[0 * 4 + 3] * v.X + Matrix[1 * 4 + 3] * v.Y + Matrix[2 * 4 + 3] * v.Z + Matrix[3 * 4 + 3] * v.W;
#else
__m128 m0 = _mm_loadu_ps(Matrix);
__m128 m1 = _mm_loadu_ps(Matrix + 4);
__m128 m2 = _mm_loadu_ps(Matrix + 8);
__m128 m3 = _mm_loadu_ps(Matrix + 12);
__m128 mv = _mm_loadu_ps(&v.X);
m0 = _mm_mul_ps(m0, _mm_shuffle_ps(mv, mv, _MM_SHUFFLE(0, 0, 0, 0)));
m1 = _mm_mul_ps(m1, _mm_shuffle_ps(mv, mv, _MM_SHUFFLE(1, 1, 1, 1)));
m2 = _mm_mul_ps(m2, _mm_shuffle_ps(mv, mv, _MM_SHUFFLE(2, 2, 2, 2)));
m3 = _mm_mul_ps(m3, _mm_shuffle_ps(mv, mv, _MM_SHUFFLE(3, 3, 3, 3)));
mv = _mm_add_ps(_mm_add_ps(_mm_add_ps(m0, m1), m2), m3);
Vec4f sv;
_mm_storeu_ps(&result.X, mv);
#endif
return result;
}
/////////////////////////////////////////////////////////////////////////////
Mat3f::Mat3f(const Mat4f &matrix)
{
for (int y = 0; y < 3; y++)
for (int x = 0; x < 3; x++)
Matrix[x + y * 3] = matrix.Matrix[x + y * 4];
}
Mat3f Mat3f::Null()
{
Mat3f m;
memset(m.Matrix, 0, sizeof(m.Matrix));
return m;
}
Mat3f Mat3f::Identity()
{
Mat3f m = Null();
m.Matrix[0] = 1.0f;
m.Matrix[4] = 1.0f;
m.Matrix[8] = 1.0f;
return m;
}
Mat3f Mat3f::FromValues(float *matrix)
{
Mat3f m;
memcpy(m.Matrix, matrix, sizeof(m.Matrix));
return m;
}
Mat3f Mat3f::Transpose(const Mat3f &matrix)
{
Mat3f m;
for (int y = 0; y < 3; y++)
for (int x = 0; x < 3; x++)
m.Matrix[x + y * 3] = matrix.Matrix[y + x * 3];
return m;
}
Mat3f Mat3f::operator*(const Mat3f &mult) const
{
Mat3f result;
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
result.Matrix[x + y * 3] =
Matrix[0 * 3 + x] * mult.Matrix[y * 3 + 0] +
Matrix[1 * 3 + x] * mult.Matrix[y * 3 + 1] +
Matrix[2 * 3 + x] * mult.Matrix[y * 3 + 2];
}
}
return result;
}
Vec3f Mat3f::operator*(const Vec3f &v) const
{
Vec3f result;
result.X = Matrix[0 * 3 + 0] * v.X + Matrix[1 * 3 + 0] * v.Y + Matrix[2 * 3 + 0] * v.Z;
result.Y = Matrix[0 * 3 + 1] * v.X + Matrix[1 * 3 + 1] * v.Y + Matrix[2 * 3 + 1] * v.Z;
result.Z = Matrix[0 * 3 + 2] * v.X + Matrix[1 * 3 + 2] * v.Y + Matrix[2 * 3 + 2] * v.Z;
return result;
}

View File

@ -1,148 +0,0 @@
/*
** Hardpoly renderer
** Copyright (c) 2016 Magnus Norddahl
**
** This software is provided 'as-is', without any express or implied
** warranty. In no event will the authors be held liable for any damages
** arising from the use of this software.
**
** Permission is granted to anyone to use this software for any purpose,
** including commercial applications, and to alter it and redistribute it
** freely, subject to the following restrictions:
**
** 1. The origin of this software must not be misrepresented; you must not
** claim that you wrote the original software. If you use this software
** in a product, an acknowledgment in the product documentation would be
** appreciated but is not required.
** 2. Altered source versions must be plainly marked as such, and must not be
** misrepresented as being the original software.
** 3. This notice may not be removed or altered from any source distribution.
**
*/
#pragma once
#include <memory>
template<typename Type> class Vec4;
template<typename Type> class Vec3;
template<typename Type>
class Vec2
{
public:
Vec2() : X(Type(0)), Y(Type(0)) { }
Vec2(float v) : X(v), Y(v) { }
Vec2(float x, float y) : X(x), Y(y) { }
float X, Y;
bool operator==(const Vec2 &other) const { return X == other.X && Y == other.Y; }
bool operator!=(const Vec2 &other) const { return X != other.X || Y != other.Y; }
};
template<typename Type>
class Vec3
{
public:
Vec3() : X(Type(0)), Y(Type(0)), Z(Type(0)) { }
Vec3(float v) : X(v), Y(v), Z(v) { }
Vec3(float x, float y, float z) : X(x), Y(y), Z(z) { }
Vec3(const Vec2<Type> &v, float z) : X(v.X), Y(v.Y), Z(z) { }
float X, Y, Z;
bool operator==(const Vec3 &other) const { return X == other.X && Y == other.Y && Z == other.Z; }
bool operator!=(const Vec3 &other) const { return X != other.X || Y != other.Y || Z != other.Z; }
};
template<typename Type>
class Vec4
{
public:
Vec4() : X(Type(0)), Y(Type(0)), Z(Type(0)), W(Type(0)) { }
Vec4(Type v) : X(v), Y(v), Z(v), W(v) { }
Vec4(Type x, Type y, Type z, Type w = 1.0f) : X(x), Y(y), Z(z), W(w) { }
Vec4(const Vec2<Type> &v, Type z, Type w) : X(v.X), Y(v.Y), Z(z), W(w) { }
Vec4(const Vec3<Type> &v, Type w) : X(v.X), Y(v.Y), Z(v.Z), W(w) { }
Type X, Y, Z, W;
bool operator==(const Vec4 &other) const { return X == other.X && Y == other.Y && Z == other.Z && W == other.W; }
bool operator!=(const Vec4 &other) const { return X != other.X || Y != other.Y || Z != other.Z || W != other.W; }
};
typedef Vec4<double> Vec4d;
typedef Vec3<double> Vec3d;
typedef Vec2<double> Vec2d;
typedef Vec4<float> Vec4f;
typedef Vec3<float> Vec3f;
typedef Vec2<float> Vec2f;
typedef Vec4<int32_t> Vec4i;
typedef Vec3<int32_t> Vec3i;
typedef Vec2<int32_t> Vec2i;
typedef Vec4<int16_t> Vec4s;
typedef Vec3<int16_t> Vec3s;
typedef Vec2<int16_t> Vec2s;
typedef Vec4<int8_t> Vec4b;
typedef Vec3<int8_t> Vec3b;
typedef Vec2<int8_t> Vec2b;
typedef Vec4<uint32_t> Vec4ui;
typedef Vec3<uint32_t> Vec3ui;
typedef Vec2<uint32_t> Vec2ui;
typedef Vec4<uint16_t> Vec4us;
typedef Vec3<uint16_t> Vec3us;
typedef Vec2<uint16_t> Vec2us;
typedef Vec4<uint8_t> Vec4ub;
typedef Vec3<uint8_t> Vec3ub;
typedef Vec2<uint8_t> Vec2ub;
enum class Handedness
{
Left,
Right
};
enum class ClipZRange
{
NegativePositiveW, // OpenGL, -wclip <= zclip <= wclip
ZeroPositiveW // Direct3D, 0 <= zclip <= wclip
};
class Mat4f
{
public:
static Mat4f Null();
static Mat4f Identity();
static Mat4f FromValues(const float *matrix);
static Mat4f Transpose(const Mat4f &matrix);
static Mat4f Translate(float x, float y, float z);
static Mat4f Scale(float x, float y, float z);
static Mat4f Rotate(float angle, float x, float y, float z);
static Mat4f SwapYZ();
static Mat4f Perspective(float fovy, float aspect, float near, float far, Handedness handedness, ClipZRange clipZ);
static Mat4f Frustum(float left, float right, float bottom, float top, float near, float far, Handedness handedness, ClipZRange clipZ);
Vec4f operator*(const Vec4f &v) const;
Mat4f operator*(const Mat4f &m) const;
float Matrix[16];
};
class Mat3f
{
public:
Mat3f() { }
Mat3f(const Mat4f &matrix);
static Mat3f Null();
static Mat3f Identity();
static Mat3f FromValues(float *matrix);
static Mat3f Transpose(const Mat3f &matrix);
Vec3f operator*(const Vec3f &v) const;
Mat3f operator*(const Mat3f &m) const;
float Matrix[9];
};

View File

@ -5,4 +5,3 @@
#include "drawers/screen_scanline_setup.cpp"
#include "drawers/screen_shader.cpp"
#include "drawers/screen_blend.cpp"
#include "math/gpu_types.cpp"

View File

@ -49,7 +49,7 @@ namespace swrenderer
float x, y, z;
FSpriteModelFrame *smf;
AActor *actor;
Mat4f WorldToClip;
VSMatrix WorldToClip;
bool MirrorWorldToClip;
};

View File

@ -64,25 +64,32 @@ namespace swrenderer
WorldToView = SoftwareWorldToView(viewpoint);
if (thread->Portal->MirrorFlags & RF_XFLIP)
WorldToView = Mat4f::Scale(-1.0f, 1.0f, 1.0f) * WorldToView;
{
WorldToView.scale(-1.0f, 1.0f, 1.0f);
}
ViewToClip = SoftwareViewToClip();
WorldToClip = ViewToClip * WorldToView;
WorldToClip.multMatrix(ViewToClip);
}
Mat4f RenderViewport::SoftwareWorldToView(const FRenderViewpoint &viewpoint)
VSMatrix RenderViewport::SoftwareWorldToView(const FRenderViewpoint &viewpoint)
{
Mat4f m = Mat4f::Null();
m.Matrix[0 + 0 * 4] = (float)viewpoint.Sin;
m.Matrix[0 + 1 * 4] = (float)-viewpoint.Cos;
m.Matrix[1 + 2 * 4] = 1.0f;
m.Matrix[2 + 0 * 4] = (float)-viewpoint.Cos;
m.Matrix[2 + 1 * 4] = (float)-viewpoint.Sin;
m.Matrix[3 + 3 * 4] = 1.0f;
return m * Mat4f::Translate((float)-viewpoint.Pos.X, (float)-viewpoint.Pos.Y, (float)-viewpoint.Pos.Z);
float m[16] = { 0.0f };
m[0 + 0 * 4] = (float)viewpoint.Sin;
m[0 + 1 * 4] = (float)-viewpoint.Cos;
m[1 + 2 * 4] = 1.0f;
m[2 + 0 * 4] = (float)-viewpoint.Cos;
m[2 + 1 * 4] = (float)-viewpoint.Sin;
m[3 + 3 * 4] = 1.0f;
VSMatrix matrix;
matrix.loadIdentity();
matrix.translate((float)-viewpoint.Pos.X, (float)-viewpoint.Pos.Y, (float)-viewpoint.Pos.Z);
matrix.multMatrix(m);
return matrix;
}
Mat4f RenderViewport::SoftwareViewToClip()
VSMatrix RenderViewport::SoftwareViewToClip()
{
float near = 5.0f;
float far = 65536.0f;
@ -92,7 +99,27 @@ namespace swrenderer
width *= near;
height *= near;
offset *= near;
return Mat4f::Frustum(-width, width, -height + offset, height + offset, near, far, Handedness::Right, ClipZRange::NegativePositiveW);
float left = -width;
float right = width;
float bottom = -height + offset;
float top = height + offset;
float a = (right + left) / (right - left);
float b = (top + bottom) / (top - bottom);
float c = -(far + near) / (far - near);
float d = -(2.0f * far) / (far - near);
float m[16] = { 0.0f };
m[0 + 0 * 4] = 2.0f * near / (right - left);
m[1 + 1 * 4] = 2.0f * near / (top - bottom);
m[0 + 2 * 4] = a;
m[1 + 2 * 4] = b;
m[2 + 2 * 4] = c;
m[2 + 3 * 4] = d;
m[3 + 2 * 4] = -1;
VSMatrix matrix;
matrix.loadMatrix(m);
return matrix;
}
void RenderViewport::SetViewport(FLevelLocals *Level, RenderThread *thread, int fullWidth, int fullHeight, float trueratio)

View File

@ -7,7 +7,7 @@
#include "r_defs.h"
#include "r_utility.h"
#include "actorinlines.h"
#include "polyrenderer/math/gpu_types.h"
#include "utility/matrix.h"
#define MINZ double((2048*4) / double(1 << 20))
@ -28,9 +28,9 @@ namespace swrenderer
void SetupPolyViewport(RenderThread *thread);
Mat4f WorldToView;
Mat4f ViewToClip;
Mat4f WorldToClip;
VSMatrix WorldToView;
VSMatrix ViewToClip;
VSMatrix WorldToClip;
DCanvas *RenderTarget = nullptr;
bool RenderingToCanvas = false;
@ -93,8 +93,8 @@ namespace swrenderer
void InitTextureMapping();
void SetupBuffer();
static Mat4f SoftwareWorldToView(const FRenderViewpoint &viewpoint);
static VSMatrix SoftwareWorldToView(const FRenderViewpoint &viewpoint);
Mat4f SoftwareViewToClip();
VSMatrix SoftwareViewToClip();
};
}