Game Development Reference
In-Depth Information
The preceding function,
loadObject
, loads the JSON object, and then parses it
in
loadObject
of the
StageObject
class (
stageObject.loadObject(data);
).
If the
materialFile
variable is not null (
stageObject.materialFile!=null
),
denoting that the object has an associated texture, we generate a simple, unique
index using the
currentDate.getMilliseconds();
function of the JavaScript
Date
object, store it in the
textureList
object (
textureList [stageObject.
materialFile.trim()] =textureIndex;
), and invoke
initTextures
. The key
in the
textureList
object that references the texture object is the name of the file
loaded. However, before doing this, we make sure that the key is not already defined
(
textureList [stageObject.materialFile.trim()]===undefined
), denoting
that the file is not already loaded. If it is, then we simply assign the key (index)
to the
StageObject(stageObject.textureIndex=textureList[stageObject.
materialFile.trim()])
object.
function initTextures(fileName,textureCount){
var image = new Image();
image.onload = function() { stage.addTexture(textureCount,fileName,im
age); }
image.src = "model/obj/"+fileName;
}
The
initTextures
function in the preceding code loads the image and passes the
data to the
stage.addTexture
function that initializes the texture data and sets the
filter mode.
function addStageObject (stageObject,location,rotationX,rotationY,rot
ationZ){
cloneObjects(stageObject);
stageObject.location=location;
stageObject.rotationX=rotationX;
stageObject.rotationY=rotationY;
stageObject.rotationZ=rotationZ;
stage.addModel(stageObject);
}
The
addStageObject
function in the preceding code simply sets the transformation
information of the model and initializes the buffers for the object in its
addModel
function.
Next, we obtain a reference of our variables (
aTextureCoord
and
hasTexture
) defined
in the shaders in the
initShaders()
function as shown in the following code:
function initShaders() {
...
shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgr
am, "aTextureCoord");