Game Development Reference
In-Depth Information
In this function, we initialize our texture object and set the texture parameters.
We make a new texture object as the active texture by using the
bindTexture
API call, and then load the pixel data in the
img
variable to that texture object by
using the
texImage2D
function. Then, we set our filter mode to
Nearest-neighbor
interpolation
using the
texParameteri
function. Refer to
Chapter 4
,
Applying
Textures
, for an in-depth understanding of the functions that have just been
mentioned. Lastly, we add our new
texture
object at the provided key (
index
)
to the
textures
array.
•
Open the
StageObject.js
file from the
primitive
folder of the code
bundle. The individual stage object will hold the index (key) to the textures
array defined in the
StageObject
class, as shown in the following code:
this.textureIndex=0;
We also added two new class variables to the
StageObject
class. The
materialFile
class variable holds the name of the texture file and
verticesTextureBuffer
holds
the reference to the vertex buffer which holds the texture coordinates. The following
code defines the variables explained previously:
StageObject=function(){
...
this.verticesTextureBuffer=null;
this.materialFile=null;
...
};
After we parse our JSON data, we check whether the texture name is present in
our JSON data. If present, we assign the filename to the
materialFile
property
as shown in the following code:
StageObject.prototype.loadObject= function (data){
this.geometry=parseJSON(data);
this.name=data.metadata.sourceFile.split(".")[0];
if(this.geometry.materials.length>0){
...
if(!(this.geometry.materials[0].mapDiffuse===undefined)){
this.materialFile=this.geometry.materials[0].mapDiffuse;
}
...
}
}
There may be cases when no texture file is present for an object. In such cases, we use
a diffuse color to render objects.