Im trying to implement a function to calculate a view matrix similar to gluLookAt, and it works fine if i define Mat4 as GLfloat[16] and Vec3 as GLfloat[3], but as soon as i use GLdouble or long double i get wrong results.
Until the call to S_Translate in GetViewMatrix all values are always the same, but then somehow m[13] gets calculated in a wrong way although x, y, z, m[1], m[5] and m[9] are always the same no matter if im using float or double.
Here is the relevant part of my program:
<div class=“ubbcode-block”><div class=“ubbcode-header”>Click to reveal… <input type=“button” class=“form-button” value=“Show me!” onclick=“toggle_spoiler(this, ‘Yikes, my eyes!’, ‘Show me!’)” />]<div style=“display: none;”>
void GetViewMatrix(Mat4& m, Vec3 camPos, Vec3 lookAt) {
Vec3 dir, right, up;
up[0] = 0.0;
up[1] = 1.0;
up[2] = 0.0;
dir[0] = lookAt[0] - camPos[0];
dir[1] = lookAt[1] - camPos[1];
dir[2] = lookAt[2] - camPos[2];
normalize(dir);
crossProduct(right, dir, up);
normalize(right);
crossProduct(up, right, dir);
normalize(up);
m[0] = right[0];
m[4] = right[1];
m[8] = right[2];
m[12] = 0.0;
m[1] = up[0];
m[5] = up[1];
m[9] = up[2];
m[13] = 0.0;
m[2] = -dir[0];
m[6] = -dir[1];
m[10] = -dir[2];
m[14] = 0.0;
m[3] = m[7] = m[11] = 0.0;
m[15] = 1.0;
S_Translate(m, -camPos[0], -camPos[1], -camPos[2]);
}
void S_Translate(Mat4& m, D3Float x, D3Float y, D3Float z) {
cout << "sizeof(D3Float): " << sizeof(D3Float) << " sizeof(Mat4): " << sizeof(Mat4) << endl;
cout << "sizeof(GLfloat): " << sizeof(GLfloat) << " sizeof(GLdouble): " << sizeof(GLdouble) << endl;
cout << "x: " << x << " y: " << y << " z: " << z << endl;
cout << "m[1]: " << m[1] << " m[5]: " << m[5] << " m[9]: " << m[9] << endl;
m[12] += x*m[0] + y*m[4] + z*m[8];
m[13] += x*m[1] + y*m[5] + z*m[9];
m[14] += x*m[2] + y*m[6] + z*m[10];
cout << "Result: " << endl
<< " m[12]: " << m[12] << endl
<< " m[13]: " << m[13] << endl
<< " m[14]: " << m[14] << endl;
}
[/QUOTE]
Output (GLfloat):
sizeof(D3Float): 4 sizeof(Mat4): 64
sizeof(GLfloat): 4 sizeof(GLdouble): 8
x: -2 y: -3 z: -5
m[1]: -0.180743 m[5]: 0.873589 m[9]: -0.451856
Result:
m[12]: 0
m[13]: 2.38419e-07
m[14]: -6.16441
Output (GLdouble):
sizeof(D3Float): 8 sizeof(Mat4): 128
sizeof(GLfloat): 4 sizeof(GLdouble): 8
x: -2 y: -3 z: -5
m[1]: -0.180743 m[5]: 0.873589 m[9]: -0.451856
Result:
m[12]: 0
m[13]: -4.44089e-16
m[14]: -6.16441
I calculated the view matrix like it is described at http://pyopengl.sourceforge.net/documentation/manual/gluLookAt.3G.html