Reputation: 7549
I'm writing my first opengl es 2.0 app and I'm confused about the purpose of Matrix.setLookAt.
I've got some code running that draws some objects with the center of my view set to 0,0 but I want to pan the view so that the center is at a different position in world coordinates e.g 5,5.
What do I need to pass to in glOrtho and setLookAt to achieve this? If I set the center of the view to 0,0 and draw lines across the viewport, that works fine. If I set the center to 0.25, 0 then the lines appear on the left as in the screendump below. Those lines should extend across the viewport.
What am I doing wrong?
My code: Shader code:
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";
Effective onDraw code (this is actually spread over several places)
// 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 is drawing lines with x:y coordinates:
Line from -0.126474:-0.250000 to 0.626474:-0.250000
Line from -0.126471:0.000000 to 0.626471:0.000000
Line from -0.126474:0.250000 to 0.626474:0.250000
Upvotes: 1
Views: 1598
Reputation: 1
You don't use look-at for orthor matrix. It's for perspective matrix
You can even don't call the Matrix.orthoM. If that, opengl world is (-1, 1) width and (-1, 1) height. if you draw line from (-0.25, 0) to (0.25, 0), it will be at the center of the screen and half width of the screen. (assuming your view port is 0,0,screenWidth,screenHeight)
you only need one matrix to translate,rotate and scale and pass it to shader.
Upvotes: 0
Reputation: 3518
That's expected behaviour.
The projection matrix takes a portion of space and maps it to the NDC cube. For orthographic ones, this is a just a block (no frustum). The block the code defines spans [-0.126471, 0.626471][-0.5, 0.5][-1, 7]
. If it wouldn't alter the source space, the lines are drawn exactly from left to right.
Now consider what the setLookAtM
does: By convention, after the modelview has been applied, the viewer is at [0,0,0]
and looks in direction [0,0,-1]
. The viewer is stepping aside for 0.25
in x direction (try to conceive this; it's the same as moving all lines to the left). If you wanted to have the lines drawn as before, the code'd have to move their x coordinates also (+0.25
).
Read here for a technical explanation of gluLookat
; it's effectively a translation followed by a rotation. Be sure to checkout OpenGL's spaces on songho or elsewhere.
Upvotes: 2