-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
121 lines (108 loc) · 3.05 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
<!DOCTYPE html>
<html>
<head>
<title>WebGL crumbs</title>
</head>
<body>
<h1>Lesson 0</h1>
<canvas id="canvas" width="800" height="600"></canvas>
<script>
// 1: Create the rendering context
{
// 1.1: Initialize the context
{
// Get a canvas
var canvas = document.getElementById('canvas');
// Create a 3D rendering context
var gl = canvas.getContext('webgl');
// Check errors
if (!gl) {
throw new Error('WebGL not supported!')
}
}
// 1.2: Initialize the data
{
// vertex shader source code
var vertex_shader_source = `
void main(void) {
//nothing todo yet
}
`;
// fragment shader source code
var fragment_shader_source = `
void main(void) {
//nothing todo yet
}
`;
var clear_color = [0.5, 0.5, 0.5, 1];
}
}
// 2: Create the shaders and the shader program
{
// 2.1: Create the vertex shader
{
// Create a vertex shader object
var vertex_shader = gl.createShader(gl.VERTEX_SHADER);
// Attach vertex shader source code
gl.shaderSource(vertex_shader, vertex_shader_source);
// Compile the vertex shader
gl.compileShader(vertex_shader);
// Check errors
{
let compiled = gl.getShaderParameter(vertex_shader, gl.COMPILE_STATUS);
if (!compiled) {
let log = gl.getShaderInfoLog(vertex_shader)
throw new Error(log)
}
}
}
// 2.2: Create the fragment shader
{
// Create fragment shader object
var fragment_shader = gl.createShader(gl.FRAGMENT_SHADER);
// Attach fragment shader source code
gl.shaderSource(fragment_shader, fragment_shader_source);
// Compile the fragmentt shader
gl.compileShader(fragment_shader);
// Check errors
{
let compiled = gl.getShaderParameter(fragment_shader, gl.COMPILE_STATUS);
if (!compiled) {
let log = gl.getShaderInfoLog(fragment_shader)
throw new Error(log)
}
}
}
// 2.3: Create the shader program
{
// Create a shader program object
var shader_program = gl.createProgram();
// Attach the vertex shader
gl.attachShader(shader_program, vertex_shader);
// Attach the fragment shader
gl.attachShader(shader_program, fragment_shader);
// Link both programs
gl.linkProgram(shader_program);
// Use the combined shader program object
gl.useProgram(shader_program);
}
}
// 3: Draw
{
// 3.1: Clear canvas
{
gl.clearColor(1, 0, 0, 0.5);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.clearColor(1, 0, 0, 0.2);
gl.clear(gl.COLOR_BUFFER_BIT);
// Set the view port
gl.viewport(0, 0, canvas.width, canvas.height);
}
// 3.2: Draw
{
//...
}
}
</script>
</body>
</html>