-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsine-cheers.html
130 lines (95 loc) · 2.84 KB
/
sine-cheers.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE html>
<head>
<meta charset="utf-8"/>
<script src="etudes-webgl.js"></script>
<link rel="stylesheet" href="../etudes.css">
<script id="vshader" type="x-shader/x-vertex">
uniform mat4 uProjectionMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uModelMatrix;
uniform mat4 uNormalMatrix;
uniform bool uUseNormalMatrix;
uniform bool uUseAmbient;
uniform vec3 uAmbientColor;
uniform bool uUseDiffuse;
uniform vec3 uDiffuseColor;
uniform vec3 uLightDir;
attribute vec3 aXYZ;
attribute vec3 aColor;
attribute vec3 aNormal;
varying vec3 vColor;
void main ()
{
mat4 mvMatrix = uViewMatrix * uModelMatrix;
gl_Position = uProjectionMatrix * mvMatrix * vec4(aXYZ,1);
mat4 nMatrix = uUseNormalMatrix?uNormalMatrix:mvMatrix;
if (uUseAmbient)
vColor = uAmbientColor*aColor;
else
vColor = aColor;
if (uUseDiffuse)
{
vec3 light = normalize(-uLightDir);
vec3 normal = vec3(normalize(nMatrix*vec4(aNormal,0)));
vColor += aColor*uDiffuseColor*max(dot(normal,light),0.0);
}
}
</script>
<script id="fshader" type="x-shader/x-fragment">
precision mediump float;
varying vec3 vColor;
void main( )
{
gl_FragColor = vec4(vColor,1);
}
</script>
<script>
function start( )
{
init();
window.addEventListener('resize',resize,false);
resize();
drawFrame();
}
function init()
{
gl = getContext("picasso");
glprog = getProgram("vshader","fshader");
gl.enable(gl.DEPTH_TEST);
gl.clearColor(1,1,0.8,1);
identity();
perspective(30,gl.canvas.width/gl.canvas.height,1,40000);
gl.uniform1i(uUseNormalMatrix,false);
gl.uniform3f(uAmbientColor,0.4,0.4,0.4);
gl.uniform1i(uUseAmbient,true);
gl.uniform3f(uDiffuseColor,0.75,1,0.75);
gl.uniform1i(uUseDiffuse,true);
gl.uniform3f(uLightDir,0,0,-1);
wine_jug = new RotationalSolid ([ 0, 4,-5],[1,1,10],function(z){return Math.sin(6*z)+2;});
wine_glass = new RotationalSolid ([ 0,-4,-5],[0.6,0.6,7],function(z){return (Math.sin(6*z+3.1))/(z+0.15)+2.8;});
}
var startTime = Date.now();
function drawFrame()
{
requestAnimationFrame(drawFrame);
time = (Date.now()-startTime)/1000;
gl.clear(gl.COLOR_BUFFER_BIT+gl.DEPTH_BUFFER_BIT);
lookAt([40*Math.cos(time),40*Math.sin(time),5+2*Math.sin(time)], [0,0,0], [0,0,1]);
wine_jug.draw();
wine_glass.draw();
}
function resize()
{
gl.canvas.width = window.innerWidth;
gl.canvas.height = window.innerHeight;
gl.viewport(0,0,window.innerWidth,window.innerHeight);
perspective(20,gl.canvas.width/gl.canvas.height,1,40000);
}
</script>
</head>
<body onload="start()">
<h1>Sine cheers <a href="https://boytchev.github.io/etudes/">←</a></h1>
<canvas id="picasso" style="border: none; position:fixed; top:0; left:0; width:100%; height:100%;">
No canvas
</canvas>
</body>