www.webdeveloper.com
Results 1 to 2 of 2

Thread: Moving global variables to prototype scope

  1. #1
    Join Date
    Jul 2010
    Posts
    2

    Moving global variables to prototype scope

    I'd like to move the global variables vertexBlaBuffer to the scope of the Geometry prototype:

    Code:
    function Geometry(position, rotation, path) {
    	this.position = position;
    	this.rotation = rotation;
    	this.path = path;
    
    	var request = new XMLHttpRequest();
    	request.open("GET", path);
    	request.onreadystatechange = function() {
    		if (request.readyState == 4) {
    			handleLoadedModel(JSON.parse(request.responseText));
    		}
    	}
    	request.send();
    
    	initTextures();
    }
    
    var vertexPositionBuffer;
    var vertexNormalBuffer;
    var vertexTextureCoordBuffer;
    var vertexIndexBuffer;
    function handleLoadedModel(modelData) {
    	vertexNormalBuffer = gl.createBuffer();
    	gl.bindBuffer(gl.ARRAY_BUFFER, vertexNormalBuffer);
    	gl.bufferData(gl.ARRAY_BUFFER, new WebGLFloatArray(modelData.vertexNormals), gl.STATIC_DRAW);
    	vertexNormalBuffer.itemSize = 3;
    	vertexNormalBuffer.numItems = modelData.vertexNormals.length / 3;
    
    	vertexTextureCoordBuffer = gl.createBuffer();
    	gl.bindBuffer(gl.ARRAY_BUFFER, vertexTextureCoordBuffer);
    	gl.bufferData(gl.ARRAY_BUFFER, new WebGLFloatArray(modelData.vertexTextureCoords), gl.STATIC_DRAW);
    	vertexTextureCoordBuffer.itemSize = 2;
    	vertexTextureCoordBuffer.numItems = modelData.vertexTextureCoords.length / 2;
    
    	vertexPositionBuffer = gl.createBuffer();
    	gl.bindBuffer(gl.ARRAY_BUFFER, vertexPositionBuffer);
    	gl.bufferData(gl.ARRAY_BUFFER, new WebGLFloatArray(modelData.vertexPositions), gl.STATIC_DRAW);
    	vertexPositionBuffer.itemSize = 3;
    	vertexPositionBuffer.numItems = modelData.vertexPositions.length / 3;
    
    	vertexIndexBuffer = gl.createBuffer();
    	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vertexIndexBuffer);
    	gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new WebGLUnsignedShortArray(modelData.indices), gl.STREAM_DRAW);
    	vertexIndexBuffer.itemSize = 1;
    	vertexIndexBuffer.numItems = modelData.indices.length;
    }
    
    Geometry.prototype.draw = function() {
    	mvPushMatrix();
    	mvTranslate(this.position);
    	multMatrix(this.rotation);
    
    	// Tell WebGL you want to work with the cube's vertex positions buffer
    	gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexPositionBuffer);
        // Feed the shader program with the cube's vertex positions
    	gl.vertexAttribPointer(currentProgram.vertexPositionAttribute,
    		this.vertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
    
    	// Tell WebGL you want to work with the cube's normal buffer
    	gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexNormalBuffer);
    	// Feed the shader program with the cube's vertex normals
    	gl.vertexAttribPointer(currentProgram.vertexNormalAttribute,
    		this.vertexNormalBuffer.itemSize, gl.FLOAT, false, 0, 0);
    
    	// Tell WebGL you want to work with the cube's texture buffer
    	gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexTextureCoordBuffer);
    	// Feed the shader program with the cube's texture data
    	gl.vertexAttribPointer(currentProgram.textureCoordAttribute,
    		this.vertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);
    
    	gl.activeTexture(gl.TEXTURE0);
    	gl.bindTexture(gl.TEXTURE_2D, texture);
    	gl.uniform1i(currentProgram.samplerUniform, 0);
    
    	// Tell WebGL you want to work with the cube's vertex index buffer
    	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.vertexIndexBuffer);
    
    		// Draw the cube
    	gl.drawElements(gl.TRIANGLES, this.vertexIndexBuffer.numItems,
    		gl.UNSIGNED_SHORT, 0);
    
    	mvPopMatrix();
    }
    Any tips?

  2. #2
    Join Date
    Jul 2010
    Posts
    2
    I've managed to do it by saving the this reference in a var, which is accessible within the anonymous function.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles