HTML and CSS Reference
In-Depth Information
Next you'll add a function to take the DXT data provided by parseDDS and upload it to the GPU, as shown in
Listing 21-5.
Listing 21-5. _uploadDXT Method
// Uploads the compressed DXT data to the GPU.
DXTLoader.prototype._uploadDXT = function(dxtData, width, height, levels, internalFormat,
bytesPerBlock, texture, callback) {
var gl = this.gl;
gl.bindTexture(gl.TEXTURE_2D, texture);
var offset = 0;
// Loop through each mip level of DXT data provided and upload it to the given texture.
for (var i = 0; i < levels; ++i) {
// Determine how big this level of DXT data is in bytes.
var levelSize = dxtLevelSize(width, height, bytesPerBlock);
// Get a view of the bytes for this level of DXT data.
var dxtLevel = new Uint8Array(dxtData.buffer, dxtData.byteOffset + offset, levelSize);
// Upload!
gl.compressedTexImage2D(gl.TEXTURE_2D, i, internalFormat, width, height, 0, dxtLevel);
// The next mip level will be half the height and width of this one.
width = width >> 1;
height = height >> 1;
// Advance the offset into the DXT data past the current mip level's data.
offset += levelSize;
}
// We can't use gl.generateMipmaps with compressed textures, so only use
// mipmapped filtering if the DXT data contained mip levels.
if (levels > 1) {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
} else {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
}
// Notify the user that the texture is ready.
if (callback) { callback(texture, null); }
}
In the case that something fails, you don't want to leave the user with an unrenderable texture or a texture with
old data in it, so you'll also define a function to clear the texture to a 1x1 opaque black pixel in the case of an error
(see Listing 21-6).
Listing 21-6. _clearOnError Method
// When an error occurs set the texture to a 1x1 black pixel
// This prevents WebGL errors from attempting to use unrenderable textures
// and clears out stale data if we're re-using a texture.
DXTLoader.prototype._clearOnError = function(error, texture, callback) {
console.error(error);
 
Search WWH ::




Custom Search