From 22cbc9644b79df60b3f6336f9563debd47fb3ea1 Mon Sep 17 00:00:00 2001 From: hwc487 Date: Thu, 28 Jun 2012 11:44:15 -0700 Subject: Added capability to split a mesh into multiiple parts to avoid buffer overflow situations. --- assets/shaders/Flag.vert.glsl | 3 +- assets/shaders/Pulse.frag.glsl | 1 + assets/shaders/test_fshader_full.glsl | 2 +- js/lib/drawing/world.js | 21 +- js/lib/geom/circle.js | 1600 +++++++++++---------- js/lib/geom/line.js | 46 +- js/lib/geom/rectangle.js | 284 ++-- js/lib/geom/shape-primitive.js | 181 ++- js/lib/rdge/materials/bump-metal-material.js | 14 +- js/lib/rdge/materials/deform-material.js | 10 + js/lib/rdge/materials/flag-material.js | 12 + js/lib/rdge/materials/fly-material.js | 10 + js/lib/rdge/materials/julia-material.js | 9 + js/lib/rdge/materials/keleidoscope-material.js | 10 + js/lib/rdge/materials/linear-gradient-material.js | 19 + js/lib/rdge/materials/mandel-material.js | 9 + js/lib/rdge/materials/material.js | 14 +- js/lib/rdge/materials/plasma-material.js | 12 + js/lib/rdge/materials/pulse-material.js | 12 + js/lib/rdge/materials/radial-blur-material.js | 10 + js/lib/rdge/materials/radial-gradient-material.js | 50 +- js/lib/rdge/materials/relief-tunnel-material.js | 10 + js/lib/rdge/materials/square-tunnel-material.js | 10 + js/lib/rdge/materials/star-material.js | 10 + js/lib/rdge/materials/taper-material.js | 29 +- js/lib/rdge/materials/tunnel-material.js | 10 + js/lib/rdge/materials/twist-material.js | 10 + js/lib/rdge/materials/twist-vert-material.js | 14 + js/lib/rdge/materials/uber-material.js | 17 + js/lib/rdge/materials/water-material.js | 13 + js/lib/rdge/materials/z-invert-material.js | 10 + 31 files changed, 1560 insertions(+), 902 deletions(-) diff --git a/assets/shaders/Flag.vert.glsl b/assets/shaders/Flag.vert.glsl index 1c842cb0..8ba00922 100644 --- a/assets/shaders/Flag.vert.glsl +++ b/assets/shaders/Flag.vert.glsl @@ -35,7 +35,8 @@ void main() const float pi = 3.14159; float angle = time; - v_uv = texcoord; + v_uv.x = texcoord.x; + v_uv.y = texcoord.y; float x = 2.0*pi*texcoord.x/u_waveWidth; float y = 2.0*pi*texcoord.y; diff --git a/assets/shaders/Pulse.frag.glsl b/assets/shaders/Pulse.frag.glsl index 9aeb05c9..b717d359 100644 --- a/assets/shaders/Pulse.frag.glsl +++ b/assets/shaders/Pulse.frag.glsl @@ -22,6 +22,7 @@ void main(void) float cLength = length(cPos); vec2 uv = gl_FragCoord.xy/u_resolution.xy+(cPos/cLength)*sin(cLength/30.0-time*10.0)/25.0; + uv.y = 1.0 - uv.y; vec3 col = texture2D(u_tex0,uv).xyz*50.0/cLength; gl_FragColor = vec4(col,1.0); diff --git a/assets/shaders/test_fshader_full.glsl b/assets/shaders/test_fshader_full.glsl index ef721b39..0ea3cf1b 100755 --- a/assets/shaders/test_fshader_full.glsl +++ b/assets/shaders/test_fshader_full.glsl @@ -38,7 +38,7 @@ varying vec3 vEyePos; void main() { - vec4 colMapTexel = vec4(texture2D(colMap, vec2(vNormal.w, vECPos.w)).rgb, 1.0); + vec4 colMapTexel = vec4(texture2D(colMap, vec2(vNormal.w, 1.0 - vECPos.w)).rgb, 1.0); // normal mapping vec3 normal = normalize(vNormal.xyz); diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js index 8068284e..39f97134 100755 --- a/js/lib/drawing/world.js +++ b/js/lib/drawing/world.js @@ -442,6 +442,7 @@ World.prototype.updateObject = function (obj) { if (prims.length != materialNodes.length) throw new Error("inconsistent material and primitive counts"); var nPrims = prims.length; + var iPrim = 0; var ctrTrNode; if (nPrims > 0) { @@ -459,8 +460,12 @@ World.prototype.updateObject = function (obj) { ); ctrTrNode.meshes = []; - ctrTrNode.attachMeshNode(this.renderer.id + "_prim_" + this._nodeCounter++, prims[0]); ctrTrNode.attachMaterial(materialNodes[0]); + while ((iPrim < nPrims) && (materialNodes[iPrim] == materialNodes[0])) + { + ctrTrNode.attachMeshNode(this.renderer.id + "_prim_" + this._nodeCounter++, prims[iPrim]); + iPrim++; + } } // delete all of the child nodes @@ -481,16 +486,18 @@ World.prototype.updateObject = function (obj) { } ctrTrNode.children = []; - for (var i = 1; i < nPrims; i++) + while (iPrim < nPrims) { - // get the next primitive childTrNode = RDGE.createTransformNode("objNode_" + this._nodeCounter++); ctrTrNode.insertAsChild(childTrNode); + var matNode = materialNodes[iPrim]; + childTrNode.attachMaterial(matNode); - // attach the instanced box goe - var prim = prims[i]; - childTrNode.attachMeshNode(this.renderer.id + "_prim_" + this._nodeCounter++, prim); - childTrNode.attachMaterial(materialNodes[i]); + while ((iPrim < nPrims) && (materialNodes[iPrim] == matNode)) + { + childTrNode.attachMeshNode(this.renderer.id + "_prim_" + this._nodeCounter++, prims[iPrim]); + iPrim++; + } } // send a notification that the tree has changed diff --git a/js/lib/geom/circle.js b/js/lib/geom/circle.js index 086c1058..374dda58 100755 --- a/js/lib/geom/circle.js +++ b/js/lib/geom/circle.js @@ -18,173 +18,173 @@ var vecUtils = require("js/helper-classes/3D/vec-utils").VecUtils; /////////////////////////////////////////////////////////////////////// exports.Circle = Object.create(GeomObj, { - /////////////////////////////////////////////////////////////////////// - // Instance variables - /////////////////////////////////////////////////////////////////////// - _width: { value : 2.0, writable: true }, - _height: { value : 2.0, writable: true }, - _xOffset: { value : 0, writable: true }, - _yOffset: { value : 0, writable: true }, - - _radius: { value : 2.0, writable: true }, - _strokeWidth: { value : 0.25, writable: true }, - _innerRadius: { value : 0, writable: true }, - _ovalHeight: { value : 4.0, writable: true }, - _strokeStyle: { value : "Solid", writable: true }, - _aspectRatio: { value : 1.0, writable: true }, + /////////////////////////////////////////////////////////////////////// + // Instance variables + /////////////////////////////////////////////////////////////////////// + _width: { value : 2.0, writable: true }, + _height: { value : 2.0, writable: true }, + _xOffset: { value : 0, writable: true }, + _yOffset: { value : 0, writable: true }, + + _radius: { value : 2.0, writable: true }, + _strokeWidth: { value : 0.25, writable: true }, + _innerRadius: { value : 0, writable: true }, + _ovalHeight: { value : 4.0, writable: true }, + _strokeStyle: { value : "Solid", writable: true }, + _aspectRatio: { value : 1.0, writable: true }, init: { - value: function(world, xOffset, yOffset, width, height, strokeSize, strokeColor, fillColor, innerRadius, strokeMaterial, fillMaterial, strokeStyle) { - if(arguments.length > 0) { - this._width = width; - this._height = height; - this._xOffset = xOffset; - this._yOffset = yOffset; - this._ovalHeight = 2.0 * this._radius; - - this._strokeWidth = strokeSize; - this._innerRadius = innerRadius; - this._strokeColor = strokeColor; - this._fillColor = fillColor; - - this._strokeStyle = strokeStyle; - - this._matrix = Matrix.I(4); - //this._matrix[12] = xOffset; - //this._matrix[13] = yOffset; - } - - this.m_world = world; - - if(strokeMaterial) { - this._strokeMaterial = strokeMaterial.dup(); - } else { - this._strokeMaterial = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); - } + value: function(world, xOffset, yOffset, width, height, strokeSize, strokeColor, fillColor, innerRadius, strokeMaterial, fillMaterial, strokeStyle) { + if(arguments.length > 0) { + this._width = width; + this._height = height; + this._xOffset = xOffset; + this._yOffset = yOffset; + this._ovalHeight = 2.0 * this._radius; + + this._strokeWidth = strokeSize; + this._innerRadius = innerRadius; + this._strokeColor = strokeColor; + this._fillColor = fillColor; + + this._strokeStyle = strokeStyle; + + this._matrix = Matrix.I(4); + //this._matrix[12] = xOffset; + //this._matrix[13] = yOffset; + } + + this.m_world = world; + + if(strokeMaterial) { + this._strokeMaterial = strokeMaterial.dup(); + } else { + this._strokeMaterial = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); + } if (strokeColor && this._strokeMaterial.hasProperty( "color" )) this._strokeMaterial.setProperty( "color", this._strokeColor ); - if(fillMaterial) { - this._fillMaterial = fillMaterial.dup(); - } else { - this._fillMaterial = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); - } + if(fillMaterial) { + this._fillMaterial = fillMaterial.dup(); + } else { + this._fillMaterial = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); + } if (fillColor && this._fillMaterial.hasProperty( "color" )) this._fillMaterial.setProperty( "color", this._fillColor ); - } - }, - - /////////////////////////////////////////////////////////////////////// - // Property Accessors - /////////////////////////////////////////////////////////////////////// - // TODO - Use getters/setters in the future - getStrokeWidth: { - value: function() { - return this._strokeWidth; - } - }, - - setStrokeWidth: { - value: function(w) { - this._strokeWidth = w; - } - }, - - getStrokeMaterial: { - value: function() { - return this._strokeMaterial; - } - }, - - setStrokeMaterial: { - value: function(m) { - this._strokeMaterial = m; - } - }, - - getFillMaterial: { - value: function() { - return this._fillMaterial; - } - }, - - setFillMaterial: { - value: function(m) { - this._fillMaterial = m; - } - }, - - getRadius: { - value: function() { - return this._radius; - } - }, - - setRadius: { - value: function(r) { - this._radius = r; - } - }, - - getInnerRadius: { - value: function() { - return this._innerRadius; - } - }, - - setInnerRadius: { - value: function(r) { - this._innerRadius = r; - } - }, - - getStrokeStyle: { - value: function() { - return this._strokeStyle; - } - }, - - setStrokeStyle: { - value: function(s) { - this._strokeStyle = s; - } - }, - - getWidth: { - value: function() { - return this._width; - } - }, - - setWidth: { - value: function(w) { - this._width = w; - } - }, - - getHeight: { - value: function() { - return this._height; - } - }, - - setHeight: { - value: function(h) { - this._height = h; - } - }, - - geomType: { - value: function() { - return this.GEOM_TYPE_CIRCLE; - } - }, - - /////////////////////////////////////////////////////////////////////// + } + }, + + /////////////////////////////////////////////////////////////////////// + // Property Accessors + /////////////////////////////////////////////////////////////////////// + // TODO - Use getters/setters in the future + getStrokeWidth: { + value: function() { + return this._strokeWidth; + } + }, + + setStrokeWidth: { + value: function(w) { + this._strokeWidth = w; + } + }, + + getStrokeMaterial: { + value: function() { + return this._strokeMaterial; + } + }, + + setStrokeMaterial: { + value: function(m) { + this._strokeMaterial = m; + } + }, + + getFillMaterial: { + value: function() { + return this._fillMaterial; + } + }, + + setFillMaterial: { + value: function(m) { + this._fillMaterial = m; + } + }, + + getRadius: { + value: function() { + return this._radius; + } + }, + + setRadius: { + value: function(r) { + this._radius = r; + } + }, + + getInnerRadius: { + value: function() { + return this._innerRadius; + } + }, + + setInnerRadius: { + value: function(r) { + this._innerRadius = r; + } + }, + + getStrokeStyle: { + value: function() { + return this._strokeStyle; + } + }, + + setStrokeStyle: { + value: function(s) { + this._strokeStyle = s; + } + }, + + getWidth: { + value: function() { + return this._width; + } + }, + + setWidth: { + value: function(w) { + this._width = w; + } + }, + + getHeight: { + value: function() { + return this._height; + } + }, + + setHeight: { + value: function(h) { + this._height = h; + } + }, + + geomType: { + value: function() { + return this.GEOM_TYPE_CIRCLE; + } + }, + + /////////////////////////////////////////////////////////////////////// // update the "color of the material - getFillColor: { - value: function() { - return this._fillColor; - } - }, + getFillColor: { + value: function() { + return this._fillColor; + } + }, // setFillColor: { // value: function(c) { @@ -192,643 +192,703 @@ exports.Circle = Object.create(GeomObj, { // } // }, - getStrokeColor: { - value: function() { - return this._strokeColor; - } - }, + getStrokeColor: { + value: function() { + return this._strokeColor; + } + }, // setStrokeColor: { // value: function(c) { // this._strokeColor = c; // } // }, - /////////////////////////////////////////////////////////////////////// - - /////////////////////////////////////////////////////////////////////// - // Methods - /////////////////////////////////////////////////////////////////////// - buildBuffers: { - value: function() { - // get the world - var world = this.getWorld(); - if (!world) throw( "null world in buildBuffers" ); - - if (!world._useWebGL) return; - - // make sure RDGE has the correct context - RDGE.globals.engine.setContext( world.getCanvas().rdgeid ); - - // create the gl buffer - var gl = world.getGLContext(); - - // determine the number of triangles to generate - var nTriangles = 60; // yes, we will do better than this - - // 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, - xRadNDC = this._width/vpw, yRadNDC = this._height/vph, - xStrokeNDC = 2*this._strokeWidth/vpw, yStrokeNDC = 2*this._strokeWidth/vph, - xInnRadNDC = this._innerRadius*xRadNDC, yInnRadNDC = this._innerRadius*yRadNDC; - - 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 radii - var xRad = -z*(r-l)/(2.0*zn)*xRadNDC, - yRad = -z*(t-b)/(2.0*zn)*yRadNDC; - - // save the overall dimensions to be used in the uv calculations - this._ovalWidth = xRad; this._ovalHeight = yRad; - - // 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 inner radius - var xInnRad = -z*(r-l)/(2.0*zn)*xInnRadNDC, - yInnRad = -z*(t-b)/(2.0*zn)*yInnRadNDC; - - // get a matrix to rotate a point around the circle - var angle = 2.0 * Math.PI/Number(nTriangles); - var mat = Matrix.RotationZ( angle ); - var reverseRotMat = Matrix.RotationZ( -angle ); - - // calculate matrices to scale the circle and stroke to fit the bounds of the ellipse - var strokeScaleMat = Matrix.I(4); - strokeScaleMat[0] = xRad; - strokeScaleMat[5] = yRad; - - var fillScaleMat = Matrix.I(4); - fillScaleMat[0] = xRad - xStroke; - fillScaleMat[5] = yRad - yStroke; - - var innerRadiusScaleMat = Matrix.I(4); - innerRadiusScaleMat[0] = xInnRad; - innerRadiusScaleMat[5] = yInnRad; - - var innerStrokeScaleMat = Matrix.I(4); - innerStrokeScaleMat[0] = xInnRad - xStroke; - innerStrokeScaleMat[5] = yInnRad - yStroke; - - var fillPrim, strokePrim0, strokePrim1; - var fillMaterial, strokeMaterial0, strokeMaterial2; - - this._primArray = []; - this._materialArray = []; - this._materialTypeArray = []; - this._materialNodeArray = []; - - ///////////////////////////////////////////////////////////// - // Strokes - if(this._strokeWidth > 0) { - var numStrokes = 1; - if(this._innerRadius !== 0) { + /////////////////////////////////////////////////////////////////////// + + /////////////////////////////////////////////////////////////////////// + // Methods + /////////////////////////////////////////////////////////////////////// + buildBuffers: { + value: function() { + // get the world + var world = this.getWorld(); + if (!world) throw( "null world in buildBuffers" ); + + if (!world._useWebGL) return; + + // make sure RDGE has the correct context + RDGE.globals.engine.setContext( world.getCanvas().rdgeid ); + + // create the gl buffer + var gl = world.getGLContext(); + + // determine the number of triangles to generate + var nTriangles = 60; // yes, we will do better than this + + // 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, + xRadNDC = this._width/vpw, yRadNDC = this._height/vph, + xStrokeNDC = 2*this._strokeWidth/vpw, yStrokeNDC = 2*this._strokeWidth/vph, + xInnRadNDC = this._innerRadius*xRadNDC, yInnRadNDC = this._innerRadius*yRadNDC; + + 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 radii + var xRad = -z*(r-l)/(2.0*zn)*xRadNDC, + yRad = -z*(t-b)/(2.0*zn)*yRadNDC; + + // save the overall dimensions to be used in the uv calculations + this._ovalWidth = xRad; this._ovalHeight = yRad; + + // 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 inner radius + var xInnRad = -z*(r-l)/(2.0*zn)*xInnRadNDC, + yInnRad = -z*(t-b)/(2.0*zn)*yInnRadNDC; + + // get a matrix to rotate a point around the circle + var angle = 2.0 * Math.PI/Number(nTriangles); + var mat = Matrix.RotationZ( angle ); + var reverseRotMat = Matrix.RotationZ( -angle ); + + // calculate matrices to scale the circle and stroke to fit the bounds of the ellipse + var strokeScaleMat = Matrix.I(4); + strokeScaleMat[0] = xRad; + strokeScaleMat[5] = yRad; + + var fillScaleMat = Matrix.I(4); + fillScaleMat[0] = xRad - xStroke; + fillScaleMat[5] = yRad - yStroke; + + var innerRadiusScaleMat = Matrix.I(4); + innerRadiusScaleMat[0] = xInnRad; + innerRadiusScaleMat[5] = yInnRad; + + var innerStrokeScaleMat = Matrix.I(4); + innerStrokeScaleMat[0] = xInnRad - xStroke; + innerStrokeScaleMat[5] = yInnRad - yStroke; + + var i; + var fillPrimArray, strokePrim0Array, strokePrim1Array; + var fillMaterial, strokeMaterial0, strokeMaterial2; + + this._primArray = []; + this._materialArray = []; + this._materialTypeArray = []; + this._materialNodeArray = []; + + ///////////////////////////////////////////////////////////// + // Strokes + if(this._strokeWidth > 0) { + var numStrokes = 1; + if(this._innerRadius !== 0) { strokeMaterial0 = this.makeStrokeMaterial(); - strokePrim0 = this.generateOvalRing(x, y, reverseRotMat, innerStrokeScaleMat, innerRadiusScaleMat, nTriangles, strokeMaterial0); - } - - strokeMaterial2 = this.makeStrokeMaterial(); - strokePrim1 = this.generateOvalRing(x, y, reverseRotMat, fillScaleMat, strokeScaleMat, nTriangles, strokeMaterial2); - } - - if (strokePrim0) { - strokeMaterial0.fitToPrimitive( strokePrim0 ); - - this._primArray.push( strokePrim0 ); - this._materialNodeArray.push( strokeMaterial0.getMaterialNode() ); - } - - if (strokePrim1) { - strokeMaterial2.fitToPrimitive( strokePrim1 ); - - this._primArray.push( strokePrim1 ); - this._materialNodeArray.push( strokeMaterial2.getMaterialNode() ); - } - - ///////////////////////////////////////////////////////////// - // Fill - fillMaterial = this.makeFillMaterial(); - if(this._innerRadius === 0) { - fillPrim = this.generateOval(x, y, mat, fillScaleMat, nTriangles, fillMaterial); - } else { - fillPrim = this.generateOvalRing(x, y, reverseRotMat, innerRadiusScaleMat, fillScaleMat, nTriangles, fillMaterial); - } - - if (fillPrim) { - fillMaterial.fitToPrimitive( fillPrim ); - - this._primArray.push( fillPrim ); - this._materialNodeArray.push( fillMaterial.getMaterialNode() ); - } - - world.updateObject(this); - } - }, - - generateOval: { - value: function(xOff, yOff, rotationMat, scaleMat, nTriangles, material) { - var pt = [1.0, 0.0, 0.0]; - //var pts = scaleMat.multiply(pt); - var pts = glmat4.multiplyVec3( scaleMat, pt, []); - var x = pts[0], y = pts[1], z = 0; - var xs = scaleMat[0], ys = scaleMat[4]; - - var vrts = [], nrms = [], uvs = [], indices = []; - var index = 0; - for (var i=0; i 0) { + xScale = 0.5*innerRad*this._width; + yScale = 0.5*innerRad*this._height; + mat[0] = xScale; + mat[5] = yScale; + + // get the bezier points + var bezPts = MathUtils.circularArcToBezier( [0,0,0], [1,0,0], -2.0*Math.PI ); + if (bezPts) { + var n = bezPts.length; + p = MathUtils.transformPoint( bezPts[0], mat ); + ctx.moveTo( p[0], p[1] ); + index = 1; + while (index < n) { + p0 = MathUtils.transformPoint( bezPts[index], mat ); + p1 = MathUtils.transformPoint( bezPts[index+1], mat ); + + var x0 = p0[0], y0 = p0[1], + x1 = p1[0], y1 = p1[1]; + ctx.quadraticCurveTo( x0, y0, x1, y1 ); + index += 2; + } + } + } + + // fill the path + ctx.fill(); + } + + // calculate the stroke matrix + xScale = 0.5*this._width - 0.5*lineWidth; + yScale = 0.5*this._height - 0.5*lineWidth; + mat[0] = xScale; + mat[5] = yScale; + + // set up the stroke style + ctx.beginPath(); + ctx.lineWidth = lineWidth; + if (this._strokeColor) { + if(this._strokeColor.gradientMode) { + if(this._strokeColor.gradientMode === "radial") { + gradient = ctx.createRadialGradient(xCtr, yCtr, 0, + xCtr, yCtr, 0.5*Math.max(this._height, this._width)); + } else { + gradient = ctx.createLinearGradient(0, this._height/2, this._width, this._height/2); + } + colors = this._strokeColor.color; + + len = colors.length; + + for(j=0; j 0) { + // calculate the stroke matrix + xScale = 0.5*innerRad*this._width - 0.5*lineWidth; + yScale = 0.5*innerRad*this._height - 0.5*lineWidth; + mat[0] = xScale; + mat[5] = yScale; + + // draw the stroke + p = MathUtils.transformPoint( bezPts[0], mat ); + ctx.moveTo( p[0], p[1] ); + index = 1; + while (index < n) { + var p0 = MathUtils.transformPoint( bezPts[index], mat ); + var p1 = MathUtils.transformPoint( bezPts[index+1], mat ); + + var x0 = p0[0], y0 = p0[1], + x1 = p1[0], y1 = p1[1]; + ctx.quadraticCurveTo( x0, y0, x1, y1 ); + index += 2; + } + } + + // render the stroke + ctx.stroke(); } } - */ - - return ShapePrimitive.create(vrts, nrms, uvs, indices, RDGE.globals.engine.getContext().renderer.TRIANGLE_STRIP, indices.length); - } - }, - - render: { - value: function() { - // get the world - var world = this.getWorld(); - if (!world) throw( "null world in buildBuffers" ); - - // get the context - var ctx = world.get2DContext(); - if (!ctx) return; - - // declare some variables - var p0, p1; - var x0, y1, x1, y1; - - // create the matrix - var lineWidth = this._strokeWidth; - var innerRad = this.getInnerRadius(); - var xScale = 0.5*this._width - lineWidth, - yScale = 0.5*this._height - lineWidth; - - // translate - var xCtr = 0.5*world.getViewportWidth() + this._xOffset, - yCtr = 0.5*world.getViewportHeight() + this._yOffset; - //ctx.setTransform( xScale, 0.0, 0.0, yScale, xCtr, yCtr ); - var mat = Matrix.create( [ - [ xScale, 0.0, 0.0, xCtr], - [ 0.0, yScale, 0.0, yCtr], - [ 0.0, 0.0, 1.0, 0.0], - [ 0.0, 0.0, 0.0, 1.0] - ] ); - - // get a bezier representation of the circle - var bezPts = MathUtils.circularArcToBezier( [0,0,0], [1,0,0], 2.0*Math.PI ); - if (bezPts) { - var n = bezPts.length; - var gradient, - colors, - len, - j, - position, - cs, - c; - - // set up the fill style - ctx.beginPath(); - ctx.lineWidth = 0; - if (this._fillColor) { - if(this._fillColor.gradientMode) { - if(this._fillColor.gradientMode === "radial") { - gradient = ctx.createRadialGradient(xCtr, yCtr, 0, - xCtr, yCtr, Math.max(this._width, this._height)/2 - lineWidth); - } else { - gradient = ctx.createLinearGradient(lineWidth, this._height/2, this._width-lineWidth, this._height/2); - } - colors = this._fillColor.color; - - len = colors.length; - - for(j=0; j 0) { - xScale = 0.5*innerRad*this._width; - yScale = 0.5*innerRad*this._height; - mat[0] = xScale; - mat[5] = yScale; - - // get the bezier points - var bezPts = MathUtils.circularArcToBezier( [0,0,0], [1,0,0], -2.0*Math.PI ); - if (bezPts) { - var n = bezPts.length; - p = MathUtils.transformPoint( bezPts[0], mat ); - ctx.moveTo( p[0], p[1] ); - index = 1; - while (index < n) { - p0 = MathUtils.transformPoint( bezPts[index], mat ); - p1 = MathUtils.transformPoint( bezPts[index+1], mat ); - - var x0 = p0[0], y0 = p0[1], - x1 = p1[0], y1 = p1[1]; - ctx.quadraticCurveTo( x0, y0, x1, y1 ); - index += 2; - } - } - } - - // fill the path - ctx.fill(); - } - - // calculate the stroke matrix - xScale = 0.5*this._width - 0.5*lineWidth; - yScale = 0.5*this._height - 0.5*lineWidth; - mat[0] = xScale; - mat[5] = yScale; - - // set up the stroke style - ctx.beginPath(); - ctx.lineWidth = lineWidth; - if (this._strokeColor) { - if(this._strokeColor.gradientMode) { - if(this._strokeColor.gradientMode === "radial") { - gradient = ctx.createRadialGradient(xCtr, yCtr, 0, - xCtr, yCtr, 0.5*Math.max(this._height, this._width)); - } else { - gradient = ctx.createLinearGradient(0, this._height/2, this._width, this._height/2); - } - colors = this._strokeColor.color; - - len = colors.length; - - for(j=0; j 0) { - // calculate the stroke matrix - xScale = 0.5*innerRad*this._width - 0.5*lineWidth; - yScale = 0.5*innerRad*this._height - 0.5*lineWidth; - mat[0] = xScale; - mat[5] = yScale; - - // draw the stroke - p = MathUtils.transformPoint( bezPts[0], mat ); - ctx.moveTo( p[0], p[1] ); - index = 1; - while (index < n) { - var p0 = MathUtils.transformPoint( bezPts[index], mat ); - var p1 = MathUtils.transformPoint( bezPts[index+1], mat ); - - var x0 = p0[0], y0 = p0[1], - x1 = p1[0], y1 = p1[1]; - ctx.quadraticCurveTo( x0, y0, x1, y1 ); - index += 2; - } - } - - // render the stroke - ctx.stroke(); - } - } - } - }, - - exportJSON: { - value: function() { - var jObj = - { - 'type' : this.geomType(), - 'xoff' : this._xOffset, - 'yoff' : this._yOffset, - 'width' : this._width, - 'height' : this._height, - 'strokeWidth' : this._strokeWidth, - 'strokeColor' : this._strokeColor, - 'fillColor' : this._fillColor, - 'innerRadius' : this._innerRadius, - 'strokeStyle' : this._strokeStyle, - 'strokeMat' : this._strokeMaterial ? this._strokeMaterial.getName() : MaterialsModel.getDefaultMaterialName(), - 'fillMat' : this._fillMaterial ? this._fillMaterial.getName() : MaterialsModel.getDefaultMaterialName(), - 'materials' : this.exportMaterialsJSON() - }; - - return jObj; - } - }, - - importJSON: { - value: function(jObj) { - this._xOffset = jObj.xoff; - this._yOffset = jObj.yoff; - this._width = jObj.width; - this._height = jObj.height; - this._strokeWidth = jObj.strokeWidth; - this._strokeColor = jObj.strokeColor; - this._fillColor = jObj.fillColor; - this._innerRadius = jObj.innerRadius; - this._strokeStyle = jObj.strokeStyle; - var strokeMaterialName = jObj.strokeMat; - var fillMaterialName = jObj.fillMat; - - var strokeMat = MaterialsModel.getMaterial( strokeMaterialName ).dup(); - if (!strokeMat) { - console.log( "object material not found in library: " + strokeMaterialName ); - strokeMat = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); - } - this._strokeMaterial = strokeMat; + } + }, + + exportJSON: { + value: function() { + var jObj = + { + 'type' : this.geomType(), + 'xoff' : this._xOffset, + 'yoff' : this._yOffset, + 'width' : this._width, + 'height' : this._height, + 'strokeWidth' : this._strokeWidth, + 'strokeColor' : this._strokeColor, + 'fillColor' : this._fillColor, + 'innerRadius' : this._innerRadius, + 'strokeStyle' : this._strokeStyle, + 'strokeMat' : this._strokeMaterial ? this._strokeMaterial.getName() : MaterialsModel.getDefaultMaterialName(), + 'fillMat' : this._fillMaterial ? this._fillMaterial.getName() : MaterialsModel.getDefaultMaterialName(), + 'materials' : this.exportMaterialsJSON() + }; + + return jObj; + } + }, + + importJSON: { + value: function(jObj) { + this._xOffset = jObj.xoff; + this._yOffset = jObj.yoff; + this._width = jObj.width; + this._height = jObj.height; + this._strokeWidth = jObj.strokeWidth; + this._strokeColor = jObj.strokeColor; + this._fillColor = jObj.fillColor; + this._innerRadius = jObj.innerRadius; + this._strokeStyle = jObj.strokeStyle; + var strokeMaterialName = jObj.strokeMat; + var fillMaterialName = jObj.fillMat; + + var strokeMat = MaterialsModel.getMaterial( strokeMaterialName ).dup(); + if (!strokeMat) { + console.log( "object material not found in library: " + strokeMaterialName ); + strokeMat = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); + } + this._strokeMaterial = strokeMat; if (this._strokeMaterial.hasProperty( 'color' )) this._strokeMaterial.setProperty( 'color', this._strokeColor ); - var fillMat = MaterialsModel.getMaterial( fillMaterialName ).dup(); - if (!fillMat) { - console.log( "object material not found in library: " + fillMaterialName ); - fillMat = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); - } - this._fillMaterial = fillMat; + var fillMat = MaterialsModel.getMaterial( fillMaterialName ).dup(); + if (!fillMat) { + console.log( "object material not found in library: " + fillMaterialName ); + fillMat = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); + } + this._fillMaterial = fillMat; if (this._fillMaterial.hasProperty( 'color' )) this._fillMaterial.setProperty( 'color', this._fillColor ); - this.importMaterialsJSON( jObj.materials ); - } - }, + this.importMaterialsJSON( jObj.materials ); + } + }, - collidesWithPoint: { - value: function(x, y) { + collidesWithPoint: { + value: 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; - } - }, - - containsPoint: { - value: function(pt, dir) { - var world = this.getWorld(); - if (!world) throw( "null world in containsPoint" ); - - // get a point on the plane of the circle - // the point is in NDC, as is the input parameters - var mat = this.getMatrix(); - var plane = [0,0,1,0]; - plane = MathUtils.transformPlane( plane, mat ); - var projPt = MathUtils.vecIntersectPlane ( pt, dir, plane ); - - // transform the projected point back to the XY plane - //var invMat = mat.inverse(); - var invMat = glmat4.inverse( mat, [] ); - var planePt = MathUtils.transformPoint( projPt, invMat ); - - // get the normalized device coordinates (NDC) for - // the position and radii. - var vpw = world.getViewportWidth(), vph = world.getViewportHeight(); - var xNDC = 2*this._xOffset/vpw, yNDC = 2*this._yOffset/vph, - xRadNDC = this._width/vpw, yRadNDC = this._height/vph; - var projMat = world.makePerspectiveMatrix(); - var z = -world.getViewDistance(); - var planePtNDC = planePt.slice(0); - planePtNDC[2] = z; - planePtNDC = MathUtils.transformHomogeneousPoint( planePtNDC, projMat ); - planePtNDC = MathUtils.applyHomogeneousCoordinate( planePtNDC ); - - // get the gl coordinates - 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; - - var angle = Math.atan2( planePtNDC[1] - yNDC, planePtNDC[0] - xNDC ); - var degrees = angle*180.0/Math.PI; - var objPtNDC = [Math.cos(angle)*xRadNDC + xNDC, Math.sin(angle)*yRadNDC + yNDC, 0]; - - var ctrNDC = [xNDC, yNDC]; - - var distToBoundary = VecUtils.vecDist( 2, ctrNDC, objPtNDC ), - distToPt = VecUtils.vecDist( 2, ctrNDC, planePtNDC ); - - return (MathUtils.fpCmp(distToPt,distToBoundary) <= 0); - } - }, - - getNearPoint: { - value: function(pt, dir) { - var world = this.getWorld(); - if (!world) throw( "null world in getNearPoint" ); - - // the input point and direction are in GL space - // project to the z == 0 plane - var mat = this.getMatrix(); - var plane = [0,0,1,0]; - plane = MathUtils.transformPlane( plane, mat ); - var projPt = MathUtils.vecIntersectPlane ( pt, dir, plane ); - - // get the center of the circle in GL space - var ctr = this.getGLCenter(); - - // transform the projected point to the plane of the circle - var planePt = MathUtils.transformPoint( projPt, mat ); - - // get a matrix mapping the circle to a 2D coordinate system - var normal = [ mat[8], mat[9], mat[10] ]; - var planeMat = drawUtils.getPlaneToWorldMatrix(normal, ctr); - var planeMatInv = glmat4.inverse( planeMat, [] ); - var planePt2D = MathUtils.transformPoint( planePt, planeMatInv ); - - // get 2 points on the axes of the oval - var wPt = this.preViewToGL( [this._xOffset + 0.5*this.getWidth(), this._yOffset, 0] ), - hPt = this.preViewToGL( [this._xOffset, this._yOffset + 0.5*this.getHeight(), 0] ); - var w = vecUtils.vecDist( 2, wPt, ctr ), - h = vecUtils.vecDist( 2, hPt, ctr ); - var aspect = w/h; - - // get the angle of the projected point relative to the circle - var angle = Math.atan2( planePt2D[1], planePt2D[0]/aspect ); - var degrees = angle*180.0/Math.PI; - - // get the corresponding point on the object - var pt = [ Math.cos(angle)*w, Math.sin(angle)*h, 0 ]; - var glPt = MathUtils.transformPoint( pt, planeMat ); - - return glPt; - } - }, + return true; + } + }, + + containsPoint: { + value: function(pt, dir) { + var world = this.getWorld(); + if (!world) throw( "null world in containsPoint" ); + + // get a point on the plane of the circle + // the point is in NDC, as is the input parameters + var mat = this.getMatrix(); + var plane = [0,0,1,0]; + plane = MathUtils.transformPlane( plane, mat ); + var projPt = MathUtils.vecIntersectPlane ( pt, dir, plane ); + + // transform the projected point back to the XY plane + //var invMat = mat.inverse(); + var invMat = glmat4.inverse( mat, [] ); + var planePt = MathUtils.transformPoint( projPt, invMat ); + + // get the normalized device coordinates (NDC) for + // the position and radii. + var vpw = world.getViewportWidth(), vph = world.getViewportHeight(); + var xNDC = 2*this._xOffset/vpw, yNDC = 2*this._yOffset/vph, + xRadNDC = this._width/vpw, yRadNDC = this._height/vph; + var projMat = world.makePerspectiveMatrix(); + var z = -world.getViewDistance(); + var planePtNDC = planePt.slice(0); + planePtNDC[2] = z; + planePtNDC = MathUtils.transformHomogeneousPoint( planePtNDC, projMat ); + planePtNDC = MathUtils.applyHomogeneousCoordinate( planePtNDC ); + + // get the gl coordinates + 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; + + var angle = Math.atan2( planePtNDC[1] - yNDC, planePtNDC[0] - xNDC ); + var degrees = angle*180.0/Math.PI; + var objPtNDC = [Math.cos(angle)*xRadNDC + xNDC, Math.sin(angle)*yRadNDC + yNDC, 0]; + + var ctrNDC = [xNDC, yNDC]; + + var distToBoundary = VecUtils.vecDist( 2, ctrNDC, objPtNDC ), + distToPt = VecUtils.vecDist( 2, ctrNDC, planePtNDC ); + + return (MathUtils.fpCmp(distToPt,distToBoundary) <= 0); + } + }, + + getNearPoint: { + value: function(pt, dir) { + var world = this.getWorld(); + if (!world) throw( "null world in getNearPoint" ); + + // the input point and direction are in GL space + // project to the z == 0 plane + var mat = this.getMatrix(); + var plane = [0,0,1,0]; + plane = MathUtils.transformPlane( plane, mat ); + var projPt = MathUtils.vecIntersectPlane ( pt, dir, plane ); + + // get the center of the circle in GL space + var ctr = this.getGLCenter(); + + // transform the projected point to the plane of the circle + var planePt = MathUtils.transformPoint( projPt, mat ); + + // get a matrix mapping the circle to a 2D coordinate system + var normal = [ mat[8], mat[9], mat[10] ]; + var planeMat = drawUtils.getPlaneToWorldMatrix(normal, ctr); + var planeMatInv = glmat4.inverse( planeMat, [] ); + var planePt2D = MathUtils.transformPoint( planePt, planeMatInv ); + + // get 2 points on the axes of the oval + var wPt = this.preViewToGL( [this._xOffset + 0.5*this.getWidth(), this._yOffset, 0] ), + hPt = this.preViewToGL( [this._xOffset, this._yOffset + 0.5*this.getHeight(), 0] ); + var w = vecUtils.vecDist( 2, wPt, ctr ), + h = vecUtils.vecDist( 2, hPt, ctr ); + var aspect = w/h; + + // get the angle of the projected point relative to the circle + var angle = Math.atan2( planePt2D[1], planePt2D[0]/aspect ); + var degrees = angle*180.0/Math.PI; + + // get the corresponding point on the object + var pt = [ Math.cos(angle)*w, Math.sin(angle)*h, 0 ]; + var glPt = MathUtils.transformPoint( pt, planeMat ); + + return glPt; + } + }, recalcTexMapCoords: { value: function(vrts, uvs) { diff --git a/js/lib/geom/line.js b/js/lib/geom/line.js index a016d7a3..f9d0625d 100755 --- a/js/lib/geom/line.js +++ b/js/lib/geom/line.js @@ -413,12 +413,48 @@ exports.Line = Object.create(GeomObj, { indices.push( index ); index++; } - var prim = ShapePrimitive.create(strokeVertices, strokeNormals, strokeTextures, indices, RDGE.globals.engine.getContext().renderer.TRIANGLES, indices.length); - var strokeMaterial = this.makeStrokeMaterial(); - - this._primArray.push( prim ); - this._materialNodeArray.push( strokeMaterial.getMaterialNode() ); +// var prim = ShapePrimitive.create(strokeVertices, strokeNormals, strokeTextures, indices, RDGE.globals.engine.getContext().renderer.TRIANGLES, indices.length); +// this._primArray.push( prim ); +// this._materialNodeArray.push( strokeMaterial.getMaterialNode() ); + + // refine the mesh for vertex deformations + if (strokeMaterial) + { + var primArray; + if (strokeMaterial.hasVertexDeformation()) + { + var paramRange = strokeMaterial.getVertexDeformationRange(); + var tolerance = strokeMaterial.getVertexDeformationTolerance(); + var nVertices = indices.length; + nVertices = ShapePrimitive.refineMesh( strokeVertices, strokeNormals, strokeTextures, indices, nVertices, paramRange, tolerance ); + var subdividedParts = ShapePrimitive.subdivideOversizedMesh( strokeVertices, strokeNormals, strokeTextures, indices ); + + primArray = []; + if (subdividedParts) + { + for (var i=0; i