opengl es - How to maintain WebGL Canvas aspect ratio? -
i have canvas i'm drawing via webgl. canvas sized: 640 width x 480 height.
im drawing simple square in middle. surprised find when drawn looks little stretched horizontally.
what need make square proper (no stretching)? note have not played viewport settings. im going thru webgl tutorials.
left edge of canvas has x = -1.0, right has x = +1.0, top edge has y = 1.0 , bottom edge has coordinate of y = -1.0.
in words, coordinates normalized across viewport (xy, z-coord handled different). such coordinates in clipspace.
the ratio between width , height of viewport known aspect ratio. aspect ratio used when constructing projection matrix.
when don't use projection matrix transform coordinates clipspace, directly providing coordinates will, depending on size of viewport, scaled accordingly.
find more details projecton matrices here , here.
to solve problem, divide x-coord aspect ratio.
Comments
Post a Comment