2

I have a simple 2D engine that renders 2D textured quads, and right now I can scale the quad or rotate it, but when I try to translate it I have a strange distortion (the quad is squashed in the half left part of the screen with an infinite perspective effect), here's the code :

private final float quad_vertex[] = {
-0.5f, 0.5f, 0.0f,   // top left
-0.5f, -0.5f, 0.0f,  // bottom left
0.5f, -0.5f, 0.0f,   // bottom right
0.5f,  0.5f, 0.0f    // top right
}; 

final float left = -width/2.0f;
final float right = width/2.0f;
final float bottom = -height/2.0f;
final float top = height/2.0f;
final float near = 0.1f;
final float far = 200.0f;

Matrix.orthoM(projection_matrix, 0, left, right, top, bottom, near, far);
Matrix.setLookAtM(view_matrix, 0, 0, 0, 1.0f, 0.0f, 0f, 0f, 0f, 1.0f, 0.0f);

...

Matrix.setIdentityM(model_matrix, 0);

Matrix.scaleM(model_matrix, 0, scale_width, scale_height, 1.0f);
Matrix.translateM(model_matrix, 0, x, 0, 0);
//Matrix.rotateM(model_matrix, 0, x, 1, 0, 0);
x = x + 1.0f;

Matrix.multiplyMM(viewprojection_matrix, 0, projection_matrix, 0, view_matrix, 0);
Matrix.multiplyMM(modelviewprojection_matrix, 0, viewprojection_matrix, 0, model_matrix, 0);

So, any idea what is the problem ? Thanks in advance :)

user1546493
  • 133
  • 1
  • 1
  • 6
  • For what it's worth, there doesn't appear to be anything wrong with any of the posted code. Your problem likely is elsewhere. Also, a screenshot of the issue might help. – Tim Jul 24 '12 at 00:16
  • Indeed, the problem was in the vertex shader, the above code is correct, thanks. – user1546493 Jul 24 '12 at 15:26

1 Answers1

1

Sounds like a similar problem that I ran into. I was using the tutorial code at http://developer.android.com/training/graphics/opengl/index.html. Changing a line in the shader code from gl_Position = vPosition * uMVPMatrix; to gl_Position = uMVPMatrix * vPosition; fixed the problem.

Matrix multiplication is a non-commutative operation - the order of the operands is important!

zyzof
  • 3,415
  • 1
  • 25
  • 21