4
4

More than 5 years have passed since last update.

OpenGL ESでオブジェクトを動かす

Posted at

iOSのOpenGL ESでオブジェクトを動かしたいときに、行列演算をどうやるといいか、いろいろ試して、一番よさげな方法を見つけた
まず必要な行列は3つ

matrixes.m
    _projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);
    _baseViewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0.0f, 0.0f, _nearness);
    _modelViewMatrix = GLKMatrix4Multiply(GLKMatrix4Identity, _baseViewMatrix);
  • _projectionMatrix 視体積などの設定
  • _baseMatrix カメラとの相対位置
  • _modelViewMatrix 視点座標での物体の位置 これらのMatrixをupdateで更新していくとよい。その時にMatrixを保持していくのではなく、位置、角度をfloatとして持っておく。 updateでは一回一回初期化して、行列を作り直すときれいに描画ができた。
update.m
- (void)update{
    _baseViewMatrix = GLKMatrix4Translate(GLKMatrix4Identity, 0.0f, 0.0f, _nearness);
    _modelViewMatrix = GLKMatrix4Rotate(_baseViewMatrix, _angle, 1.0f, 0.0f, 0.0f);
    _modelViewMatrix = GLKMatrix4Rotate(_modelViewMatrix, _rotation, 0.0f, 1.0f, 0.0f);
}
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4