问题
I'm working with Android and OpenGL ES 2.0 and I am having an issue that I can't really formulate into a solid question. In the image, http://i.imgur.com/XuCHF.png, I basically have a shape to represent a ship in the middle and when it's moved to the side it gets stretched toward the vanishing point. What I am wanting to accomplish is to have the ship maintain most of its shape when it is moved. I believe it may be due to my matrices but every resource I've looked seems to use the same method.
//Setting up the projection matrix
final float ratio = (float) width / height;
final float left = -ratio;
final float right = ratio;
final float bottom = -1.0f;
final float top = 1.0f;
final float near = 1.0f;
final float far = 1000.0f;
Matrix.frustumM(projection_matrix, 0, left, right, bottom, top, near, far);
//Setting the view matrix
Matrix.setLookAtM(view_matrix, 0, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f);
//Setting the model matrix
Matrix.setIdentityM(model_matrix, 0);
Matrix.translateM(model_matrix, 0, 2f, 0f, 0f);
//Setting the model-view-projection matrix
Matrix.multiplyMM(mvp_matrix, 0, view_matrix, 0, model_matrix, 0);
Matrix.multiplyMM(mvp_matrix, 0, GL.projection_matrix, 0, mvp_matrix, 0);
GLES20.glUniformMatrix4fv(mvp_matrix_location, 1, false, mvp_matrix, 0);
The shaders are very basic as well:
private final static String vertex_shader =
"uniform mat4 u_mvp_matrix;"
+ "attribute vec4 a_position;"
+ "void main()"
+ "{"
+ " gl_Position = u_mvp_matrix * a_position;"
+ "}";
private final static String fragment_shader =
"precision mediump float;"
+ "void main()"
+ "{"
+ " gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);"
+ "}";
Any thoughts/insight is greatly appreciated.
Thanks.
回答1:
That is normal - that is how perspective projection should look like. Although in your case it looks really stretched one - with wide field of view.
Try using instead of frustumM
method perspectiveM
(projection_matrix, 0, 45.0f, ratio, near, far).
Or if you must use frustumM
, calculate left/right/bottom/top like this:
float fov = 60; // degrees, try also 45, or different number if you like
top = tan(fov * PI / 360.0f) * near;
bottom = -top;
left = ratio * bottom;
right = ratio * top;
回答2:
If you don't want any perspective effect at all, then use Matrix.orthoM instead of Matrix.frustumM.
To just make the perspective effect less extreme, you need to reduce the field of view -- That is, increase near
or bring top
and bottom
closer to zero. (You probably want right = top * ratio
, and similarly with left
if you're going to fiddle with top
and bottom
values.)
来源:https://stackoverflow.com/questions/8891289/opengl-es-2-0-camera-issues