"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var stardust_core_1 = require("stardust-core"); var stardust_core_2 = require("stardust-core"); var generator_1 = require("./generator"); var stardust_core_3 = require("stardust-core"); var AllofwUtils = require("./allofwutils"); var allofw_1 = require("allofw"); var AllofwPlatformMarkProgram = /** @class */ (function () { function AllofwPlatformMarkProgram(platform, spec, shader, asUniform) { var generator = new generator_1.Generator(platform.getPrefixCode(), spec, shader, asUniform); var codes = { vertex: generator.getVertexCode(), geometry: generator.getGeometryCode(), fragment: generator.getFragmentCode() }; // console.log(codes.vertex); // console.log(codes.geometry); // console.log(codes.fragment); this._program = AllofwUtils.compileShaders(codes); this._uniformLocations = new stardust_core_2.Dictionary(); this._attribLocations = new stardust_core_2.Dictionary(); } AllofwPlatformMarkProgram.prototype.use = function () { allofw_1.GL3.useProgram(this._program); }; AllofwPlatformMarkProgram.prototype.id = function () { return this._program.id(); }; AllofwPlatformMarkProgram.prototype.setUniform = function (name, type, value) { var location = this.getUniformLocation(name); if (location == null) return; if (type.primitive == "float") { var va = value; switch (type.primitiveCount) { case 1: allofw_1.GL3.uniform1f(location, value); break; case 2: allofw_1.GL3.uniform2f(location, va[0], va[1]); break; case 3: allofw_1.GL3.uniform3f(location, va[0], va[1], va[2]); break; case 4: allofw_1.GL3.uniform4f(location, va[0], va[1], va[2], va[3]); break; } } if (type.primitive == "int") { var va = value; switch (type.primitiveCount) { case 1: allofw_1.GL3.uniform1i(location, value); break; case 2: allofw_1.GL3.uniform2i(location, va[0], va[1]); break; case 3: allofw_1.GL3.uniform3i(location, va[0], va[1], va[2]); break; case 4: allofw_1.GL3.uniform4i(location, va[0], va[1], va[2], va[3]); break; } } }; AllofwPlatformMarkProgram.prototype.getUniformLocation = function (name) { if (this._uniformLocations.has(name)) { return this._uniformLocations.get(name); } else { var location_1 = allofw_1.GL3.getUniformLocation(this._program, name); this._uniformLocations.set(name, location_1); return location_1; } }; AllofwPlatformMarkProgram.prototype.getAttribLocation = function (name) { if (this._attribLocations.has(name)) { return this._attribLocations.get(name); } else { var location_2 = allofw_1.GL3.getAttribLocation(this._program, name); if (location_2 < 0) location_2 = null; this._attribLocations.set(name, location_2); return location_2; } }; return AllofwPlatformMarkProgram; }()); var AllofwPlatformMarkData = /** @class */ (function (_super) { __extends(AllofwPlatformMarkData, _super); function AllofwPlatformMarkData() { return _super !== null && _super.apply(this, arguments) || this; } return AllofwPlatformMarkData; }(stardust_core_1.PlatformMarkData)); exports.AllofwPlatformMarkData = AllofwPlatformMarkData; var AllofwPlatformMark = /** @class */ (function (_super) { __extends(AllofwPlatformMark, _super); function AllofwPlatformMark(platform, mark, spec, shader, bindings, shiftBindings) { var _this = _super.call(this) || this; _this._platform = platform; _this._mark = mark; _this._bindings = bindings; _this._shiftBindings = shiftBindings; _this._spec = spec; _this._shader = shader; _this._program = new AllofwPlatformMarkProgram(_this._platform, _this._spec, _this._shader, function (name) { return _this.isUniform(name); }); var minOffset = 0; var maxOffset = 0; _this._shiftBindings.forEach(function (shift, name) { if (shift.offset > maxOffset) maxOffset = shift.offset; if (shift.offset < minOffset) minOffset = shift.offset; }); _this._minOffset = minOffset; _this._maxOffset = maxOffset; _this.initializeUniforms(); return _this; } AllofwPlatformMark.prototype.initializeUniforms = function () { for (var name_1 in this._spec.input) { if (this.isUniform(name_1)) { this.updateUniform(name_1, this._bindings.get(name_1).specValue); } } }; AllofwPlatformMark.prototype.initializeBuffers = function () { var _this = this; var data = new AllofwPlatformMarkData(); data.vertexArray = new allofw_1.GL3.VertexArray(); data.buffers = new stardust_core_2.Dictionary(); ; this._bindings.forEach(function (binding, name) { if (!_this.isUniform(name)) { var location_3 = _this._program.getAttribLocation(name); if (location_3 != null) { data.buffers.set(name, new allofw_1.GL3.Buffer()); } } }); var spec = this._spec; var program = this._program; var bindings = this._bindings; var minOffset = this._minOffset; var maxOffset = this._maxOffset; allofw_1.GL3.bindVertexArray(data.vertexArray); // Assign attributes to buffers for (var name_2 in spec.input) { var attributeLocation = program.getAttribLocation(name_2); if (attributeLocation == null) continue; if (this._shiftBindings.has(name_2)) { var shift = this._shiftBindings.get(name_2); allofw_1.GL3.bindBuffer(allofw_1.GL3.ARRAY_BUFFER, data.buffers.get(shift.name)); allofw_1.GL3.enableVertexAttribArray(attributeLocation); var type = bindings.get(shift.name).valueType; allofw_1.GL3.vertexAttribPointer(attributeLocation, type.primitiveCount, type.primitive == "float" ? allofw_1.GL3.FLOAT : allofw_1.GL3.INT, allofw_1.GL3.FALSE, 0, type.size * (shift.offset - minOffset)); } else { allofw_1.GL3.bindBuffer(allofw_1.GL3.ARRAY_BUFFER, data.buffers.get(name_2)); allofw_1.GL3.enableVertexAttribArray(attributeLocation); var type = bindings.get(name_2).valueType; allofw_1.GL3.vertexAttribPointer(attributeLocation, type.primitiveCount, type.primitive == "float" ? allofw_1.GL3.FLOAT : allofw_1.GL3.INT, allofw_1.GL3.FALSE, 0, type.size * (-minOffset)); } } allofw_1.GL3.bindVertexArray(0); data.ranges = []; return data; }; // Is the input attribute compiled as uniform? AllofwPlatformMark.prototype.isUniform = function (name) { // Extra variables we add are always not uniforms. if (this._bindings.get(name) == null) { if (this._shiftBindings.get(name) == null) { throw new stardust_core_3.RuntimeError("attribute " + name + " is not specified."); } else { return this._bindings.get(this._shiftBindings.get(name).name).bindingType != stardust_core_1.BindingType.FUNCTION; } } else { // Look at the binding to determine. return this._bindings.get(name).bindingType != stardust_core_1.BindingType.FUNCTION; } }; AllofwPlatformMark.prototype.updateUniform = function (name, value) { var binding = this._bindings.get(name); var type = binding.valueType; this._program.use(); this._program.setUniform(name, type, value); }; AllofwPlatformMark.prototype.updateTexture = function (name, value) { }; AllofwPlatformMark.prototype.uploadData = function (datas) { var buffers = this.initializeBuffers(); buffers.ranges = []; var repeatBegin = this._spec.repeatBegin || 0; var repeatEnd = this._spec.repeatEnd || 0; var bindings = this._bindings; var totalCount = 0; datas.forEach(function (data) { var n = data.length; if (n == 0) { buffers.ranges.push(null); return; } else { var c1 = totalCount; totalCount += n + repeatBegin + repeatEnd; var c2 = totalCount; buffers.ranges.push([c1, c2]); } }); this._bindings.forEach(function (binding, name) { var buffer = buffers.buffers.get(name); if (buffer == null) return; var type = binding.valueType; var array = new Float32Array(type.primitiveCount * totalCount); var currentIndex = 0; var multiplier = type.primitiveCount; datas.forEach(function (data) { if (data.length == 0) return; for (var i = 0; i < repeatBegin; i++) { binding.fillBinary([data[0]], 1, array.subarray(currentIndex, currentIndex + multiplier)); currentIndex += multiplier; } binding.fillBinary(data, 1, array.subarray(currentIndex, currentIndex + data.length * multiplier)); currentIndex += data.length * multiplier; for (var i = 0; i < repeatEnd; i++) { binding.fillBinary([data[data.length - 1]], 1, array.subarray(currentIndex, currentIndex + multiplier)); currentIndex += multiplier; } }); allofw_1.GL3.bindBuffer(allofw_1.GL3.ARRAY_BUFFER, buffer); allofw_1.GL3.bufferData(allofw_1.GL3.ARRAY_BUFFER, array.byteLength, array, allofw_1.GL3.STATIC_DRAW); }); return buffers; }; // Render the graphics. AllofwPlatformMark.prototype.renderBase = function (buffers, onRender) { var _this = this; if (buffers.ranges.length > 0) { var spec = this._spec; var bindings = this._bindings; // Decide which program to use var program_1 = this._program; program_1.use(); allofw_1.GL3.bindVertexArray(buffers.vertexArray); this._platform.omnistereo.setUniforms(program_1.id()); // Draw arrays buffers.ranges.forEach(function (range, index) { if (onRender) { onRender(index); } if (range != null) { program_1.use(); allofw_1.GL3.drawArrays(allofw_1.GL3.POINTS, range[0], range[1] - range[0] - (_this._maxOffset - _this._minOffset)); } }); allofw_1.GL3.bindVertexArray(0); } }; AllofwPlatformMark.prototype.render = function (buffers, onRender) { this.renderBase(buffers, onRender); }; return AllofwPlatformMark; }(stardust_core_1.PlatformMark)); exports.AllofwPlatformMark = AllofwPlatformMark; var AllofwPlatform3D = /** @class */ (function (_super) { __extends(AllofwPlatform3D, _super); function AllofwPlatform3D(window, omnistereo) { var _this = _super.call(this) || this; _this._window = window; _this._omnistereo = omnistereo; return _this; } Object.defineProperty(AllofwPlatform3D.prototype, "omnistereo", { get: function () { return this._omnistereo; }, enumerable: true, configurable: true }); AllofwPlatform3D.prototype.getPrefixCode = function () { return this._omnistereo.getShaderCode(); }; AllofwPlatform3D.prototype.compile = function (mark, spec, shader, bindings, shiftBindings) { return new AllofwPlatformMark(this, mark, spec, shader, bindings, shiftBindings); }; return AllofwPlatform3D; }(stardust_core_1.Platform)); exports.AllofwPlatform3D = AllofwPlatform3D; //# sourceMappingURL=allofw.js.map