From dde16a9dd6bb480fadfa7e62d8c886e20c22fdc7 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 18 Feb 2011 00:09:24 +0000 Subject: [PATCH] Implement +=, -= and *= for matrices and vectors. Add new types, closer to the GLSL ones. --- src/matrix.h | 48 ++++++++++++++++++++++++++++++++++++++++++------ src/scene.cpp | 8 ++++---- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/matrix.h b/src/matrix.h index aec3e077..cb3c10f0 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -26,6 +26,12 @@ for (int n = 0; n < elems; n++) \ ret[n] = (*this)[n] op val[n]; \ return ret; \ + } \ + \ + template \ + inline Vec##elems operator op##=(Vec##elems const &val) \ + { \ + return *this = (*this) op val; \ } #define BOOL_OP(elems, op, ret) \ @@ -44,6 +50,11 @@ for (int n = 0; n < elems; n++) \ ret[n] = (*this)[n] op val; \ return ret; \ + } \ + \ + inline Vec##elems operator op##=(T const &val) \ + { \ + return *this = (*this) op val; \ } #define CAST_OP(elems, dest) \ @@ -114,8 +125,8 @@ template struct Vec2 union { T y; T b; T j; }; }; -typedef Vec2 float2; -typedef Vec2 int2; +typedef Vec2 vec2; +typedef Vec2 vec2i; template struct Vec3 { @@ -130,8 +141,8 @@ template struct Vec3 union { T z; T c; T k; }; }; -typedef Vec3 float3; -typedef Vec3 int3; +typedef Vec3 vec3; +typedef Vec3 vec3i; template struct Vec4 { @@ -147,8 +158,8 @@ template struct Vec4 union { T w; T d; T l; }; }; -typedef Vec4 float4; -typedef Vec4 int4; +typedef Vec4 vec4; +typedef Vec4 vec4i; #define SCALAR_GLOBAL(elems, op, U) \ template \ @@ -210,6 +221,11 @@ template struct Mat4 return ret; } + inline Mat4 operator +=(Mat4 const val) + { + return *this = *this + val; + } + inline Mat4 operator -(Mat4 const val) const { Mat4 ret; @@ -219,6 +235,11 @@ template struct Mat4 return ret; } + inline Mat4 operator -=(Mat4 const val) + { + return *this = *this - val; + } + inline Mat4 operator *(Mat4 const val) const { Mat4 ret; @@ -233,6 +254,11 @@ template struct Mat4 return ret; } + inline Mat4 operator *=(Mat4 const val) + { + return *this = *this * val; + } + inline Vec4 operator *(Vec4 const val) const { Vec4 ret; @@ -249,6 +275,16 @@ template struct Mat4 Vec4 v[4]; }; +typedef Mat4 mat4; +typedef Mat4 mat4i; + +/* Aliases for deprecated stuff */ +typedef Vec2 float2; +typedef Vec2 int2; +typedef Vec3 float3; +typedef Vec3 int3; +typedef Vec4 float4; +typedef Vec4 int4; typedef Mat4 float4x4; typedef Mat4 int4x4; diff --git a/src/scene.cpp b/src/scene.cpp index 4b45ceb3..d60d7550 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -137,14 +137,14 @@ void Scene::Render() // XXX: rename to Blit() // XXX: debug stuff model_matrix = float4x4::translate(320.0f, 240.0f, 0.0f); - model_matrix = model_matrix * float4x4::rotate(-data->angle, 1.0f, 0.0f, 0.0f); + model_matrix *= float4x4::rotate(-data->angle, 1.0f, 0.0f, 0.0f); #if 0 static float f = 0.0f; f += 0.01f; - model_matrix = model_matrix * float4x4::rotate(0.1f * sinf(f), 1.0f, 0.0f, 0.0f); - model_matrix = model_matrix * float4x4::rotate(0.3f * cosf(f), 0.0f, 0.0f, 1.0f); + model_matrix *= float4x4::rotate(0.1f * sinf(f), 1.0f, 0.0f, 0.0f); + model_matrix *= float4x4::rotate(0.3f * cosf(f), 0.0f, 0.0f, 1.0f); #endif - model_matrix = model_matrix * float4x4::translate(-320.0f, -240.0f, 0.0f); + model_matrix *= float4x4::translate(-320.0f, -240.0f, 0.0f); // XXX: end of debug stuff #if LOL_EXPERIMENTAL