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

Scoping Functions and Adding Multiple GL Context Support #37

Open
wants to merge 3 commits into
base: master
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
3 changes: 2 additions & 1 deletion src/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
(function () {
//Define some locals
var Vec3 = PhiloGL.Vec3,
Mat4 = PhiloGL.Mat4;
Mat4 = PhiloGL.Mat4,
$ = PhiloGL.$;

//Camera class
var Camera = function(fov, aspect, near, far, opt) {
Expand Down
143 changes: 74 additions & 69 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ this.PhiloGL = null;
//with a gl context, a camera, a program, a scene, and an event system.
(function () {
PhiloGL = function(canvasId, opt) {
var $ = PhiloGL.$;
opt = $.merge({
context: {
/*
Expand Down Expand Up @@ -50,16 +51,21 @@ this.PhiloGL = null;
optEvents = opt.events,
optTextures = opt.textures,
optProgram = $.splat(opt.program),
optScene = opt.scene;
optScene = opt.scene
program = null;

//get Context global to all framework
gl = PhiloGL.WebGL.getContext(canvasId, optContext);
//Get the 3D context, holds the application
var gl = PhiloGL.WebGL.getContext(canvasId, optContext);
PhiloGL.glConstants = gl;

if (!gl) {
opt.onError("The WebGL context couldn't been initialized");
return null;
}

//make app instance
var app = new PhiloGL.WebGL.Application({gl: gl});

//get Program
var popt = {
'defaults': 'fromDefaultShaders',
Expand Down Expand Up @@ -93,13 +99,15 @@ this.PhiloGL = null;

optProgram.forEach(function(optProgram, i) {
var pfrom = optProgram.from, program;
optProgram.gl = gl;
optProgram.app = app;
for (var p in popt) {
if (pfrom == p) {
try {
//try {
program = PhiloGL.Program[popt[p]]($.extend(programCallback, optProgram));
} catch(e) {
programCallback.onError(e);
}
//} catch(e) {
// programCallback.onError(e);
//}
break;
}
}
Expand All @@ -121,14 +129,10 @@ this.PhiloGL = null;
//get Scene
var scene = new PhiloGL.Scene(program, camera, optScene);

//make app instance global to all framework
app = new PhiloGL.WebGL.Application({
gl: gl,
canvas: canvas,
program: program,
scene: scene,
camera: camera
});
app.program = program;
app.canvas = canvas;
app.scene = scene;
app.camera = camera;

//Use program
if (program.$$family == 'program') {
Expand All @@ -148,7 +152,7 @@ this.PhiloGL = null;
onComplete: function() {
callback(app);
}
}));
}), app);
} else {
callback(app);
}
Expand All @@ -158,65 +162,66 @@ this.PhiloGL = null;
})();


//Unpacks the submodules to the global space.
PhiloGL.unpack = function(branch) {
branch = branch || globalContext;
['Vec3', 'Mat4', 'Quat', 'Camera', 'Program', 'WebGL', 'O3D',
'Scene', 'Shaders', 'IO', 'Events', 'WorkerGroup', 'Fx', 'Media'].forEach(function(module) {
branch[module] = PhiloGL[module];
});
branch.gl = gl;
branch.Utils = $;
};
(function() {
globalContext = this;
//Unpacks the submodules to the global space.
PhiloGL.unpack = function(branch) {
branch = branch || globalContext;
['Vec3', 'Mat4', 'Quat', 'Camera', 'Program', 'WebGL', 'O3D',
'Scene', 'Shaders', 'IO', 'Events', 'WorkerGroup', 'Fx', 'Media'].forEach(function(module) {
branch[module] = PhiloGL[module];
});
branch.gl = gl;
branch.Utils = PhiloGL.$;
};
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this wrapper function never gets called


//Version
PhiloGL.version = '1.5.2';

//Holds the 3D context, holds the application
var gl, app, globalContext = this;

//Utility functions
function $(d) {
return document.getElementById(d);
}
(function() {
PhiloGL.$ = function (d) {
return document.getElementById(d);
}
var $ = PhiloGL.$;

$.empty = function() {};
$.empty = function() {};

$.time = Date.now;
$.time = Date.now;

$.uid = (function() {
var t = $.time();
$.uid = (function() {
var t = $.time();

return function() {
return t++;
};
})();
return function() {
return t++;
};
})();

$.extend = function(to, from) {
for (var p in from) {
to[p] = from[p];
}
return to;
};

$.type = (function() {
var oString = Object.prototype.toString,
type = function(e) {
var t = oString.call(e);
return t.substr(8, t.length - 9).toLowerCase();
};

return function(elem) {
var elemType = type(elem);
if (elemType != 'object') {
return elemType;
$.extend = function(to, from) {
for (var p in from) {
to[p] = from[p];
}
if (elem.$$family) return elem.$$family;
return (elem && elem.nodeName && elem.nodeType == 1) ? 'element' : elemType;
return to;
};
})();

(function() {
$.type = (function() {
var oString = Object.prototype.toString,
type = function(e) {
var t = oString.call(e);
return t.substr(8, t.length - 9).toLowerCase();
};

return function(elem) {
var elemType = type(elem);
if (elemType != 'object') {
return elemType;
}
if (elem.$$family) return elem.$$family;
return (elem && elem.nodeName && elem.nodeType == 1) ? 'element' : elemType;
};
})();

function detach(elem) {
var type = $.type(elem), ans;
if (type == 'object') {
Expand Down Expand Up @@ -252,12 +257,12 @@ $.type = (function() {
}
return mix;
};
})();

$.splat = (function() {
var isArray = Array.isArray;
return function(a) {
return isArray(a) && a || [a];
};
})();
$.splat = (function() {
var isArray = Array.isArray;
return function(a) {
return isArray(a) && a || [a];
};
})();

})();
1 change: 1 addition & 0 deletions src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//Handle keyboard/mouse/touch events in the Canvas

(function() {
var $ = PhiloGL.$;

//returns an O3D object or false otherwise.
function toO3D(n) {
Expand Down
2 changes: 2 additions & 0 deletions src/fx.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(function() {
var $ = PhiloGL.$;

//Timer based animation
var Fx = function(options) {
this.opt = $.merge({
Expand Down
3 changes: 2 additions & 1 deletion src/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//Provides loading of assets with XHR and JSONP methods.

(function () {
var $ = PhiloGL.$;
var IO = {};

var XHR = function(opt) {
Expand Down Expand Up @@ -265,7 +266,7 @@
};

//Load multiple textures from images
var Textures = function(opt) {
var Textures = function(opt, app) {
opt = $.merge({
src: [],
noCache: false,
Expand Down
1 change: 1 addition & 0 deletions src/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//Vec3, Mat4 and Quat classes

(function() {

var sqrt = Math.sqrt,
sin = Math.sin,
cos = Math.cos,
Expand Down
6 changes: 4 additions & 2 deletions src/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//media has utility functions for image, video and audio manipulation (and
//maybe others like device, etc).
(function() {
var $ = PhiloGL.$;
var Media = {};

var Image = function() {};
Expand All @@ -17,8 +18,9 @@
position: { x: 0, y: 0, z: 1.205 }
}), scene = new PhiloGL.Scene({}, camera);

return function(opt) {
var program = app.program.$$family ? app.program : app.program[opt.program],
return function(opt, app) {
var gl = app.gl,
program = app.program.$$family ? app.program : app.program[opt.program],
textures = opt.fromTexture ? $.splat(opt.fromTexture) : [],
framebuffer = opt.toFrameBuffer,
screen = !!opt.toScreen,
Expand Down
15 changes: 10 additions & 5 deletions src/o3d.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//Scene Objects

(function () {
var $ = PhiloGL.$;
//Define some locals
var Vec3 = PhiloGL.Vec3,
Mat4 = PhiloGL.Mat4,
Expand Down Expand Up @@ -156,12 +157,13 @@
},

setIndices: function(program) {
var glc = PhiloGL.glConstants
if (!this.$indices) return;

if (this.dynamic) {
program.setBuffer('indices-' + this.id, {
bufferType: gl.ELEMENT_ARRAY_BUFFER,
drawType: gl.STATIC_DRAW,
bufferType: glc.ELEMENT_ARRAY_BUFFER,
drawType: glc.STATIC_DRAW,
value: this.$indices,
size: 1
});
Expand Down Expand Up @@ -236,17 +238,19 @@
},

setTextures: function(program, force) {
var app = program.app;
glc = PhiloGL.glConstants;
this.textures = this.textures? $.splat(this.textures) : [];
var dist = 5;
for (var i = 0, texs = this.textures, l = texs.length, mtexs = PhiloGL.Scene.MAX_TEXTURES; i < mtexs; i++) {
if (i < l) {
var isCube = app.textureMemo[texs[i]].isCube;
if (isCube) {
program.setUniform('hasTextureCube' + (i + 1), true);
program.setTexture(texs[i], gl['TEXTURE' + (i + dist)]);
program.setTexture(texs[i], glc['TEXTURE' + (i + dist)]);
} else {
program.setUniform('hasTexture' + (i + 1), true);
program.setTexture(texs[i], gl['TEXTURE' + i]);
program.setTexture(texs[i], glc['TEXTURE' + i]);
}
} else {
program.setUniform('hasTextureCube' + (i + 1), false);
Expand All @@ -270,7 +274,8 @@
},

unsetState: function(program) {
var attributes = program.attributes;
var attributes = program.attributes,
gl = program.gl;

//unbind the array and element buffers
gl.bindBuffer(gl.ARRAY_BUFFER, null);
Expand Down
Loading