Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

destroy functions #7

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/alfrid/Batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ class Batch {
get mesh() { return this._mesh; }

get shader() { return this._shader; }

destroy() {
this.mesh.destroy();
const gl = GL.gl;

gl.useProgram(null);
gl.deleteProgram(this._shader.shaderProgram);
}
}

export default Batch;
12 changes: 12 additions & 0 deletions src/alfrid/FrameBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ class FrameBuffer {
}

get numTargets() { return this._numTargets; }

// destroy
destroy() {
const gl = this.gl;
gl.deleteFramebuffer(this.framebuffer);

this._textures.forEach((texture)=>{
gl.deleteTexture(texture._texture);
});

gl.deleteTexture(this.glDepthTexture._texture);
}
}


Expand Down
30 changes: 19 additions & 11 deletions src/alfrid/GLShader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
'use strict';

import GL from './GLTool';
const glslify = require('glslify');
import defaultFragmentShader from './shaders/basic.frag';
import defaultVertexShader from './shaders/basic.vert';
import glslify from 'glslify';

const isSame = (array1, array2) => {
if(array1.length !== array2.length) {
return false;
Expand Down Expand Up @@ -36,8 +39,6 @@ const cloneArray = (mArray) => {
};

let gl;
const defaultVertexShader = require('./shaders/basic.vert');
const defaultFragmentShader = require('./shaders/basic.frag');

const uniformMapping = {
float: 'uniform1f',
Expand All @@ -52,7 +53,7 @@ const uniformMapping = {
class GLShader {
constructor(strVertexShader = defaultVertexShader, strFragmentShader = defaultFragmentShader, mVaryings) {

gl = GL.gl;
this.gl = GL.gl;
this.parameters = [];
this.uniformTextures = [];
this._varyings = mVaryings;
Expand All @@ -72,7 +73,7 @@ class GLShader {
if(GL.shader === this) {
return;
}
gl.useProgram(this.shaderProgram);
this.gl.useProgram(this.shaderProgram);
GL.useShader(this);
this.uniformTextures = [];

Expand Down Expand Up @@ -110,7 +111,7 @@ class GLShader {

if(!hasUniform) {
isNumber = uniformType === 'uniform1i' || uniformType === 'uniform1f';
this.shaderProgram[mName] = gl.getUniformLocation(this.shaderProgram, mName);
this.shaderProgram[mName] = this.gl.getUniformLocation(this.shaderProgram, mName);
if(isNumber) {
this.parameters.push({ name : mName, type: uniformType, value: mValue, uniformLoc: this.shaderProgram[mName], isNumber });
} else {
Expand All @@ -132,20 +133,20 @@ class GLShader {
if(uniformType.indexOf('Matrix') === -1) {
if(!isNumber) {
if(!isSame(this.parameters[parameterIndex].value, mValue) || !hasUniform) {
gl[uniformType](this.shaderProgram[mName], mValue);
this.gl[uniformType](this.shaderProgram[mName], mValue);
this.parameters[parameterIndex].value = cloneArray(mValue);
}
} else {
const needUpdate = (this.parameters[parameterIndex].value !== mValue || !hasUniform);
if(needUpdate) {
gl[uniformType](this.shaderProgram[mName], mValue);
this.gl[uniformType](this.shaderProgram[mName], mValue);
this.parameters[parameterIndex].value = mValue;
}
}

} else {
if(!isSame(this.parameters[parameterIndex].value, mValue) || !hasUniform) {
gl[uniformType](this.shaderProgram[mName], false, mValue);
this.gl[uniformType](this.shaderProgram[mName], false, mValue);
this.parameters[parameterIndex].value = cloneArray(mValue);

}
Expand Down Expand Up @@ -173,7 +174,7 @@ class GLShader {


_createShaderProgram(mShaderStr, isVertexShader) {

const gl = this.gl;
const shaderType = isVertexShader ? GL.VERTEX_SHADER : GL.FRAGMENT_SHADER;
const shader = gl.createShader(shaderType);

Expand All @@ -190,7 +191,7 @@ class GLShader {
}

_attachShaderProgram(mVertexShader, mFragmentShader) {

const gl = this.gl;
this.shaderProgram = gl.createProgram();
gl.attachShader(this.shaderProgram, mVertexShader);
gl.attachShader(this.shaderProgram, mFragmentShader);
Expand All @@ -207,6 +208,13 @@ class GLShader {

}

destroy() {
const gl = GL.gl;

gl.useProgram(null);
gl.deleteProgram(this.shaderProgram);
}

}

GLShader.getUniformType = function (mValue) {
Expand Down
21 changes: 9 additions & 12 deletions src/alfrid/Geom.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,19 +513,16 @@ Geom.skybox = function skybox(size, drawType = 4) {

Geom.bigTriangle = function bigTriangle() {

if(!meshTri) {
const indices = [2, 1, 0];
const positions = [
[-1, -1],
[-1, 4],
[4, -1]
];

meshTri = new Mesh();
meshTri.bufferData(positions, 'aPosition', 2);
meshTri.bufferIndex(indices);
}
const indices = [2, 1, 0];
const positions = [
[-1, -1],
[-1, 4],
[4, -1]
];

const meshTri = new Mesh();
meshTri.bufferData(positions, 'aPosition', 2);
meshTri.bufferIndex(indices);

return meshTri;
};
Expand Down
38 changes: 33 additions & 5 deletions src/alfrid/Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import getAttribLoc from './utils/getAttribLoc';
let gl;
const STATIC_DRAW = 35044;

const getBuffer = function (attr) {
const getBuffer = function (attr, gl) {
let buffer;

if(attr.buffer !== undefined) {
Expand Down Expand Up @@ -38,7 +38,7 @@ const formBuffer = function (mData, mNum) {

class Mesh {
constructor(mDrawingType = 4, mUseVao = true) {
gl = GL.gl;
this.gl = GL.gl;
this.drawType = mDrawingType;
this._attributes = [];
this._numInstance = -1;
Expand Down Expand Up @@ -86,7 +86,7 @@ class Mesh {


bufferIndex(mArrayIndices, isDynamic = false) {

const gl = this.gl;
this._drawType = isDynamic ? gl.DYNAMIC_DRAW : gl.STATIC_DRAW;
this._indices = new Uint16Array(mArrayIndices);
this._numItems = this._indices.length;
Expand Down Expand Up @@ -148,6 +148,7 @@ class Mesh {


bind(mShaderProgram) {
const gl = this.gl;
this.generateBuffers(mShaderProgram);

if(this.hasVAO) {
Expand All @@ -170,6 +171,7 @@ class Mesh {
}

generateBuffers(mShaderProgram) {
const gl = this.gl;
if(this._bufferChanged.length == 0) { return; }

if(this._useVAO) { // IF SUPPORTED, CREATE VAO
Expand All @@ -185,7 +187,7 @@ class Mesh {
this._attributes.forEach((attrObj) => {

if(this._bufferChanged.indexOf(attrObj.name) !== -1) {
const buffer = getBuffer(attrObj);
const buffer = getBuffer(attrObj, this.gl);
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, attrObj.dataArray, attrObj.drawType);

Expand Down Expand Up @@ -214,7 +216,7 @@ class Mesh {
this._attributes.forEach((attrObj) => {
// SKIP IF BUFFER HASN'T CHANGED
if(this._bufferChanged.indexOf(attrObj.name) !== -1) {
const buffer = getBuffer(attrObj);
const buffer = getBuffer(attrObj, this.gl);
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, attrObj.dataArray, attrObj.drawType);

Expand All @@ -238,6 +240,7 @@ class Mesh {


unbind() {
const gl = this.gl;
if(this._useVAO) {
gl.bindVertexArray(null);
}
Expand All @@ -251,6 +254,7 @@ class Mesh {


_updateIndexBuffer() {
const gl = this.gl;
if(!this._hasIndexBufferChanged) {
if (!this.iBuffer) { this.iBuffer = gl.createBuffer(); }
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.iBuffer);
Expand Down Expand Up @@ -361,6 +365,30 @@ class Mesh {
return attr ? attr.source : [];
}

destroy() {

const gl = this.gl;
this._attributes.forEach((attrObj) => {
// SKIP IF BUFFER HASN'T CHANGED
const buffer = getBuffer(attrObj, this.gl);

gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, 1, gl.STATIC_DRAW);
gl.deleteBuffer(buffer);

attrObj.buffer = null;
});

gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.iBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, 1, gl.STATIC_DRAW);
gl.deleteBuffer(this.iBuffer);


if (this.hasVAO) gl.deleteVertexArray(this._vao);
this.iBuffer = null
this._vao = null;
}


// GETTER AND SETTERS

Expand Down
11 changes: 10 additions & 1 deletion src/alfrid/helpers/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class Scene {
this._initViews();

this._efIndex = Scheduler.addEF(()=>this._loop());
window.addEventListener('resize', ()=>this.resize());

this._resize = this.resize.bind(this);
window.addEventListener('resize', this._resize);
}


Expand Down Expand Up @@ -97,6 +99,7 @@ class Scene {
this.camera = new CameraPerspective();
this.camera.setPerspective(45 * Math.PI / 180, GL.aspectRatio, 0.1, 100);
this.orbitalControl = new OrbitalControl(this.camera, window, 15);
this.orbitalControl.connect();
this.orbitalControl.radius.value = 10;

this.cameraOrtho = new CameraOrtho();
Expand All @@ -115,6 +118,12 @@ class Scene {
this.render();
}

destroy() {
this.orbitalControl.disconnect();
Scheduler.removeEF(this._efIndex);
window.removeEventListener('resize', this._resize);
}

}


Expand Down
5 changes: 4 additions & 1 deletion src/alfrid/helpers/View.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// View.js

import GLShader from '../GLShader';

class View {
Expand All @@ -21,6 +20,10 @@ class View {
render() {

}

destroy() {
this.shader.destroy();
}
}

export default View;
8 changes: 6 additions & 2 deletions src/alfrid/helpers/View3D.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// View3D.js

import Object3D from '../objects/Object3D';
import GLShader from '../GLShader';
import GL from '../GLTool';
import GLShader from '../GLShader';
import Object3D from '../objects/Object3D';

class View3D extends Object3D {
constructor(mStrVertex, mStrFrag) {
Expand All @@ -25,6 +25,10 @@ class View3D extends Object3D {
render() {
}

destroy() {
this.shader.destroy();
}

}


Expand Down
Loading