12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049 |
- /****************************************************************************
- Copyright (c) 2010-2012 cocos2d-x.org
- 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.
- ****************************************************************************/
- /**
- * <p>
- * Drawing primitives Utility Class. this class is base class, it contain some render type version: Canvas, WebGL, DOM.<br/>
- * this class contain some primitive Drawing Method: <br/>
- * - drawPoint<br/>
- * - drawLine<br/>
- * - drawPoly<br/>
- * - drawCircle<br/>
- * - drawQuadBezier<br/>
- * - drawCubicBezier<br/>
- * You can change the color, width and other property by calling these WebGL API:<br/>
- * glColor4ub(), glLineWidth(), glPointSize().<br/>
- * </p>
- * @class
- * @extends cc.Class
- * @warning These functions draws the Line, Point, Polygon, immediately. They aren't batched. <br/>
- * If you are going to make a game that depends on these primitives, I suggest creating a batch.
- */
- cc.DrawingPrimitive = cc.Class.extend(/** @lends cc.DrawingPrimitive# */{
- _renderContext:null,
- /**
- * set render context of drawing primitive
- * @param context
- */
- setRenderContext:function (context) {
- this._renderContext = context;
- },
- /**
- * returns render context of drawing primitive
- * @return {CanvasRenderingContext2D}
- */
- getRenderContext:function () {
- return this._renderContext;
- },
- /**
- * Constructor
- * @param {CanvasRenderingContext2D} renderContext
- */
- ctor:function (renderContext) {
- this._renderContext = renderContext;
- },
- /**
- * draws a point given x and y coordinate measured in points
- * @param {cc.Point} point
- */
- drawPoint:function (point) {
- cc.log("DrawingPrimitive.drawPoint() not implement!");
- },
- /**
- * draws an array of points.
- * @param {Array} points point of array
- * @param {Number} numberOfPoints
- */
- drawPoints:function (points, numberOfPoints) {
- cc.log("DrawingPrimitive.drawPoints() not implement!");
- },
- /**
- * draws a line given the origin and destination point measured in points
- * @param {cc.Point} origin
- * @param {cc.Point} destination
- */
- drawLine:function (origin, destination) {
- cc.log("DrawingPrimitive.drawLine() not implement!");
- },
- /**
- * draws a rectangle given the origin and destination point measured in points.
- * @param {cc.Point} origin
- * @param {cc.Point} destination
- */
- drawRect:function (origin, destination) {
- cc.log("DrawingPrimitive.drawRect() not implement!");
- },
- /**
- * draws a solid rectangle given the origin and destination point measured in points.
- * @param {cc.Point} origin
- * @param {cc.Point} destination
- * @param {cc.Color4F} color
- */
- drawSolidRect:function (origin, destination, color) {
- cc.log("DrawingPrimitive.drawSolidRect() not implement!");
- },
- /**
- * draws a poligon given a pointer to cc.Point coordiantes and the number of vertices measured in points.
- * @param {Array} vertices a pointer to cc.Point coordiantes
- * @param {Number} numOfVertices the number of vertices measured in points
- * @param {Boolean} closePolygon The polygon can be closed or open
- * @param {Boolean} fill The polygon can be closed or open and optionally filled with current color
- */
- drawPoly:function (vertices, numOfVertices, closePolygon, fill) {
- cc.log("DrawingPrimitive.drawPoly() not implement!");
- },
- /**
- * draws a solid polygon given a pointer to CGPoint coordiantes, the number of vertices measured in points, and a color.
- * @param {Array} poli
- * @param {Number} numberOfPoints
- * @param {cc.Color4F} color
- */
- drawSolidPoly:function (poli, numberOfPoints, color) {
- cc.log("DrawingPrimitive.drawSolidPoly() not implement!");
- },
- /**
- * draws a circle given the center, radius and number of segments.
- * @param {cc.Point} center center of circle
- * @param {Number} radius
- * @param {Number} angle angle in radians
- * @param {Number} segments
- * @param {Boolean} drawLineToCenter
- */
- drawCircle:function (center, radius, angle, segments, drawLineToCenter) {
- cc.log("DrawingPrimitive.drawCircle() not implement!");
- },
- /**
- * draws a quad bezier path
- * @param {cc.Point} origin
- * @param {cc.Point} control
- * @param {cc.Point} destination
- * @param {Number} segments
- */
- drawQuadBezier:function (origin, control, destination, segments) {
- cc.log("DrawingPrimitive.drawQuadBezier() not implement!");
- },
- /**
- * draws a cubic bezier path
- * @param {cc.Point} origin
- * @param {cc.Point} control1
- * @param {cc.Point} control2
- * @param {cc.Point} destination
- * @param {Number} segments
- */
- drawCubicBezier:function (origin, control1, control2, destination, segments) {
- cc.log("DrawingPrimitive.drawCubicBezier() not implement!");
- },
- /**
- * draw a catmull rom line
- * @param {Array} points
- * @param {Number} segments
- */
- drawCatmullRom:function (points, segments) {
- cc.log("DrawingPrimitive.drawCardinalSpline() not implement!");
- },
- /**
- * draw a cardinal spline path
- * @param {Array} config
- * @param {Number} tension
- * @param {Number} segments
- */
- drawCardinalSpline:function (config, tension, segments) {
- cc.log("DrawingPrimitive.drawCardinalSpline() not implement!");
- }
- });
- /**
- * Canvas of DrawingPrimitive implement version
- * @class
- * @extends cc.DrawingPrimitive
- */
- cc.DrawingPrimitiveCanvas = cc.DrawingPrimitive.extend(/** @lends cc.DrawingPrimitiveCanvas# */{
- _cacheArray:[],
- /**
- * draws a point given x and y coordinate measured in points
- * @override
- * @param {cc.Point} point
- * @param {Number} size
- */
- drawPoint:function (point, size) {
- if (!size) {
- size = 1;
- }
- var locScaleX = cc.EGLView.getInstance().getScaleX(), locScaleY = cc.EGLView.getInstance().getScaleY();
- var newPoint = cc.p(point.x * locScaleX, point.y * locScaleY);
- this._renderContext.beginPath();
- this._renderContext.arc(newPoint.x, -newPoint.y, size * locScaleX, 0, Math.PI * 2, false);
- this._renderContext.closePath();
- this._renderContext.fill();
- },
- /**
- * draws an array of points.
- * @override
- * @param {Array} points point of array
- * @param {Number} numberOfPoints
- * @param {Number} size
- */
- drawPoints:function (points, numberOfPoints, size) {
- if (points == null) {
- return;
- }
- if (!size) {
- size = 1;
- }
- var locContext = this._renderContext,locScaleX = cc.EGLView.getInstance().getScaleX(), locScaleY = cc.EGLView.getInstance().getScaleY();
- locContext.beginPath();
- for (var i = 0, len = points.length; i < len; i++)
- locContext.arc(points[i].x * locScaleX, -points[i].y * locScaleY, size * locScaleX, 0, Math.PI * 2, false);
- locContext.closePath();
- locContext.fill();
- },
- /**
- * draws a line given the origin and destination point measured in points
- * @override
- * @param {cc.Point} origin
- * @param {cc.Point} destination
- */
- drawLine:function (origin, destination) {
- var locContext = this._renderContext, locScaleX = cc.EGLView.getInstance().getScaleX(), locScaleY = cc.EGLView.getInstance().getScaleY();
- locContext.beginPath();
- locContext.moveTo(origin.x * locScaleX, -origin.y * locScaleY);
- locContext.lineTo(destination.x * locScaleX, -destination.y * locScaleY);
- locContext.closePath();
- locContext.stroke();
- },
- /**
- * draws a rectangle given the origin and destination point measured in points.
- * @param {cc.Point} origin
- * @param {cc.Point} destination
- */
- drawRect:function (origin, destination) {
- this.drawLine(cc.p(origin.x, origin.y), cc.p(destination.x, origin.y));
- this.drawLine(cc.p(destination.x, origin.y), cc.p(destination.x, destination.y));
- this.drawLine(cc.p(destination.x, destination.y), cc.p(origin.x, destination.y));
- this.drawLine(cc.p(origin.x, destination.y), cc.p(origin.x, origin.y));
- },
- /**
- * draws a solid rectangle given the origin and destination point measured in points.
- * @param {cc.Point} origin
- * @param {cc.Point} destination
- * @param {cc.Color4F} color
- */
- drawSolidRect:function (origin, destination, color) {
- var vertices = [
- origin,
- cc.p(destination.x, origin.y),
- destination,
- cc.p(origin.x, destination.y)
- ];
- this.drawSolidPoly(vertices, 4, color);
- },
- /**
- * draws a polygon given a pointer to cc.Point coordinates and the number of vertices measured in points.
- * @override
- * @param {Array} vertices a pointer to cc.Point coordinates
- * @param {Number} numOfVertices the number of vertices measured in points
- * @param {Boolean} closePolygon The polygon can be closed or open
- * @param {Boolean} [fill=] The polygon can be closed or open and optionally filled with current color
- */
- drawPoly:function (vertices, numOfVertices, closePolygon, fill) {
- fill = fill || false;
- if (vertices == null)
- return;
- if (vertices.length < 3)
- throw new Error("Polygon's point must greater than 2");
- var firstPoint = vertices[0], locContext = this._renderContext;
- var locScaleX = cc.EGLView.getInstance().getScaleX(), locScaleY = cc.EGLView.getInstance().getScaleY();
- locContext.beginPath();
- locContext.moveTo(firstPoint.x * locScaleX, -firstPoint.y * locScaleY);
- for (var i = 1, len = vertices.length; i < len; i++)
- locContext.lineTo(vertices[i].x * locScaleX, -vertices[i].y * locScaleY);
- if (closePolygon)
- locContext.closePath();
- if (fill)
- locContext.fill();
- else
- locContext.stroke();
- },
- /**
- * draws a solid polygon given a pointer to CGPoint coordinates, the number of vertices measured in points, and a color.
- * @param {Array} polygons
- * @param {Number} numberOfPoints
- * @param {cc.Color4F} color
- */
- drawSolidPoly:function (polygons, numberOfPoints, color) {
- this.setDrawColor4F(color.r, color.g, color.b, color.a);
- this.drawPoly(polygons, numberOfPoints, true, true);
- },
- /**
- * draws a circle given the center, radius and number of segments.
- * @override
- * @param {cc.Point} center center of circle
- * @param {Number} radius
- * @param {Number} angle angle in radians
- * @param {Number} segments
- * @param {Boolean} [drawLineToCenter=]
- */
- drawCircle: function (center, radius, angle, segments, drawLineToCenter) {
- drawLineToCenter = drawLineToCenter || false;
- var locContext = this._renderContext;
- var locScaleX = cc.EGLView.getInstance().getScaleX(), locScaleY = cc.EGLView.getInstance().getScaleY();
- locContext.beginPath();
- var endAngle = angle - Math.PI * 2;
- locContext.arc(0 | (center.x * locScaleX), 0 | -(center.y * locScaleY), radius * locScaleX, -angle, -endAngle, false);
- if (drawLineToCenter) {
- locContext.lineTo(0 | (center.x * locScaleX), 0 | -(center.y * locScaleY));
- }
- locContext.stroke();
- },
- /**
- * draws a quad bezier path
- * @override
- * @param {cc.Point} origin
- * @param {cc.Point} control
- * @param {cc.Point} destination
- * @param {Number} segments
- */
- drawQuadBezier:function (origin, control, destination, segments) {
- //this is OpenGL Algorithm
- var vertices = this._cacheArray;
- vertices.length =0;
- var t = 0.0;
- for (var i = 0; i < segments; i++) {
- var x = Math.pow(1 - t, 2) * origin.x + 2.0 * (1 - t) * t * control.x + t * t * destination.x;
- var y = Math.pow(1 - t, 2) * origin.y + 2.0 * (1 - t) * t * control.y + t * t * destination.y;
- vertices.push(cc.p(x, y));
- t += 1.0 / segments;
- }
- vertices.push(cc.p(destination.x, destination.y));
- this.drawPoly(vertices, segments + 1, false, false);
- },
- /**
- * draws a cubic bezier path
- * @override
- * @param {cc.Point} origin
- * @param {cc.Point} control1
- * @param {cc.Point} control2
- * @param {cc.Point} destination
- * @param {Number} segments
- */
- drawCubicBezier:function (origin, control1, control2, destination, segments) {
- //this is OpenGL Algorithm
- var vertices = this._cacheArray;
- vertices.length =0;
- var t = 0;
- for (var i = 0; i < segments; i++) {
- var x = Math.pow(1 - t, 3) * origin.x + 3.0 * Math.pow(1 - t, 2) * t * control1.x + 3.0 * (1 - t) * t * t * control2.x + t * t * t * destination.x;
- var y = Math.pow(1 - t, 3) * origin.y + 3.0 * Math.pow(1 - t, 2) * t * control1.y + 3.0 * (1 - t) * t * t * control2.y + t * t * t * destination.y;
- vertices.push(cc.p(x , y ));
- t += 1.0 / segments;
- }
- vertices.push(cc.p(destination.x , destination.y));
- this.drawPoly(vertices, segments + 1, false, false);
- },
- /**
- * draw a CatmullRom curve
- * @override
- * @param {Array} points
- * @param {Number} segments
- */
- drawCatmullRom:function (points, segments) {
- this.drawCardinalSpline(points, 0.5, segments);
- },
- /**
- * draw a cardinal spline path
- * @override
- * @param {Array} config
- * @param {Number} tension
- * @param {Number} segments
- */
- drawCardinalSpline:function (config, tension, segments) {
- //lazy_init();
- cc.renderContext.strokeStyle = "rgba(255,255,255,1)";
- var points = this._cacheArray;
- points.length = 0;
- var p, lt;
- var deltaT = 1.0 / config.length;
- for (var i = 0; i < segments + 1; i++) {
- var dt = i / segments;
- // border
- if (dt == 1) {
- p = config.length - 1;
- lt = 1;
- } else {
- p = 0 | (dt / deltaT);
- lt = (dt - deltaT * p) / deltaT;
- }
- // Interpolate
- var newPos = cc.CardinalSplineAt(
- cc.getControlPointAt(config, p - 1),
- cc.getControlPointAt(config, p - 0),
- cc.getControlPointAt(config, p + 1),
- cc.getControlPointAt(config, p + 2),
- tension, lt);
- points.push(newPos);
- }
- this.drawPoly(points, segments + 1, false, false);
- },
- /**
- * draw an image
- * @override
- * @param {HTMLImageElement|HTMLCanvasElement} image
- * @param {cc.Point} sourcePoint
- * @param {cc.Size} sourceSize
- * @param {cc.Point} destPoint
- * @param {cc.Size} destSize
- */
- drawImage:function (image, sourcePoint, sourceSize, destPoint, destSize) {
- var len = arguments.length;
- switch (len) {
- case 2:
- var height = image.height;
- this._renderContext.drawImage(image, sourcePoint.x, -(sourcePoint.y + height));
- break;
- case 3:
- this._renderContext.drawImage(image, sourcePoint.x, -(sourcePoint.y + sourceSize.height), sourceSize.width, sourceSize.height);
- break;
- case 5:
- this._renderContext.drawImage(image, sourcePoint.x, sourcePoint.y, sourceSize.width, sourceSize.height, destPoint.x, -(destPoint.y + destSize.height),
- destSize.width, destSize.height);
- break;
- default:
- throw new Error("Argument must be non-nil");
- break;
- }
- },
- /**
- * draw a star
- * @param {CanvasRenderingContext2D} ctx canvas context
- * @param {Number} radius
- * @param {cc.Color3B|cc.Color4B|cc.Color4F} color
- */
- drawStar:function (ctx, radius, color) {
- var context = ctx || this._renderContext;
- radius *= cc.EGLView.getInstance().getScaleX();
- if (color instanceof cc.Color4F)
- color = new cc.Color3B(0 | (color.r * 255), 0 | (color.g * 255), 0 | (color.b * 255));
- var colorStr = "rgba(" + color.r + "," + color.g + "," + color.b;
- context.fillStyle = colorStr + ",1)";
- var subRadius = radius / 10;
- context.beginPath();
- context.moveTo(-radius, radius);
- context.lineTo(0, subRadius);
- context.lineTo(radius, radius);
- context.lineTo(subRadius, 0);
- context.lineTo(radius, -radius);
- context.lineTo(0, -subRadius);
- context.lineTo(-radius, -radius);
- context.lineTo(-subRadius, 0);
- context.lineTo(-radius, radius);
- context.closePath();
- context.fill();
- var g1 = context.createRadialGradient(0, 0, subRadius, 0, 0, radius);
- g1.addColorStop(0, colorStr + ", 1)");
- g1.addColorStop(0.3, colorStr + ", 0.8)");
- g1.addColorStop(1.0, colorStr + ", 0.0)");
- context.fillStyle = g1;
- context.beginPath();
- var startAngle_1 = 0;
- var endAngle_1 = cc.PI2;
- context.arc(0, 0, radius - subRadius, startAngle_1, endAngle_1, false);
- context.closePath();
- context.fill();
- },
- /**
- * draw a color ball
- * @param {CanvasRenderingContext2D} ctx canvas context
- * @param {Number} radius
- * @param {cc.Color3B|cc.Color4B|cc.Color4F} color
- */
- drawColorBall:function (ctx, radius, color) {
- var context = ctx || this._renderContext;
- radius *= cc.EGLView.getInstance().getScaleX();
- if (color instanceof cc.Color4F)
- color = new cc.Color3B(0 | (color.r * 255), 0 | (color.g * 255), 0 | (color.b * 255));
- var colorStr = "rgba(" + color.r + "," + color.g + "," + color.b;
- var subRadius = radius / 10;
- var g1 = context.createRadialGradient(0, 0, subRadius, 0, 0, radius);
- g1.addColorStop(0, colorStr + ", 1)");
- g1.addColorStop(0.3, colorStr + ", 0.8)");
- g1.addColorStop(0.6, colorStr + ", 0.4)");
- g1.addColorStop(1.0, colorStr + ", 0.0)");
- context.fillStyle = g1;
- context.beginPath();
- var startAngle_1 = 0;
- var endAngle_1 = cc.PI2;
- context.arc(0, 0, radius, startAngle_1, endAngle_1, false);
- context.closePath();
- context.fill();
- },
- /**
- * fill text
- * @param {String} strText
- * @param {Number} x
- * @param {Number} y
- */
- fillText:function (strText, x, y) {
- this._renderContext.fillText(strText, x, -y);
- },
- /**
- * set the drawing color with 4 unsigned bytes
- * @param {Number} r red value (0 to 255)
- * @param {Number} g green value (0 to 255)
- * @param {Number} b blue value (0 to 255)
- * @param {Number} a Alpha value (0 to 255)
- */
- setDrawColor4B:function (r, g, b, a) {
- this._renderContext.fillStyle = "rgba(" + r + "," + g + "," + b + "," + a / 255 + ")";
- this._renderContext.strokeStyle = "rgba(" + r + "," + g + "," + b + "," + a / 255 + ")";
- },
- /**
- * set the drawing color with 4 floats
- * @param {Number} r red value (0 to 1)
- * @param {Number} g green value (0 to 1)
- * @param {Number} b blue value (0 to 1)
- * @param {Number} a Alpha value (0 to 1)
- */
- setDrawColor4F:function (r, g, b, a) {
- this._renderContext.fillStyle = "rgba(" + (0 | (r * 255)) + "," + (0 | (g * 255)) + "," + (0 | (b * 255)) + "," + a + ")";
- this._renderContext.strokeStyle = "rgba(" + (0 | (r * 255)) + "," + (0 | (g * 255)) + "," + (0 | (b * 255)) + "," + a + ")";
- },
- /**
- * set the point size in points. Default 1.
- * @param {Number} pointSize
- */
- setPointSize:function (pointSize) {
- },
- /**
- * set the line width. Default 1.
- * @param {Number} width
- */
- setLineWidth:function (width) {
- this._renderContext.lineWidth = width * cc.EGLView.getInstance().getScaleX();
- }
- });
- /**
- * Canvas of DrawingPrimitive implement version
- * @class
- * @extends cc.DrawingPrimitive
- */
- cc.DrawingPrimitiveWebGL = cc.DrawingPrimitive.extend({
- _initialized:false,
- _shader: null,
- _colorLocation:-1,
- _color: null,
- _pointSizeLocation:-1,
- _pointSize:-1,
- ctor:function (ctx) {
- if (ctx == null)
- ctx = cc.renderContext;
- if (!ctx instanceof WebGLRenderingContext)
- throw "Can't initialise DrawingPrimitiveWebGL. context need is WebGLRenderingContext";
- cc.DrawingPrimitive.prototype.ctor.call(this, ctx);
- this._color = new cc.Color4F(1.0, 1.0, 1.0, 1.0);
- },
- lazy_init:function () {
- if (!this._initialized) {
- //
- // Position and 1 color passed as a uniform (to similate glColor4ub )
- //
- this._shader = cc.ShaderCache.getInstance().programForKey(cc.SHADER_POSITION_UCOLOR);
- this._colorLocation = this._renderContext.getUniformLocation(this._shader.getProgram(), "u_color");
- this._pointSizeLocation = this._renderContext.getUniformLocation(this._shader.getProgram(), "u_pointSize");
- //cc.CHECK_GL_ERROR_DEBUG();
- this._initialized = true;
- }
- },
- /**
- * initlialize context
- */
- drawInit:function () {
- this._initialized = false;
- },
- /**
- * draws a point given x and y coordinate measured in points
- * @param {cc.Point} point
- */
- drawPoint:function (point) {
- this.lazy_init();
- this._shader.use();
- this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
- cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION);
- this._shader.setUniformLocationWith4fv(this._colorLocation, new Float32Array(this._color._arrayBuffer,0,4), 1);
- this._shader.setUniformLocationWith1f(this._pointSizeLocation, this._pointSize);
- var glContext = this._renderContext;
- var pointBuffer = glContext.createBuffer();
- glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer);
- glContext.bufferData(glContext.ARRAY_BUFFER, new Float32Array([point.x, point.y]), glContext.STATIC_DRAW);
- glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
- glContext.drawArrays(glContext.POINTS, 0, 1);
- glContext.deleteBuffer(pointBuffer);
- cc.INCREMENT_GL_DRAWS(1);
- },
- /**
- * draws an array of points.
- * @param {Array} points point of array
- * @param {Number} numberOfPoints
- */
- drawPoints:function (points, numberOfPoints) {
- if (!points || points.length == 0)
- return;
- this.lazy_init();
- this._shader.use();
- this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
- cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION);
- this._shader.setUniformLocationWith4fv(this._colorLocation, new Float32Array(this._color._arrayBuffer,0,4), 1);
- this._shader.setUniformLocationWith1f(this._pointSizeLocation, this._pointSize);
- var glContext = this._renderContext;
- var pointBuffer = glContext.createBuffer();
- glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer);
- glContext.bufferData(glContext.ARRAY_BUFFER, this._pointsToTypeArray(points), glContext.STATIC_DRAW);
- glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
- glContext.drawArrays(glContext.POINTS, 0, points.length);
- glContext.deleteBuffer(pointBuffer);
- cc.INCREMENT_GL_DRAWS(1);
- },
- _pointsToTypeArray:function (points) {
- var typeArr = new Float32Array(points.length * 2);
- for (var i = 0; i < points.length; i++) {
- typeArr[i * 2] = points[i].x;
- typeArr[i * 2 + 1] = points[i].y;
- }
- return typeArr;
- },
- /**
- * draws a line given the origin and destination point measured in points
- * @param {cc.Point} origin
- * @param {cc.Point} destination
- */
- drawLine:function (origin, destination) {
- this.lazy_init();
- this._shader.use();
- this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
- cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION);
- this._shader.setUniformLocationWith4fv(this._colorLocation, new Float32Array(this._color._arrayBuffer,0,4), 1);
- var glContext = this._renderContext;
- var pointBuffer = glContext.createBuffer();
- glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer);
- glContext.bufferData(glContext.ARRAY_BUFFER, this._pointsToTypeArray([origin, destination]), glContext.STATIC_DRAW);
- glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
- glContext.drawArrays(glContext.LINES, 0, 2);
- glContext.deleteBuffer(pointBuffer);
- cc.INCREMENT_GL_DRAWS(1);
- },
- /**
- * draws a rectangle given the origin and destination point measured in points.
- * @param {cc.Point} origin
- * @param {cc.Point} destination
- */
- drawRect:function (origin, destination) {
- this.drawLine(cc.p(origin.x, origin.y), cc.p(destination.x, origin.y));
- this.drawLine(cc.p(destination.x, origin.y), cc.p(destination.x, destination.y));
- this.drawLine(cc.p(destination.x, destination.y), cc.p(origin.x, destination.y));
- this.drawLine(cc.p(origin.x, destination.y), cc.p(origin.x, origin.y));
- },
- /**
- * draws a solid rectangle given the origin and destination point measured in points.
- * @param {cc.Point} origin
- * @param {cc.Point} destination
- * @param {cc.Color4F} color
- */
- drawSolidRect:function (origin, destination, color) {
- var vertices = [
- origin,
- cc.p(destination.x, origin.y),
- destination,
- cc.p(origin.x, destination.y)
- ];
- this.drawSolidPoly(vertices, 4, color);
- },
- /**
- * draws a polygon given a pointer to cc.Point coordiantes and the number of vertices measured in points.
- * @param {Array} vertices a pointer to cc.Point coordiantes
- * @param {Number} numOfVertices the number of vertices measured in points
- * @param {Boolean} closePolygon The polygon can be closed or open
- */
- drawPoly:function (vertices, numOfVertices, closePolygon) {
- this.lazy_init();
- this._shader.use();
- this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
- cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION);
- this._shader.setUniformLocationWith4fv(this._colorLocation, new Float32Array(this._color._arrayBuffer,0,4), 1);
- var glContext = this._renderContext;
- var pointBuffer = glContext.createBuffer();
- glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer);
- glContext.bufferData(glContext.ARRAY_BUFFER, this._pointsToTypeArray(vertices), glContext.STATIC_DRAW);
- glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
- if (closePolygon)
- glContext.drawArrays(glContext.LINE_LOOP, 0, vertices.length);
- else
- glContext.drawArrays(glContext.LINE_STRIP, 0, vertices.length);
- glContext.deleteBuffer(pointBuffer);
- cc.INCREMENT_GL_DRAWS(1);
- },
- /**
- * draws a solid polygon given a pointer to CGPoint coordiantes, the number of vertices measured in points, and a color.
- * @param {Array} poli
- * @param {Number} numberOfPoints
- * @param {cc.Color4F} color
- */
- drawSolidPoly:function (poli, numberOfPoints, color) {
- this.lazy_init();
- if (!color)
- color = this._color;
- this._shader.use();
- this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
- cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION);
- this._shader.setUniformLocationWith4fv(this._colorLocation, new Float32Array(color._arrayBuffer,0,4), 1);
- var glContext = this._renderContext;
- var pointBuffer = glContext.createBuffer();
- glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer);
- glContext.bufferData(glContext.ARRAY_BUFFER, this._pointsToTypeArray(poli), glContext.STATIC_DRAW);
- glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
- glContext.drawArrays(glContext.TRIANGLE_FAN, 0, poli.length);
- glContext.deleteBuffer(pointBuffer);
- cc.INCREMENT_GL_DRAWS(1);
- },
- /**
- * draws a circle given the center, radius and number of segments.
- * @param {cc.Point} center center of circle
- * @param {Number} radius
- * @param {Number} angle angle in radians
- * @param {Number} segments
- * @param {Boolean} drawLineToCenter
- */
- drawCircle:function (center, radius, angle, segments, drawLineToCenter) {
- this.lazy_init();
- var additionalSegment = 1;
- if (drawLineToCenter)
- additionalSegment++;
- var coef = 2.0 * Math.PI / segments;
- var vertices = new Float32Array((segments + 2) * 2);
- if (!vertices)
- return;
- for (var i = 0; i <= segments; i++) {
- var rads = i * coef;
- var j = radius * Math.cos(rads + angle) + center.x;
- var k = radius * Math.sin(rads + angle) + center.y;
- vertices[i * 2] = j;
- vertices[i * 2 + 1] = k;
- }
- vertices[(segments + 1) * 2] = center.x;
- vertices[(segments + 1) * 2 + 1] = center.y;
- this._shader.use();
- this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
- cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION);
- this._shader.setUniformLocationWith4fv(this._colorLocation, new Float32Array(this._color._arrayBuffer,0,4), 1);
- var glContext = this._renderContext;
- var pointBuffer = glContext.createBuffer();
- glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer);
- glContext.bufferData(glContext.ARRAY_BUFFER, vertices, glContext.STATIC_DRAW);
- glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
- glContext.drawArrays(glContext.LINE_STRIP, 0, segments + additionalSegment);
- glContext.deleteBuffer(pointBuffer);
- cc.INCREMENT_GL_DRAWS(1);
- },
- /**
- * draws a quad bezier path
- * @param {cc.Point} origin
- * @param {cc.Point} control
- * @param {cc.Point} destination
- * @param {Number} segments
- */
- drawQuadBezier:function (origin, control, destination, segments) {
- this.lazy_init();
- var vertices = new Float32Array((segments + 1) * 2);
- var t = 0.0;
- for (var i = 0; i < segments; i++) {
- vertices[i * 2] = Math.pow(1 - t, 2) * origin.x + 2.0 * (1 - t) * t * control.x + t * t * destination.x;
- vertices[i * 2 + 1] = Math.pow(1 - t, 2) * origin.y + 2.0 * (1 - t) * t * control.y + t * t * destination.y;
- t += 1.0 / segments;
- }
- vertices[segments * 2] = destination.x;
- vertices[segments * 2 + 1] = destination.y;
- this._shader.use();
- this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
- cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION);
- this._shader.setUniformLocationWith4fv(this._colorLocation, new Float32Array(this._color._arrayBuffer,0,4), 1);
- var glContext = this._renderContext;
- var pointBuffer = glContext.createBuffer();
- glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer);
- glContext.bufferData(glContext.ARRAY_BUFFER, vertices, glContext.STATIC_DRAW);
- glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
- glContext.drawArrays(glContext.LINE_STRIP, 0, segments + 1);
- glContext.deleteBuffer(pointBuffer);
- cc.INCREMENT_GL_DRAWS(1);
- },
- /**
- * draws a cubic bezier path
- * @param {cc.Point} origin
- * @param {cc.Point} control1
- * @param {cc.Point} control2
- * @param {cc.Point} destination
- * @param {Number} segments
- */
- drawCubicBezier:function (origin, control1, control2, destination, segments) {
- this.lazy_init();
- var vertices = new Float32Array((segments + 1) * 2);
- var t = 0;
- for (var i = 0; i < segments; i++) {
- vertices[i * 2] = Math.pow(1 - t, 3) * origin.x + 3.0 * Math.pow(1 - t, 2) * t * control1.x + 3.0 * (1 - t) * t * t * control2.x + t * t * t * destination.x;
- vertices[i * 2 + 1] = Math.pow(1 - t, 3) * origin.y + 3.0 * Math.pow(1 - t, 2) * t * control1.y + 3.0 * (1 - t) * t * t * control2.y + t * t * t * destination.y;
- t += 1.0 / segments;
- }
- vertices[segments * 2] = destination.x;
- vertices[segments * 2 + 1] = destination.y;
- this._shader.use();
- this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
- cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION);
- this._shader.setUniformLocationWith4fv(this._colorLocation, new Float32Array(this._color._arrayBuffer,0,4), 1);
- var glContext = this._renderContext;
- var pointBuffer = glContext.createBuffer();
- glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer);
- glContext.bufferData(glContext.ARRAY_BUFFER, vertices, glContext.STATIC_DRAW);
- glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
- glContext.drawArrays(glContext.LINE_STRIP, 0, segments + 1);
- glContext.deleteBuffer(pointBuffer);
- cc.INCREMENT_GL_DRAWS(1);
- },
- /**
- * draw a catmull rom line
- * @param {Array} points
- * @param {Number} segments
- */
- drawCatmullRom:function (points, segments) {
- this.drawCardinalSpline(points, 0.5, segments);
- },
- /**
- * draw a cardinal spline path
- * @param {Array} config
- * @param {Number} tension
- * @param {Number} segments
- */
- drawCardinalSpline:function (config, tension, segments) {
- this.lazy_init();
- var vertices = new Float32Array((segments + 1) * 2);
- var p, lt, deltaT = 1.0 / config.length;
- for (var i = 0; i < segments + 1; i++) {
- var dt = i / segments;
- // border
- if (dt == 1) {
- p = config.length - 1;
- lt = 1;
- } else {
- p = 0 | (dt / deltaT);
- lt = (dt - deltaT * p) / deltaT;
- }
- var newPos = cc.CardinalSplineAt(
- cc.getControlPointAt(config, p - 1),
- cc.getControlPointAt(config, p),
- cc.getControlPointAt(config, p + 1),
- cc.getControlPointAt(config, p + 2),
- tension, lt);
- // Interpolate
- vertices[i * 2] = newPos.x;
- vertices[i * 2 + 1] = newPos.y;
- }
- this._shader.use();
- this._shader.setUniformForModelViewAndProjectionMatrixWithMat4();
- cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION);
- this._shader.setUniformLocationWith4fv(this._colorLocation, new Float32Array(this._color._arrayBuffer,0,4), 1);
- var glContext = this._renderContext;
- var pointBuffer = glContext.createBuffer();
- glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer);
- glContext.bufferData(glContext.ARRAY_BUFFER, vertices, glContext.STATIC_DRAW);
- glContext.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0);
- glContext.drawArrays(glContext.LINE_STRIP, 0, segments + 1);
- glContext.deleteBuffer(pointBuffer);
- cc.INCREMENT_GL_DRAWS(1);
- },
- /**
- * set the drawing color with 4 unsigned bytes
- * @param {Number} r red value (0 to 255)
- * @param {Number} g green value (0 to 255)
- * @param {Number} b blue value (0 to 255)
- * @param {Number} a Alpha value (0 to 255)
- */
- setDrawColor4B:function (r, g, b, a) {
- this._color.r = r / 255.0;
- this._color.g = g / 255.0;
- this._color.b = b / 255.0;
- this._color.a = a / 255.0;
- },
- /**
- * set the drawing color with 4 floats
- * @param {Number} r red value (0 to 1)
- * @param {Number} g green value (0 to 1)
- * @param {Number} b blue value (0 to 1)
- * @param {Number} a Alpha value (0 to 1)
- */
- setDrawColor4F:function (r, g, b, a) {
- this._color.r = r;
- this._color.g = g;
- this._color.b = b;
- this._color.a = a;
- },
- /**
- * set the point size in points. Default 1.
- * @param {Number} pointSize
- */
- setPointSize:function (pointSize) {
- this._pointSize = pointSize * cc.CONTENT_SCALE_FACTOR();
- },
- /**
- * set the line width. Default 1.
- * @param {Number} width
- */
- setLineWidth:function (width) {
- if(this._renderContext.lineWidth)
- this._renderContext.lineWidth(width);
- }
- });
- cc.PI2 = Math.PI * 2;
|