123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707 |
- /****************************************************************************
- Copyright (c) 2010-2013 cocos2d-x.org
- Copyright (c) 2009 On-Core
- Copyright (c) 2008-2010 Ricardo Quesada
- Copyright (c) 2011 Zynga Inc.
- http://www.cocos2d-x.org
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- /**
- * Base class for cc.Grid
- * @class
- * @extends cc.Class
- */
- cc.GridBase = cc.Class.extend(/** @lends cc.GridBase# */{
- _active:false,
- _reuseGrid:0,
- _gridSize:null,
- _texture:null,
- _step:null,
- _grabber:null,
- _isTextureFlipped:false,
- _shaderProgram:null,
- _directorProjection:0,
- _dirty:false,
- ctor:function () {
- this._active=false;
- this._reuseGrid=0;
- this._gridSize=null;
- this._texture=null;
- this._step = cc.p(0, 0);
- this._grabber=null;
- this._isTextureFlipped=false;
- this._shaderProgram=null;
- this._directorProjection=0;
- this._dirty=false;
- },
- /**
- * whether or not the grid is active
- * @return {Boolean}
- */
- isActive:function () {
- return this._active;
- },
- /**
- * whether or not the grid is active
- * @param {Number} active
- */
- setActive:function (active) {
- this._active = active;
- if (!active) {
- var director = cc.Director.getInstance();
- var proj = director.getProjection();
- director.setProjection(proj);
- }
- },
- /**
- * get number of times that the grid will be reused
- * @return {Number}
- */
- getReuseGrid:function () {
- return this._reuseGrid;
- },
- /**
- * set number of times that the grid will be reused
- * @param reuseGrid
- */
- setReuseGrid:function (reuseGrid) {
- this._reuseGrid = reuseGrid;
- },
- /**
- * get size of the grid
- * @return {cc.Size}
- */
- getGridSize:function () {
- return cc.size(this._gridSize.width, this._gridSize.height);
- },
- /**
- * set size of the grid
- * @param {cc.Size} gridSize
- */
- setGridSize:function (gridSize) {
- this._gridSize.width = parseInt(gridSize.width);
- this._gridSize.height = parseInt(gridSize.height);
- },
- /**
- * get pixels between the grids
- * @return {cc.Point}
- */
- getStep:function () {
- return cc.p(this._step.x, this._step.y);
- },
- /**
- * set pixels between the grids
- * @param {cc.Point} step
- */
- setStep:function (step) {
- this._step.x = step.x;
- this._step.y = step.y;
- },
- /**
- * get wheter or not the texture is flipped
- * @return {Boolean}
- */
- isTextureFlipped:function () {
- return this._isTextureFlipped;
- },
- /**
- * set wheter or not the texture is flipped
- * @param {Boolean} flipped
- */
- setTextureFlipped:function (flipped) {
- if (this._isTextureFlipped != flipped) {
- this._isTextureFlipped = flipped;
- this.calculateVertexPoints();
- }
- },
- /**
- *
- * @param {cc.Size} gridSize
- * @param {cc.Texture2D} [texture=]
- * @param {Boolean} [flipped=false]
- * @returns {boolean}
- */
- initWithSize:function (gridSize, texture, flipped) {
- if (!texture) {
- var director = cc.Director.getInstance();
- var winSize = director.getWinSizeInPixels();
- var POTWide = cc.NextPOT(winSize.width);
- var POTHigh = cc.NextPOT(winSize.height);
- var data = new Uint8Array(POTWide * POTHigh * 4);
- if (!data) {
- cc.log("cocos2d: CCGrid: not enough memory.");
- return false;
- }
- texture = new cc.Texture2D();
- // we only use rgba8888
- texture.initWithData(data, cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888, POTWide, POTHigh, winSize);
- if (!texture) {
- cc.log("cocos2d: CCGrid: error creating texture");
- return false;
- }
- }
- flipped = flipped || false;
- this._active = false;
- this._reuseGrid = 0;
- this._gridSize = gridSize;
- this._texture = texture;
- this._isTextureFlipped = flipped;
- var texSize = this._texture.getContentSize();
- this._step.x = texSize.width / gridSize.width;
- this._step.y = texSize.height / gridSize.height;
- this._grabber = new cc.Grabber();
- if (!this._grabber)
- return false;
- this._grabber.grab(this._texture);
- this._shaderProgram = cc.ShaderCache.getInstance().programForKey(cc.SHADER_POSITION_TEXTURE);
- this.calculateVertexPoints();
- return true;
- },
- beforeDraw:function () {
- // save projection
- this._directorProjection = cc.Director.getInstance().getProjection();
- // 2d projection
- // [director setProjection:kCCDirectorProjection2D];
- this.set2DProjection();
- this._grabber.beforeRender(this._texture);
- },
- afterDraw:function (target) {
- this._grabber.afterRender(this._texture);
- // restore projection
- cc.Director.getInstance().setProjection(this._directorProjection);
- if (target.getCamera().isDirty()) {
- var offset = target.getAnchorPointInPoints();
- //
- // XXX: Camera should be applied in the AnchorPoint
- //
- cc.kmGLTranslatef(offset.x, offset.y, 0);
- target.getCamera().locate();
- cc.kmGLTranslatef(-offset.x, -offset.y, 0);
- }
- cc.glBindTexture2D(this._texture);
- // restore projection for default FBO .fixed bug #543 #544
- //TODO: CCDirector::sharedDirector().setProjection(CCDirector::sharedDirector().getProjection());
- //TODO: CCDirector::sharedDirector().applyOrientation();
- this.blit();
- },
- blit:function () {
- cc.log("cc.GridBase.blit(): Shall be overridden in subclass.");
- },
- reuse:function () {
- cc.log("cc.GridBase.reuse(): Shall be overridden in subclass.");
- },
- calculateVertexPoints:function () {
- cc.log("cc.GridBase.calculateVertexPoints(): Shall be overridden in subclass.");
- },
- set2DProjection:function () {
- var winSize = cc.Director.getInstance().getWinSizeInPixels();
- var gl = cc.renderContext;
- gl.viewport(0, 0, winSize.width , winSize.height);
- cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
- cc.kmGLLoadIdentity();
- var orthoMatrix = new cc.kmMat4();
- cc.kmMat4OrthographicProjection(orthoMatrix, 0, winSize.width, 0, winSize.height, -1, 1);
- cc.kmGLMultMatrix(orthoMatrix);
- cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
- cc.kmGLLoadIdentity();
- cc.setProjectionMatrixDirty()
- }
- });
- /**
- * create one cc.GridBase Object
- * @param {cc.Size} gridSize
- * @param {cc.Texture2D} [texture=]
- * @param {Boolean} [flipped=]
- * @return {cc.GridBase}
- */
- cc.GridBase.create = function (gridSize, texture, flipped) {
- var gridBase = new cc.GridBase();
- if (gridBase && gridBase.initWithSize(gridSize, texture, flipped))
- return gridBase;
- return null;
- };
- /**
- * cc.Grid3D is a 3D grid implementation. Each vertex has 3 dimensions: x,y,z
- * @class
- * @extends cc.GridBase
- */
- cc.Grid3D = cc.GridBase.extend(/** @lends cc.Grid3D# */{
- _texCoordinates:null,
- _vertices:null,
- _originalVertices:null,
- _indices:null,
- _texCoordinateBuffer:null,
- _verticesBuffer:null,
- _indicesBuffer:null,
- ctor:function () {
- cc.GridBase.prototype.ctor.call(this);
- this._texCoordinates=null;
- this._vertices=null;
- this._originalVertices=null;
- this._indices=null;
- this._texCoordinateBuffer=null;
- this._verticesBuffer=null;
- this._indicesBuffer=null;
- },
- /**
- * returns the vertex at a given position
- * @param {cc.Point} pos
- * @return {cc.Vertex3F}
- */
- vertex:function (pos) {
- if(pos.x !== (0| pos.x) || pos.y !== (0| pos.y))
- cc.log("cc.Grid3D.vertex() : Numbers must be integers");
- var index = 0 | ((pos.x * (this._gridSize.height + 1) + pos.y) * 3);
- var locVertices = this._vertices;
- return new cc.Vertex3F(locVertices[index], locVertices[index + 1], locVertices[index + 2]);
- },
- /**
- * returns the original (non-transformed) vertex at a given position
- * @param {cc.Point} pos
- * @return {cc.Vertex3F}
- */
- originalVertex:function (pos) {
- if(pos.x !== (0| pos.x) || pos.y !== (0| pos.y))
- cc.log("cc.Grid3D.originalVertex() : Numbers must be integers");
- var index = 0 | ((pos.x * (this._gridSize.height + 1) + pos.y) * 3);
- var locOriginalVertices = this._originalVertices;
- return new cc.Vertex3F(locOriginalVertices[index], locOriginalVertices[index + 1], locOriginalVertices[index + 2]);
- },
- /**
- * sets a new vertex at a given position
- * @param {cc.Point} pos
- * @param {cc.Vertex3F} vertex
- */
- setVertex:function (pos, vertex) {
- if(pos.x !== (0| pos.x) || pos.y !== (0| pos.y))
- cc.log("cc.Grid3D.setVertex() : Numbers must be integers");
- var index = 0 | ((pos.x * (this._gridSize.height + 1) + pos.y) * 3);
- var vertArray = this._vertices;
- vertArray[index] = vertex.x;
- vertArray[index + 1] = vertex.y;
- vertArray[index + 2] = vertex.z;
- this._dirty = true;
- },
- blit:function () {
- var n = this._gridSize.width * this._gridSize.height;
- cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_TEX_COORDS);
- this._shaderProgram.use();
- this._shaderProgram.setUniformsForBuiltins();
- var gl = cc.renderContext, locDirty = this._dirty;
- //
- // Attributes
- //
- // position
- gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer);
- if (locDirty)
- gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW);
- gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 3, gl.FLOAT, false, 0, 0);
- // texCoords
- gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer);
- if (locDirty)
- gl.bufferData(gl.ARRAY_BUFFER, this._texCoordinates, gl.DYNAMIC_DRAW);
- gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 0, 0);
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer);
- if (locDirty)
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indices, gl.STATIC_DRAW);
- gl.drawElements(gl.TRIANGLES, n * 6, gl.UNSIGNED_SHORT, 0);
- if (locDirty)
- this._dirty = false;
- cc.INCREMENT_GL_DRAWS(1);
- },
- reuse:function () {
- if (this._reuseGrid > 0) {
- var locOriginalVertices = this._originalVertices, locVertices = this._vertices;
- for (var i = 0, len = this._vertices.length; i < len; i++)
- locOriginalVertices[i] = locVertices[i];
- --this._reuseGrid;
- }
- },
- calculateVertexPoints:function () {
- var gl = cc.renderContext;
- var width = this._texture.getPixelsWide();
- var height = this._texture.getPixelsHigh();
- var imageH = this._texture.getContentSizeInPixels().height;
- var locGridSize = this._gridSize;
- var numOfPoints = (locGridSize.width + 1) * (locGridSize.height + 1);
- this._vertices = new Float32Array(numOfPoints * 3);
- this._texCoordinates = new Float32Array(numOfPoints * 2);
- this._indices = new Uint16Array(locGridSize.width * locGridSize.height * 6);
- if(this._verticesBuffer)
- gl.deleteBuffer(this._verticesBuffer);
- this._verticesBuffer = gl.createBuffer();
- if(this._texCoordinateBuffer)
- gl.deleteBuffer(this._texCoordinateBuffer);
- this._texCoordinateBuffer = gl.createBuffer();
- if(this._indicesBuffer)
- gl.deleteBuffer(this._indicesBuffer);
- this._indicesBuffer = gl.createBuffer();
- var x, y, i, locIndices = this._indices, locTexCoordinates = this._texCoordinates;
- var locIsTextureFlipped = this._isTextureFlipped, locVertices = this._vertices;
- for (x = 0; x < locGridSize.width; ++x) {
- for (y = 0; y < locGridSize.height; ++y) {
- var idx = (y * locGridSize.width) + x;
- var x1 = x * this._step.x;
- var x2 = x1 + this._step.x;
- var y1 = y * this._step.y;
- var y2 = y1 + this._step.y;
- var a = (x * (locGridSize.height + 1) + y);
- var b = ((x + 1) * (locGridSize.height + 1) + y);
- var c = ((x + 1) * (locGridSize.height + 1) + (y + 1));
- var d = (x * (locGridSize.height + 1) + (y + 1));
- locIndices[idx * 6] = a;
- locIndices[idx * 6 + 1] = b;
- locIndices[idx * 6 + 2] = d;
- locIndices[idx * 6 + 3] = b;
- locIndices[idx * 6 + 4] = c;
- locIndices[idx * 6 + 5] = d;
- var l1 = [a * 3, b * 3, c * 3, d * 3];
- var e = {x:x1, y:y1, z:0}; //new cc.Vertex3F(x1, y1, 0);
- var f = {x:x2, y:y1, z:0}; //new cc.Vertex3F(x2, y1, 0);
- var g = {x:x2, y:y2, z:0}; // new cc.Vertex3F(x2, y2, 0);
- var h = {x:x1, y:y2, z:0}; //new cc.Vertex3F(x1, y2, 0);
- var l2 = [e, f, g, h];
- var tex1 = [a * 2, b * 2, c * 2, d * 2];
- var tex2 = [cc.p(x1, y1), cc.p(x2, y1), cc.p(x2, y2), cc.p(x1, y2)];
- for (i = 0; i < 4; ++i) {
- locVertices[l1[i]] = l2[i].x;
- locVertices[l1[i] + 1] = l2[i].y;
- locVertices[l1[i] + 2] = l2[i].z;
- locTexCoordinates[tex1[i]] = tex2[i].x / width;
- if (locIsTextureFlipped)
- locTexCoordinates[tex1[i] + 1] = (imageH - tex2[i].y) / height;
- else
- locTexCoordinates[tex1[i] + 1] = tex2[i].y / height;
- }
- }
- }
- this._originalVertices = new Float32Array(this._vertices);
- gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer);
- gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW);
- gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer);
- gl.bufferData(gl.ARRAY_BUFFER, this._texCoordinates, gl.DYNAMIC_DRAW);
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer);
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indices, gl.STATIC_DRAW);
- this._dirty = true;
- }
- });
- /**
- * create one Grid3D object
- * @param {cc.Size} gridSize
- * @param {cc.Texture2D} [texture=]
- * @param {Boolean} [flipped=]
- * @return {cc.Grid3D}
- */
- cc.Grid3D.create = function (gridSize, texture, flipped) {
- var grid3D = new cc.Grid3D();
- if (grid3D && grid3D.initWithSize(gridSize, texture, flipped))
- return grid3D;
- return null;
- };
- /**
- * cc.TiledGrid3D is a 3D grid implementation. It differs from Grid3D in that <br/>
- * the tiles can be separated from the grid.
- * @class
- * @extends cc.GridBase
- */
- cc.TiledGrid3D = cc.GridBase.extend(/** @lends cc.TiledGrid3D# */{
- _texCoordinates:null,
- _vertices:null,
- _originalVertices:null,
- _indices:null,
- _texCoordinateBuffer:null,
- _verticesBuffer:null,
- _indicesBuffer:null,
- ctor:function () {
- cc.GridBase.prototype.ctor.call(this);
- this._texCoordinates=null;
- this._vertices=null;
- this._originalVertices=null;
- this._indices=null;
- this._texCoordinateBuffer=null;
- this._verticesBuffer=null;
- this._indicesBuffer=null;
- },
- /**
- * returns the tile at the given position
- * @param {cc.Point} pos
- * @return {cc.Quad3}
- */
- tile:function (pos) {
- if(pos.x !== (0| pos.x) || pos.y !== (0| pos.y))
- cc.log("cc.TiledGrid3D.tile() : Numbers must be integers");
- var idx = (this._gridSize.height * pos.x + pos.y) * 4 * 3;
- var locVertices = this._vertices;
- return new cc.Quad3(new cc.Vertex3F(locVertices[idx], locVertices[idx + 1], locVertices[idx + 2]),
- new cc.Vertex3F(locVertices[idx + 3], locVertices[idx + 4], locVertices[idx + 5]),
- new cc.Vertex3F(locVertices[idx + 6 ], locVertices[idx + 7], locVertices[idx + 8]),
- new cc.Vertex3F(locVertices[idx + 9], locVertices[idx + 10], locVertices[idx + 11]));
- },
- /**
- * returns the original tile (untransformed) at the given position
- * @param {cc.Point} pos
- * @return {cc.Quad3}
- */
- originalTile:function (pos) {
- if(pos.x !== (0| pos.x) || pos.y !== (0| pos.y))
- cc.log("cc.TiledGrid3D.originalTile() : Numbers must be integers");
- var idx = (this._gridSize.height * pos.x + pos.y) * 4 * 3;
- var locOriginalVertices = this._originalVertices;
- return new cc.Quad3(new cc.Vertex3F(locOriginalVertices[idx], locOriginalVertices[idx + 1], locOriginalVertices[idx + 2]),
- new cc.Vertex3F(locOriginalVertices[idx + 3], locOriginalVertices[idx + 4], locOriginalVertices[idx + 5]),
- new cc.Vertex3F(locOriginalVertices[idx + 6 ], locOriginalVertices[idx + 7], locOriginalVertices[idx + 8]),
- new cc.Vertex3F(locOriginalVertices[idx + 9], locOriginalVertices[idx + 10], locOriginalVertices[idx + 11]));
- },
- /**
- * sets a new tile
- * @param {cc.Point} pos
- * @param {cc.Quad3} coords
- */
- setTile:function (pos, coords) {
- if(pos.x !== (0| pos.x) || pos.y !== (0| pos.y))
- cc.log("cc.TiledGrid3D.setTile() : Numbers must be integers");
- var idx = (this._gridSize.height * pos.x + pos.y) * 12;
- var locVertices = this._vertices;
- locVertices[idx] = coords.bl.x;
- locVertices[idx + 1] = coords.bl.y;
- locVertices[idx + 2] = coords.bl.z;
- locVertices[idx + 3] = coords.br.x;
- locVertices[idx + 4] = coords.br.y;
- locVertices[idx + 5] = coords.br.z;
- locVertices[idx + 6] = coords.tl.x;
- locVertices[idx + 7] = coords.tl.y;
- locVertices[idx + 8] = coords.tl.z;
- locVertices[idx + 9] = coords.tr.x;
- locVertices[idx + 10] = coords.tr.y;
- locVertices[idx + 11] = coords.tr.z;
- this._dirty = true;
- },
- blit:function () {
- var n = this._gridSize.width * this._gridSize.height;
- this._shaderProgram.use();
- this._shaderProgram.setUniformsForBuiltins();
- //
- // Attributes
- //
- var gl = cc.renderContext, locDirty = this._dirty;
- cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_TEX_COORDS);
- // position
- gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer);
- if (locDirty)
- gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW);
- gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 3, gl.FLOAT, false, 0, this._vertices);
- // texCoords
- gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer);
- if (locDirty)
- gl.bufferData(gl.ARRAY_BUFFER, this._texCoordinates, gl.DYNAMIC_DRAW);
- gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 0, this._texCoordinates);
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer);
- if (locDirty)
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indices, gl.STATIC_DRAW);
- gl.drawElements(gl.TRIANGLES, n * 6, gl.UNSIGNED_SHORT, 0);
- if (locDirty)
- this._dirty = false;
- cc.INCREMENT_GL_DRAWS(1);
- },
- reuse:function () {
- if (this._reuseGrid > 0) {
- var locVertices = this._vertices, locOriginalVertices = this._originalVertices;
- for (var i = 0; i < locVertices.length; i++)
- locOriginalVertices[i] = locVertices[i];
- --this._reuseGrid;
- }
- },
- calculateVertexPoints:function () {
- var width = this._texture.getPixelsWide();
- var height = this._texture.getPixelsHigh();
- var imageH = this._texture.getContentSizeInPixels().height;
- var locGridSize = this._gridSize;
- var numQuads = locGridSize.width * locGridSize.height;
- this._vertices = new Float32Array(numQuads * 12);
- this._texCoordinates = new Float32Array(numQuads * 8);
- this._indices = new Uint16Array(numQuads * 6);
- var gl = cc.renderContext;
- if(this._verticesBuffer)
- gl.deleteBuffer(this._verticesBuffer);
- this._verticesBuffer = gl.createBuffer();
- if(this._texCoordinateBuffer)
- gl.deleteBuffer(this._texCoordinateBuffer);
- this._texCoordinateBuffer = gl.createBuffer();
- if(this._indicesBuffer)
- gl.deleteBuffer(this._indicesBuffer);
- this._indicesBuffer = gl.createBuffer();
- var x, y, i = 0;
- var locStep = this._step, locVertices = this._vertices, locTexCoords = this._texCoordinates, locIsTextureFlipped = this._isTextureFlipped;
- for (x = 0; x < locGridSize.width; x++) {
- for (y = 0; y < locGridSize.height; y++) {
- var x1 = x * locStep.x;
- var x2 = x1 + locStep.x;
- var y1 = y * locStep.y;
- var y2 = y1 + locStep.y;
- locVertices[i * 12] = x1;
- locVertices[i * 12 + 1] = y1;
- locVertices[i * 12 + 2] = 0;
- locVertices[i * 12 + 3] = x2;
- locVertices[i * 12 + 4] = y1;
- locVertices[i * 12 + 5] = 0;
- locVertices[i * 12 + 6] = x1;
- locVertices[i * 12 + 7] = y2;
- locVertices[i * 12 + 8] = 0;
- locVertices[i * 12 + 9] = x2;
- locVertices[i * 12 + 10] = y2;
- locVertices[i * 12 + 11] = 0;
- var newY1 = y1;
- var newY2 = y2;
- if (locIsTextureFlipped) {
- newY1 = imageH - y1;
- newY2 = imageH - y2;
- }
- locTexCoords[i * 8] = x1 / width;
- locTexCoords[i * 8 + 1] = newY1 / height;
- locTexCoords[i * 8 + 2] = x2 / width;
- locTexCoords[i * 8 + 3] = newY1 / height;
- locTexCoords[i * 8 + 4] = x1 / width;
- locTexCoords[i * 8 + 5] = newY2 / height;
- locTexCoords[i * 8 + 6] = x2 / width;
- locTexCoords[i * 8 + 7] = newY2 / height;
- i++;
- }
- }
- var locIndices = this._indices;
- for (x = 0; x < numQuads; x++) {
- locIndices[x * 6 + 0] = (x * 4 + 0);
- locIndices[x * 6 + 1] = (x * 4 + 1);
- locIndices[x * 6 + 2] = (x * 4 + 2);
- locIndices[x * 6 + 3] = (x * 4 + 1);
- locIndices[x * 6 + 4] = (x * 4 + 2);
- locIndices[x * 6 + 5] = (x * 4 + 3);
- }
- this._originalVertices = new Float32Array(this._vertices);
- gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer);
- gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW);
- gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer);
- gl.bufferData(gl.ARRAY_BUFFER, this._texCoordinates, gl.DYNAMIC_DRAW);
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer);
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indices, gl.DYNAMIC_DRAW);
- this._dirty = true;
- }
- });
- /**
- * create one TiledGrid3D object
- * @param {cc.Size} gridSize
- * @param {cc.Texture2D} [texture=]
- * @param {Boolean} [flipped=]
- * @return {cc.TiledGrid3D}
- */
- cc.TiledGrid3D.create = function (gridSize, texture, flipped) {
- var ret = new cc.TiledGrid3D();
- ret.initWithSize(gridSize, texture, flipped);
- return ret;
- };
|