diff options
Diffstat (limited to 'js/lib/geom/shape-primitive.js')
-rw-r--r-- | js/lib/geom/shape-primitive.js | 486 |
1 files changed, 333 insertions, 153 deletions
diff --git a/js/lib/geom/shape-primitive.js b/js/lib/geom/shape-primitive.js index 34db60e8..3e9b0a0b 100644 --- a/js/lib/geom/shape-primitive.js +++ b/js/lib/geom/shape-primitive.js | |||
@@ -1,24 +1,25 @@ | |||
1 | /* <copyright> | 1 | /* <copyright> |
2 | Copyright (c) 2012, Motorola Mobility, Inc | 2 | Copyright (c) 2012, Motorola Mobility LLC. |
3 | All Rights Reserved. | 3 | All Rights Reserved. |
4 | BSD License. | ||
5 | 4 | ||
6 | Redistribution and use in source and binary forms, with or without | 5 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met: | 6 | modification, are permitted provided that the following conditions are met: |
8 | 7 | ||
9 | - Redistributions of source code must retain the above copyright notice, | 8 | * Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. | 9 | this list of conditions and the following disclaimer. |
11 | - Redistributions in binary form must reproduce the above copyright | 10 | |
12 | notice, this list of conditions and the following disclaimer in the | 11 | * Redistributions in binary form must reproduce the above copyright notice, |
13 | documentation and/or other materials provided with the distribution. | 12 | this list of conditions and the following disclaimer in the documentation |
14 | - Neither the name of Motorola Mobility nor the names of its contributors | 13 | and/or other materials provided with the distribution. |
15 | may be used to endorse or promote products derived from this software | 14 | |
16 | without specific prior written permission. | 15 | * Neither the name of Motorola Mobility LLC nor the names of its |
16 | contributors may be used to endorse or promote products derived from this | ||
17 | software without specific prior written permission. | ||
17 | 18 | ||
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 19 | 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 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
22 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 23 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 24 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 25 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
@@ -32,106 +33,319 @@ POSSIBILITY OF SUCH DAMAGE. | |||
32 | var ShapePrimitive = {}; | 33 | var ShapePrimitive = {}; |
33 | 34 | ||
34 | ShapePrimitive.create = function(coords, normals, uvs, indices, primType, vertexCount) { | 35 | ShapePrimitive.create = function(coords, normals, uvs, indices, primType, vertexCount) { |
35 | var renderer = RDGE.globals.engine.getContext().renderer; | 36 | var renderer = RDGE.globals.engine.getContext().renderer; |
36 | 37 | ||
37 | // to setup a primitive you must define it | 38 | // to setup a primitive you must define it |
38 | // create a new primitive definition here to then fill out | 39 | // create a new primitive definition here to then fill out |
39 | var prim = new RDGE.rdgePrimitiveDefinition(); | 40 | var prim = new RDGE.rdgePrimitiveDefinition(); |
40 | 41 | ||
41 | // the vertex definition declares how the data will be delivered to the shader | 42 | // the vertex definition declares how the data will be delivered to the shader |
42 | // the position of an element in array determines which attribute in a shader the | 43 | // the position of an element in array determines which attribute in a shader the |
43 | // data is bound to | 44 | // data is bound to |
44 | prim.vertexDefinition = { | 45 | prim.vertexDefinition = { |
45 | // this shows two ways to map this data to an attribute | 46 | // this shows two ways to map this data to an attribute |
46 | "vert":{'type':renderer.VS_ELEMENT_POS, 'bufferIndex':0, 'bufferUsage': renderer.BUFFER_STATIC}, | 47 | "vert":{'type':renderer.VS_ELEMENT_POS, 'bufferIndex':0, 'bufferUsage': renderer.BUFFER_STATIC}, |
47 | "a_pos":{'type':renderer.VS_ELEMENT_POS, 'bufferIndex':0, 'bufferUsage': renderer.BUFFER_STATIC}, | 48 | "a_pos":{'type':renderer.VS_ELEMENT_POS, 'bufferIndex':0, 'bufferUsage': renderer.BUFFER_STATIC}, |
48 | 49 | ||
49 | "normal":{'type':renderer.VS_ELEMENT_FLOAT3, 'bufferIndex':1, 'bufferUsage': renderer.BUFFER_STATIC}, | 50 | "normal":{'type':renderer.VS_ELEMENT_FLOAT3, 'bufferIndex':1, 'bufferUsage': renderer.BUFFER_STATIC}, |
50 | "a_nrm":{'type':renderer.VS_ELEMENT_FLOAT3, 'bufferIndex':1, 'bufferUsage': renderer.BUFFER_STATIC}, | 51 | "a_nrm":{'type':renderer.VS_ELEMENT_FLOAT3, 'bufferIndex':1, 'bufferUsage': renderer.BUFFER_STATIC}, |
51 | "a_normal":{'type':renderer.VS_ELEMENT_FLOAT3, 'bufferIndex':1, 'bufferUsage': renderer.BUFFER_STATIC}, | 52 | "a_normal":{'type':renderer.VS_ELEMENT_FLOAT3, 'bufferIndex':1, 'bufferUsage': renderer.BUFFER_STATIC}, |
52 | 53 | ||
53 | "texcoord":{'type':renderer.VS_ELEMENT_FLOAT2, 'bufferIndex':2, 'bufferUsage': renderer.BUFFER_STATIC}, | 54 | "texcoord":{'type':renderer.VS_ELEMENT_FLOAT2, 'bufferIndex':2, 'bufferUsage': renderer.BUFFER_STATIC}, |
54 | "a_texcoord":{'type':renderer.VS_ELEMENT_FLOAT2, 'bufferIndex':2, 'bufferUsage': renderer.BUFFER_STATIC} | 55 | "a_texcoord":{'type':renderer.VS_ELEMENT_FLOAT2, 'bufferIndex':2, 'bufferUsage': renderer.BUFFER_STATIC} |
55 | }; | 56 | }; |
56 | 57 | ||
57 | // the actual data that correlates to the vertex definition | 58 | // the actual data that correlates to the vertex definition |
58 | prim.bufferStreams = [ coords, normals, uvs ]; | 59 | prim.bufferStreams = [ coords, normals, uvs ]; |
59 | 60 | ||
60 | // what type of buffers the data resides in, static is the most common case | 61 | // what type of buffers the data resides in, static is the most common case |
61 | prim.streamUsage = [ renderer.BUFFER_STATIC, renderer.BUFFER_STATIC, renderer.BUFFER_STATIC ]; | 62 | prim.streamUsage = [ renderer.BUFFER_STATIC, renderer.BUFFER_STATIC, renderer.BUFFER_STATIC ]; |
62 | 63 | ||
63 | // this tells the renderer to draw the primitive as a list of triangles | 64 | // this tells the renderer to draw the primitive as a list of triangles |
64 | prim.type = primType; | 65 | prim.type = primType; |
65 | 66 | ||
66 | prim.indexUsage = renderer.BUFFER_STREAM; | 67 | prim.indexUsage = renderer.BUFFER_STREAM; |
67 | prim.indexBuffer = indices; | 68 | prim.indexBuffer = indices; |
68 | 69 | ||
69 | // finally the primitive is created, buffers are generated and the system determines | 70 | // finally the primitive is created, buffers are generated and the system determines |
70 | // the data it needs to draw this primitive according to the previous definition | 71 | // the data it needs to draw this primitive according to the previous definition |
71 | renderer.createPrimitive(prim, vertexCount); | 72 | renderer.createPrimitive(prim, vertexCount); |
72 | 73 | ||
73 | return prim; | 74 | return prim; |
74 | }; | 75 | }; |
75 | 76 | ||
76 | ShapePrimitive.getMeshBounds = function( verts, nVerts ) | 77 | ShapePrimitive.getMeshBounds = function( verts, nVerts ) |
77 | { | 78 | { |
78 | if (!verts || (nVerts <= 0)) return null; | 79 | if (!verts || (nVerts <= 0)) return null; |
80 | |||
81 | var bounds = [verts[0], verts[1], verts[2], verts[0], verts[1], verts[2]]; | ||
82 | var index = 3; | ||
83 | for (var i=1; i<nVerts; i++) | ||
84 | { | ||
85 | var x = verts[index], y = verts[index+1], z = verts[index+2]; | ||
86 | index += 3; | ||
87 | |||
88 | if (x < bounds[0]) bounds[0] = x; | ||
89 | else if (x > bounds[3]) bounds[3] = x; | ||
90 | if (y < bounds[1]) bounds[1] = y; | ||
91 | else if (y > bounds[4]) bounds[4] = y; | ||
92 | if (z < bounds[2]) bounds[2] = z; | ||
93 | else if (z > bounds[5]) bounds[5] = z; | ||
94 | } | ||
95 | |||
96 | return bounds; | ||
97 | }; | ||
79 | 98 | ||
80 | var bounds = [verts[0], verts[1], verts[2], verts[0], verts[1], verts[2]]; | 99 | ShapePrimitive.getBounds = function( prim ) |
81 | var index = 3; | 100 | { |
82 | for (var i=1; i<nVerts; i++) | 101 | var verts = prim.bufferStreams[0]; |
83 | { | 102 | var nVerts = verts.length; |
84 | var x = verts[index], y = verts[index+1], z = verts[index+2]; | 103 | var xMin = verts[0], xMax = verts[0], |
85 | index += 3; | 104 | yMin = verts[1], yMax = verts[1], |
105 | zMin = verts[2], zMax = verts[2]; | ||
106 | |||
107 | for (var index=3; index<verts.length; ) | ||
108 | { | ||
109 | if (verts[index] < xMin) xMin = verts[index]; | ||
110 | else if (verts[index] > xMax) xMax = verts[index]; | ||
111 | |||
112 | index++; | ||
113 | if (verts[index] < yMin) yMin = verts[index]; | ||
114 | else if (verts[index] > yMax) yMax = verts[index]; | ||
115 | |||
116 | index++; | ||
117 | if (verts[index] < zMin) zMin = verts[index]; | ||
118 | else if (verts[index] > zMax) zMax = verts[index]; | ||
119 | |||
120 | index++; | ||
121 | } | ||
122 | |||
123 | return [xMin, yMin, zMin, xMax, yMax, zMax]; | ||
124 | }; | ||
86 | 125 | ||
87 | if (x < bounds[0]) bounds[0] = x; | 126 | ShapePrimitive.refineMesh = function( verts, norms, uvs, indices, nVertices, paramRange, tolerance ) |
88 | else if (x > bounds[3]) bounds[3] = x; | 127 | { |
89 | if (y < bounds[1]) bounds[1] = y; | 128 | var oldVrtCount = nVertices; |
90 | else if (y > bounds[4]) bounds[4] = y; | 129 | |
91 | if (z < bounds[2]) bounds[2] = z; | 130 | // get the param range |
92 | else if (z > bounds[5]) bounds[5] = z; | 131 | var pUMin = paramRange[0], pVMin = paramRange[1], |
132 | pUMax = paramRange[2], pVMax = paramRange[3]; | ||
133 | var iTriangle = 0; | ||
134 | var nTriangles = indices.length/3; | ||
135 | var index = 0; | ||
136 | while (iTriangle < nTriangles) | ||
137 | { | ||
138 | // get the indices of the 3 vertices | ||
139 | var i0 = indices[index], | ||
140 | i1 = indices[index+1], | ||
141 | i2 = indices[index+2]; | ||
142 | |||
143 | // get the uv values | ||
144 | //var vrtIndex = 3*iTriangle; | ||
145 | var iuv0 = 2 * i0, | ||
146 | iuv1 = 2 * i1, | ||
147 | iuv2 = 2 * i2; | ||
148 | var u0 = uvs[iuv0], v0 = uvs[iuv0+1], | ||
149 | u1 = uvs[iuv1], v1 = uvs[iuv1+1], | ||
150 | u2 = uvs[iuv2], v2 = uvs[iuv2+1]; | ||
151 | |||
152 | // find the u and v range | ||
153 | var uMin = u0, vMin = v0; | ||
154 | if (u1 < uMin) uMin = u1; if (v1 < vMin) vMin = v1; | ||
155 | if (u2 < uMin) uMin = u2; if (v2 < vMin) vMin = v2; | ||
156 | var uMax = u0, vMax = v0; | ||
157 | if (u1 > uMax) uMax = u1; if (v1 > vMax) vMax = v1; | ||
158 | if (u2 > uMax) uMax = u2; if (v2 > vMax) vMax = v2; | ||
159 | |||
160 | // if the parameter range of the triangle is outside the | ||
161 | // desired parameter range, advance to the next polygon and continue | ||
162 | if ((uMin > pUMax) || (uMax < pUMin) || (vMin > pVMax) || (vMax < pVMin)) | ||
163 | { | ||
164 | // go to the next triangle | ||
165 | iTriangle++; | ||
166 | index += 3; | ||
167 | } | ||
168 | else | ||
169 | { | ||
170 | // check thesize of the triangle in uv space. If small enough, advance | ||
171 | // to the next triangle. If not small enough, split the triangle into 3; | ||
172 | var du = Math.abs(uMax) - uMin, dv = Math.abs(vMax - vMin); | ||
173 | if ((du < tolerance) && (dv < tolerance)) | ||
174 | { | ||
175 | iTriangle++; | ||
176 | index += 3; | ||
177 | } | ||
178 | else // split the triangle into 4 parts | ||
179 | { | ||
180 | //calculate the position of the new vertex | ||