- Hands-On Game Development with WebAssembly
- Rick Battagline
- 402字
- 2021-06-24 13:41:04
WebGL coordinate system versus 2D canvas
With WebGL, the center of the canvas element is the origin point (0,0). Positive Y is up, whereas Positive X is to the right. This is a bit more intuitive for someone who has never worked with 2D graphics, as it is similar to quadrants in coordinate geometry, which we learned about in grade school. With the 2D canvas, you are always working with pixels, and there are no negative numbers that appear on the canvas:

When you called drawImage, the X and Y coordinates were where the top left corner of the image would draw. WebGL is a bit different. Everything is using geometry, and both a vertex and a pixel shader are required. We convert the image into a texture and then stretch it over the geometry so that it's displayed. Here is what the WebGL coordinate system looks like:

If you want to place an image at a specific pixel location on the canvas, you have to know the width and height of your canvas. The center point of your canvas is (0,0), the Top left corner is (-1, 1), and the Bottom right corner is (1, -1). So, if you want to place an image at x=150, y=160 you need to use the following equation to find the WebGL x coordinate:
webgl_x = (pixel_x - canvas_width / 2) / (canvas_width / 2)
So, for a pixel_x position of 150, we have to subtract 400 from 150 to get -250. Then, we have to divide -250 by 400, and we would get -0.625. We have to do something similar to get the y coordinate for WebGL, but the sign of the axes are flipped, so instead of what we did for the pixel_x value, we need to do the following:
((canvas_height / 2) - pixel_y) / (canvas_height / 2)
By plugging in the values, we get ((600 / 2) - 160) / (600 / 2) or (300 - 160) / 300 = 0.47.
- 24小時(shí)學(xué)會(huì)電腦組裝與維護(hù)
- Augmented Reality with Kinect
- 數(shù)字道路技術(shù)架構(gòu)與建設(shè)指南
- 硬件產(chǎn)品經(jīng)理成長(zhǎng)手記(全彩)
- Learning Stencyl 3.x Game Development Beginner's Guide
- 筆記本電腦維修300問(wèn)
- 筆記本電腦使用、維護(hù)與故障排除從入門(mén)到精通(第5版)
- 電腦高級(jí)維修及故障排除實(shí)戰(zhàn)
- 筆記本電腦維修實(shí)踐教程
- VMware Workstation:No Experience Necessary
- Spring Security 3.x Cookbook
- USB應(yīng)用分析精粹:從設(shè)備硬件、固件到主機(jī)端程序設(shè)計(jì)
- 微服務(wù)實(shí)戰(zhàn)(Dubbox +Spring Boot+Docker)
- Instant Website Touch Integration
- 嵌入式系統(tǒng)設(shè)計(jì)大學(xué)教程(第2版)