/    Sign up×
Community /Pin to ProfileBookmark

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();
}
[/code]

Any tips?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@zagibuauthorJul 15.2010 — I've managed to do it by saving the this reference in a var, which is accessible within the anonymous function.
×

Success!

Help @zagibu spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...