|
5 | 5 | canvas { width:100%; height:100%; overflow: hidden; } |
6 | 6 | </style> |
7 | 7 | <script type="text/javascript" src="../glutil_min.js"></script> |
| 8 | +<script type="text/javascript" src="../extras/evaluators.js"></script> |
8 | 9 | <script type="text/javascript" src="../extras/camera.js"></script> |
9 | 10 | <script type="text/javascript" src="../extras/frame.js"></script> |
10 | 11 | <script type="text/javascript" src="extras.js"></script> |
|
93 | 94 | return Math.sign(r)*Math.pow(Math.abs(r),n); |
94 | 95 | } |
95 | 96 |
|
96 | | -/** |
97 | | -* Parametric evaluator for a surface of revolution, which results by revolving |
98 | | -* an X/Y curve around an axis. |
99 | | -* @class |
100 | | -* @alias SurfaceOfRevolution |
101 | | -* @param {Function} curve Curve to rotate about the X-axis. |
102 | | -* The curve function must contain a function |
103 | | -* named "evaluate", which takes the following parameter:<ul> |
104 | | -* <li><code>u</code> - A curve coordinate, generally from 0 to 1. |
105 | | -* </ul> |
106 | | -* The evaluator function returns an array of at least 2 elements: the first |
107 | | -* element is the X coordinate of the curve's position, and the second |
108 | | -* element is the Y coordinate. |
109 | | -* @param {number} minval Smallest U-coordinate. |
110 | | -* @param {number} maxval Largest U-coordinate. |
111 | | -*/ |
112 | | -var SurfaceOfRevolution=function(curve,minval,maxval){ |
113 | | - this.curve=curve |
114 | | - this.minval=minval |
115 | | - this.maxval=maxval |
116 | | - this.evaluate=function(u,v){ |
117 | | - v=1-v; |
118 | | - v*=GLMath.PiTimes2; |
119 | | - u=minval+(maxval-minval)*u; |
120 | | - var cosv = Math.cos(v); |
121 | | - var sinv = (v>=0 && v<6.283185307179586) ? (v<=3.141592653589793 ? Math.sqrt(1.0-cosv*cosv) : -Math.sqrt(1.0-cosv*cosv)) : Math.sin(v); |
122 | | - var curvepos=this.curve.evaluate(u); |
123 | | - return [curvepos[0],curvepos[1]*cosv,curvepos[1]*sinv]; |
124 | | - } |
125 | | -} |
126 | | -/** |
127 | | -* Parametric evaluator for a surface of revolution whose curve is the graph of |
128 | | -* a single-variable function. |
129 | | -* @param {Function} func Function whose graph will be |
130 | | -* rotated about the X-axis. The function takes a number |
131 | | -* as a single parameter and returns a number. |
132 | | -* @param {number} minval Smallest parameter of the function. |
133 | | -* @param {number} maxval Largest parameter of the function. |
134 | | -* @return {SurfaceOfRevolution} |
135 | | -*/ |
136 | | -SurfaceOfRevolution.fromFunction=function(func,minval,maxval){ |
137 | | - return new SurfaceOfRevolution({ |
138 | | - "evaluate":function(u){ |
139 | | - return [u,func(u),0]; |
140 | | - }},minval,maxval); |
141 | | -} |
142 | | -/** |
143 | | -* Parametric evaluator for a torus, a special case of a surface of revolution. |
144 | | -* @param {number} outerRadius Radius from the center to the innermost |
145 | | -* part of the torus. |
146 | | -* @param {number} innerRadius Radius from the inner edge to the innermost |
147 | | -* part of the torus. |
148 | | -* @param {Function|undefined} curve Object describing |
149 | | -* a curve to serve as the cross section of the torus. |
150 | | -* The curve need not be closed; in fact, certain special surfaces can result |
151 | | -* by leaving the ends open. |
152 | | -* The curve function must contain a function |
153 | | -* named "evaluate", which takes the following parameter:<ul> |
154 | | -* <li><code>u</code> - A curve coordinate, generally from 0 to 1. |
155 | | -* </ul> |
156 | | -* The evaluator function returns an array of at least 2 elements: the first |
157 | | -* element is the X coordinate of the curve's position, and the second |
158 | | -* element is the Y coordinate. If null or omitted, uses a circular cross section. |
159 | | -* @return {SurfaceOfRevolution} |
160 | | -*/ |
161 | | -SurfaceOfRevolution.torus=function(outerRadius,innerRadius,curve){ |
162 | | - if(!curve)curve={ |
163 | | - "evaluate":function(u){ |
164 | | - u*=GLMath.PiTimes2; |
165 | | - return [Math.sin(u),Math.cos(v)] |
166 | | - } |
167 | | - } |
168 | | - return new SurfaceOfRevolution({ |
169 | | - "evaluate":function(u){ |
170 | | - var curvept=curve.evaluate(u) |
171 | | - var x=innerRadius*curvept[1]; |
172 | | - var y=outerRadius+innerRadius*curvept[0]; |
173 | | - return [x,y,0]; |
174 | | - }},0,GLMath.PiTimes2); |
175 | | -} |
176 | | - |
177 | 97 | var KleinBottle=function(){ |
178 | 98 | this.evaluate=function(u,v){ |
179 | 99 | var cospi; |
|
238 | 158 | return [x,y,z] |
239 | 159 | } |
240 | 160 | } |
241 | | - function makeMesh(func,resolution){ |
242 | | - // Default resolution is 50 |
243 | | - if(resolution==null)resolution=50 |
244 | | - // create a new mesh |
245 | | - var mesh=new Mesh(); |
246 | | - // define a color gradient evaluator for |
247 | | - // demonstration purposes. Instead of X, Y, and Z, |
248 | | - // generate a Red/Green/Blue color based on |
249 | | - // the same parameters U and V as the surface |
250 | | - // function for 3D points. |
251 | | - var colorGradient={ |
252 | | - "evaluate":function(u,v){ return [1-u,v,u]; } |
253 | | - } |
254 | | - // generate the parametric surface. |
255 | | - |
256 | | - var ev=new SurfaceEval() |
257 | | - .vertex(func) |
258 | | - // Specify the color gradient evaluator defined above |
259 | | - .color(colorGradient) |
260 | | - // Generate normals for the parametric surface, |
261 | | - // which is required for lighting to work correctly |
262 | | - .setAutoNormal(true) |
263 | | - // Evaluate the surface and generate a triangle |
264 | | - // mesh, using resolution+1 different U-coordinates ranging |
265 | | - // from 0 to 1, and resolution+1 |
266 | | - // different V-coordinates ranging from 0 to 1 |
267 | | - // Instead of Mesh.TRIANGLES, we could use |
268 | | - // Mesh.LINES to create a wireframe mesh, |
269 | | - // or Mesh.POINTS to create a point mesh. |
270 | | - .evalSurface(mesh,Mesh.TRIANGLES,resolution,resolution); |
271 | | - // Surface generated, return the mesh |
272 | | - return mesh; |
273 | | - } |
274 | 161 |
|
275 | 162 | var shapeGroup=new ShapeGroup(); |
276 | 163 | var allsettings={} |
|
0 commit comments