- Hands-On Game Development with WebAssembly
- Rick Battagline
- 352字
- 2021-06-24 13:41:05
WebGL global variables
The first thing we are going to do is create a lot of JavaScript global variables. If this code were meant for more than demonstration, using this many global variables is generally frowned upon and considered bad practice. But for what we are doing right now, it helps simplify things:
<script type='text/javascript'>
var gl = null; // WebGLRenderingContext
var program = null; // WebGLProgram
var texture = null; // WebGLTexture
var img = null; // HTMLImageElement
var canvas = null;
var image_width = 0;
var image_height = 0;
var vertex_texture_buffer = null; // WebGLBuffer
var a_texcoord_location = null; // GLint
var a_position_location = null; // GLint
var u_translate_location = null; // WebGLUniformLocation
var u_texture_location = null; // WebGLUniformLocation
The first variable, gl, is the new version of the rendering context. Typically, if you are using a 2D rendering context, you call it ctx, and if you are using a WebGL rendering context, you name it gl. The second line defines the program variable. When we compile the vertex and fragment shaders, we get a compiled version in the form of a WebGLProgram object stored inside of this program variable. The texture variable will hold a WebGLTexture that we will be loading from the spaceship.png image file. That is the image that we used in the previous chapter for the 2D canvas tutorial. The img variable will be used to load the spaceship.png image file that will be used to load the texture. The canvas variable will once again be a reference to our HTML canvas element and image_width, and image_height will hold the height and width of the spaceship.png image once it is loaded.
The vertex_texture_buffer attribute is a buffer that will be used to transfer vertex geometry and texture data to the GPU so that the shader we wrote in the previous section can use it. The a_texcoord_location and a_position_location variables will be used to hold references to the a_texcoord and a_position attribute variables in the vertex shader, and finally, u_translate_location and u_texture_location are used to reference the u_translate and u_texture uniform variables in the shader.
- 觸摸屏實用技術(shù)與工程應(yīng)用
- 電腦常見問題與故障排除
- 電腦組裝與維修從入門到精通(第2版)
- Linux運維之道(第2版)
- 硬件產(chǎn)品經(jīng)理成長手記(全彩)
- OUYA Game Development by Example
- 計算機組裝維修與外設(shè)配置(高等職業(yè)院校教改示范教材·計算機系列)
- Blender Quick Start Guide
- Wireframing Essentials
- 新編電腦組裝與硬件維修從入門到精通
- Spring Cloud實戰(zhàn)
- Building Machine Learning Systems with Python
- The Deep Learning with PyTorch Workshop
- 施耐德M241/251可編程序控制器應(yīng)用技術(shù)
- MicroPython Cookbook