Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics with WebGL / Project / Project.html
blob: 1f1ff0e32ad025e0146363d4e70fe7f3df3fc271 (plain) (blame)
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
<!DOCTYPE html>

<head>
	<meta charset="utf-8"/>
	<title>Черната дупка</title>

	<script src="webgl-fmi.js"></script>
	<script src="shaders.js"></script>

	<style>
body {
	padding: 0;
}
canvas {
	display: block;
	pos: fixed;
	left: 0;
	top: 0;
	width: 100vw;
	height: 100vh;
	/* cursor: none; */
}
	</style>

	<script src="spaceship.js"></script>
	<script src="skybox.js"></script>
	<script>
		/*
		 * WebGL
		 */

		function start()
		{
			canvas = document.getElementById("picasso");
			canvas.addEventListener('webglcontextlost',function(event){event.preventDefault();},false);
			canvas.addEventListener('webglcontextrestored',function(){init();},false);

			mouseInit();
			init();
		}

		function init()
		{
			gl = getContext("picasso");
			glprog = getProgram(vShader,fShader);
			getVariables();

			gl.enable(gl.DEPTH_TEST);
			gl.clearColor(1,1,1,1);
			// For proper transparency
			gl.enable(gl.BLEND);
			gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);

			identity();
			perspective(30,gl.canvas.width/gl.canvas.height,1,40000);
			gl.uniform1i(uUseNormalMatrix,false);

			gl.uniform3f(uAmbientColor,0.3,0.3,0.3);
			gl.uniform3f(uDiffuseColor,1,1,1);
			gl.uniform3f(uLightDir,0,0,-1);

			skybox = new Skybox(10000, [0.1,0.1,0.1], undefined);
			planets = [new Sphere([1000,0,1000], 50),
						new Sphere([1000,0,-1000], 50),
						new Sphere([700,0,1000], 40),
						new Sphere([0,0,0], 100),
						new Sphere([1000, 0, -30], 20)];
			planets.forEach((p) => p.color = [1,0,0]);

			resizeglcanvas();
		}

		function resizeglcanvas() {
			gl.canvas.width = gl.canvas.offsetWidth;
			gl.canvas.height = gl.canvas.offsetHeight;
			gl.viewport(0,0,gl.canvas.width,gl.canvas.height);

			// var proj=orthoMatrix(gl.canvas.width,gl.canvas.height,-1000,1000);
			// gl.uniformMatrix4fv(uProjectionMatrix,false,proj);

			drawFrame();
		}
		window.onresize = resizeglcanvas;
	
		function drawFrame()
		{
			gl.clear(gl.COLOR_BUFFER_BIT+gl.DEPTH_BUFFER_BIT);

			lookUpdate();
			skybox.draw();
			planets.forEach((p) => p.draw());

			requestAnimationFrame(drawFrame);
		}
	</script>
</head>

<body onload="start()">
	<canvas id="picasso"></canvas>
</body>