gluLookAt
is not available in OpenGLES 2. OpenGLES 2 is also using floats not doubles.
Taking the description on the gluLookAt
man-page, I have implemented it like this:
void graphics_t::set_mvp_look_at(const vec_t& eye,const vec_t& centre,const vec_t& up) {
#if !defined(GL2) && !defined(NDEBUG)
matrix_t check;
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(pimpl->mvp.f);
gluLookAt(eye.x,eye.y,eye.z,centre.x,centre.y,centre.z,up.x,up.y,up.z);
glGetFloatv(GL_PROJECTION_MATRIX,check.f);
#endif
const vec_t f((centre-eye).normalised()),
s(f.cross(up.normalised())),
u(s.cross(f));
const matrix_t m = {{
s.x, s.y, s.z, 0,
u.x, u.y, u.z, 0,
-f.x, -f.y, -f.z, 0,
0, 0, 0, 1
}};
set_mvp_matrix(matrix_t::translation(-eye).transposition() * (m.transposition() * pimpl->mvp)); // setter notifies shader
#if !defined(GL2) && !defined(NDEBUG)
if(pimpl->mvp != check)
std::cout << "WARNING! Error setting look-up: " << std::endl << pimpl->mvp << std::endl << " != " << std::endl << check << std::endl;
#endif
}
As you can see I have some inline unit-testing when building for a non-OpenGLES 2 target (that's my own GL2 define).
Now the code seems to work, although it feels somehow slightly wrong depth but I can't put my fingers on that.
However, the code is printing warnings all the time, e.g.:
WARNING! Error setting look-up: matrix_t<[4.79627,0,0.424102,-261.019],[0.243815,5.79255,-2.75737,51.5329],[0.158943,-0.862347,-1.79752,269.079],[0.0794715,-0.431174,-0.898762,350.119]> != matrix_t<[5.31579,0,0.470039,-289.291],[0.270225,6.41998,-3.05604,57.1149],[0.158943,-0.862347,-1.79752,269.079],[0.0794715,-0.431174,-0.898762,350.119]>
As you can see, the numbers are very nearly the same but not-quite.
My comparision function for my matrix class is fuzzy with if(fabs(f[i]-rhs.f[i])>0.001) return false;
.
Is this error within expected bounds when using float precision instead of doubles? Or is there a bigger bug in my translation?
normalise()
call that I mistakenly introduced. I hope you now understand why! :-) – sam hocevar Dec 17 '11 at 13:01