-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
194 lines (161 loc) · 5.69 KB
/
index.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!doctype html>
<html><head>
<script id="vshader" type="x-shader">
precision highp float;
attribute vec3 aVtxPos;
attribute vec3 aVtxNormal;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying vec3 vTransformedNormal;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVtxPos, 1.0);;
vec4 tnorm = uMVMatrix * vec4(aVtxNormal, 0.0);
vTransformedNormal = tnorm.xyz;
}
</script>
<script id="fshader" type="x-shader">
precision highp float;
varying vec3 vTransformedNormal;
void main(void) {
vec3 normal = normalize(vTransformedNormal);
vec3 lightDir = vec3(0,1,1);
float c = dot(normal, lightDir);
vec3 col = vec3(1.0, 0.5, 0.0) * c;
gl_FragColor = vec4(col, 1.0);
}
</script>
<script type="text/javascript" src="glMatrix-0.9.4.min.js"></script>
<script type="text/javascript" src="cube.json"></script>
<script type="text/javascript">
var gl = null, canvas = null
var prog = null
var model = { }
function start() {
// init 3d view
canvas = document.getElementById("canvas")
gl = canvas.getContext("webgl")
gl.clearColor(255.0, 255.0, 255.0, 1.0);
gl.clearDepth(1.0)
gl.enable(gl.DEPTH_TEST)
// compile shaders
makeShader = function(type, elemId) {
s = gl.createShader(type)
gl.shaderSource(s, document.getElementById(elemId).text)
gl.compileShader(s)
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) {
var err = gl.getShaderInfoLog(s)
throw "FAILED " + elemId + "\n" + err;
}
return s
}
prog = gl.createProgram();
gl.attachShader(prog, makeShader(gl.VERTEX_SHADER, "vshader"))
gl.attachShader(prog, makeShader(gl.FRAGMENT_SHADER, "fshader"))
gl.linkProgram(prog)
prog.attrVtx = gl.getAttribLocation(prog, "aVtxPos");
gl.enableVertexAttribArray(prog.attrVtx);
prog.attrNorm = gl.getAttribLocation(prog, "aVtxNormal");
gl.enableVertexAttribArray(prog.attrNorm);
prog.uMVMat = gl.getUniformLocation(prog, "uMVMatrix");
prog.uPMat = gl.getUniformLocation(prog, "uPMatrix");
gl.useProgram(prog)
loadModel(simple_cube)
// init user input
rotMat = mat4.create()
mat4.identity(rotMat)
var mousePressed = false
var lastX = 0, lastY = 0
canvas.onmousedown = function(e) {
mousePressed = true;
lastX = event.clientX;
lastY = event.clientY;
}
document.onmouseup = function(e) {
mousePressed = false;
}
document.onmousemove = function(e) {
if (!mousePressed)
return
mat4.rotateY(rotMat, (event.clientX - lastX) / 200)
mat4.rotateX(rotMat, (event.clientY - lastY) / 200)
lastX = event.clientX
lastY = event.clientY
}
// start drawing
requestAnimationFrame(draw)
}
simple_cube = {
"vertexPositions" :
[ 1.0, -1.0, 0.0,
-1.0, 1.0, 0.0,
-1.0, -1.0, 0.0,
1.0, 1.0, 0.0 ],
"vertexNormals":
[ 0, 0, 1.0,
0, 0, 1.0,
0, 0, 1.0,
0, 0, 1.0,
],
"triangles" :
[1, 2, 3,
3, 2, 0]
}
function loadModel(cube)
{
// create model
var vtxData = new Float32Array(cube.vertexPositions) //new Float32Array([0,0,0, 0.5,0,0, 0.5,0.5,0])
model.vtxBuf = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, model.vtxBuf)
gl.bufferData(gl.ARRAY_BUFFER, vtxData, gl.STATIC_DRAW);
model.vtxBuf.numItems = vtxData.length / 3
var normalData = new Float32Array(cube.vertexNormals)
model.normBuf = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, model.normBuf)
gl.bufferData(gl.ARRAY_BUFFER, normalData, gl.STATIC_DRAW);
model.normBuf.numItems = normalData.length / 3
var iData = new Uint16Array(cube.triangles)
model.indices = gl.createBuffer()
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, model.indices);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, iData, gl.STATIC_DRAW);
model.indices.numItems = iData.length
model.indices.type = gl.UNSIGNED_SHORT
}
var camera = { eye: [0,1,5], toPoint: [0,0,0], upDir: [0,1,0] }
var rotMat = null
function draw()
{
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
pr = mat4.create()
mat4.identity(pr)
mat4.perspective(45, canvas.width / canvas.height, 0.1, 100.0, pr);
mv = mat4.create()
mat4.identity(mv)
mat4.multiply(mv, mat4.lookAt(camera.eye, camera.toPoint, camera.upDir))
mat4.multiply(mv, rotMat)
gl.uniformMatrix4fv(prog.uPMat, false, pr);
gl.uniformMatrix4fv(prog.uMVMat, false, mv);
// draw model
gl.bindBuffer(gl.ARRAY_BUFFER, model.vtxBuf)
gl.vertexAttribPointer(prog.attrVtx, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, model.normBuf)
gl.vertexAttribPointer(prog.attrNorm, 3, gl.FLOAT, false, 0, 0);
//gl.drawArrays(gl.TRIANGLES, 0, model.vtxBuf.numItems);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, model.indices)
gl.drawElements(gl.TRIANGLES, model.indices.numItems, model.indices.type, 0);
// next frame
requestAnimationFrame(draw)
}
function triggerSimple() {
if (usesimple.checked)
loadModel(simple_cube)
else
loadModel(cube)
}
</script>
</head>
<body onload="start();">
<input id="usesimple" type="checkbox" checked onchange="triggerSimple()"/>Simple<br>
<canvas id="canvas" width="900" height="650"></canvas>
</body>
</html>