Game Development Reference
In-Depth Information
function handleTextureLoaded(img, texture) {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA,
gl.UNSIGNED_BYTE, img);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER,
gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER,
gl.NEAREST);
gl.bindTexture(gl.TEXTURE_2D, null);
initScene();
}
function loadModel(url){
rotateX=0.0;
rotateY=0.0;
$.getJSON(path+url, function(data) {
geometry=parseJSON(data);
if(geometry.materials.length>0) {
if(!(geometry.materials[0].colorDiffuse===undefined))
diffuseColor=geometry.materials[0].colorDiffuse;
if(!(geometry.materials[0].colorAmbient===undefined))
ambientColor=geometry.materials[0].colorAmbient;
if(!(geometry.materials[0].colorSpecular===undefined))
specularColor=geometry.materials[0].colorSpecular;
}
initTextures(geometry.materials[materialIndex].mapDiffuse);
});
}
The initBuffers function creates vertex buffers ( gl.ARRAY_BUFFER ) for vertices,
normals, and texture coordinates. It initializes the buffers with geometry variables
(vertices, normals, and UVs). Then, it creates the index buffer ( gl.ELEMENT_ARRAY_
BUFFER ) and loads geometry.indices into it, as shown in the following code snippet:
function initBuffers() {
vertexPositionBuffer = gl.createBuffer();
indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexPositionBuffer);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(geometry.vertices), gl.STATIC_DRAW);
vertexPositionBuffer.itemSize = 3;
vertexPositionBuffer.numItems = geometry.vertices.length/3;
 
Search WWH ::




Custom Search