Skip to content

Commit ec8b131

Browse files
committed
maths: fix ambiguous Lua to Matrix cast
1 parent cf715ab commit ec8b131

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/maths/Matrix.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,42 @@
2121

2222
#include <functional>
2323
#include "Vector.h"
24+
#include <Debug.h>
25+
#ifdef LUA_CLASS
26+
#include <Lua.h>
27+
#endif
28+
29+
using namespace std;
2430

2531
class Matrix
2632
{
2733
public:
2834
Matrix( int w = 4, int h = 4 );
2935
Matrix( const Matrix& other );
3036
Matrix( const vector<float>& vec, bool asColumn = true );
37+
#ifdef LUA_CLASS
38+
explicit Matrix( const LuaValue& v ) {
39+
if ( v.type() == LuaValue::Table ) {
40+
try {
41+
auto rows = std::vector<LuaValue>( v );
42+
const int32_t nRows = rows.size();
43+
const int32_t nColumns = std::vector<LuaValue>( rows[0] ).size();
44+
mWidth = nColumns;
45+
mHeight = nRows;
46+
m = (float*)malloc( sizeof(float) * mWidth * mHeight );
47+
Identity();
48+
for ( int32_t y = 0; y < mHeight; y++ ) {
49+
auto row = std::vector<LuaValue>( rows[y] );
50+
for ( int32_t x = 0; x < mWidth; x++ ) {
51+
m[ y * mWidth + x ] = row[x].toNumber();
52+
}
53+
}
54+
} catch ( std::exception& e ) {
55+
gError() << "Failed to load Lua matrix (" << v.serialize() << ")";
56+
}
57+
}
58+
}
59+
#endif
3160
virtual ~Matrix();
3261

3362
void Orthogonal( float left, float right, float bottom, float top, float zNear, float zFar );

lib/maths/Vector.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef VECTOR_H
2020
#define VECTOR_H
2121

22+
#include <vector>
2223
#include <cmath>
2324
#include <ostream>
2425
#ifdef LUA_CLASS

0 commit comments

Comments
 (0)