WebGL texture mapping and animation

Next, we include texture coordinates, enabling us to send the vertices with a texture map to the shader. Texture mapping is just pasting an image onto a 3D mesh, which is similar to pasting wallpaper onto a wall. Nearly all 3D objects are drawn using texture maps because you can achieve a higher level of realism instead of just solid colors.

Engage thrusters

In initShaders(), a new code passes an array that contains texture coordinates (which are in pairs for the vertical and horizontal directions) and there is one texture coordinate for every vertex. The texture map image is passed to the shader as a sampler, as presented in the following code:

shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, ...

Get WebGL HOTSHOT now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.