-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_6.php
114 lines (87 loc) · 2.87 KB
/
demo_6.php
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>WebGL Demo using "only" fragment Shader</title>
<link rel="stylesheet" href="./styles/style.css" type="text/css">
</head>
<body>
<h1>WebGL the fragment Shader</h1>
<canvas id="glcanvas" width="640" height="480"></canvas>
<p>Image generated with the fragment shader using position as color.</p>
<?php include("_navigation.php"); ?>
</body>
<script type="module">
startGL();
function startGL() {
let canvas = document.querySelector('#glcanvas');
let gl = canvas.getContext('webgl');
if (!gl) {
alert('Unable to initialize WebGL. Your browser or machine may not support it.');
return;
}
let buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array([
-1.0, -1.0,
1.0, -1.0,
-1.0, 1.0,
-1.0, 1.0,
1.0, -1.0,
1.0, 1.0]),
gl.STATIC_DRAW
);
let shaderScript = document.getElementById("2d-vertex-shader");
let shaderSource = shaderScript.text;
let vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, shaderSource);
gl.compileShader(vertexShader);
shaderScript = document.getElementById("2d-fragment-shader");
shaderSource = shaderScript.text;
let fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, shaderSource);
gl.compileShader(fragmentShader);
let program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
gl.useProgram(program);
gl.clearColor(1.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
let positionLocation = gl.getAttribLocation(program, "position");
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLES, 0, 6);
//window.requestAnimationFrame(render, canvas);
}
function render() {
window.requestAnimationFrame(render, canvas);
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
</script>
<script id="2d-vertex-shader" type="x-shader/x-vertex">//
attribute vec2 position;
void main() {
gl_Position = vec4(position, 0, 1);
}
// ]]></script>
<script id="2d-fragment-shader" type="x-shader/x-fragment">//
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
precision mediump int;
void main() {
vec2 z;
float x,y;
x = gl_FragCoord.x / 640.0;
y = gl_FragCoord.y / 240.0;
z.x = x;
z.y = y;
gl_FragColor = vec4((z.x * z.y)/2.0, z.x, z.y, 0.5);
}
// ]]></script>
</html>