This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment-0.html
182 lines (150 loc) · 5.71 KB
/
assignment-0.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
<!DOCTYPE html>
<html>
<head>
<title>Assignment 0 - Hello not triangle</title>
</head>
<body onload="startup()">
<script type="text/javascript">
function startup() {
let canvas = document.getElementById("glCanvas");
let gl = createGLContext(canvas);
let shaderProgram = setupShaders(gl);
buffers = setupBuffers(gl);
draw(gl, shaderProgram, buffers.vertexBuffer, buffers.colorBuffer);
}
function createGLContext(canvas) {
let names = ["webgl", "experimental-webgl"];
let context = null;
names.forEach(name => {
if (!context) {
try {
context = canvas.getContext(name);
}
catch(_) {}
}
});
if (context) {
context.viewportWidth = canvas.width;
context.viewportHeight = canvas.height;
} else {
alert("Failed to create WebGL context");
}
return context;
}
function setupShaders(gl) {
let vertexShaderSource = "attribute vec3 aVertexPosition;"
+ "attribute vec4 aVertexColor;"
+ "varying lowp vec4 vColor;"
+ "void main() {"
+ " gl_Position = vec4(aVertexPosition, 1.0);"
+ " vColor = aVertexColor;"
+ "}";
// implementasi vertex color
let fragmentShaderSource = "precision mediump float;"
+ "varying lowp vec4 vColor;"
+ "void main() {"
+ " gl_FragColor = vColor;"
+ "}";
gl.loadShader = (type, shaderSource) => {
let shader = gl.createShader(type);
gl.shaderSource(shader, shaderSource);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert("Error compiling shader " + gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
let vertexShader = gl.loadShader(gl.VERTEX_SHADER, vertexShaderSource);
let fragmentShader = gl.loadShader(gl.FRAGMENT_SHADER, fragmentShaderSource);
shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Failed to setup shaders");
}
gl.useProgram(shaderProgram);
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
return shaderProgram;
}
function setupBuffers(gl) {
let triangleVertices = [];
let triangleColors = [];
let circle = (percent) => {
return {
x: Math.cos(2 * Math.PI * percent),
y: Math.sin(2 * Math.PI * percent)
};
}
let hsv2rgb = (h, s, v) => {
let i = Math.floor(h * 6);
let f = h * 6 - i;
let p = v * (1 - s);
let q = v * (1 - f * s);
let t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: return { r: v, g: t, b: p };
case 1: return { r: q, g: v, b: p };
case 2: return { r: p, g: v, b: t };
case 3: return { r: p, g: q, b: v };
case 4: return { r: t, g: p, b: v };
case 5: return { r: v, g: p, b: q };
}
}
// pengisian array untuk menggambar lebih dari satu segitiga membentuk lingkaran
// sekaligus pengisian array untuk warna vertex mengikuti gradasi hue
let slices = 64;
for (let i = 0; i < slices; ++i) {
let p1 = circle(i / slices);
let p2 = circle((i + 1) /slices);
triangleVertices = triangleVertices.concat([
0.0, 0.0, 0.0,
p1.x * 0.5, p1.y * 0.5, 0.0,
p2.x * 0.5, p2.y * 0.5, 0.0,
]);
let c1 = hsv2rgb(i / slices, 1.0, 0.5);
let c2 = hsv2rgb((i + 1) / slices, 1.0, 0.5);
triangleColors = triangleColors.concat([
0.0, 0.0, 0.0, 0.0,
c1.r, c1.g, c1.b, 1.0,
c2.r, c2.g, c2.b, 1.0,
]);
}
let vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(triangleVertices), gl.STATIC_DRAW);
vertexBuffer.itemSize = 3;
vertexBuffer.numberOfItems = vertexBuffer.itemSize * slices;
let colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(triangleColors), gl.STATIC_DRAW);
colorBuffer.itemSize = 4;
colorBuffer.numberOfItems = colorBuffer.itemSize * slices;
return {
vertexBuffer: vertexBuffer,
colorBuffer: colorBuffer,
};
}
function draw(gl, shaderProgram, vertexBuffer, colorBuffer) {
// warna background putih keabuan
gl.clearColor(0.8, 0.8, 0.8, 1.0);
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, vertexBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, colorBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
gl.drawArrays(gl.TRIANGLES, 0, vertexBuffer.numberOfItems);
}
</script>
<a href="index.html">Return to index</a><br/>
<a href="https://github.com/threeal/grafika-komputer/blob/master/assignment-0.html">Source Code</a><br/>
<canvas id="glCanvas" width="500" height="500">
</canvas>
</body>
</html>