I would like to rotate view matrix (I supposed to think this represents camera position).
I already rotate view matrix about Y-axis by using Matrix4f#rotateY successfully.
However, about rotateX this requires the target position at (0,0,0) center of the world, I would like to rotate the view matrix around a specified position.
I tried to rotate the matrix like following:
targetCenter = Vector3f(x, y, z)
viewMatrix
.translate(targetCenter)
.rotateX(rotation)
.translate(targetCenter.mul(-1.0f))
However, this make the rendered model twice. Is this the correct method to rotate view matrix around specified axis ?
EDIT
- I've implemented camera with Kotlin according to Draykoon D's answer, thank you
- I'm using JOML as a library to manipulate Matrix
fun Matrix4f.orbitBy(modelCenter: Vector3f, angleY: Float, angleX: Float, focalLength: Float) {
// Setup both camera's view matrices
val modelCenterCp = Vector3f(modelCenter)
val recenter = Matrix4f().translate(modelCenterCp.mul(-1.0f)) //not needed if world origin
val rotation = Matrix4f()
.rotateY(angleY)
.rotateX(angleX)
val moveBack = Matrix4f().translate(modelCenter) //not needed if world origin
val transfer = moveBack.mul(rotation).mul(recenter) //order from right to left
val eye = Vector3f(0f, modelCenter.y, -focalLength).mulProject(transfer)
val up = Vector3f(0f, 1f, 0f)
this.setLookAt(eye, modelCenter, up)
}