Let's cover an example where we add a texture map to a cube:
- Open the ch07_01_textured-cube.html file in your editor. If you open it in a browser, you should see a scene that looks like the following screenshot:
- Let's load the texture image. At the top of the script block, add a new variable to hold the texture:
let texture;
- At the bottom of the configure function, add the following code, which creates the texture object, loads an image, and sets the image as the texture data. In this case, we'll use a PNG image with the WebGL logo as our texture:
texture = gl.createTexture();
const image = new Image();
image.src = '/common/images/webgl.png';
image.onload = () => {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA,
gl.UNSIGNED_BYTE, image);
gl.texParameteri(gl.TEXTURE_2D...