围绕x和z轴的OpenGL旋转错误。你好吗?

时间:2015-09-24 10:35:08

标签: scala opengl matrix rotation

我完全坚持使用OpenGL旋转(再次)。我不确定它究竟是什么,但我有一些理论。

首先让我分享一些代码:

我在这里渲染了三个实体,这些只是在混合器中创建的Ico-Spheres的.obj模型。

val testEntity: Test = new Test(new Vector3(-3.0f,0,0), new Vector3(0,0,0), 0.75f)
val testEntity2: Test = new Test(new Vector3(0,0,0), new Vector3(0,0,0), 0.75f)
val testEntity3: Test = new Test(new Vector3(3.0f,0,0), new Vector3(0,0,0), 0.75f)

override def render(): Unit =
{
  camera.update()
  testEntity.addRotation(new Vector3(1,0,0))
  testEntity.render(camera.getViewProjectionMatrix, camera.getViewMatrix)

  testEntity2.addRotation(new Vector3(0,1,0))
  testEntity2.render(camera.getViewProjectionMatrix, camera.getViewMatrix)

  testEntity3.addRotation(new Vector3(0,0,1))
  testEntity3.render(camera.getViewProjectionMatrix, camera.getViewMatrix)
}

正如你所看到的,我所做的就是将三个球体彼此相邻并试图围绕一个轴旋转。

addRotation(new Vector3(1,0,0))只是向实体的方向添加一个旋转(在开头是(0,0,0)),所以在这种情况下它在x上加1每帧-axis。

理论#1 - 模型矩阵的计算错误

这是我为每个实体计算模型矩阵的方法

protected def calculateModelMatrix(position: Vector3, orientation: Vector3, scale: Float): Matrix4 =
{
    val matrix: Matrix4 = Matrix4.Identity

    matrix.scale(new Vector3(scale, scale, scale))
    matrix.rotate(new Vector3(1,0,0), Math.toRadians(orientation.x).toFloat)
    matrix.rotate(new Vector3(0,1,0), Math.toRadians(orientation.y).toFloat)
    matrix.rotate(new Vector3(0,0,1), Math.toRadians(orientation.z).toFloat)
    matrix.translate(position)

    matrix
}

旋转应始终在原点发生,但Identity矩阵会处理此问题。 首先我们进行缩放,然后我们进行旋转,然后将实体转换到正确的位置。

Matrix4-class的rotate - 方法如下所示:

def rotate(axis: Vector3, angle: Float): Unit =
{
    if(angle == 0) return

    axis match
    {
      case Vector3(1,0,0) => rotateX(angle)
      case Vector3(0,1,0) => rotateY(angle)
      case Vector3(0,0,1) => rotateZ(angle)
      case _ => //println("Rotation not yet implemented!")
    }
}

private def rotateX(angle: Float): Unit =
{
    val cos: Float = Math.cos(angle).toFloat
    val sin: Float = Math.sin(angle).toFloat

    this.m11 = cos
    this.m12 = -sin
    this.m21 = sin
    this.m22 = cos
}

private def rotateY(angle: Float): Unit =
{
    val cos: Float = Math.cos(angle).toFloat
    val sin: Float = Math.sin(angle).toFloat

    this.m00 = cos
    this.m02 = sin
    this.m20 = -sin
    this.m22 = cos
}

private def rotateZ(angle: Float): Unit =
{
    val cos: Float = Math.cos(angle).toFloat
    val sin: Float = Math.sin(angle).toFloat

    this.m00 = cos
    this.m01 = -sin
    this.m10 = sin
    this.m12 = cos
}

对于帮助方法,我严格遵守数学(或至少尝试过)(参考:http://inside.mines.edu/fs_home/gmurray/ArbitraryAxisRotation/https://en.wikipedia.org/wiki/Rotation_matrix

理论#2 - 投影和/或视图矩阵的计算错误

对于每个帧,我调用camera.update()。它应该根据相机的位置更新我的视图和projectionMatrix:

def update(): Unit =
{
    updateViewMatrix()
    updateViewProjectionMatrix()
}   

private def calculateViewProjectionMatrix(): Matrix4 =
{
    Matrix4.multiply(this.projectionMatrix, this.viewMatrix)
}

private def updateViewProjectionMatrix(): Unit =
{
    this.viewProjectionMatrix = calculateViewProjectionMatrix()
}

private def calculateProjectionMatrix(): Matrix4 =
{
    val aspectRatio: Float = 1024 / 768 // TODO get this from somewhere
    //  val yScale: Float = ((1.0f / Math.tan(Math.toRadians(FOV / 2f))) * aspectRatio).toFloat
    val yScale: Float = (1.0f / Math.tan(Math.toRadians(FOV / 2f))).toFloat
    val xScale: Float = yScale / aspectRatio
    val frustumLength = FAR_PLANE - NEAR_PLANE

    val matrix: Matrix4 = Matrix4.Zero
    matrix.m00 = xScale
    matrix.m11 = yScale
    matrix.m22 = -((FAR_PLANE + NEAR_PLANE) / frustumLength)
    matrix.m23 = -1.0f
    matrix.m32 = -((2.0f * NEAR_PLANE * FAR_PLANE) / frustumLength)

    matrix
}

private def calculateViewMatrix(): Matrix4 =
{
    val matrix: Matrix4 = Matrix4.Identity
    matrix.rotate(new Vector3(1,0,0), Math.toRadians(this.pitch).toFloat)
    matrix.rotate(new Vector3(0,1,0), Math.toRadians(this.yaw).toFloat)
    //  matrix.rotate(new Vector3(0,0,1), Math.toRadians(this.roll).toFloat)
    matrix.translate(new Vector3(-this.position.x, -this.position.y, -this.position.z))

    matrix
}

结果如下:

enter image description here

基本绝对垃圾!围绕y轴(在中间)的一次旋转是唯一一个在旋转时不平移的旋转。它仍然看起来有点压缩/捣碎(或者这只是我?!)和绕z旋转的那个?它完全关闭了!

现在我还尝试从https://github.com/LWJGL/lwjgl/blob/master/src/java/org/lwjgl/util/vector/Matrix4f.java

重现rotate

在代码中看起来像这样:

def rotate(axis: Vector3, angle: Float): Unit =
{
    val cosAngle: Float = Math.cos(angle).toFloat
    val sinAngle: Float = Math.sin(angle).toFloat

    val oneMinusCosAngle: Float = 1.0f - cosAngle

    val xy: Float = axis.x * axis.y
    val xz: Float = axis.x * axis.z

    val yz: Float = axis.y * axis.z

    val xs: Float = axis.x * sinAngle
    val ys: Float = axis.y * sinAngle
    val zs: Float = axis.z * sinAngle

    val f00: Float = axis.x * axis.x * oneMinusCosAngle + cosAngle
    val f01: Float = xy * oneMinusCosAngle + zs
    val f02: Float = xz * oneMinusCosAngle - ys

    val f10: Float = xy * oneMinusCosAngle - zs
    val f11: Float = axis.y * axis.y * oneMinusCosAngle + cosAngle
    val f12: Float = yz * oneMinusCosAngle + xs

    val f20: Float = xz * oneMinusCosAngle + ys
    val f21: Float = yz * oneMinusCosAngle - xs
    val f22: Float = axis.z * axis.z * oneMinusCosAngle + cosAngle

    val t00: Float = this.m00 * f00 + this.m10 * f01 + this.m20 * f02
    val t01: Float = this.m01 * f00 + this.m11 * f01 + this.m21 * f02
    val t02: Float = this.m02 * f00 + this.m12 * f01 + this.m22 * f02
    val t03: Float = this.m03 * f00 + this.m13 * f01 + this.m23 * f02
    val t10: Float = this.m00 * f10 + this.m10 * f11 + this.m20 * f12
    val t11: Float = this.m01 * f10 + this.m11 * f11 + this.m21 * f12
    val t12: Float = this.m02 * f10 + this.m12 * f11 + this.m22 * f12
    val t13: Float = this.m03 * f10 + this.m13 * f11 + this.m23 * f12

    this.m20 = this.m00 * f20 + this.m10 * f21 + this.m20 * f22
    this.m21 = this.m01 * f20 + this.m11 * f21 + this.m21 * f22
    this.m22 = this.m02 * f20 + this.m12 * f21 + this.m22 * f22
    this.m23 = this.m03 * f20 + this.m13 * f21 + this.m23 * f22

    this.m00 = t00
    this.m01 = t01
    this.m02 = t02
    this.m03 = t03

    this.m10 = t10
    this.m11 = t11
    this.m12 = t12
    this.m13 = t13
}

这就是结果:

enter image description here

它看起来比我自己的实现更好,但是对于x轴和z轴,实体在旋转时进行平移。

我无法理解为什么会出现这种情况并且我现在调试了很长时间,但没有真正有用。我所做的事情一定是错的。

后来我想切换到Quaternions,但我想,如果基本轮换不起作用,那么实施Quaternions并不是一个好主意。

1 个答案:

答案 0 :(得分:0)

所以,根据@BDL,有两个错误,我(有点)修复了它们:

  1. 翻译时旋转 -Bug
  2. 问题是.obj文件没有居中于原点,所以我在Blender的帮助下解决了这个问题。当它居中时不再发生翻译。愚蠢的错误!

    1. 实际的旋转错误
    2. lwjgl-implementation中没有错误。我修复1.后一切正常。但我也想修复自己的实现,所以再次感谢BDL我知道我必须繁殖我的矩阵。

      对于X和Y,这已经有效,因为Z不太好,但基本上我做的是:

      protected def calculateModelMatrix(position: Vector3, orientation: Vector3, scale: Float): Matrix4 =
      {
          val matrix: Matrix4 = Matrix4.Identity
      
          matrix.scale(new Vector3(scale, scale, scale))
          matrix.rotate(new Vector3(1,0,0), Math.toRadians(orientation.x).toFloat)
          val rotXYMatrix: Matrix4 = Matrix4.multiply(Matrix4.rotate(new Vector3(0,1,0), Math.toRadians(orientation.y).toFloat), matrix)
          val rotXYZMatrix: Matrix4 = Matrix4.multiply(Matrix4.rotate(new Vector3(0,0,1), Math.toRadians(orientation.z).toFloat), rotXYMatrix)
          rotXYZMatrix.translate(position)
      
          rotXYZMatrix
      }
      

      不确定为什么它对z不起作用,但这是(半)工作结果:

      enter image description here

      编辑:现在修复:

      是一个错误:

      private def rotateZ(angle: Float): Unit =
        {
          val cos: Float = Math.cos(angle).toFloat
          val sin: Float = Math.sin(angle).toFloat
      
          this.m00 = cos
          this.m01 = -sin
          this.m10 = sin
          this.m12 = cos
        }
      

      当然不是this.m12 = cos而是this.m11 = cos。按预期工作!

相关问题