Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL ES 2.0 Camera Issues

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.

like image 486
Matt Avatar asked Jan 17 '12 07:01

Matt


People also ask

What is OpenGL ES 2. 0?

OpenGL is a cross-platform graphics API that specifies a standard software interface for 3D graphics processing hardware. OpenGL ES is a flavor of the OpenGL specification intended for embedded devices.

What does OpenGL ES 3.0 mean?

OpenGL ES is the dominant graphics API in today's smartphones and has even extended its reach onto the desktop. The list of platforms supporting OpenGL ES includes iOS, Android, BlackBerry, bada, Linux, and Windows. OpenGL ES also underpins WebGL, a web standard for browser-based 3D graphics.


1 Answers

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;
like image 199
Mārtiņš Možeiko Avatar answered Nov 15 '22 01:11

Mārtiņš Možeiko