admin管理员组文章数量:1429067
In this tutorial: /?p=28 we draw a triangle and a square in the 3d space, and I want to get the vertices' x,y coordinates on the canvas.
So I want to get the 2d coordinates of these vertices: .png
Sorry for my bad english, I hope you understand it.
In this tutorial: http://learningwebgl./blog/?p=28 we draw a triangle and a square in the 3d space, and I want to get the vertices' x,y coordinates on the canvas.
So I want to get the 2d coordinates of these vertices: http://s11.postimage/ig6irk9lf/static.png
Sorry for my bad english, I hope you understand it.
Share Improve this question edited May 27, 2014 at 20:38 brainjam 19k8 gold badges60 silver badges84 bronze badges asked Dec 11, 2011 at 20:13 Danny FoxDanny Fox 40.8k29 gold badges71 silver badges96 bronze badges2 Answers
Reset to default 6You have to do the same calculation that WebGL does. It takes a 3d point [X,Y,Z]
to homogeneous point [x,y,z,w]
via
[x,y,z,w] = pMatrix * mvMatrix * [X,Y,Z,1]
To get clip space coordinates, divide through by w
:
[x/w,y/w,z/w]
x/w
and y/w
are in the range [-1,1]. To convert them to viewport coordinates, scale them according to the canvas size.
[x/w,y/w] -> [(1 + x/w)*canvas.width/2, (1 - y/w)*canvas.height/2]
Note how the 'direction' of the y coordinate changes in the last transformation.
For a little more information, you can Google "graphics pipeline". E.g. http://en.wikipedia/wiki/Graphics_pipeline
You have to do the math to pute them manually. WebGL only putes them for its own purposes, ie: rendering.
Desktop GL has ways of getting those positions back (transform feedback), but WebGL does not.
本文标签: javascriptHow to get the 2d coordinates of webgl verticesStack Overflow
版权声明:本文标题:javascript - How to get the 2d coordinates of webgl vertices? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745532326a2662115.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论