1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
</copyright> */
///////////////////////////////////////////////////////////////////////
// Class GLLine
// GL representation of a line.
// Derived from class GLGeomObj
///////////////////////////////////////////////////////////////////////
function GLLine( world, xOffset, yOffset, width, height, slope, strokeSize, strokeColor, strokeMaterial, strokeStyle, xAdj, yAdj)
{
///////////////////////////////////////////////////////////////////////
// Instance variables
///////////////////////////////////////////////////////////////////////
this._width = 2.0;
this._height = 2.0;
this._xOffset = 0;
this._yOffset = 0;
// If line doesn't fit in canvas world, we had to grow the canvas by this much on either side
this._xAdj = 0;
this._yAdj = 0;
this._slope = 0;
this._strokeWidth = 0.25;
// stroke colors
this._strokeColor = [0.4, 0.4, 0.4, 1.0];
// stroke materials
this._strokeMaterial;
this._strokeStyle = "Solid";
if (arguments.length > 0)
{
this._width = width;
this._height = height;
this._xOffset = xOffset;
this._yOffset = yOffset;
this._xAdj = xAdj;
this._yAdj = yAdj;
this._slope = slope;
this._strokeWidth = strokeSize;
this._strokeColor = strokeColor;
this._strokeStyle = strokeStyle;
}
this._scaleX = 1.0;
this._scaleY = 1.0;
this._scaleX = (world._viewportWidth)/(world._viewportHeight);
this._strokeVerticesLen = 0;
this.m_world = world;
this._materialAmbient = [0.2, 0.2, 0.2, 1.0];
this._materialDiffuse = [0.4, 0.4, 0.4, 1.0];
this._materialSpecular = [0.4, 0.4, 0.4, 1.0];
// initialize the inherited members
this.inheritedFrom = GLGeomObj;
this.inheritedFrom();
if(strokeMaterial)
{
this._strokeMaterial = strokeMaterial;
}
///////////////////////////////////////////////////////////////////////
// Property Accessors
///////////////////////////////////////////////////////////////////////
this.getStrokeWidth = function() { return this._strokeWidth; }
this.setStrokeWidth = function(w) { this._strokeWidth = w; }
this.getStrokeMaterial = function() { return this._strokeMaterial; }
this.setStrokeMaterial = function(m) { this._strokeMaterial = m; }
this.getStrokeColor = function() { return this._strokeColor; }
//this.setStrokeColor = function(c) { this._strokeColor = c; }
this.getStrokeStyle = function() { return this._strokeStyle; }
this.setStrokeStyle = function(s) { this._strokeStyle = s; }
this.getWidth = function() { return this._width; }
this.setWidth = function(w) { this._width = w; }
this.getHeight = function() { return this._height; }
this.setHeight = function(h) { this._height = h; }
this.getXAdj = function() { return this._xAdj; }
this.setXAdj = function(x) { this._xAdj = x; }
this.getYAdj = function() { return this._yAdj; }
this.setYAdj = function(y) { this._yAdj = y; }
this.getSlope = function() { return this._slope; }
this.setSlope = function(m) { this._slope = m; }
this.geomType = function() { return this.GEOM_TYPE_LINE; }
///////////////////////////////////////////////////////////////////////
// Methods
///////////////////////////////////////////////////////////////////////
this.buildBuffers = function()
{
// get the world
var world = this.getWorld();
if (!world) throw( "null world in buildBuffers" );
if (!world._useWebGL) return;
// create the gl buffer
var gl = world.getGLContext();
this._strokeVerticesLen = 0;
var strokeVertices = [];
var strokeTextures = [];
var strokeNormals = [];
var strokeColors = [];
// var scaleMat = Matrix.I(3);
// scaleMat.elements[0][0] = this._scaleX;
// scaleMat.elements[1][1] = this._scaleY;
// get the normalized device coordinates (NDC) for
// all position and dimensions.
var vpw = world.getViewportWidth(), vph = world.getViewportHeight();
var xNDC = 2*this._xOffset/vpw, yNDC = 2*this._yOffset/vph,
xFillNDC = this._width/vpw, yFillNDC = this._height/vph,
xAdjNDC = this._xAdj/vpw, yAdjNDC = this._yAdj/vph,
xStrokeNDC = this._strokeWidth/vpw, yStrokeNDC = this._strokeWidth/vph;
var aspect = world.getAspect();
var zn = world.getZNear(), zf = world.getZFar();
var t = zn * Math.tan(world.getFOV() * Math.PI / 360.0),
b = -t,
r = aspect*t,
l = -r;
// calculate the object coordinates from their NDC coordinates
var z = -world.getViewDistance();
// get the position of the origin
var x = -z*(r-l)/(2.0*zn)*xNDC,
y = -z*(t-b)/(2.0*zn)*yNDC;
// get the x and y fill
var xFill = -z*(r-l)/(2.0*zn)*xFillNDC,
yFill = -z*(t-b)/(2.0*zn)*yFillNDC;
// get the x & y stroke size
var xStroke = -z*(r-l)/(2.0*zn)*xStrokeNDC,
yStroke = -z*(t-b)/(2.0*zn)*yStrokeNDC;
// get the x & y adjustments size
var xAdj = -z*(r-l)/(2.0*zn)*xAdjNDC*2,
yAdj = -z*(t-b)/(2.0*zn)*yAdjNDC*2;
this._primArray = [];
this._materialArray = [];
this._materialTypeArray = [];
this._materialNodeArray = [];
this._scaleX = (world._viewportWidth)/(world._viewportHeight);
var innerX = xFill-xStroke;
var innerY = yFill-yStroke;
// if( (this._strokeStyle === "Dashed") || (this._strokeStyle === "Dotted") )
// {
// var sLen = this.createStippledStrokes(strokeVertices, this._strokeWidth, this._slope, this._strokeStyle, innerX, innerY);
// this._strokeVerticesLen += sLen;
// }
// else
{
if(this._slope === "vertical")
{
strokeVertices = [
-xFill+x, yFill+y, 0.0,
xFill+x, yFill+y, 0.0,
-xFill+x, -yFill+y, 0.0,
xFill+x, yFill+y, 0.0,
-xFill+x, -yFill+y, 0.0,
xFill+x, -yFill+y, 0.0
];
}
else if(this._slope === "horizontal")
{
// right now, this is the same as vertical line because,
// our canvas is the same size as us.
// But, we will need to use this when drawing in an existing GLWorld with other shapes
strokeVertices = [
-xFill+x, yFill+y, 0.0,
xFill+x, yFill+y, 0.0,
-xFill+x, -yFill+y, 0.0,
xFill+x, yFill+y, 0.0,
-xFill+x, -yFill+y, 0.0,
xFill+x, -yFill+y, 0.0
];
}
else if(this._slope > 0)
{
// if slope is positive, draw a line from top-left to bottom-right
strokeVertices = [
-xFill+x, yFill-2*yAdj+y, 0.0,
-xFill+2*xAdj+x, yFill+y, 0.0,
xFill-2*xAdj+x, -yFill+y, 0.0,
-xFill+2*xAdj+x, yFill+y, 0.0,
xFill-2*xAdj+x, -yFill+y, 0.0,
xFill+x, -yFill+2*yAdj+y, 0.0
];
}
else
{
// else slope is negative, draw a line from bottom-left to top-right
strokeVertices = [
-xFill+x, -yFill+2*yAdj+y, 0.0,
-xFill+2*xAdj+x, -yFill+y, 0.0,
xFill-2*xAdj+x, yFill+y, 0.0,
-xFill+2*xAdj+x, -yFill+y, 0.0,
xFill-2*xAdj+x, yFill+y, 0.0,
xFill+x, yFill-2*yAdj+y, 0.0
];
}
}
var z = 0;
var indices = [];
var nVerts = strokeVertices.length/3;
// for(var i=0; i<nVerts; i++)
// {
// var vertex = Vector.create([strokeVertices[i], strokeVertices[i+1], strokeVertices[i+2]]);
// strokeTextures.push(vertex.e(1)*0.5 + 0.5); strokeTextures.push(vertex.e(2)*0.5 + 0.5);
// }
strokeTextures = [
0, 1,
1, 1,
0, 0,
1, 1,
0, 0,
1, 0
];
// stroke normals
var index = 0;
for (var i=0; i<nVerts; i++)
{
// push a normal for each vertex in the stroke
strokeNormals.push(0.0); strokeNormals.push(0.0); strokeNormals.push(1);
indices.push( index ); index++;
}
var prim = ShapePrimitive.create(strokeVertices, strokeNormals, strokeTextures, indices, g_Engine.getContext().renderer.TRIANGLES, indices.length);
this._primArray.push( prim );
var strokeMaterial;
if (this.getStrokeMaterial())
strokeMaterial = this.getStrokeMaterial().dup();
else
strokeMaterial = new FlatMaterial();
if (strokeMaterial)
{
strokeMaterial.init( this.getWorld() );
if(!this.getStrokeMaterial() && this._strokeColor)
{
strokeMaterial.setProperty("color", this._strokeColor);
}
}
this._materialArray.push( strokeMaterial );
this._materialTypeArray.push( "stroke" );
this._materialNodeArray.push( strokeMaterial.getMaterialNode() );
world.updateObject(this);
}
this.render = function()
{
// get the world
var world = this.getWorld();
if (!world) throw( "null world in rectangle render" );
// get the context
var ctx = world.get2DContext();
if (!ctx) return;
// set up the stroke style
var lineWidth = this._strokeWidth;
ctx.beginPath();
ctx.lineWidth = lineWidth;
ctx.strokeStyle = "#0000ff";
if (this._strokeColor)
ctx.strokeStyle = MathUtils.colorToHex( this._strokeColor );
// get the points
var p0, p1;
var w = this._width, h = this._height;
if(this._slope === "vertical")
{
p0 = [0.5*w, 0];
p1 = [0.5*w, h];
}
else if(this._slope === "horizontal")
{
p0 = [0, 0.5*h];
p1 = [w, 0.5*h];
}
else if(this._slope > 0)
{
p0 = [this._xAdj, this._yAdj];
p1 = [w - this._xAdj, h - this._yAdj];
}
else
{
p0 = [this._xAdj, h - this._yAdj];
p1 = [w - this._xAdj, this._yAdj];
}
// draw the line
ctx.moveTo( p0[0], p0[1] );
ctx.lineTo( p1[0], p1[1] );
ctx.stroke();
}
/*
// type is Dotted or Dashed
this.createStippledStrokes = function(strokeVertices, strokeSize, slope, type, innerX, innerY)
{
var stippleLength = strokeSize;
if (type === "Dotted")
{
stippleLength = this.DOT_LENGTH;
}
else if(type === "Dashed")
{
stippleLength = this.DASH_LENGTH;
}
var numStrokes = 0;
var startPos = -innerX;
var endPos = innerX;
var gap = this.GAP_LENGTH;
if(slope === "horizontal")
{
gap *= this._scaleX;
stippleLength *= this._scaleX;
}
else if(slope === "vertical")
{
startPos = -innerY;
endPos = innerY;
}
else
{
if(this._scaleX < 1)
{
startPos = -innerY;
endPos = innerY;
strokeSize = (strokeSize*this._scaleX)/2;
// strokeSize *= this._scaleX;
stippleLength *= this._scaleX;
}
else
{
strokeSize = strokeSize/2;
gap *= this._scaleX;
stippleLength *= this._scaleX;
}
}
while(startPos + stippleLength < endPos)
{
if(slope === "horizontal")
{
strokeVertices.push(startPos); strokeVertices.push(-strokeSize); strokeVertices.push(0);
strokeVertices.push(startPos); strokeVertices.push(strokeSize); strokeVertices.push(0);
strokeVertices.push(startPos+stippleLength); strokeVertices.push(-strokeSize); strokeVertices.push(0);
strokeVertices.push(startPos); strokeVertices.push(strokeSize); strokeVertices.push(0);
strokeVertices.push(startPos+stippleLength); strokeVertices.push(-strokeSize); strokeVertices.push(0);
strokeVertices.push(startPos+stippleLength); strokeVertices.push(strokeSize); strokeVertices.push(0);
}
else if(slope === "vertical")
{
strokeVertices.push(-strokeSize); strokeVertices.push(startPos); strokeVertices.push(0);
strokeVertices.push(strokeSize); strokeVertices.push(startPos); strokeVertices.push(0);
strokeVertices.push(-strokeSize); strokeVertices.push(startPos+stippleLength); strokeVertices.push(0);
strokeVertices.push(strokeSize); strokeVertices.push(startPos); strokeVertices.push(0);
strokeVertices.push(-strokeSize); strokeVertices.push(startPos+stippleLength); strokeVertices.push(0);
strokeVertices.push(strokeSize); strokeVertices.push(startPos+stippleLength); strokeVertices.push(0);
}
else
{
strokeVertices.push(startPos); strokeVertices.push(startPos*-slope-strokeSize); strokeVertices.push(0);
strokeVertices.push(startPos); strokeVertices.push(startPos*-slope+strokeSize); strokeVertices.push(0);
strokeVertices.push(startPos+stippleLength); strokeVertices.push((startPos+stippleLength)*-slope-strokeSize); strokeVertices.push(0);
strokeVertices.push(startPos); strokeVertices.push(startPos*-slope+strokeSize); strokeVertices.push(0);
strokeVertices.push(startPos+stippleLength); strokeVertices.push((startPos+stippleLength)*-slope-strokeSize); strokeVertices.push(0);
strokeVertices.push(startPos+stippleLength); strokeVertices.push((startPos+stippleLength)*-slope+strokeSize); strokeVertices.push(0);
}
numStrokes += 6;
startPos += (stippleLength+gap);
}
//
return numStrokes;
}
*/
this.collidesWithPoint = function( x, y )
{
if(x < this._xOffset) return false;
if(x > (this._xOffset + this._width)) return false;
if(y < this._yOffset) return false;
if(y > (this._yOffset + this._height)) return false;
return true;
}
}
|