diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index ee63b60..5533b15 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -2,7 +2,7 @@ RenderTexture = require('../textures/RenderTexture'), EventEmitter = require('eventemitter3'), CONST = require('../const'), - Transform = require('./TransformStatic'), + Transform = require('./Transform'), _tempMatrix = new math.Matrix(), _tempDisplayObjectParent = {worldTransform:new math.Matrix(), worldAlpha:1, children:[]}; @@ -27,7 +27,6 @@ * @member {PIXI.Point} */ this.position = this.transform.position; - // this._position = new CachePoint(this); /** * The scale factor of the object. diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index ee63b60..5533b15 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -2,7 +2,7 @@ RenderTexture = require('../textures/RenderTexture'), EventEmitter = require('eventemitter3'), CONST = require('../const'), - Transform = require('./TransformStatic'), + Transform = require('./Transform'), _tempMatrix = new math.Matrix(), _tempDisplayObjectParent = {worldTransform:new math.Matrix(), worldAlpha:1, children:[]}; @@ -27,7 +27,6 @@ * @member {PIXI.Point} */ this.position = this.transform.position; - // this._position = new CachePoint(this); /** * The scale factor of the object. diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 7c95f91..47572e2 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -8,6 +8,7 @@ ObjectRenderer = require('./utils/ObjectRenderer'), FXAAFilter = require('./filters/FXAAFilter'), TextureManager = require('./TextureManager'), + WebGLState = require('./WebGLState'), createContext = require('pixi-gl-core').createContext, mapWebGLBlendModesToPixi = require('./utils/mapWebGLBlendModesToPixi'), mapWebGLDrawModesToPixi = require('./utils/mapWebGLDrawModesToPixi'), @@ -122,12 +123,16 @@ // initialize the context so it is ready for the managers. this.gl = createContext(this.view, this._contextOptions); + this.state = new WebGLState(this.gl); + this._initContext(); // map some webGL blend and drawmodes.. this.blendModes = mapWebGLBlendModesToPixi(gl); this.drawModes = mapWebGLDrawModesToPixi(gl) + + //alert(this.state ) this._activeShader = null; /** @@ -155,14 +160,12 @@ { var gl = this.gl; + + // create a texture manager... this.textureManager = new TextureManager(gl); - - // set up the default pixi settings.. - gl.disable(gl.DEPTH_TEST); - gl.disable(gl.CULL_FACE); - gl.enable(gl.BLEND); - + + this.state.resetToDefault(); this.rootRenderTarget = new RenderTarget(gl, this.width, this.height, null, this.resolution, true); this.rootRenderTarget.clearColor = this._backgroundColorRgba; @@ -259,7 +262,7 @@ this.filterManager.resize(width, height); this.rootRenderTarget.resize(width, height); - if(this.currentRenderTarget === this.renderTarget) + if(this._activeRenderTarget === this.rootRenderTarget) { this.rootRenderTarget.activate(); } diff --git a/src/core/display/DisplayObject.js b/src/core/display/DisplayObject.js index ee63b60..5533b15 100644 --- a/src/core/display/DisplayObject.js +++ b/src/core/display/DisplayObject.js @@ -2,7 +2,7 @@ RenderTexture = require('../textures/RenderTexture'), EventEmitter = require('eventemitter3'), CONST = require('../const'), - Transform = require('./TransformStatic'), + Transform = require('./Transform'), _tempMatrix = new math.Matrix(), _tempDisplayObjectParent = {worldTransform:new math.Matrix(), worldAlpha:1, children:[]}; @@ -27,7 +27,6 @@ * @member {PIXI.Point} */ this.position = this.transform.position; - // this._position = new CachePoint(this); /** * The scale factor of the object. diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 7c95f91..47572e2 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -8,6 +8,7 @@ ObjectRenderer = require('./utils/ObjectRenderer'), FXAAFilter = require('./filters/FXAAFilter'), TextureManager = require('./TextureManager'), + WebGLState = require('./WebGLState'), createContext = require('pixi-gl-core').createContext, mapWebGLBlendModesToPixi = require('./utils/mapWebGLBlendModesToPixi'), mapWebGLDrawModesToPixi = require('./utils/mapWebGLDrawModesToPixi'), @@ -122,12 +123,16 @@ // initialize the context so it is ready for the managers. this.gl = createContext(this.view, this._contextOptions); + this.state = new WebGLState(this.gl); + this._initContext(); // map some webGL blend and drawmodes.. this.blendModes = mapWebGLBlendModesToPixi(gl); this.drawModes = mapWebGLDrawModesToPixi(gl) + + //alert(this.state ) this._activeShader = null; /** @@ -155,14 +160,12 @@ { var gl = this.gl; + + // create a texture manager... this.textureManager = new TextureManager(gl); - - // set up the default pixi settings.. - gl.disable(gl.DEPTH_TEST); - gl.disable(gl.CULL_FACE); - gl.enable(gl.BLEND); - + + this.state.resetToDefault(); this.rootRenderTarget = new RenderTarget(gl, this.width, this.height, null, this.resolution, true); this.rootRenderTarget.clearColor = this._backgroundColorRgba; @@ -259,7 +262,7 @@ this.filterManager.resize(width, height); this.rootRenderTarget.resize(width, height); - if(this.currentRenderTarget === this.renderTarget) + if(this._activeRenderTarget === this.rootRenderTarget) { this.rootRenderTarget.activate(); } diff --git a/src/core/renderers/webgl/WebGLState.js b/src/core/renderers/webgl/WebGLState.js new file mode 100755 index 0000000..dbae0f4 --- /dev/null +++ b/src/core/renderers/webgl/WebGLState.js @@ -0,0 +1,175 @@ + + + + +var WebGLState = function(gl) +{ + this.activeState = new Uint8Array(16); + this.defaultState = new Uint8Array(16); + + // default blend mode.. + this.defaultState[0] = 1; + + this.stackIndex = 0; + + this.stack = []; + + this.gl = gl; + + this.maxAttribs = gl.getParameter(gl.MAX_VERTEX_ATTRIBS); + + // check we have vao.. + this.nativeVaoExtension = ( + gl.getExtension('OES_vertex_array_object') || + gl.getExtension('MOZ_OES_vertex_array_object') || + gl.getExtension('WEBKIT_OES_vertex_array_object') + ); +} + +WebGLState.prototype.push = function() +{ + // next state.. + var state = this.state[++this.stackIndex]; + + if(!state) + { + state = this.state[this.stackIndex] = new Uint8Array(16); + } + + // copy state.. + // set active state so we can force overrides of gl state + for (var i = 0; i < this.activeState.length; i++) + { + this.activeState[i] = state[i]; + }; +} + +var BLEND = 0, + DEPTH_TEST = 1, + FRONT_FACE = 2, + CULL_FACE = 3; + +WebGLState.prototype.pop = function() +{ + var state = this.state[--this.stackIndex]; + this.setState(state); +} + +WebGLState.prototype.setState = function(state) +{ + this.setBlend(state[BLEND]); + this.setDepthTest(state[DEPTH_TEST]); + this.setDepthTest(state[FRONT_FACE]); + this.setDepthTest(state[CULL_FACE]); +} + +WebGLState.prototype.setBlendMode = function(blendMode) +{ + +} + +WebGLState.prototype.setBlend = function(value) +{ + if(this.activeState[BLEND] === value|0)return; + + this.activeState[BLEND] = value|0; + + var gl = this.gl; + + if(value) + { + gl.enable(gl.BLEND); + } + else + { + gl.disable(gl.BLEND); + } +} + +WebGLState.prototype.setDepthTest = function(value) +{ + if(this.activeState[DEPTH_TEST] === value|0)return; + + this.activeState[DEPTH_TEST] = value|0; + + var gl = this.gl; + + if(value) + { + gl.enable(gl.DEPTH_TEST); + } + else + { + gl.disable(gl.DEPTH_TEST); + } +} + +WebGLState.prototype.setCullFace = function(value) +{ + if(this.activeState[CULL_FACE] === value|0)return; + + this.activeState[CULL_FACE] = value|0; + + var gl = this.gl; + + if(value) + { + gl.enable(gl.CULL_FACE); + } + else + { + gl.disable(gl.CULL_FACE); + } +} + +WebGLState.prototype.setFrontFace = function(value) +{ + if(this.activeState[FRONT_FACE] === value|0)return; + + this.activeState[FRONT_FACE] = value|0; + + var gl = this.gl; + + if(value) + { + gl.frontFace(gl.CW); + } + else + { + gl.frontFace(gl.CCW); + } +} + +WebGLState.prototype.resetAttributes = function(value) +{ + var gl = this.gl; + + // im going to assume one is always active for performance reasons. + for (i = 1; i < this.maxAttribs; i++) + { + gl.disableVertexAttribArray(i); + } +} + +//used +WebGLState.prototype.resetToDefault = function() +{ + // unbind any VAO if they exist.. + if(this.nativeVaoExtension) + { + this.nativeVaoExtension.bindVertexArrayOES(null); + } + + // reset all attributs.. + this.resetAttributes(); + + // set active state so we can force overrides of gl state + for (var i = 0; i < this.activeState.length; i++) + { + this.activeState[i] = 2; + }; + + this.setState(this.defaultState); +} + +module.exports = WebGLState;