TINYGL: Done some more optimization on maths code.

This commit is contained in:
Stefano Musumeci 2014-06-08 15:20:27 +02:00
parent 5642d195e0
commit d45c455be4
7 changed files with 94 additions and 91 deletions

View file

@ -45,10 +45,10 @@ void tglGetFloatv(int pname, float *v) {
mnr++;
case TGL_MODELVIEW_MATRIX: {
for (i = 0; i < 4; i++) {
*v++ = c->matrix_stack_ptr[mnr]->get(i,0);
*v++ = c->matrix_stack_ptr[mnr]->get(i,1);
*v++ = c->matrix_stack_ptr[mnr]->get(i,2);
*v++ = c->matrix_stack_ptr[mnr]->get(i,3);
*v++ = c->matrix_stack_ptr[mnr]->_m[i][0];
*v++ = c->matrix_stack_ptr[mnr]->_m[i][1];
*v++ = c->matrix_stack_ptr[mnr]->_m[i][2];
*v++ = c->matrix_stack_ptr[mnr]->_m[i][3];
}
}
break;

View file

@ -79,14 +79,18 @@ void glopLight(GLContext *c, GLParam *p) {
l->position = pos;
if (l->position.W == 0) {
l->norm_position = pos.toVector3();
l->norm_position.X = pos.X;
l->norm_position.Y = pos.Y;
l->norm_position.Z = pos.Z;
l->norm_position.normalize();
}
}
break;
case TGL_SPOT_DIRECTION:
l->spot_direction = v.toVector3();
l->norm_spot_direction = v.toVector3();
l->spot_direction.X = v.X;
l->spot_direction.Y = v.Y;
l->spot_direction.Z = v.Z;
l->norm_spot_direction = l->spot_direction;
l->norm_spot_direction.normalize();
break;
case TGL_SPOT_EXPONENT:
@ -193,7 +197,9 @@ void gl_shade_vertex(GLContext *c, GLVertex *v) {
if (l->position.W == 0) {
// light at infinity
d = l->position.toVector3();
d.X = l->position.X;
d.Y = l->position.Y;
d.Z = l->position.Z;
att = 1;
} else {
// distance attenuation
@ -237,7 +243,9 @@ void gl_shade_vertex(GLContext *c, GLVertex *v) {
if (c->local_light_model) {
Vector3 vcoord;
vcoord = v->ec.toVector3();
vcoord.X = v->ec.X;
vcoord.Y = v->ec.Y;
vcoord.Z = v->ec.Z;
vcoord.normalize();
s.X = (d.X - vcoord.X);
s.Y = (d.Y - vcoord.X);
@ -280,7 +288,10 @@ void gl_shade_vertex(GLContext *c, GLVertex *v) {
B += att * lB;
}
v->color = Vector4(clampf(R, 0, 1), clampf(G, 0, 1), clampf(B, 0, 1), A);
v->color.X = clampf(R, 0, 1);
v->color.Y = clampf(G, 0, 1);
v->color.Z = clampf(B, 0, 1);
v->color.W = clampf(A, 0, 1);
}
} // end of namespace TinyGL

View file

@ -40,10 +40,10 @@ void glopLoadMatrix(GLContext *c, GLParam *p) {
q = p + 1;
for (int i = 0; i < 4; i++) {
m->set(0, i, q[0].f);
m->set(1, i, q[1].f);
m->set(2, i, q[2].f);
m->set(3, i, q[3].f);
m->_m[0][i] = q[0].f;
m->_m[1][i] = q[1].f;
m->_m[2][i] = q[2].f;
m->_m[3][i] = q[3].f;
q += 4;
}
@ -51,8 +51,7 @@ void glopLoadMatrix(GLContext *c, GLParam *p) {
}
void glopLoadIdentity(GLContext *c, GLParam *) {
*c->matrix_stack_ptr[c->matrix_mode] = Matrix4::identity();
c->matrix_stack_ptr[c->matrix_mode]->identity();
gl_matrix_update(c);
}
@ -62,10 +61,10 @@ void glopMultMatrix(GLContext *c, GLParam *p) {
q = p + 1;
for (int i = 0; i < 4; i++) {
m.set(0, i, q[0].f);
m.set(1, i, q[1].f);
m.set(2, i, q[2].f);
m.set(3, i, q[3].f);
m._m[0][i] = q[0].f;
m._m[1][i] = q[1].f;
m._m[2][i] = q[2].f;
m._m[3][i] = q[3].f;
q += 4;
}
@ -112,19 +111,19 @@ void glopRotate(GLContext *c, GLParam *p) {
switch (dir_code) {
case 0:
m = Matrix4::identity();
m.identity();
break;
case 4:
if (u[0] < 0) angle = -angle;
m = Matrix4::rotation(angle, 0);
m.rotation(angle, 0);
break;
case 2:
if (u[1] < 0) angle = -angle;
m = Matrix4::rotation(angle, 1);
m.rotation(angle, 1);
break;
case 1:
if (u[2] < 0) angle = -angle;
m = Matrix4::rotation(angle, 2);
m.rotation(angle, 2);
break;
default: {
float cost, sint;
@ -143,24 +142,23 @@ void glopRotate(GLContext *c, GLParam *p) {
sint = sin(angle);
// fill in the values
m.set(3,0,0.0f);
m.set(3,1,0.0f);
m.set(3,2,0.0f);
m.set(0,3,0.0f);
m.set(1,3,0.0f);
m.set(2,3,0.0f);
m.set(3,3,1.0f);
m._m[3][0] = 0.0f;
m._m[3][2] = 0.0f;
m._m[0][3] = 0.0f;
m._m[1][3] = 0.0f;
m._m[2][3] = 0.0f;
m._m[3][3] = 1.0f;
// do the math
m.set(0,0, u[0] * u[0] + cost * (1 - u[0] * u[0]));
m.set(1,0, u[0] * u[1] * (1 -cost) - u[2] * sint);
m.set(2,0, u[2] * u[0] * (1 -cost) + u[1] * sint);
m.set(0,1, u[0] * u[1] * (1 -cost) + u[2] * sint);
m.set(1,1, u[1] * u[1] + cost * (1 - u[1] * u[1]));
m.set(2,1, u[1] * u[2] * (1 - cost) - u[0] * sint);
m.set(0,2, u[2] * u[0] * (1 - cost) - u[1] * sint);
m.set(1,2, u[1] * u[2] * (1 - cost) + u[0] * sint);
m.set(2,2, u[2] * u[2] + cost * (1 - u[2] * u[2]));
m._m[0][0] = u[0] * u[0] + cost * (1 - u[0] * u[0]);
m._m[1][0] = u[0] * u[1] * (1 -cost) - u[2] * sint;
m._m[2][0] = u[2] * u[0] * (1 -cost) + u[1] * sint;
m._m[0][1] = u[0] * u[1] * (1 -cost) + u[2] * sint;
m._m[1][1] = u[1] * u[1] + cost * (1 - u[1] * u[1]);
m._m[2][1] = u[1] * u[2] * (1 - cost) - u[0] * sint;
m._m[0][2] = u[2] * u[0] * (1 - cost) - u[1] * sint;
m._m[1][2] = u[1] * u[2] * (1 - cost) + u[0] * sint;
m._m[2][2] = u[2] * u[2] + cost * (1 - u[2] * u[2]);
}
}

View file

@ -4,11 +4,17 @@
namespace TinyGL {
void glopNormal(GLContext *c, GLParam *p) {
c->current_normal = Vector4(p[1].f, p[2].f, p[3].f, 0.0f);
c->current_normal.X = p[1].f;
c->current_normal.Y = p[2].f;
c->current_normal.Z = p[3].f;
c->current_normal.W = 0.0f;
}
void glopTexCoord(GLContext *c, GLParam *p) {
c->current_tex_coord = Vector4(p[1].f, p[2].f, p[3].f, p[4].f);
c->current_tex_coord.X = p[1].f;
c->current_tex_coord.Y = p[2].f;
c->current_tex_coord.Z = p[3].f;
c->current_tex_coord.W = p[4].f;
}
void glopEdgeFlag(GLContext *c, GLParam *p) {
@ -16,7 +22,10 @@ void glopEdgeFlag(GLContext *c, GLParam *p) {
}
void glopColor(GLContext *c, GLParam *p) {
c->current_color = Vector4(p[1].f, p[2].f, p[3].f, p[4].f);
c->current_color.X = p[1].f;
c->current_color.Y = p[2].f;
c->current_color.Z = p[3].f;
c->current_color.W = p[4].f;
c->longcurrent_color[0] = p[5].ui;
c->longcurrent_color[1] = p[6].ui;
c->longcurrent_color[2] = p[7].ui;
@ -71,7 +80,7 @@ void glopBegin(GLContext *c, GLParam *p) {
c->matrix_model_projection = (*c->matrix_stack_ptr[1]) * (*c->matrix_stack_ptr[0]);
// test to accelerate computation
c->matrix_model_projection_no_w_transform = 0;
if (c->matrix_model_projection.get(3,0) == 0.0 && c->matrix_model_projection.get(3,1) == 0.0 && c->matrix_model_projection.get(3,2) == 0.0)
if (c->matrix_model_projection._m[3][0] == 0.0 && c->matrix_model_projection._m[3][1] == 0.0 && c->matrix_model_projection._m[3][2] == 0.0)
c->matrix_model_projection_no_w_transform = 1;
}
@ -143,7 +152,7 @@ static inline void gl_vertex_transform(GLContext *c, GLVertex *v) {
m = &c->matrix_model_view_inv;
n = &c->current_normal;
v->normal = m->transform3x3(n->toVector3());
v->normal = m->transform3x3(*n);
if (c->normalize_enabled) {
v->normal.normalize();
@ -155,7 +164,7 @@ static inline void gl_vertex_transform(GLContext *c, GLVertex *v) {
v->pc = m->transform3x4(v->coord);
if (c->matrix_model_projection_no_w_transform) {
v->pc.W = (m->get(3,3));
v->pc.W = (m->_m[3][3]);
}
}
@ -189,7 +198,10 @@ void glopVertex(GLContext *c, GLParam *p) {
v = &c->vertex[n];
n++;
v->coord = Vector4(p[1].f, p[2].f, p[3].f, p[4].f);
v->coord.X = p[1].f;
v->coord.Y = p[2].f;
v->coord.Z = p[3].f;
v->coord.W = p[4].f;
gl_vertex_transform(c, v);

View file

@ -1,4 +1,3 @@
#include "common/scummsys.h"
#include "graphics/tinygl/zmath.h"
@ -166,17 +165,16 @@ Vector4::Vector4(const Vector3 &vec, float w) {
_v[3] = w;
}
TinyGL::Matrix4 Matrix4::identity() {
Matrix4 a;
a.fill(0);
a.set(0, 0, 1.0f);
a.set(1, 1, 1.0f);
a.set(2, 2, 1.0f);
a.set(3, 3, 1.0f);
return a;
void Matrix4::identity()
{
memset(_m,0,sizeof(_m));
_m[0][0] = 1.0f;
_m[1][1] = 1.0f;
_m[2][2] = 1.0f;
_m[3][3] = 1.0f;
}
TinyGL::Matrix4 Matrix4::transpose() const {
Matrix4 Matrix4::transpose() const {
Matrix4 a;
a._m[0][0] = this->_m[0][0];
@ -233,7 +231,7 @@ Matrix4 Matrix4::inverseOrtho() const {
float s;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
a.set(i, j, this->_m[j][i]);
a._m[i][j] = this->_m[j][i];
a._m[3][0] = 0.0f;
a._m[3][1] = 0.0f;
a._m[3][2] = 0.0f;
@ -256,8 +254,8 @@ Matrix4 Matrix4::inverse() const {
return result;
}
Matrix4 Matrix4::rotation(float t, int u) {
Matrix4 a = identity();
void Matrix4::rotation(float t, int u) {
identity();
float s, c;
int v, w;
@ -267,12 +265,10 @@ Matrix4 Matrix4::rotation(float t, int u) {
w = 0;
s = sin(t);
c = cos(t);
a._m[v][v] = c;
a._m[v][w] = -s;
a._m[w][v] = s;
a._m[w][w] = c;
return a;
_m[v][v] = c;
_m[v][w] = -s;
_m[w][v] = s;
_m[w][w] = c;
}
bool Matrix4::IsIdentity() const {

View file

@ -124,10 +124,6 @@ public:
return *this;
}
Vector3 toVector3() const {
return Vector3(_v[0],_v[1],_v[2]);
}
float _v[4];
};
@ -184,27 +180,10 @@ public:
return *this;
}
static Matrix4 identity();
static Matrix4 rotation(float t, int u);
void fill(float val) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
_m[i][j] = val;
}
}
}
inline void set(int x,int y,float val) {
_m[x][y] = val;
}
inline float get(int x, int y) const {
return _m[x][y];
}
void scale(float x, float y, float z);
void translate(float x, float y, float z);
void identity();
void rotation(float t, int);
void invert();
void transpose();
@ -230,6 +209,14 @@ public:
vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2]);
}
// Transform the vector as if this were a 3x3 matrix.
inline Vector3 transform3x3(const Vector4 &vector) const {
return Vector3(
vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2],
vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2],
vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2]);
}
// Transform the vector as if this were a 3x4 matrix.
inline Vector4 transform3x4(const Vector4 &vector) const {
return Vector4(
@ -247,7 +234,6 @@ public:
vector.X * _m[3][0] + vector.Y * _m[3][1] + vector.Z * _m[3][2] + vector.W * _m[3][3]);
}
private:
float _m[4][4];
};