-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject3.html
362 lines (303 loc) · 12.3 KB
/
project3.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<doctype html>
<!-- Copyright 2020, Cem Yuksel, University of Utah -->
<html>
<head>
<script type="text/javascript" src="src/obj.js"></script>
<script type="text/javascript" src="src/trs.js"></script>
<script type="text/javascript" src="src/meshDrawer.js"></script>
<script type="text/javascript" src="src/sceneNode.js"></script>
<script type="text/javascript" src="src/sphere.js"></script>
<script type="text/javascript" src="src/camera.js"></script>
<script type="text/javascript" src="src/perspective.js"></script>
<script type="text/javascript" src="src/utils.js"></script>
<script type="text/javascript">
// math utils
function getIdentityMatrix() {
return [
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
];
}
function add(a, b) {
return [
a[0] + b[0],
a[1] + b[1],
a[2] + b[2]
];
}
function subtract(a, b) {
return [
a[0] - b[0],
a[1] - b[1],
a[2] - b[2]
];
}
function MatrixMult(A, B) {
var C = [];
for (var i = 0; i < 4; ++i) {
for (var j = 0; j < 4; ++j) {
var v = 0;
for (var k = 0; k < 4; ++k) {
v += A[j + 4 * k] * B[k + 4 * i];
}
C.push(v);
}
}
return C;
}
function dot(A, B) {
var v = 0;
for (var i = 0; i < A.length; ++i) {
v += A[i] * B[i];
}
return v;
}
function cross(A, B) {
return [
A[1] * B[2] - A[2] * B[1],
A[2] * B[0] - A[0] * B[2],
A[0] * B[1] - A[1] * B[0]
];
}
function normalize(v) {
dst = new Float32Array(3);
var length = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
// make sure we don't divide by 0.
if (length > 0.00001) {
dst[0] = v[0] / length;
dst[1] = v[1] / length;
dst[2] = v[2] / length;
}
return dst;
}
function isPowerOf2(value) {
return (value & (value - 1)) == 0;
}
</script>
<script type="text/javascript">
var gl; // WebGL context
var canvas; // HTML canvas
var prog; // The shader program
// Called once to initialize
function InitWebGL() {
// Initialize the WebGL canvas
canvas = document.getElementById("canvas");
canvas.oncontextmenu = function () { return false; };
gl = canvas.getContext("webgl", { antialias: false, depth: true }); // Initialize the GL context
if (!gl) {
alert("Unable to initialize WebGL. Your browser or machine may not support it.");
return;
} else {
console.log("WebGL initialized");
}
// Initialize settings
gl.clearColor(0, 0, 0, 0);
gl.enable(gl.DEPTH_TEST);
}
// This is a helper function for compiling the given vertex and fragment shader source code into a program.
function InitShaderProgram(vsSource, fsSource) {
const vs = CompileShader(gl.VERTEX_SHADER, vsSource);
const fs = CompileShader(gl.FRAGMENT_SHADER, fsSource);
const prog = gl.createProgram();
gl.attachShader(prog, vs);
gl.attachShader(prog, fs);
gl.linkProgram(prog);
if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {
alert('Unable to initialize the shader program: ' + gl.getProgramInfoLog(prog));
return null;
}
return prog;
}
// This is a helper function for compiling a shader, called by InitShaderProgram().
function CompileShader(type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert('An error occurred compiling shader:\n' + gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
function UpdateCanvasSize() {
canvas.style.width = "100%";
canvas.style.height = "100%";
const pixelRatio = window.devicePixelRatio || 1;
canvas.width = pixelRatio * canvas.clientWidth;
canvas.height = pixelRatio * canvas.clientHeight;
const width = (canvas.width / pixelRatio);
const height = (canvas.height / pixelRatio);
canvas.style.width = width + 'px';
canvas.style.height = height + 'px';
gl.viewport(0, 0, canvas.width, canvas.height);
}
function setTextureImg(meshDrawer, imgUrl) {
var img = new Image();
img.src = imgUrl;
img.crossOrigin = "anonymous";
img.onload = function () {
meshDrawer.setTexture(img);
}
}
</script>
<script type="text/javascript">
function renderLoop() {
UpdateCanvasSize();
var timeOffset = (Date.now() / 1000) % 9;
var zRotation = timeOffset * 40 * Math.PI / 180;
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.viewport(0, 0, canvas.width, canvas.height);
var modelMatrix = getIdentityMatrix();
var modelViewMatrix = MatrixMult(camera.getLookAt(), modelMatrix);
var mvp = MatrixMult(perspective.getPerspectiveMatrix(), modelViewMatrix);
var normalMatrix = getNormalMatrix(modelMatrix);
sunNode.trs.setRotation(0, 0, zRotation);
earthNode.trs.setRotation(0, 0, zRotation * 2);
/**
*@task3 : add rotation to mars on z-axis.
the rotation should be 1.5 * zRotation
*/
nodeofmars.trs.setRotation(0, 0, zRotation * 1.5);
sunNode.draw(mvp, modelViewMatrix, normalMatrix, modelMatrix);
requestAnimationFrame(renderLoop);
}
window.onload = function () {
InitWebGL();
UpdateCanvasSize();
sphereMesh = new ObjMesh();
sphereMesh.parse(sphere);
sphereBuffers = sphereMesh.getVertexBuffers();
camera = new Camera([0, 0, -1], [0, 0, 1], [0, 1, 0]);
perspective = new Perspective(Math.PI / 2, canvas.width / canvas.height, 1, 500);
sunMeshDrawer = new MeshDrawer(true);
earthMeshDrawer = new MeshDrawer();
moonMeshDrawer = new MeshDrawer();
marsmesh = new MeshDrawer();
sunMeshDrawer.setMesh(sphereBuffers.positionBuffer, sphereBuffers.texCoordBuffer, sphereBuffers.normalBuffer);
setTextureImg(sunMeshDrawer, "https://i.imgur.com/gl8zBLI.jpg");
sunTrs = new TRS();
sunTrs.setTranslation(0, 0, 5);
sunTrs.setScale(0.8, 0.8, 0.8);
sunNode = new SceneNode(sunMeshDrawer, sunTrs);
earthMeshDrawer.setMesh(sphereBuffers.positionBuffer, sphereBuffers.texCoordBuffer, sphereBuffers.normalBuffer);
setTextureImg(earthMeshDrawer, "https://i.imgur.com/eCpD7bM.jpg");
earthTrs = new TRS();
earthTrs.setTranslation(3, 0, 0);
earthTrs.setScale(0.5, 0.5, 0.5);
earthNode = new SceneNode(earthMeshDrawer, earthTrs, sunNode);
moonMeshDrawer.setMesh(sphereBuffers.positionBuffer, sphereBuffers.texCoordBuffer, sphereBuffers.normalBuffer);
setTextureImg(moonMeshDrawer, "https://i.imgur.com/oLiU4fm.jpg");
moonTrs = new TRS();
moonTrs.setTranslation(2.0, 0, 0);
moonTrs.setScale(0.25, 0.25, 0.25);
moonNode = new SceneNode(moonMeshDrawer, moonTrs, earthNode);
/**
* @Task3 : Add Mars to the solar system
* Mars should be a child of the sun.
* Mars should use sphere as the mesh object.
* Mars should be translated by -6 units on the X axis with respect to the sun
* Mars should be scaled to 0.35 for x,y and z coordinates
* use the image on the link below as texture:
* @link : https://i.imgur.com/Mwsa16j.jpeg
*
*/
marsmesh.setMesh(sphereBuffers.positionBuffer, sphereBuffers.texCoordBuffer, sphereBuffers.normalBuffer);
setTextureImg(marsmesh, "https://i.imgur.com/Mwsa16j.jpeg");
var trMars = new TRS();
trMars.setTranslation(-6, 0, 0);
trMars.setScale(0.35, 0.35, 0.35);
nodeofmars = new SceneNode(marsmesh, trMars,sunNode);
renderLoop();
};
window.onresize = function () {
UpdateCanvasSize();
};
///////////////////////////////////////////////////////////////////////////////////
</script>
<style>
html {
color: white;
background-color: black;
font-family: Arial;
overflow: hidden;
}
body {
padding: 0;
margin: 0;
}
input[type=checkbox],
label {
cursor: hand;
}
input[type=range] {
width: 100%;
}
canvas {
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#canvas-div {
position: relative;
}
#controls-div {
vertical-align: top;
background-color: #333;
overflow: auto;
}
#controls {
padding: 1em;
}
.control {
padding: 0.2em;
}
@media (orientation: landscape) {
#canvas-div {
display: inline-block;
width: 80%;
height: 100%;
}
#controls-div {
display: inline-block;
width: 20%;
height: 100%;
}
.control-group {
margin-top: 1em;
overflow: hidden;
}
#texture-img {
width: 100%;
height: auto;
}
}
@media (orientation: portrait) {
#canvas-div {
width: 100%;
height: 80%;
}
#controls-div {
width: 100%;
height: 20%;
}
.control-group {
display: inline-block;
vertical-align: top;
margin-left: 2em;
}
#texture-img {
width: auto;
height: calc(100% - 2em);
}
}
</style>
</head>
<div id="canvas-div"><canvas id="canvas"></canvas></div>
</body>
</html>