diff options
author | hwc487 | 2012-03-06 16:03:26 -0800 |
---|---|---|
committer | hwc487 | 2012-03-06 16:03:26 -0800 |
commit | 855e8727b147771ff7b05e71bed481e65fe4b6b0 (patch) | |
tree | 60d7f091fcf0870495aeaa63748492dbd558619d /js/lib/geom/circle.js | |
parent | 1207735f05f202b5bdc5f70c73445f8e0934a227 (diff) | |
parent | 4f498b43264327f5886e0370bd3536453d47570a (diff) | |
download | ninja-855e8727b147771ff7b05e71bed481e65fe4b6b0.tar.gz |
Merge branch 'master' of github.com:Motorola-Mobility/ninja-internal into integration
Conflicts:
js/preloader/Preloader.js
Diffstat (limited to 'js/lib/geom/circle.js')
-rwxr-xr-x | js/lib/geom/circle.js | 751 |
1 files changed, 751 insertions, 0 deletions
diff --git a/js/lib/geom/circle.js b/js/lib/geom/circle.js new file mode 100755 index 00000000..dd82a4cc --- /dev/null +++ b/js/lib/geom/circle.js | |||
@@ -0,0 +1,751 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | var GeomObj = require("js/lib/geom/geom-obj").GeomObj; | ||
8 | var ShapePrimitive = require("js/lib/geom/shape-primitive").ShapePrimitive; | ||
9 | var MaterialsModel = require("js/models/materials-model").MaterialsModel; | ||
10 | /////////////////////////////////////////////////////////////////////// | ||
11 | // Class GLCircle | ||
12 | // GL representation of a circle. | ||
13 | // Derived from class GLGeomObj | ||
14 | // The position and dimensions of the stroke, fill, and inner Radius should be in pixels | ||
15 | /////////////////////////////////////////////////////////////////////// | ||
16 | var Circle = function GLCircle() { | ||
17 | |||
18 | this.init = function( world, xOffset, yOffset, width, height, strokeSize, strokeColor, fillColor, innerRadius, strokeMaterial, fillMaterial, strokeStyle) { | ||
19 | /////////////////////////////////////////////////////////////////////// | ||
20 | // Instance variables | ||
21 | /////////////////////////////////////////////////////////////////////// | ||
22 | this._width = 2.0; | ||
23 | this._height = 2.0; | ||
24 | this._xOffset = 0; | ||
25 | this._yOffset = 0; | ||
26 | |||
27 | this._radius = 2.0; | ||
28 | this._strokeWidth = 0.25; | ||
29 | this._innerRadius = 0; | ||
30 | |||
31 | this._ovalHeight = this._ovalHeight = 2.0 * this._radius; | ||
32 | |||
33 | this._strokeStyle = "Solid"; | ||
34 | |||
35 | this._aspectRatio = 1.0; | ||
36 | |||
37 | if (arguments.length > 0) { | ||
38 | this._width = width; | ||
39 | this._height = height; | ||
40 | this._xOffset = xOffset; | ||
41 | this._yOffset = yOffset; | ||
42 | |||
43 | this._strokeWidth = strokeSize; | ||
44 | this._innerRadius = innerRadius; | ||
45 | if (strokeColor) this._strokeColor = strokeColor; | ||
46 | if (fillColor) this._fillColor = fillColor; | ||
47 | |||
48 | this._strokeStyle = strokeStyle; | ||
49 | } | ||
50 | |||
51 | this.m_world = world; | ||
52 | |||
53 | if(strokeMaterial){ | ||
54 | this._strokeMaterial = strokeMaterial; | ||
55 | } else { | ||
56 | this._strokeMaterial = MaterialsModel.exportFlatMaterial(); | ||
57 | } | ||
58 | |||
59 | if(fillMaterial) { | ||
60 | this._fillMaterial = fillMaterial; | ||
61 | } else { | ||
62 | this._fillMaterial = MaterialsModel.exportFlatMaterial(); | ||
63 | } | ||
64 | }; | ||
65 | |||
66 | /////////////////////////////////////////////////////////////////////// | ||
67 | // Property Accessors | ||
68 | /////////////////////////////////////////////////////////////////////// | ||
69 | this.getStrokeWidth = function() { | ||
70 | return this._strokeWidth; | ||
71 | }; | ||
72 | |||
73 | this.setStrokeWidth = function(w) { | ||
74 | this._strokeWidth = w; | ||
75 | }; | ||
76 | |||
77 | this.getStrokeMaterial = function() { | ||
78 | return this._strokeMaterial; | ||
79 | }; | ||
80 | |||
81 | this.setStrokeMaterial = function(m) { | ||
82 | this._strokeMaterial = m; | ||
83 | }; | ||
84 | |||
85 | this.getFillMaterial = function() { | ||
86 | return this._fillMaterial; | ||
87 | }; | ||
88 | |||
89 | this.setFillMaterial = function(m) { | ||
90 | this._fillMaterial = m; | ||
91 | }; | ||
92 | |||
93 | this.getRadius = function() { | ||
94 | return this._radius; | ||
95 | }; | ||
96 | |||
97 | this.setRadius = function(r) { | ||
98 | this._radius = r; | ||
99 | }; | ||
100 | |||
101 | this.getWorld = function() { | ||
102 | return this._world; | ||
103 | }; | ||
104 | |||
105 | this.setWorld = function(w) { | ||
106 | this._world = w; | ||
107 | }; | ||
108 | |||
109 | this.getInnerRadius = function() { | ||
110 | return this._innerRadius; | ||
111 | }; | ||
112 | |||
113 | this.setInnerRadius = function(r) { | ||
114 | this._innerRadius = r; | ||
115 | }; | ||
116 | |||
117 | this.getStrokeStyle = function() { | ||
118 | return this._strokeStyle; | ||
119 | }; | ||
120 | this.setStrokeStyle = function(s) { | ||
121 | this._strokeStyle = s; | ||
122 | }; | ||
123 | |||
124 | this.getWidth = function() { | ||
125 | return this._width; | ||
126 | }; | ||
127 | |||
128 | this.setWidth = function(w) { | ||
129 | this._width = w; | ||
130 | }; | ||
131 | |||
132 | this.getHeight = function() { | ||
133 | return this._height; | ||
134 | }; | ||
135 | |||
136 | this.setHeight = function(h) { | ||
137 | this._height = h; | ||
138 | }; | ||
139 | |||
140 | this.geomType = function() { | ||
141 | return this.GEOM_TYPE_CIRCLE; | ||
142 | }; | ||
143 | |||
144 | /////////////////////////////////////////////////////////////////////// | ||
145 | // Methods | ||
146 | /////////////////////////////////////////////////////////////////////// | ||
147 | |||
148 | /////////////////////////////////////////////////////////////////////// | ||
149 | // update the "color of the material | ||
150 | this.getFillColor = function() | ||
151 | { | ||
152 | return this._fillColor; | ||
153 | } | ||
154 | |||
155 | // this.setFillColor = function(c) | ||
156 | // { | ||
157 | // this._fillColor = c; | ||
158 | // } | ||
159 | |||
160 | this.getStrokeColor = function() | ||
161 | { | ||
162 | return this._strokeColor; | ||
163 | } | ||
164 | |||
165 | // this.setStrokeColor = function(c) | ||
166 | // { | ||
167 | // this._strokeColor = c; | ||
168 | // } | ||
169 | /////////////////////////////////////////////////////////////////////// | ||
170 | |||
171 | this.buildBuffers = function() { | ||
172 | // get the world | ||
173 | var world = this.getWorld(); | ||
174 | if (!world) throw( "null world in buildBuffers" ); | ||
175 | |||
176 | if (!world._useWebGL) return; | ||
177 | |||
178 | // make sure RDGE has the correct context | ||
179 | g_Engine.setContext( world.getCanvas().rdgeid ); | ||
180 | |||
181 | // create the gl buffer | ||
182 | var gl = world.getGLContext(); | ||
183 | |||
184 | // determine the number of triangles to generate | ||
185 | var nTriangles = 60; // yes, we will do better than this | ||
186 | |||
187 | // get the normalized device coordinates (NDC) for | ||
188 | // all position and dimensions. | ||
189 | var vpw = world.getViewportWidth(), vph = world.getViewportHeight(); | ||
190 | var xNDC = 2*this._xOffset/vpw, yNDC = 2*this._yOffset/vph, | ||
191 | xRadNDC = this._width/vpw, yRadNDC = this._height/vph, | ||
192 | xStrokeNDC = 2*this._strokeWidth/vpw, yStrokeNDC = 2*this._strokeWidth/vph, | ||
193 | xInnRadNDC = this._innerRadius*xRadNDC, yInnRadNDC = this._innerRadius*yRadNDC; | ||
194 | |||
195 | var aspect = world.getAspect(); | ||
196 | var zn = world.getZNear(), zf = world.getZFar(); | ||
197 | var t = zn * Math.tan(world.getFOV() * Math.PI / 360.0), | ||
198 | b = -t, | ||
199 | r = aspect*t, | ||
200 | l = -r; | ||
201 | |||
202 | // calculate the object coordinates from their NDC coordinates | ||
203 | var z = -world.getViewDistance(); | ||
204 | |||
205 | // get the position of the origin | ||
206 | var x = -z*(r-l)/(2.0*zn)*xNDC, | ||
207 | y = -z*(t-b)/(2.0*zn)*yNDC; | ||
208 | |||
209 | // get the x and y radii | ||
210 | var xRad = -z*(r-l)/(2.0*zn)*xRadNDC, | ||
211 | yRad = -z*(t-b)/(2.0*zn)*yRadNDC; | ||
212 | |||
213 | // save the overall dimensions to be used in the uv calculations | ||
214 | this._ovalWidth = xRad; this._ovalHeight = yRad; | ||
215 | |||
216 | // get the x & y stroke size | ||
217 | var xStroke = -z*(r-l)/(2.0*zn)*xStrokeNDC, | ||
218 | yStroke = -z*(t-b)/(2.0*zn)*yStrokeNDC; | ||
219 | |||
220 | // get the inner radius | ||
221 | var xInnRad = -z*(r-l)/(2.0*zn)*xInnRadNDC, | ||
222 | yInnRad = -z*(t-b)/(2.0*zn)*yInnRadNDC; | ||
223 | |||
224 | // get a matrix to rotate a point around the circle | ||
225 | var angle = 2.0 * Math.PI/Number(nTriangles); | ||
226 | var mat = Matrix.RotationZ( angle ); | ||
227 | var reverseRotMat = Matrix.RotationZ( -angle ); | ||
228 | |||
229 | // calculate matrices to scale the circle and stroke to fit the bounds of the ellipse | ||
230 | var strokeScaleMat = Matrix.I(4); | ||
231 | strokeScaleMat[0] = xRad; | ||
232 | strokeScaleMat[5] = yRad; | ||
233 | |||
234 | var fillScaleMat = Matrix.I(4); | ||
235 | fillScaleMat[0] = xRad - xStroke; | ||
236 | fillScaleMat[5] = yRad - yStroke; | ||
237 | |||
238 | var innerRadiusScaleMat = Matrix.I(4); | ||
239 | innerRadiusScaleMat[0] = xInnRad; | ||
240 | innerRadiusScaleMat[5] = yInnRad; | ||
241 | |||
242 | var innerStrokeScaleMat = Matrix.I(4); | ||