function GameLevel(playerStartPosition, bottomHeight, platforms, trampolines, goal) { this.platforms = platforms; this.trampolines = trampolines; this.playerStartPosition = playerStartPosition; this.bottomHeight = bottomHeight; this.goal = goal; this.platformVBO = null; this.trampolineVBO = null; } GameLevel.prototype.draw = function(gl) { if (this.platformVBO == null) { this.constructPlatformVBO(gl); } if (this.trampolineVBO == null && this.trampolines.length > 0) { this.constructTrampolineVBO(gl); } gl.enableVertexAttribArray(0); gl.uniformMatrix4fv(gl.getUniformLocation(gl.program, "uPMatrix"), false, new WebGLFloatArray(gl.projectionMatrix.m)); gl.uniformMatrix4fv(gl.getUniformLocation(gl.program, "uMVMatrix"), false, new WebGLFloatArray(gl.modelviewMatrix.m)); gl.bindBuffer(gl.ARRAY_BUFFER, this.platformVBO); gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.platformEBO); gl.uniform4f(gl.getUniformLocation(gl.program, "constantColor"), 1.0, 1.0, 1.0, 1.0); gl.drawElements(gl.TRIANGLES, this.platformIndexCount, gl.UNSIGNED_SHORT, 0); if (this.trampolineVBO != null) { gl.bindBuffer(gl.ARRAY_BUFFER, this.trampolineVBO); gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.trampolineEBO); gl.uniform4f(gl.getUniformLocation(gl.program, "constantColor"), 1.0, 0.5, 0.0, 1.0); gl.drawElements(gl.TRIANGLES, this.trampolineIndexCount, gl.UNSIGNED_SHORT, 0); } this.goal.draw(gl); } GameLevel.prototype.constructPlatformVBO = function(gl) { var vertices = new Array(); var indexes = new Array(); for (var platformIndex = 0; platformIndex < this.platforms.length; platformIndex++) { var platform = this.platforms[platformIndex]; vertices.push(platform.left, platform.y, platform.back, platform.left, platform.y, platform.front, platform.right, platform.y, platform.front, platform.right, platform.y, platform.back); indexes.push(platformIndex * 4 + 0, platformIndex * 4 + 1, platformIndex * 4 + 2, platformIndex * 4 + 2, platformIndex * 4 + 3, platformIndex * 4 + 0); } this.platformVBO = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, this.platformVBO); gl.bufferData(gl.ARRAY_BUFFER, new WebGLFloatArray(vertices), gl.STATIC_DRAW); this.platformEBO = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.platformEBO); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new WebGLUnsignedShortArray(indexes), gl.STATIC_DRAW); this.platformIndexCount = indexes.length; } GameLevel.prototype.constructTrampolineVBO = function(gl) { var vertices = new Array(); var indexes = new Array(); for (var trampolineIndex = 0; trampolineIndex < this.trampolines.length; trampolineIndex++) { var trampoline = this.trampolines[trampolineIndex]; vertices.push(trampoline.left, trampoline.y, trampoline.back, trampoline.left, trampoline.y, trampoline.front, trampoline.right, trampoline.y, trampoline.front, trampoline.right, trampoline.y, trampoline.back); indexes.push(trampolineIndex * 4 + 0, trampolineIndex * 4 + 1, trampolineIndex * 4 + 2, trampolineIndex * 4 + 2, trampolineIndex * 4 + 3, trampolineIndex * 4 + 0); } this.trampolineVBO = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, this.trampolineVBO); gl.bufferData(gl.ARRAY_BUFFER, new WebGLFloatArray(vertices), gl.STATIC_DRAW); this.trampolineEBO = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.trampolineEBO); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new WebGLUnsignedShortArray(indexes), gl.STATIC_DRAW); this.trampolineIndexCount = indexes.length; }