Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a smooth transition between two different camera view in opengl

gluLookAt is defined as follows

void gluLookAt(GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ,
               GLdouble centerX, GLdouble centerY, GLdouble centerZ,
               GLdouble upX, GLdouble upY, GLdouble upZ
              );

I have two different cameras parameters corresponding to gluLookAt,I am confused about how to implement a smooth transition between views of these two camera parameters.

I hope that somebody can give me some cue or some code example.

like image 498
user3219735 Avatar asked Nov 26 '25 05:11

user3219735


1 Answers

I would consider using Spherical Linear Interpolation (slerp) on the rotations produced by gluLookAt (...). The GLM math library (C++) provides everything you need for this, including an implementation of LookAt.

Very roughly, this is what a GLM-based implementation might look like:

// Create quaternions from the rotation matrices produced by glm::lookAt
glm::quat quat_start (glm::lookAt (eye_start, center_start, up_start));
glm::quat quat_end   (glm::lookAt (eye_end,   center_end,   up_end));

// Interpolate half way from original view to the new.
float interp_factor = 0.5; // 0.0 == original, 1.0 == new

// First interpolate the rotation
glm::quat  quat_interp = glm::slerp (quat_start, quat_end, interp_factor);

// Then interpolate the translation
glm::vec3  pos_interp  = glm::mix   (eye_start,  eye_end,  interp_factor);

glm::mat4  view_matrix = glm::mat4_cast (quat_interp); // Setup rotation
view_matrix [3]        = glm::vec4 (pos_interp, 1.0);  // Introduce translation
like image 92
Andon M. Coleman Avatar answered Nov 28 '25 21:11

Andon M. Coleman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!