forked from skulpt/skulpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebgl-0005-3d.html
146 lines (128 loc) · 4.88 KB
/
webgl-0005-3d.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL 3D</title>
<style type="text/css">
body { background-color: grey;}
canvas { background-color: white;}
</style>
<script src="../../dist/skulpt.js" type="text/javascript"></script>
<script src="../../dist/builtin.js" type="text/javascript"></script>
</head>
<body>
<h1>WebGL 3D</h1>
<form>
<textarea id="code" name="code" cols="120" rows="40">
import webgl
from math import sin
gl = webgl.Context("my-canvas")
trianglesVerticeBuffer = gl.createBuffer()
trianglesColorBuffer = gl.createBuffer()
program = None
uViewMatrix = None
uProjMatrix = None
viewMatrix = webgl.Matrix4x4([1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0])
projMatrix = webgl.Matrix4x4([1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0])
def setup():
print viewMatrix
print projMatrix
global program, uViewMatrix, uProjMatrix
vs = gl.createShader(gl.VERTEX_SHADER)
gl.shaderSource(vs, "" +
"attribute vec3 aVertexPosition;" +
"attribute vec3 aVertexColor;" +
"" +
"uniform mat4 uViewMatrix;" +
"uniform mat4 uProjMatrix;" +
"" +
"varying highp vec4 vColor;" +
"void main(void) {" +
" gl_Position = uProjMatrix * uViewMatrix * vec4(aVertexPosition, 1.0);" +
" vColor = vec4(aVertexColor, 1.0);" +
"}")
gl.compileShader(vs)
print "Vertex shader COMPILE_STATUS: " + str(gl.getShaderParameter(vs, gl.COMPILE_STATUS))
fs = gl.createShader(gl.FRAGMENT_SHADER)
gl.shaderSource(fs, "" +
"varying highp vec4 vColor;" +
"void main(void) {" +
" gl_FragColor = vColor;" +
"}")
gl.compileShader(fs)
print "Fragment shader COMPILE_STATUS: " + str(gl.getShaderParameter(fs, gl.COMPILE_STATUS))
program = gl.createProgram()
gl.attachShader(program, vs)
gl.attachShader(program, fs)
gl.linkProgram(program)
print "Program LINK_STATUS: " + str(gl.getProgramParameter(program, gl.LINK_STATUS))
gl.useProgram(program)
triangleVerticeColors = [1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0]
gl.bindBuffer(gl.ARRAY_BUFFER, trianglesColorBuffer)
gl.bufferData(gl.ARRAY_BUFFER, webgl.Float32Array(triangleVerticeColors), gl.STATIC_DRAW)
uViewMatrix = gl.getUniformLocation(program, "uViewMatrix")
uProjMatrix = gl.getUniformLocation(program, "uProjMatrix")
projMatrix.perspective(45, 4.0/3.0, 0.1, 100.0)
viewMatrix.identity()
viewMatrix.translate([0.0, 0.0, -2.0])
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.viewport(0, 0, 400, 300);
def draw(gl, elapsed):
gl.clear(gl.COLOR_BUFFER_BIT);
translation = sin(elapsed * 2.0 * 3.14159 / 10000.0)/2.0;
triangleVertices = [-0.5 + translation, 0.5, -0.5,
0.0 + translation, 0.0, -0.5,
-0.5 + translation, -0.5, -0.5,
0.5 + translation, 0.5, 0.5,
0.0 + translation, 0.0, 0.5,
0.5 + translation, -0.5, 0.5]
gl.bindBuffer(gl.ARRAY_BUFFER, trianglesVerticeBuffer)
gl.bufferData(gl.ARRAY_BUFFER, webgl.Float32Array(triangleVertices), gl.DYNAMIC_DRAW)
gl.uniformMatrix4fv(uProjMatrix, False, projMatrix)
gl.uniformMatrix4fv(uViewMatrix, False, viewMatrix)
vertexPositionAttribute = gl.getAttribLocation(program, "aVertexPosition")
gl.enableVertexAttribArray(vertexPositionAttribute)
gl.bindBuffer(gl.ARRAY_BUFFER, trianglesVerticeBuffer)
gl.vertexAttribPointer(vertexPositionAttribute, 3, gl.FLOAT, False, 0, 0)
vertexColorAttribute = gl.getAttribLocation(program, "aVertexColor")
gl.enableVertexAttribArray(vertexColorAttribute)
gl.bindBuffer(gl.ARRAY_BUFFER, trianglesColorBuffer)
gl.vertexAttribPointer(vertexColorAttribute, 3, gl.FLOAT, False, 0, 0)
gl.drawArrays(gl.TRIANGLES, 0, 6)
setup()
gl.setDrawFunc(draw);
</textarea>
<button onclick="runit()" type="button">Run</button>
</form>
<canvas id="my-canvas" height="300" width="400">
Your browser does not support the HTML5 canvas element.
</canvas>
<pre id="my-output" ></pre>
<script>
function outputHandler(text) {
var output = document.getElementById("my-output");
output.innerHTML = output.innerHTML + text.replace(/</g, '<');
}
function builtinRead(x) {
if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined) {
throw "File not found: '" + x + "'";
}
return Sk.builtinFiles["files"][x];
}
function runit() {
var prog = document.getElementById("code").value;
// Clear the output
document.getElementById("my-output").innerHTML = '';
Sk.canvas = "my-canvas";
Sk.pre = "my-output";
Sk.configure({"output":outputHandler, "read":builtinRead});
try {
eval(Sk.importMainWithBody("<stdin>", false, prog));
}
catch(e) {
alert(e);
}
}
</script>
</body>
</html>