1

I have a question regarding transformations in OpenGL ES 2. I'm currently drawing a rectangle using triangle fans as depicted in the image below. The origin is located in its center, while its width and height are 0.6 and 2 respectively. I assume that these sizes are related to the model space. However, in order to maintain the ratio of height and width on a tablet or phone one has to do a projection that considers the proportion of the device lengths (again width and height). This is why I call orthoM(projectionMatrix, 0, -aspectRatio, aspectRatio, -1f, 1f, -1f, 1f);and the aspectRatio is given by float aspectRatio = (float) width / (float) height. This finally leads to the rectangle shown in the image below. Now, I would like to move the rectangle along the x-axis to the border of the screen. However, I was not able to come up with the correct calculation to do so, either I moved it too little or too much. So how would the calculation look like? Furtermore, I'm a little bit confused about the sizes given in the model space. What are the max and min values that can be achieved there?

Thanks a lot!

enter image description here

0xCursor
  • 2,242
  • 4
  • 15
  • 33
Grimfandango
  • 11
  • 1
  • 5

1 Answers1

0

Vertex position of the rectangle are in world space. A way to do this it could be get the screen coordinates you want to move to and then transform them into world space.

For example:

If the screen is 300 x 200 and you are in the center 0,0 in world space (or 150, 100) in screen space). You want to translate to 300.

So the transformation should be screen_position to normalized device coordiantes and then multiply by inverseOf(projection matrix * view matrix) and divided by the w component.

Here it is explained for mouse that it is finally the same, just that you know the z because it is the one you used for your rectangle already (if it is on the plane x,y): OpenGL Math - Projecting Screen space to World space coords.

Alex G. G.
  • 133
  • 1
  • 7
  • Thanks for the answer. I think, I'm still a little bit confused. Somehow I need to know the width of the rectangle in screen space to move it to the border correctly. As far as I know, my current width and height of the rectangle are given in world coordinates, am I right? – Grimfandango Apr 01 '19 at 14:52
  • This is how my vertexArray looks like: `float[] verticesWithTriangles = { // Order of coordinates: X, Y, R, G, B // Triangle Fan 0f, 0f, 1f, 1f, 1f, -x_val, -y_val, 0.7f, 0.7f, 0.7f, x_val, -y_val, 0.7f, 0.7f, 0.7f, x_val, y_val, 0.7f, 0.7f, 0.7f, -x_val, y_val, 0.7f, 0.7f, 0.7f, -x_val, -y_val, 0.7f, 0.7f, 0.7f, }; ` , while x_val = 0.3f and y_val = 1f – Grimfandango Apr 01 '19 at 14:52