aboutsummaryrefslogtreecommitdiff
path: root/js/lib/geom/line.js
diff options
context:
space:
mode:
authorJose Antonio Marquez2012-03-06 11:18:30 -0800
committerJose Antonio Marquez2012-03-06 11:18:30 -0800
commit3c4967fa93b3abc529fc404115707307ba72d5cd (patch)
treef3737db979727ee050f8b9c85ef994b0e6ddae27 /js/lib/geom/line.js
parentbee2df0d4da72677aaa2adae669ffdd4ac210dd6 (diff)
parent84332ab81c1b445195f1d9be8bbeae0725c8e758 (diff)
downloadninja-3c4967fa93b3abc529fc404115707307ba72d5cd.tar.gz
Merge branch 'refs/heads/Ninja-Internal' into FileIO
Diffstat (limited to 'js/lib/geom/line.js')
-rwxr-xr-xjs/lib/geom/line.js488
1 files changed, 488 insertions, 0 deletions
diff --git a/js/lib/geom/line.js b/js/lib/geom/line.js
new file mode 100755
index 00000000..da63b21c
--- /dev/null
+++ b/js/lib/geom/line.js
@@ -0,0 +1,488 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No 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
7var GeomObj = require("js/lib/geom/geom-obj").GeomObj;
8var ShapePrimitive = require("js/lib/geom/shape-primitive").ShapePrimitive;
9var MaterialsModel = require("js/models/materials-model").MaterialsModel;
10///////////////////////////////////////////////////////////////////////
11// Class GLLine
12// GL representation of a line.
13// Derived from class GeomObj
14///////////////////////////////////////////////////////////////////////
15var Line = function GLLine( world, xOffset, yOffset, width, height, slope, strokeSize, strokeColor, strokeMaterial, strokeStyle, xAdj, yAdj) {
16 ///////////////////////////////////////////////////////////////////////
17 // Instance variables
18 ///////////////////////////////////////////////////////////////////////
19 this._width = 2.0;
20 this._height = 2.0;
21 this._xOffset = 0;
22 this._yOffset = 0;
23
24 // If line doesn't fit in canvas world, we had to grow the canvas by this much on either side
25 this._xAdj = 0;
26 this._yAdj = 0;
27
28 this._slope = 0;
29
30 this._strokeWidth = 0.25;
31
32 this._strokeStyle = "Solid";
33 this._scaleX = 1.0;
34 this._scaleY = 1.0;
35
36 if (arguments.length > 0) {
37 this._width = width;
38 this._height = height;
39 this._xOffset = xOffset;
40 this._yOffset = yOffset;
41
42 this._xAdj = xAdj;
43 this._yAdj = yAdj;
44
45 this._slope = slope;
46 this._strokeWidth = strokeSize;
47 if (strokeColor) this._strokeColor = strokeColor.slice();
48
49 this._strokeStyle = strokeStyle;
50 this._scaleX = (world.getViewportWidth())/(world.getViewportHeight());
51 }
52
53 this._strokeVerticesLen = 0;
54
55 this.m_world = world;
56
57 this._materialAmbient = [0.2, 0.2, 0.2, 1.0];
58 this._materialDiffuse = [0.4, 0.4, 0.4, 1.0];
59 this._materialSpecular = [0.4, 0.4, 0.4, 1.0];
60
61 if(strokeMaterial) {
62 this._strokeMaterial = strokeMaterial;
63 }
64
65 ///////////////////////////////////////////////////////////////////////
66 // Property Accessors
67 ///////////////////////////////////////////////////////////////////////
68 this.getStrokeWidth = function() { return this._strokeWidth; }
69 this.setStrokeWidth = function(w) { this._strokeWidth = w; }
70
71 this.getStrokeMaterial = function() { return this._strokeMaterial; }
72 this.setStrokeMaterial = function(m) { this._strokeMaterial = m; }
73
74 this.getStrokeColor = function() { return this._strokeColor; }
75 //this.setStrokeColor = function(c) { this._strokeColor = c; }
76
77 this.getStrokeStyle = function() { return this._strokeStyle; }
78 this.setStrokeStyle = function(s) { this._strokeStyle = s; }
79
80 this.getFillMaterial = function() { return null; }
81
82 this.setStrokeMaterial = function(m) { this._strokeMaterial = m; }
83 this.getStrokeMaterial = function() { return this._strokeMaterial; }
84
85 this.getWidth = function() { return this._width; }
86 this.setWidth = function(w) { this._width = w; }
87
88 this.getHeight = function() { return this._height; }
89 this.setHeight = function(h) { this._height = h; }
90
91 this.getXAdj = function() { return this._xAdj; }
92 this.setXAdj = function(x) { this._xAdj = x; }
93
94 this.getYAdj = function() { return this._yAdj; }
95 this.setYAdj = function(y) { this._yAdj = y; }
96
97 this.getSlope = function() { return this._slope; }
98 this.setSlope = function(m) { this._slope = m; }
99
100 this.geomType = function() { return this.GEOM_TYPE_LINE; }
101
102 ///////////////////////////////////////////////////////////////////////
103 // Methods
104 ///////////////////////////////////////////////////////////////////////
105 this.export = function() {
106 var rtnStr = "type: " + this.geomType() + "\n";
107
108 rtnStr += "xoff: " + this._xOffset + "\n";
109 rtnStr += "yoff: " + this._yOffset + "\n";
110 rtnStr += "width: " + this._width + "\n";
111 rtnStr += "height: " + this._height + "\n";
112 rtnStr += "xAdj: " + this._xAdj + "\n";
113 rtnStr += "yAdj: " + this._yAdj + "\n";
114 rtnStr += "strokeWidth: " + this._strokeWidth + "\n";
115 rtnStr += "strokeColor: " + String(this._strokeColor) + "\n";
116 rtnStr += "strokeStyle: " + this._strokeStyle + "\n";
117 rtnStr += "slope: " + String(this._slope) + "\n";
118
119 rtnStr += "strokeMat: ";
120 if (this._strokeMaterial) {
121 rtnStr += this._strokeMaterial.getName();
122 } else {
123 rtnStr += "flatMaterial";
124 }
125
126 rtnStr += "\n";
127 return rtnStr;
128 };
129
130 this.import = function( importStr ) {
131 this._xOffset = Number( this.getPropertyFromString( "xoff: ", importStr ) );
132 this._yOffset = Number( this.getPropertyFromString( "yoff: ", importStr ) );
133 this._width = Number( this.getPropertyFromString( "width: ", importStr ) );
134 this._height = Number( this.getPropertyFromString( "height: ", importStr ) );
135 this._xAdj = Number( this.getPropertyFromString( "xAdj: ", importStr ) );
136 this._yAdj = Number( this.getPropertyFromString( "yAdj: ", importStr ) );
137 this._strokeWidth = Number( this.getPropertyFromString( "strokeWidth: ", importStr ) );
138 var slope = this.getPropertyFromString( "slope: ", importStr );
139
140 if(isNaN(Number(slope))) {
141 this._slope = slope;
142 } else {
143 this._slope = Number(slope);
144 }
145
146 var strokeMaterialName = this.getPropertyFromString( "strokeMat: ", importStr );
147 this._strokeStyle = this.getPropertyFromString( "strokeStyle: ", importStr );
148 this._strokeColor = eval( "[" + this.getPropertyFromString( "strokeColor: ", importStr ) + "]" );
149
150 var strokeMat = MaterialsModel.getMaterial( strokeMaterialName );
151 if (!strokeMat) {
152 console.log( "object material not found in library: " + strokeMaterialName );
153 strokeMat = MaterialsModel.exportFlatMaterial();
154 }
155
156 this._strokeMaterial = strokeMat;
157
158 };
159
160 ///////////////////////////////////////////////////////////////////////
161 // Methods
162 ///////////////////////////////////////////////////////////////////////
163 this.buildBuffers = function() {
164 // get the world
165 var world = this.getWorld();
166 if (!world) throw( "null world in buildBuffers" );
167 if (!world._useWebGL) return;
168
169 // make sure RDGE has the correct context
170 g_Engine.setContext( world.getCanvas().rdgeid );
171
172 // create the gl buffer
173 var gl = world.getGLContext();
174
175 this._strokeVerticesLen = 0;
176
177 var strokeVertices = [];
178 var strokeTextures = [];
179 var strokeNormals = [];
180 var strokeColors = [];
181
182// var scaleMat = Matrix.I(3);
183// scaleMat.elements[0][0] = this._scaleX;
184// scaleMat.elements[1][1] = this._scaleY;
185
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 xFillNDC = this._width/vpw, yFillNDC = this._height/vph,
192 xAdjNDC = this._xAdj/vpw, yAdjNDC = this._yAdj/vph,
193 xStrokeNDC = this._strokeWidth/vpw, yStrokeNDC = this._strokeWidth/vph;
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 fill
210 var xFill = -z*(r-l)/(2.0*zn)*xFillNDC,
211 yFill = -z*(t-b)/(2.0*zn)*yFillNDC;
212
213 // get the x & y stroke size
214 var xStroke = -z*(r-l)/(2.0*zn)*xStrokeNDC,
215 yStroke = -z*(t-b)/(2.0*zn)*yStrokeNDC;
216
217 // get the x & y adjustments size
218 var xAdj = -z*(r-l)/(2.0*zn)*xAdjNDC*2,
219 yAdj = -z*(t-b)/(2.0*zn)*yAdjNDC*2;
220
221
222 this._primArray = [];
223 this._materialArray = [];
224 this._materialTypeArray = [];
225 this._materialNodeArray = [];
226
227 this._scaleX = (world._viewportWidth)/(world._viewportHeight);
228
229 var innerX = xFill-xStroke;
230 var innerY = yFill-yStroke;