Skip to content

Commit 87eb85c

Browse files
committed
add RevolutionSurface to surfaces demo
1 parent 85e0d42 commit 87eb85c

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

glutil-eval.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,17 @@ SurfaceEval.prototype.evaluate=function(mesh,u,v){
648648
GLMath.vec3scaleInPlace(vv,1.0/dv);
649649
GLMath.vec3normInPlace(vu);
650650
GLMath.vec3normInPlace(vv);
651-
vu=GLMath.vec3cross(vu,vv);
652-
GLMath.vec3normInPlace(vu);
651+
if(GLMath.vec3length(vu)==0){
652+
// partial derivative of u is degenerate
653+
//console.log([vu,vv]+" u degen")
654+
vu=vv;
655+
} else if(GLMath.vec3length(vv)!=0){
656+
vu=GLMath.vec3cross(vu,vv);
657+
GLMath.vec3normInPlace(vu);
658+
} else {
659+
// partial derivative of v is degenerate
660+
//console.log([vu,vv]+" v degen")
661+
}
653662
mesh.normal3(vu[0],vu[1],vu[2]);
654663
} else if(normal){
655664
mesh.normal3(normal[0],normal[1],normal[2]);

glutil_min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

surfaces.html

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
<body>
77
<a href="javascript:link1()">Ellipsoid</a>,
88
<a href="javascript:link2()">Klein bottle</a>,
9-
<a href="javascript:link3()">M&ouml;bius strip</a>
9+
<a href="javascript:link3()">M&ouml;bius strip</a>,
10+
<a href="javascript:link4()">Surface of revolution for f(x) = sin x</a>
11+
<a href="javascript:link5()">Surface of revolution for f(x) = x<sup>2</sup></a>
1012
<br>
1113
<canvas width="600" height="450" id=canvas></canvas>
1214
<script id="demo">
@@ -34,6 +36,22 @@
3436
}
3537
}
3638

39+
var RevolutionSurface=function(func,minval,maxval){
40+
this.func=func
41+
this.minval=minval
42+
this.maxval=maxval
43+
this.evaluate=function(u,v,output){
44+
v=1-v;
45+
v*=Math.PI*2;
46+
u=minval+(maxval-minval)*u;
47+
var r=this.func(u);
48+
output[0]=u;
49+
output[1]=r*Math.cos(v);
50+
output[2]=r*Math.sin(v);
51+
return 3;
52+
}
53+
}
54+
3755
var KleinBottle=function(){
3856
this.evaluate=function(u,v,output){
3957
u*=Math.PI*2;
@@ -82,8 +100,6 @@
82100
}
83101
}
84102

85-
86-
87103
var ColorGradient={
88104
evaluate:function(u,v,output){
89105
output[0]=1.0-u;
@@ -100,7 +116,6 @@
100116
.color(ColorGradient)
101117
.setAutoNormal(true)
102118
.evalSurface(mesh,Mesh.TRIANGLES,50,50);
103-
console.log(mesh)
104119
return mesh;
105120
}
106121

@@ -119,6 +134,26 @@
119134
scene.addShape(shape=scene.makeShape(makeMesh(new MoebiusStrip())));
120135
}
121136

137+
function link4(){
138+
scene.removeShape(shape);
139+
scene.addShape(shape=scene.makeShape(makeMesh(new RevolutionSurface(function(x){
140+
return Math.sin(x)
141+
},-Math.PI,Math.PI))));
142+
}
143+
144+
function link5(){
145+
scene.removeShape(shape);
146+
scene.addShape(shape=scene.makeShape(makeMesh(new RevolutionSurface(function(x){
147+
return x*x
148+
},-1,1))));
149+
}
150+
151+
function link5(){
152+
scene.removeShape(shape);
153+
scene.addShape(shape=scene.makeShape(makeMesh(new RevolutionSurface(function(x){
154+
return x*x
155+
},-1,1))));
156+
}
122157

123158
// Create the 3D scene; find the HTML canvas and pass it
124159
// to Scene3D.

0 commit comments

Comments
 (0)