SKJ
SKJ

Reputation: 396

Scale and rotate in OpenGL ES 2.0

Is there a way to include aspect ratio correction without using matrices in OpenGL ES?

I am writing a simple shader to rotate a texture.

void main()  
{  
    mat2 rotX = mat2(cosA, sinA, -sinA, cosA);  
    vec4 a_pos = a_position;
    a_pos.xy = a_position.xy * rotZ;  
    gl_Position = a_pos;  
}

But the problem is that the image is getting skewed when rotating.
In normal openGL, we use something like gluPerspective(fov, (float)windowWidth/(float)windowHeight, zNear, zFar);
How do i do the same with shaders?
Note: I'd prefer not using a matrix.

Upvotes: 2

Views: 3966

Answers (3)

dirhem
dirhem

Reputation: 621

Just change the matrix multiplication order for rotation:

a_pos.xy = rotZ * a_position.xy;

Upvotes: 0

Yuriy Vikulov
Yuriy Vikulov

Reputation: 2499

You can manually translate a code of GluPerspective to shader: http://www.opengl.org/wiki/GluPerspective_code

But it is not efficient to calcuelte this matrix for each vertex. So you can manually calculate it for your device screen. Look at these posts

Upvotes: 0

ZZZ
ZZZ

Reputation: 788

Include aspect ratio fix in geometry of rendered object? I did so in my font rendering tool, position of verts in each rect is corrected by aspect ratio of screen, and yes i know that its better and easier do use matrix fix but i didnt know it when i was writing this tool, and it works fine:)

Upvotes: 2

Related Questions