TINYGL: Added a few utility methods and operators.

This commit is contained in:
Stefano Musumeci 2014-06-02 01:01:26 +01:00
parent b7853b48f8
commit b1b498d336
3 changed files with 48 additions and 43 deletions

View file

@ -40,10 +40,10 @@ void glopLoadMatrix(GLContext *c, GLParam *p) {
q = p + 1; q = p + 1;
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
m->set(0,i, q[0].f); m->set(0,i, q[0].f);
m->set(1,i, q[1].f); m->set(1,i, q[1].f);
m->set(2,i, q[2].f); m->set(2,i, q[2].f);
m->set(3,i, q[3].f); m->set(3,i, q[3].f);
q += 4; q += 4;
} }
@ -69,7 +69,7 @@ void glopMultMatrix(GLContext *c, GLParam *p) {
q += 4; q += 4;
} }
*c->matrix_stack_ptr[c->matrix_mode] = (*c->matrix_stack_ptr[c->matrix_mode]) * m; *c->matrix_stack_ptr[c->matrix_mode] *= m;
gl_matrix_update(c); gl_matrix_update(c);
} }
@ -126,46 +126,45 @@ void glopRotate(GLContext *c, GLParam *p) {
if (u[2] < 0) angle = -angle; if (u[2] < 0) angle = -angle;
m = Matrix4::rotation(angle,2); m = Matrix4::rotation(angle,2);
break; break;
default: default: {
{ float cost, sint;
float cost, sint;
// normalize vector // normalize vector
float len = u[0] * u[0] + u[1] * u[1] + u[2] * u[2]; float len = u[0] * u[0] + u[1] * u[1] + u[2] * u[2];
if (len == 0.0f) if (len == 0.0f)
return; return;
len = 1.0f / sqrt(len); len = 1.0f / sqrt(len);
u[0] *= len; u[0] *= len;
u[1] *= len; u[1] *= len;
u[2] *= len; u[2] *= len;
// store cos and sin values // store cos and sin values
cost = cos(angle); cost = cos(angle);
sint = sin(angle); sint = sin(angle);
// fill in the values // fill in the values
m.set(3,0,0.0f); m.set(3,0,0.0f);
m.set(3,1,0.0f); m.set(3,1,0.0f);
m.set(3,2,0.0f); m.set(3,2,0.0f);
m.set(0,3,0.0f); m.set(0,3,0.0f);
m.set(1,3,0.0f); m.set(1,3,0.0f);
m.set(2,3,0.0f); m.set(2,3,0.0f);
m.set(3,3,1.0f); m.set(3,3,1.0f);
// do the math // do the math
m.set(0,0, u[0] * u[0] + cost * (1 - u[0] * u[0])); 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(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(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(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(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(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(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(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.set(2,2, u[2] * u[2] + cost * (1 - u[2] * u[2]));
} }
} }
*c->matrix_stack_ptr[c->matrix_mode] = (*c->matrix_stack_ptr[c->matrix_mode]) * m; *c->matrix_stack_ptr[c->matrix_mode] *= m;
gl_matrix_update(c); gl_matrix_update(c);
} }
@ -179,7 +178,7 @@ void glopScale(GLContext *c, GLParam *p) {
m.set(1,1,y); m.set(1,1,y);
m.set(2,2,z); m.set(2,2,z);
*c->matrix_stack_ptr[c->matrix_mode] = (*c->matrix_stack_ptr[c->matrix_mode]) * m; *c->matrix_stack_ptr[c->matrix_mode] *= m;
gl_matrix_update(c); gl_matrix_update(c);
} }
@ -192,7 +191,7 @@ void glopTranslate(GLContext *c, GLParam *p) {
m.set(3,1,y); m.set(3,1,y);
m.set(3,2,z); m.set(3,2,z);
*c->matrix_stack_ptr[c->matrix_mode] = (*c->matrix_stack_ptr[c->matrix_mode]) * m; *c->matrix_stack_ptr[c->matrix_mode] *= m;
gl_matrix_update(c); gl_matrix_update(c);
} }
@ -219,7 +218,7 @@ void glopFrustum(GLContext *c, GLParam *p) {
m.set(0,2, 0); m.set(1,2, 0); m.set(2,2, C); m.set(3,2, D); m.set(0,2, 0); m.set(1,2, 0); m.set(2,2, C); m.set(3,2, D);
m.set(0,3, 0); m.set(1,3, 0); m.set(2,3, -1); m.set(3,3, 0); m.set(0,3, 0); m.set(1,3, 0); m.set(2,3, -1); m.set(3,3, 0);
*c->matrix_stack_ptr[c->matrix_mode] = (*c->matrix_stack_ptr[c->matrix_mode]) * m; *c->matrix_stack_ptr[c->matrix_mode] *= m;
gl_matrix_update(c); gl_matrix_update(c);
} }

View file

@ -172,7 +172,6 @@ Vector4::Vector4() {
// Empty constructor, no overhead // Empty constructor, no overhead
} }
Vector4::Vector4(float x, float y, float z, float w) { Vector4::Vector4(float x, float y, float z, float w) {
_v[0] = x; _v[0] = x;
_v[1] = y; _v[1] = y;

View file

@ -214,6 +214,13 @@ public:
return result; return result;
} }
Matrix4& operator*=(const Matrix4 &b)
{
Matrix4 result = *this * b;
*this = result;
return *this;
}
static Matrix4 identity(); static Matrix4 identity();
static Matrix4 rotation(float t, int u); static Matrix4 rotation(float t, int u);