diff options
| Commit | 6dd51a351bebd3347c1c4d9d8caf4d7b2f2a4338 (patch) | |
|---|---|---|
| Author | 2023-08-25 13:39:13 +0300 | |
| Committer | 2023-08-25 13:39:13 +0300 | |
| Files | b408c20abe1d23b92075f1c22c898246f40d3c2b | |
| uni-projects-6dd51a351bebd3347c1c4d9d8caf4d7b2f2a4338.tar uni-projects-6dd51a351bebd3347c1c4d9d8caf4d7b2f2a4338.tar.gz uni-projects-6dd51a351bebd3347c1c4d9d8caf4d7b2f2a4338.zip | ||
| Parent | 10889181959cada7e397ac377cb6d43dd977cf62 (diff) | |
[WGL/P] Implemented very roughly simple ray marching, without support for anything
3 files changed, 43 insertions, 3 deletions
diff --git a/Year 2/Computer Graphics with WebGL/Project/Project.html b/Year 2/Computer Graphics with WebGL/Project/Project.html index 9852ab8..23e3759 100644 --- a/Year 2/Computer Graphics with WebGL/Project/Project.html +++ b/Year 2/Computer Graphics with WebGL/Project/Project.html @@ -61,6 +61,7 @@ h2 { <script src="spaceship.js"></script>
<script src="skybox.js"></script>
+ <script src="blackhole.js"></script>
<script>
/*
* WebGL
@@ -99,10 +100,11 @@ h2 { 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([950,0,1100], 40),
new Sphere([0,0,0], 100),
new Sphere([1000, 0, -30], 20)];
- planets.forEach((p) => p.color = [0.956,0.5,0.215]);
+
+ blackHole = new BlackHole([1000,0,900], 30);
resizeglcanvas();
}
@@ -124,8 +126,13 @@ h2 { gl.clear(gl.COLOR_BUFFER_BIT+gl.DEPTH_BUFFER_BIT);
lookUpdate();
+ gl.uniform3f(uEye, pos[0], pos[1], pos[2]);
+ gl.uniform3f(uEyeForward, forward.toCartesian()[0], forward.toCartesian()[1], forward.toCartesian()[2]);
+ gl.vertexAttrib4fv(aSphere, [pos[0], pos[1], pos[2], 0]);
+
skybox.draw();
planets.forEach((p) => p.draw());
+ blackHole.draw();
requestAnimationFrame(drawFrame);
}
diff --git a/Year 2/Computer Graphics with WebGL/Project/blackhole.js b/Year 2/Computer Graphics with WebGL/Project/blackhole.js new file mode 100644 index 0000000..50e7edb --- /dev/null +++ b/Year 2/Computer Graphics with WebGL/Project/blackhole.js @@ -0,0 +1,8 @@ +BlackHole = function(center, size) { + this.boundary = new Sphere(center, size); + this.rs = 2/3 * size; +} +BlackHole.prototype.draw = function() { + gl.vertexAttrib4fv(aSphere, [this.boundary.center[0], this.boundary.center[1], this.boundary.center[2], this.rs]); + this.boundary.draw(); +} diff --git a/Year 2/Computer Graphics with WebGL/Project/shaders.js b/Year 2/Computer Graphics with WebGL/Project/shaders.js index 3821899..d9597df 100644 --- a/Year 2/Computer Graphics with WebGL/Project/shaders.js +++ b/Year 2/Computer Graphics with WebGL/Project/shaders.js @@ -13,6 +13,9 @@ attribute vec3 aXYZ;
attribute vec3 aColor;
attribute vec3 aNormal;
+
+ attribute vec4 aSphere;
+ varying vec4 vSphere;
varying vec3 vColor;
varying vec3 vXYZ;
@@ -29,6 +32,8 @@ vec3 normal = vec3(normalize(nMatrix*vec4(aNormal,0)));
vColor += aColor*uDiffuseColor*max(dot(normal,light),0.0);
vXYZ = (uModelMatrix * vec4(aXYZ,1)).xyz;
+
+ vSphere = aSphere;
}
`;
@@ -37,9 +42,29 @@ var fShader = ` varying vec3 vColor;
varying vec3 vXYZ;
+ varying vec4 vSphere;
+
+ uniform vec3 uEye;
+ uniform vec3 uEyeForward;
void main()
{
- gl_FragColor = vec4(vColor,1.0);
+ vec3 ray = uEye;
+ vec3 step = normalize(uEyeForward);
+
+ for (int i = 0; i < 100; i++) {
+ float dist = distance(ray, vSphere.xyz) - vSphere.a;
+ if (dist < 0.001) {
+ if (vSphere.a == 0.0) {
+ gl_FragColor = vec4(vColor,1.0);
+ }
+ else {
+ gl_FragColor = vec4(1,0,0,1);
+ }
+ return;
+ }
+ ray += step * dist;
+ }
+ gl_FragColor = vec4(0,0,0,1);
}
`;
|
