diff options
Diffstat (limited to 'js/lib/geom/rectangle.js')
-rwxr-xr-x | js/lib/geom/rectangle.js | 335 |
1 files changed, 223 insertions, 112 deletions
diff --git a/js/lib/geom/rectangle.js b/js/lib/geom/rectangle.js index 81a8556d..ea12385f 100755 --- a/js/lib/geom/rectangle.js +++ b/js/lib/geom/rectangle.js | |||
@@ -1,8 +1,32 @@ | |||
1 | /* <copyright> | 1 | /* <copyright> |
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | 2 | Copyright (c) 2012, Motorola Mobility, Inc |
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | 3 | All Rights Reserved. |
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | 4 | BSD License. |
5 | </copyright> */ | 5 | |
6 | Redistribution and use in source and binary forms, with or without | ||
7 | modification, are permitted provided that the following conditions are met: | ||
8 | |||
9 | - Redistributions of source code must retain the above copyright notice, | ||
10 | this list of conditions and the following disclaimer. | ||
11 | - Redistributions in binary form must reproduce the above copyright | ||
12 | notice, this list of conditions and the following disclaimer in the | ||
13 | documentation and/or other materials provided with the distribution. | ||
14 | - Neither the name of Motorola Mobility nor the names of its contributors | ||
15 | may be used to endorse or promote products derived from this software | ||
16 | without specific prior written permission. | ||
17 | |||
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
22 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
23 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
25 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
27 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
28 | POSSIBILITY OF SUCH DAMAGE. | ||
29 | </copyright> */ | ||
6 | 30 | ||
7 | var GeomObj = require("js/lib/geom/geom-obj").GeomObj; | 31 | var GeomObj = require("js/lib/geom/geom-obj").GeomObj; |
8 | var ShapePrimitive = require("js/lib/geom/shape-primitive").ShapePrimitive; | 32 | var ShapePrimitive = require("js/lib/geom/shape-primitive").ShapePrimitive; |
@@ -77,15 +101,28 @@ exports.Rectangle = Object.create(GeomObj, { | |||
77 | } else { | 101 | } else { |
78 | this._strokeMaterial = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); | 102 | this._strokeMaterial = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); |
79 | } | 103 | } |
80 | if (strokeColor && this._strokeMaterial.hasProperty( "color" )) this._strokeMaterial.setProperty( "color", this._strokeColor ); | ||
81 | 104 | ||
105 | if(strokeColor) { | ||
106 | if(this._strokeMaterial.hasProperty("color")) { | ||
107 | this._strokeMaterial.setProperty( "color", this._strokeColor ); | ||
108 | } else if (this._strokeMaterial && (this._strokeMaterial.gradientType === this._strokeColor.gradientMode)) { | ||
109 | this._strokeMaterial.setGradientData(this._strokeColor.color); | ||
110 | } | ||
111 | } | ||
82 | 112 | ||
83 | if(fillMaterial) { | 113 | if(fillMaterial) { |
84 | this._fillMaterial = fillMaterial.dup(); | 114 | this._fillMaterial = fillMaterial.dup(); |
85 | } else { | 115 | } else { |
86 | this._fillMaterial = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); | 116 | this._fillMaterial = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); |
87 | } | 117 | } |
88 | if (fillColor && this._fillMaterial.hasProperty( "color" )) this._fillMaterial.setProperty( "color", this._fillColor ); | 118 | |
119 | if(fillColor) { | ||
120 | if(this._fillMaterial.hasProperty("color")) { | ||
121 | this._fillMaterial.setProperty( "color", this._fillColor ); | ||
122 | } else if (this._fillMaterial && (this._fillMaterial.gradientType === this._fillColor.gradientMode)) { | ||
123 | this._fillMaterial.setGradientData(this._fillColor.color); | ||
124 | } | ||
125 | } | ||
89 | } | 126 | } |
90 | }, | 127 | }, |
91 | 128 | ||
@@ -314,98 +351,105 @@ exports.Rectangle = Object.create(GeomObj, { | |||
314 | } | 351 | } |
315 | }, | 352 | }, |
316 | 353 | ||
317 | buildBuffers: { | 354 | buildBuffers: { |
318 | value: function() { | 355 | value: function() { |
319 | // get the world | 356 | // get the world |
320 | var world = this.getWorld(); | 357 | var world = this.getWorld(); |
321 | if (!world) throw( "null world in buildBuffers" ); | 358 | if (!world) throw( "null world in buildBuffers" ); |
322 | //console.log( "GLRectangle.buildBuffers " + world._worldCount ); | 359 | //console.log( "GLRectangle.buildBuffers " + world._worldCount ); |
323 | if (!world._useWebGL) return; | 360 | if (!world._useWebGL) return; |
324 | 361 | ||
325 | // make sure RDGE has the correct context | 362 | // make sure RDGE has the correct context |
326 | RDGE.globals.engine.setContext( world.getCanvas().rdgeid ); | 363 | RDGE.globals.engine.setContext( world.getCanvas().rdgeid ); |
327 | 364 | ||
328 | // create the gl buffer | 365 | // create the gl buffer |
329 | var gl = world.getGLContext(); | 366 | var gl = world.getGLContext(); |
330 | 367 | ||
331 | var tlRadius = this._tlRadius; //top-left radius | 368 | var tlRadius = this._tlRadius; //top-left radius |
332 | var trRadius = this._trRadius; | 369 | var trRadius = this._trRadius; |
333 | var blRadius = this._blRadius; | 370 | var blRadius = this._blRadius; |
334 | var brRadius = this._brRadius; | 371 | var brRadius = this._brRadius; |
335 | 372 | ||
336 | // declare the arrays to hold the parts | 373 | // declare the arrays to hold the parts |
337 | this._primArray = []; | 374 | this._primArray = []; |
338 | this._materialArray = []; | 375 | this._materialArray = []; |
339 | this._materialTypeArray = []; | 376 | this._materialTypeArray = []; |
340 | this._materialNodeArray = []; | 377 | this._materialNodeArray = []; |
341 | 378 | ||
342 | // get the normalized device coordinates (NDC) for | 379 | // get the normalized device coordinates (NDC) for |
343 | // all position and dimensions. | 380 | // all position and dimensions. |
344 | var vpw = world.getViewportWidth(), vph = world.getViewportHeight(); | 381 | var vpw = world.getViewportWidth(), vph = world.getViewportHeight(); |
345 | var xNDC = 2*this._xOffset/vpw, yNDC = 2*this._yOffset/vph, | 382 | var xNDC = 2*this._xOffset/vpw, yNDC = 2*this._yOffset/vph, |
346 | xFillNDC = this._width/vpw, yFillNDC = this._height/vph, | 383 | xFillNDC = this._width/vpw, yFillNDC = this._height/vph, |
347 | strokeSizeNDC = 2*this._strokeWidth/vpw, | 384 | strokeSizeNDC = 2*this._strokeWidth/vpw, |
348 | tlRadiusNDC = 2*tlRadius/vpw, yTLRadiusNDC = 2*tlRadius/vph, | 385 | tlRadiusNDC = 2*tlRadius/vpw, yTLRadiusNDC = 2*tlRadius/vph, |
349 | trRadiusNDC = 2*trRadius/vpw, yTRRadiusNDC = 2*trRadius/vph, | 386 | trRadiusNDC = 2*trRadius/vpw, yTRRadiusNDC = 2*trRadius/vph, |
350 | blRadiusNDC = 2*blRadius/vpw, yBLRadiusNDC = 2*blRadius/vph, | 387 | blRadiusNDC = 2*blRadius/vpw, yBLRadiusNDC = 2*blRadius/vph, |
351 | brRadiusNDC = 2*brRadius/vpw, yBRRadiusNDC = 2*brRadius/vph; | 388 | brRadiusNDC = 2*brRadius/vpw, yBRRadiusNDC = 2*brRadius/vph; |
352 | 389 | ||
353 | var aspect = world.getAspect(); | 390 | var aspect = world.getAspect(); |
354 | var zn = world.getZNear(), zf = world.getZFar(); | 391 | var zn = world.getZNear(), zf = world.getZFar(); |
355 | var t = zn * Math.tan(world.getFOV() * Math.PI / 360.0), | 392 | var t = zn * Math.tan(world.getFOV() * Math.PI / 360.0), |
356 | b = -t, | 393 | b = -t, |
357 | r = aspect*t, | 394 | r = aspect*t, |
358 | l = -r; | 395 | l = -r; |
396 | |||
397 | // calculate the object coordinates from their NDC coordinates | ||
398 | var z = -world.getViewDistance(); | ||
399 | |||
400 | // get the position of the origin | ||
401 | var x = -z*(r-l)/(2.0*zn)*xNDC, | ||
402 | y = -z*(t-b)/(2.0*zn)*yNDC; | ||
403 | |||
404 | // get the x and y fill | ||
405 | var xFill = -z*(r-l)/(2.0*zn)*xFillNDC, | ||
406 | yFill = -z*(t-b)/(2.0*zn)*yFillNDC; | ||
407 | |||
408 | // keep some variables giving the overall dimensions of the | ||
409 | // rectangle. These values are used to calculate consistent | ||
410 | // texture map coordinates across all pieces. | ||
411 | this._rectWidth = xFill; this._rectHeight = yFill; | ||
412 | |||
413 | // get the stroke size | ||
414 | var strokeSize = -z*(r-l)/(2.0*zn)*strokeSizeNDC; | ||
415 | |||
416 | // get the absolute corner radii | ||
417 | tlRadius = -z*(r-l)/(2.0*zn)*tlRadiusNDC, | ||
418 | trRadius = -z*(r-l)/(2.0*zn)*trRadiusNDC, | ||
419 | blRadius = -z*(r-l)/(2.0*zn)*blRadiusNDC, | ||
420 | brRadius = -z*(r-l)/(2.0*zn)*brRadiusNDC; | ||
421 | |||
422 | // stroke | ||
423 | var i; | ||
424 | var strokeMaterial = this.makeStrokeMaterial(); | ||
425 | var strokePrimArray = this.createStroke([x,y], 2*xFill, 2*yFill, strokeSize, tlRadius, blRadius, brRadius, trRadius, strokeMaterial); | ||
426 | strokeMaterial.fitToPrimitiveArray( strokePrimArray ); | ||
427 | for (i=0; i<strokePrimArray.length; i++) | ||
428 | { | ||
429 | this._primArray.push( strokePrimArray[i] ); | ||
430 | this._materialNodeArray.push( strokeMaterial.getMaterialNode() ); | ||
431 | } | ||
359 | 432 | ||
360 | // calculate the object coordinates from their NDC coordinates | 433 | // fill |
361 | var z = -world.getViewDistance(); | 434 | tlRadius -= strokeSize; if (tlRadius < 0) tlRadius = 0.0; |
435 | blRadius -= strokeSize; if (blRadius < 0) blRadius = 0.0; | ||
436 | brRadius -= strokeSize; if (brRadius < 0) brRadius = 0.0; | ||
437 | trRadius -= strokeSize; if (trRadius < 0) trRadius = 0.0; | ||
438 | xFill -= strokeSize; | ||
439 | yFill -= strokeSize; | ||
440 | var fillMaterial = this.makeFillMaterial(); | ||
441 | //console.log( "fillMaterial: " + fillMaterial.getName() ); | ||
442 | var fillPrimArray = this.createFill([x,y], 2*xFill, 2*yFill, tlRadius, blRadius, brRadius, trRadius, fillMaterial); | ||
443 | fillMaterial.fitToPrimitiveArray( fillPrimArray ); | ||
444 | for (i=0; i<fillPrimArray.length; i++) | ||
445 | { | ||
446 | this._primArray.push( fillPrimArray[i] ); | ||
447 | this._materialNodeArray.push( fillMaterial.getMaterialNode() ); | ||
448 | } | ||
362 | 449 | ||
363 | // get the position of the origin | 450 | world.updateObject(this); |
364 | var x = -z*(r-l)/(2.0*zn)*xNDC, | 451 | } |
365 | y = -z*(t-b)/(2.0*zn)*yNDC; | 452 | }, |
366 | |||
367 | // get the x and y fill | ||
368 | var xFill = -z*(r-l)/(2.0*zn)*xFillNDC, | ||
369 | yFill = -z*(t-b)/(2.0*zn)*yFillNDC; | ||
370 | |||
371 | // keep some variables giving the overall dimensions of the | ||
372 | // rectangle. These values are used to calculate consistent | ||
373 | // texture map coordinates across all pieces. | ||
374 | this._rectWidth = xFill; this._rectHeight = yFill; | ||
375 | |||
376 | // get the stroke size | ||
377 | var strokeSize = -z*(r-l)/(2.0*zn)*strokeSizeNDC; | ||
378 | |||
379 | // get the absolute corner radii | ||
380 | tlRadius = -z*(r-l)/(2.0*zn)*tlRadiusNDC, | ||
381 | trRadius = -z*(r-l)/(2.0*zn)*trRadiusNDC, | ||
382 | blRadius = -z*(r-l)/(2.0*zn)*blRadiusNDC, | ||
383 | brRadius = -z*(r-l)/(2.0*zn)*brRadiusNDC; | ||
384 | |||
38 |