- Hands-On Game Development with WebAssembly
- Rick Battagline
- 198字
- 2021-06-24 13:41:05
The ShipPosition function
If this were production quality code, I would be doing a lot of the work that I am currently doing inside of the initialization routine in this rendering function. Moving sprites around independently on the canvas would require updates to our array buffers. I probably wouldn't define my geometry in the way I did, that is, calculating the sizes by hand. I am not currently making any changes to the array buffer or the texture buffer; I am trying to keep this code to the bare minimum necessary to render a sprite onto the canvas using WebGL. Here is what I have:
function ShipPosition( ship_x, ship_y ) {
if( image_width == 0 ) {
return;
}
gl.uniform4fv(u_translate_location, [ship_x, ship_y, 0.0, 0.0]);
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
- The first few lines check to see whether the image download has completed. If not, we will exit out of this function:
if( image_width == 0 ) {
return;
}
- Next, we tell WebGL to load the uniform u_translate uniform variable with our spaceship's coordinates:
gl.uniform4fv(u_translate_location, [ship_x, ship_y, 0.0, 0.0]);
- Finally, we instruct WebGL to draw triangles with the six vertices in our array buffer:
gl.drawArrays(gl.TRIANGLES, 0, 6);
推薦閱讀
- 用“芯”探核:龍芯派開發(fā)實(shí)戰(zhàn)
- Learning Cocos2d-x Game Development
- 電腦組裝與維修從入門到精通(第2版)
- Creating Dynamic UI with Android Fragments
- The Applied AI and Natural Language Processing Workshop
- Large Scale Machine Learning with Python
- VCD、DVD原理與維修
- 計(jì)算機(jī)組裝與維護(hù)(第3版)
- Spring Cloud微服務(wù)架構(gòu)實(shí)戰(zhàn)
- 單片機(jī)系統(tǒng)設(shè)計(jì)與開發(fā)教程
- Intel Edison智能硬件開發(fā)指南:基于Yocto Project
- Hands-On Artificial Intelligence for Banking
- LPC1100系列處理器原理及應(yīng)用
- Spring Cloud微服務(wù)和分布式系統(tǒng)實(shí)踐
- Building Machine Learning Systems with Python