opengl - Make Camera Look at point using a world transformation matrix? -
variants of question might have been asked on site, none of answers found worked in case.
i trying make camera @ point. camera has world transformation matrix, used calculate front vector, eye position when making view matrix finally. camera treated other object in scene , has world transform. want able modify world transform, , should automatically affect camera. want modify world transform of camera "lookat" point.
for this, modify rotation term of world transform (r in t * s * r) this:
void worldtransform::setlookat( float x, float y, float z ) { vec3 lookpoint = vec3 ( x, y, z ); vec3 lookdir = normalize( lookpoint - m_position ); float looklengthonxz = sqrtf( lookdir.z*lookdir.z + lookdir.x*lookdir.x ); m_rotationx = degrees(atan2f( lookdir.y, looklengthonxz )); m_rotationy = degrees(atan2f( lookdir.x, lookdir.z )); rotateabs( m_rotationx, m_rotationy, m_rotationz ); updatematlocal( ); }
basically, have function can set rotation (rotateabs()
), calculate pitch, yaw , roll angles , pass them there.
while calculating view matrix, do:
void camera::update( ) { if (m_ptransform.m_pointer == nullptr) return; vec3& position = m_ptransform->getposition( ); m_look = m_ptransform->getforward( ); m_lookat = position + m_look; //calculate new matrix m_view = glm::lookat( position, m_lookat, glm::vec3( 0.0f, 1.f, 0.0f )); }
this sort of works, camera starts deviating should looking @ after while. doing wrong?
create view "look at" matrix camera @ point "eye", looking "at", i.e. mine looks this:
matrix4x4<t> & lookat(vec3<t> const & eye, vec3<t> const & at, vec3<t> const & up) { auto d = vec3<t>(); auto u = vec3<t>(); auto r = vec3<t>(); d = unit(at - eye); u = unit(up); r = unit(cross(d, u)); u = cross(r, d); v[0] = r[0]; v[1] = u[0]; v[2] = -d[0]; v[3] = 0; v[4] = r[1]; v[5] = u[1]; v[6] = -d[1]; v[7] = 0; v[8] = r[2]; v[9] = u[2]; v[10] = -d[2]; v[11] = 0; v[12] = 0; v[13] = 0; v[14] = 0; v[15] = 1; return operator *= (math::translate<t>(-eye[0], -eye[1], -eye[2])); }
. won't need world matrix move eye
. or did misunderstand question?
Comments
Post a Comment