私は最初の opengl es 2.0 アプリを書いていますが、Matrix.setLookAt の目的について混乱しています。
ビューの中心を 0,0 に設定していくつかのオブジェクトを描画するコードを実行していますが、ビューをパンして、中心がワールド座標 (5,5 など) の別の位置になるようにしたいと考えています。
これを達成するには、glOrtho と setLookAt で何を渡す必要がありますか? ビューの中心を 0,0 に設定し、ビューポート全体に線を引くと、問題なく動作します。中心を 0.25, 0 に設定すると、下のスクリーンダンプのように線が左側に表示されます。これらの線は、ビューポート全体に拡張する必要があります。
私は何を間違っていますか?
私のコード: シェーダー コード:
private static final String vertexShader =
"uniform mat4 uMVPMatrix;\n"
+ "attribute vec4 aPosition;\n"
+ "attribute vec4 aColor;\n"
+ "varying vec4 vColor;\n"
+ "vec4 temp;"
+ "void main() {\n"
+ " temp = aPosition;\n"
+ " gl_Position = uMVPMatrix * temp;\n"
+ " vColor = aColor;\n"
+ "}\n";
private static final String fragmentShader =
"precision mediump float;\n"
+ "varying vec4 vColor;\n"
+ "void main() {\n"
+ " gl_FragColor = vColor;\n"
+ "}\n";
効果的な onDraw コード (これは実際には複数の場所に分散されています)
// center of view should be at 0.25, 0. Height of view 1.0, width determined by ratio of viewport
Matrix.orthoM(mProjMatrix, 0, -0.126471, 0.626471, -0.5, 0.5, -1, 7);
Matrix.setLookAtM(mVMatrix, 0, 0.25, 0.0, -5, 0.25, 0.0, 0f, 0f, 1.0f, 0.0f);
Matrix.setIdentityM(mMMatrix, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mVMatrix, 0, mMMatrix, 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(MVPMatrixHandle, 1, false, MVPMatrix, 0);
GLES20.glVertexAttrib4f(aColorHandle, 0f, 0f, 0f, 1f); // constant color
lineVertices.position(0);
GLES20.glVertexAttribPointer(aPositionHandle, 3, GLES20.GL_FLOAT, false, 0, lineVertices);
GLES20.glEnableVertexAttribArray(aPositionHandle);
GLES20.glDrawArrays(GLES20.GL_LINES, 0, lineCount*2);
LineVertices は x:y 座標で線を描画しています:
-0.126474:-0.250000 から 0.626474:-0.250000 までの行
-0.126471:0.000000 から 0.626471:0.000000 までの行
-0.126474:0.250000 から 0.626474:0.250000 までの行