実現したいこと
こんにちは。
最近OpenGL(OpenTK)の勉強を始めたので、勘違いしている点など多いかもしれませんが、
数時間試行錯誤しても解決しなかったのので質問させてください。
OpenGLを使用して赤色の四角形を描画したのですが、
カメラの位置と注視点を変更して描画すると、意図しない結果となります。
どこの指定が間違っているのか自分では判断が付かないので、
教えていただきたいです。
該当のソースコード
バーテックスシェーダー:
GLSL
1#version 460 2 3uniform mat4 viewMatrix; 4uniform mat4 projMatrix; 5uniform mat4 modelMatrix; 6 7layout (location = 0) in vec3 position; 8layout (location = 1) in vec4 color; 9 10out vec4 vColor; 11 12void main() 13{ 14 gl_Position = modelMatrix * viewMatrix * projMatrix * vec4(position, 1.0); 15 vColor = color; 16}
フラグメントシェーダー:
GLSL
1#version 460 2 3precision mediump float; 4 5in vec4 vColor; 6out vec4 fragColor; 7 8void main() 9{ 10 fragColor = vColor; 11}
C#
1//行列を設定 2public void LoadMatrices() 3{ 4 //射影行列 5 _projMatrix = Matrix4.CreatePerspectiveOffCenter(-Width / 2, Width / 2, -Height / 2, Height / 2, 0.5f, 600f); 6 //ビュー行列 7 //位置を100, 0, 130, 注視点を100, 0, 0に 8 _viewMatrix = Matrix4.LookAt( 100, 0, 130, 100, 0, 0, 0, 1, 0 ); 9 //モデル行列 10 _modelMatrix = Matrix4.Identity; 11 //128倍に拡大 12 _modelMatrix.M11 = 128; 13 _modelMatrix.M22 = 128; 14 _modelMatrix.M33 = 128; 15} 16 17protected override void OnRenderFrame( FrameEventArgs e ) 18{ 19 GL.Clear( ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit ); 20 21 //四角形の頂点 22 float[] vertices = new float[] 23 { 24 -128, 128, 0, 25 -128, -128, 0, 26 128, 128, 0, 27 128, -128, 0 28 } 29 30 //すべての頂点を赤色 31 float[] colors = new float[] 32 { 33 1f, 0f, 0f, 1f, 34 1f, 0f, 0f, 1f, 35 1f, 0f, 0f, 1f, 36 1f, 0f, 0f, 1f 37 } 38 39 //インデックス 40 short[] indices = new short[] 41 { 42 0, 1, 2, 3 43 } 44 45 //GL.UniformMatrix4( int location, bool transpose, ref Matrix4 matrix ) 46 GL.UniformMatrix4( GL.GetUniformLocation( _program, "viewMatrix" ), true, ref _viewMatrix ); 47 GL.UniformMatrix4( GL.GetUniformLocation( _program, "projMatrix" ), true, ref _projMatrix ); 48 GL.UniformMatrix4( GL.GetUniformLocation( _program, "modelMatrix" ), true, ref _modelMatrix ); 49 50 GL.EnableVertexAttribArray(0); 51 GL.EnableVertexAttribArray(1); 52 53 GL.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, 0, colors); 54 GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, vertices); 55 56 //描画 57 GL.DrawElements( PrimitiveType.TriangleStrip, 4, DrawElementsType.UnsignedShort, indices ); 58 59 GL.DisableVertexAttribArray(0); 60 GL.DisableVertexAttribArray(1); 61 62 GL.Flush(); 63 SwapBuffers(); 64}
上記のコードにて、Matrix4.LookAt
の引数に0, 0, 130, 0, 0, 0, 0, 1, 0
(位置を0, 0, 130、注視点を0, 0, 0)
を指定すると、画面の中心に赤い四角形が表示されるのですが、コードにある通り、
100, 0, 130, 100, 0, 0, 0, 1, 0
を指定すると、意図している結果としては、四角形が画面の左に向かって
平行移動したような結果なのですが、原点を注視点に設定しているような結果が得られます。
0, 0, 130から原点を注視点に描画した結果:
100, 0, 130から100, 0, 0を注視点に描画した結果:
補足情報(FW/ツールのバージョンなど)
・OpenGLのバージョン: 4.6.0
・GPU: NVIDIA GeForce GTX 1650
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/03/28 08:24 編集
2022/03/28 08:22
2022/03/28 08:33 編集
2022/03/28 08:55
2022/03/28 09:59
2022/03/28 22:41