TINYGL: Changed the way vertices are transformed, increased performance (now it's comparable to the previous version and sometimes it performs even faster).

This commit is contained in:
Stefano Musumeci 2014-06-09 19:53:40 +02:00
parent 6c7eaca2eb
commit f40cfb020c
4 changed files with 74 additions and 78 deletions

View File

@ -74,7 +74,8 @@ void glopLight(GLContext *c, GLParam *p) {
l->specular = v;
break;
case TGL_POSITION: {
Vector4 pos = c->matrix_stack_ptr[0]->transform(v);
Vector4 pos;
c->matrix_stack_ptr[0]->transform(v, pos);
l->position = pos;

View File

@ -49,13 +49,13 @@ void gl_eval_viewport(GLContext *c) {
v = &c->viewport;
v->trans.X = ((float)(((v->xsize - 0.5) / 2.0) + v->xmin));
v->trans.Y = ((float)(((v->ysize - 0.5) / 2.0) + v->ymin));
v->trans.Z = ((float)(((zsize - 0.5) / 2.0) + ((1 << ZB_POINT_Z_FRAC_BITS)) / 2));
v->trans.X = (float)(((v->xsize - 0.5) / 2.0) + v->xmin);
v->trans.Y = (float)(((v->ysize - 0.5) / 2.0) + v->ymin);
v->trans.Z = (float)(((zsize - 0.5) / 2.0) + ((1 << ZB_POINT_Z_FRAC_BITS)) / 2);
v->scale.X = ((float)((v->xsize - 0.5) / 2.0));
v->scale.Y = ((float)(-(v->ysize - 0.5) / 2.0));
v->scale.Z = ((float)(-((zsize - 0.5) / 2.0)));
v->scale.X = (float)((v->xsize - 0.5) / 2.0);
v->scale.Y = (float)(-(v->ysize - 0.5) / 2.0);
v->scale.Z = (float)(-((zsize - 0.5) / 2.0));
}
void glopBegin(GLContext *c, GLParam *p) {
@ -135,16 +135,16 @@ static inline void gl_vertex_transform(GLContext *c, GLVertex *v) {
// eye coordinates needed for lighting
m = c->matrix_stack_ptr[0];
v->ec = m->transform3x4(v->coord);
m->transform3x4(v->coord,v->ec);
// projection coordinates
m = c->matrix_stack_ptr[1];
v->pc = m->transform(v->ec);
m->transform(v->ec, v->pc);
m = &c->matrix_model_view_inv;
n = &c->current_normal;
v->normal = m->transform3x3(*n);
m->transform3x3(*n, v->normal);
if (c->normalize_enabled) {
v->normal.normalize();
@ -154,7 +154,7 @@ static inline void gl_vertex_transform(GLContext *c, GLVertex *v) {
// NOTE: W = 1 is assumed
m = &c->matrix_model_projection;
v->pc = m->transform3x4(v->coord);
m->transform3x4(v->coord, v->pc);
if (c->matrix_model_projection_no_w_transform) {
v->pc.W = (m->_m[3][3]);
}
@ -209,7 +209,7 @@ void glopVertex(GLContext *c, GLParam *p) {
if (c->texture_2d_enabled) {
if (c->apply_texture_matrix) {
v->tex_coord = c->matrix_stack_ptr[2]->transform(c->current_tex_coord);
c->matrix_stack_ptr[2]->transform(c->current_tex_coord, v->tex_coord);
} else {
v->tex_coord = c->current_tex_coord;
}

View File

@ -137,19 +137,19 @@ int Matrix_Inv(float *r, float *m, int n) {
void Vector3::normalize() {
float n;
n = sqrt(_v[0] * _v[0] + _v[1] * _v[1] + _v[2] * _v[2]);
n = sqrt(X * X + Y * Y + Z * Z);
if (n != 0) {
_v[0] /= n;
_v[1] /= n;
_v[2] /= n;
X /= n;
Y /= n;
Z /= n;
}
}
Vector4::Vector4(const Vector3 &vec, float w) {
_v[0] = vec.X;
_v[1] = vec.Y;
_v[2] = vec.Z;
_v[3] = w;
X = vec.X;
Y = vec.Y;
Z = vec.Z;
W = w;
}
void Matrix4::identity()

View File

@ -31,39 +31,39 @@ public:
}
Vector3 operator-() const {
return Vector3(-_v[0],-_v[1],-_v[2]);
return Vector3(-X,-Y,-Z);
}
Vector3 operator*(float factor) const {
return Vector3(_v[0] * factor, _v[1] * factor, _v[2] * factor);
return Vector3(X * factor, Y * factor, Z * factor);
}
Vector3 operator+(const Vector3 &other) const {
return Vector3(_v[0] + other._v[0], _v[1] + other._v[1], _v[2] + other._v[2]);
return Vector3(X + other.X, Y + other.Y, Z + other.Z);
}
Vector3 operator-(const Vector3 &other) const {
return Vector3(_v[0] - other._v[0], _v[1] - other._v[1], _v[2] - other._v[2]);
return Vector3(X - other.X, Y - other.Y, Z - other.Z);
}
Vector3 &operator*=(float factor) {
_v[0] *= factor;
_v[1] *= factor;
_v[2] *= factor;
X *= factor;
Y *= factor;
Z *= factor;
return *this;
}
Vector3 &operator+=(float value) {
_v[0] += value;
_v[1] += value;
_v[2] += value;
X += value;
Y += value;
Z += value;
return *this;
}
Vector3 &operator-=(float value) {
_v[0] -= value;
_v[1] -= value;
_v[2] -= value;
X -= value;
Y -= value;
Z -= value;
return *this;
}
@ -83,50 +83,50 @@ public:
}
bool operator==(const Vector4 &other) const {
return _v[0] == other._v[0] && _v[1] == other._v[1] && _v[2] == other._v[2] && _v[3] == other._v[3];
return X == other.X && Y == other.Y && Z == other.Z && W == other.W;
}
bool operator!=(const Vector4 &other) const {
return _v[0] != other._v[0] || _v[1] != other._v[1] || _v[2] != other._v[2] || _v[3] != other._v[3];
return X != other.X || Y != other.Y || Z != other.Z || W != other.W;
}
Vector4 operator-() const {
return Vector4(-_v[0],-_v[1],-_v[2],-_v[3]);
return Vector4(-X,-Y,-Z,-W);
}
Vector4 operator*(float factor) const {
return Vector4(_v[0] * factor, _v[1] * factor, _v[2] * factor,_v[3] * factor);
return Vector4(X * factor, Y * factor, Z * factor,W * factor);
}
Vector4 operator+(const Vector4 &other) const {
return Vector4(_v[0] + other._v[0], _v[1] + other._v[1], _v[2] + other._v[2], _v[3] + other._v[3]);
return Vector4(X + other.X, Y + other.Y, Z + other.Z, W + other.W);
}
Vector4 operator-(const Vector4 &other) const {
return Vector4(_v[0] - other._v[0], _v[1] - other._v[1], _v[2] - other._v[2], _v[3] - other._v[3]);
return Vector4(X - other.X, Y - other.Y, Z - other.Z, W - other.W);
}
Vector4 &operator*=(float factor) {
_v[0] *= factor;
_v[1] *= factor;
_v[2] *= factor;
_v[3] *= factor;
X *= factor;
Y *= factor;
Z *= factor;
W *= factor;
return *this;
}
Vector4 &operator+=(float value) {
_v[0] += value;
_v[1] += value;
_v[2] += value;
_v[3] += value;
X += value;
Y += value;
Z += value;
W += value;
return *this;
}
Vector4 &operator-=(float value) {
_v[0] -= value;
_v[1] -= value;
_v[2] -= value;
_v[3] -= value;
X -= value;
Y -= value;
Z -= value;
W -= value;
return *this;
}
@ -200,44 +200,39 @@ public:
static Matrix4 frustrum(float left, float right, float bottom, float top, float nearp, float farp);
inline Vector3 transform(const Vector3 &vector) const {
return Vector3(
vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + _m[0][3],
vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + _m[1][3],
vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + _m[2][3]);
inline void transform(const Vector3 &vector, Vector3 &out) const {
out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + _m[0][3];
out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + _m[1][3];
out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + _m[2][3];
}
// Transform the vector as if this were a 3x3 matrix.
inline Vector3 transform3x3(const Vector3 &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]);
inline void transform3x3(const Vector3 &vector, Vector3 &out) const {
out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2];
out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2];
out.Z = 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]);
inline void transform3x3(const Vector4 &vector, Vector3 &out) const {
out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2];
out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2];
out.Z = 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(
vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + _m[0][3],
vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + _m[1][3],
vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + _m[2][3],
vector.X * _m[3][0] + vector.Y * _m[3][1] + vector.Z * _m[3][2] + _m[3][3]);
inline void transform3x4(const Vector4 &vector, Vector4 &out) const {
out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + _m[0][3];
out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + _m[1][3];
out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + _m[2][3];
out.W = vector.X * _m[3][0] + vector.Y * _m[3][1] + vector.Z * _m[3][2] + _m[3][3];
}
inline Vector4 transform(const Vector4 &vector) const {
return Vector4(
vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + vector.W * _m[0][3],
vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + vector.W * _m[1][3],
vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + vector.W * _m[2][3],
vector.X * _m[3][0] + vector.Y * _m[3][1] + vector.Z * _m[3][2] + vector.W * _m[3][3]);
inline void transform(const Vector4 &vector, Vector4 &out) const {
out.X = vector.X * _m[0][0] + vector.Y * _m[0][1] + vector.Z * _m[0][2] + vector.W * _m[0][3];
out.Y = vector.X * _m[1][0] + vector.Y * _m[1][1] + vector.Z * _m[1][2] + vector.W * _m[1][3];
out.Z = vector.X * _m[2][0] + vector.Y * _m[2][1] + vector.Z * _m[2][2] + vector.W * _m[2][3];
out.W = vector.X * _m[3][0] + vector.Y * _m[3][1] + vector.Z * _m[3][2] + vector.W * _m[3][3];
}
float _m[4][4];