Skip to content

Commit

Permalink
Redesigned architecture; can draw names
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Badea committed Jul 5, 2018
1 parent 88a623e commit 0945a18
Show file tree
Hide file tree
Showing 23 changed files with 422 additions and 323 deletions.
68 changes: 0 additions & 68 deletions Arrow.js

This file was deleted.

47 changes: 0 additions & 47 deletions CoordSystem.js

This file was deleted.

14 changes: 14 additions & 0 deletions DrawableBlueprint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class DrawableBlueprint {
constructor(name, color) {
if (new.target === DrawableBlueprint)
throw new TypeError("Cannot instantiate abstract class");
if (name == undefined || color == undefined)
throw new Error("Undefined or null params");
this.name = name;
this.color = color;
}

instantiate(gl, programInfo) {
throw new Error("Cannot call abstract method");
}
}
74 changes: 32 additions & 42 deletions Drawable.js → DrawableInstance.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,64 @@
class Drawable {
constructor(name) {
if (new.target === Drawable)
class DrawableInstance {
constructor(drawable, gl, programInfo) {
if (new.target === DrawableInstance)
throw new TypeError("Cannot instantiate abstract class");
if (name == undefined)
throw new Error("Undefined or null params");
this.name = name;
}

init(gl) {
if (gl == undefined)
if (drawable == undefined || gl == undefined || programInfo == undefined)
throw new Error("Undefined or null params");
this.drawable = drawable;
this.gl = gl;
this.programInfo = programInfo;
this.buffers = {
position: gl.createBuffer(),
color: gl.createBuffer()
};

this._INIT_NAME();
this._INIT_NAME(drawable, gl);
}

// Depreciat: va fi înocuită când se va găsi o metodă mai bună
_INIT_NAME() {
if (this.gl == undefined)
throw new Error("Must initialize drawable before drawing name");
draw() {
if (this.gl.getUniform(this.programInfo.program, this.programInfo.uniformLocations.modelMatrix, 0).every((e, i) => mat4.fromValues(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)[i] === e))
throw new Error("Stray drawable not bound to any drawing context (coordinate system): " + this.name);

this._DRAW_NAME(this.programInfo);
}

getNamePos() {
throw new Error("Cannot call abstract method");
}

// Depreciat: va fi înocuitã când se va gãsi o metodã mai bunã
_INIT_NAME(drawable, gl) {
if (drawable == undefined || gl == undefined)
throw new Error("Undefined or null params");

// NU MAI CREA DACÃ NUMELE E GOL@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
this.nameContainer = document.createElement("div");
this.nameContainer.innerHTML = this.name;
this.nameContainer.innerHTML = drawable.name;
this.nameContainer.setAttribute("style", "position: absolute;-webkit-touch-callout: none;-webkit-user-select: none;-khtml-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;");
document.body.appendChild(this.nameContainer);
}

draw(programInfo) {
if (this.gl == undefined)
throw new Error("Must initialize WebGL context before drawing");
// Depreciat: va fi înocuitã când se va gãsi o metodã mai bunã
// DE TESTAT DACÃ SE DESENEAZÃ RELATIV LA PÂNZÃ SAU RELATIV LA FEREASTRÃ; REPARÃ DACÃ RELATIV LA FEREASTRÃ
_DRAW_NAME(programInfo) {
if (programInfo == undefined)
throw new Error("Undefined or null params");
if (this.gl.getUniform(programInfo.program, programInfo.uniformLocations.modelMatrix, 0).every((e, i) => mat4.fromValues(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)[i] === e))
throw new Error("Stray drawable not bound to any drawing context (coordinate system): " + this.name);
this.programInfo = programInfo;

this._DRAW_NAME();
}

// Depreciat: va fi înocuită când se va găsi o metodă mai bună
_DRAW_NAME() {
if (this.gl == undefined)
throw new Error("Must initialize WebGL context before drawing name");
if (this.programInfo == undefined)
throw new Error("Must draw drawable before drawing name");

const projectionMatrix = this.gl.getUniform(this.programInfo.program, this.programInfo.uniformLocations.projectionMatrix, 0);
const viewMatrix = this.gl.getUniform(this.programInfo.program, this.programInfo.uniformLocations.viewMatrix, 0);
const modelMatrix = this.gl.getUniform(this.programInfo.program, this.programInfo.uniformLocations.modelMatrix, 0);

const projectionMatrix = this.gl.getUniform(programInfo.program, programInfo.uniformLocations.projectionMatrix, 0);
const viewMatrix = this.gl.getUniform(programInfo.program, programInfo.uniformLocations.viewMatrix, 0);
const modelMatrix = this.gl.getUniform(programInfo.program, programInfo.uniformLocations.modelMatrix, 0);

const namePos = vec4.fromValues(...this.getNamePos(), 1);
const mvp = mat4.multiply(mat4.create(), mat4.multiply(mat4.create(), projectionMatrix, viewMatrix), modelMatrix);
const nameMvpPos = vec4.transformMat4(vec4.create(), namePos, mvp);
const nameNormScrPos = [nameMvpPos[0]/nameMvpPos[3], nameMvpPos[1]/nameMvpPos[3]];
const nameScrPos = [(nameNormScrPos[0]*0.5+0.5)*this.gl.canvas.width,
(nameNormScrPos[1]*-0.5+0.5)*this.gl.canvas.height];
if (nameScrPos[0] > 0 && nameScrPos[0] < this.gl.canvas.width && nameScrPos[1] > 0 && nameScrPos[1] < this.gl.canvas.height && nameMvpPos[2] > 0
&& this.nameContainer.innerHTML !== "") {
if (nameScrPos[0] > 0 && nameScrPos[0] < this.gl.canvas.width && nameScrPos[1] > 0 && nameScrPos[1] < this.gl.canvas.height && nameMvpPos[2] > 0) {
this.nameContainer.style.left = nameScrPos[0];
this.nameContainer.style.top = nameScrPos[1];
this.nameContainer.style.display = "inline";
} else
this.nameContainer.style.display = "none";
}

getNamePos() {
throw new Error("Cannot call abstract method");
}
}
37 changes: 0 additions & 37 deletions Point.js

This file was deleted.

5 changes: 2 additions & 3 deletions Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ class Scene {
}

insert(drawable) {
drawable.init(this.gl);
this.drawList.push(drawable);
this.drawList.push(drawable.instantiate(this.gl, this.programInfo));
}

render() {
Expand All @@ -65,7 +64,7 @@ class Scene {
renderFrame() {
this.gl.clearColor(0.5, 0.5, 0.5, 1.0);
this.gl.clearDepth(1.0);
// Testul de adâncime dezactivat pentru transparență
// Testul de adâncime dezactivat pentru transparen?ã
this.gl.disable(this.gl.DEPTH_TEST);
this.gl.enable(this.gl.BLEND);
//this.gl.depthFunc(this.gl.LEQUAL);
Expand Down
39 changes: 0 additions & 39 deletions Segment.js

This file was deleted.

40 changes: 0 additions & 40 deletions Triangle.js

This file was deleted.

8 changes: 0 additions & 8 deletions Vector.js

This file was deleted.

Loading

0 comments on commit 0945a18

Please sign in to comment.