Skip to content

Commit

Permalink
Merge pull request #21 from cadubentzen/colored-floor
Browse files Browse the repository at this point in the history
colored floor
  • Loading branch information
cadubentzen authored Nov 21, 2017
2 parents d72153d + 3355509 commit 6fd3e4e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
8 changes: 4 additions & 4 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ function initDemo() {
const floorRadiusLocation = gl.getUniformLocation(program, 'floorRadius');
const floorHeightLocation = gl.getUniformLocation(program, 'floorHeight');

gl.uniform3f(sphereCenterLocation, 3.0, 0.0, 1.0);
gl.uniform3f(cubeCenterLocation, 0.5, 0.0, 0.0);
gl.uniform1f(floorRadiusLocation, 10.0);
gl.uniform3f(sphereCenterLocation, 0.0, 2.5, -5.0);
gl.uniform3f(cubeCenterLocation, 0.0, 2.5, 5.0);
gl.uniform1f(floorRadiusLocation, 20.0);
gl.uniform1f(floorHeightLocation, -1.0);

const up = vec3.fromValues(0.0, 1.0, 0.0);
const cameraTo = vec3.fromValues(0.0, 0.0, 0.0);
const cameraInitialPosition = vec3.fromValues(5.0, 0.0, 5.0);
const cameraInitialPosition = vec3.fromValues(10.0, 2.0, 10.0);
const cameraPosition = new Float32Array(3);

const cameraDirection = new Float32Array(3);
Expand Down
18 changes: 12 additions & 6 deletions js/fragment_shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool intersectSphere(vec3 origin, vec3 rayDirection, out vec3 intersection,
out vec3 V, out vec3 N, out vec3 L) {
vec3 rayToSphere = sphereCenter - origin;
float b = dot(rayDirection, -rayToSphere);
float d = (b * b) - dot(rayToSphere, rayToSphere) + 1.0;
float d = (b * b) - dot(rayToSphere, rayToSphere) + 2.5 * 2.5;
if (d < 0.0) return false;
Expand All @@ -49,14 +49,20 @@ bool intersectSphere(vec3 origin, vec3 rayDirection, out vec3 intersection,
bool intersectFloor(vec3 origin, vec3 rayDirection, out vec3 intersection,
out float dist,
out vec3 V, out vec3 N, out vec3 L) {
out vec3 V, out vec3 N, out vec3 L,
out vec3 color) {
dist = (floorHeight - origin.y) / rayDirection.y;
if (dist < 0.0) return false;
float x = origin.x + rayDirection.x * dist;
float z = origin.z + rayDirection.z * dist;
if (x*x + z*z > floorRadius*floorRadius) return false;
if (x < 0.0 && z < 0.0) color = vec3(1.0, 1.0, 0.0);
else if (x < 0.0 && z > 0.0) color = vec3(1.0, 0.0, 1.0);
else if (x > 0.0 && z < 0.0) color = vec3(0.0, 0.5, 0.5);
else if (x > 0.0 && z > 0.0) color = vec3(0.5, 0.5, 0.5);
intersection = vec3(x, floorHeight, z);
V = -rayDirection;
N = vec3(0.0, 1.0, 0.0);
Expand Down Expand Up @@ -180,8 +186,8 @@ bool intersectSomething(vec3 origin, vec3 rayDirection, out vec3 intersection,
}
} else if (intersectCube(origin, rayDirection, intersection, dist1, V, N, L)) {
reflectedColor = vec3(0.0, 1.0, 0.0);
} else if (intersectFloor(origin, rayDirection, intersection, dist1, V, N, L)) {
reflectedColor = vec3(0.0, 0.0, 0.0);
} else if (intersectFloor(origin, rayDirection, intersection, dist1, V, N, L, reflectedColor)) {
// moved as parameter
} else {
return false;
}
Expand Down Expand Up @@ -210,12 +216,12 @@ bool intersectSomethingGoingToLight(vec3 origin, vec3 N_aux) {
}
void main() {
lightPosition = vec3(10.0, 10.0, 10.0);
lightPosition = vec3(0.0, 20.0, 0.0);
vec3 rayDirection = normalize(nearPosition - cameraPosition);
vec3 intersection1, intersection2;
cs = 2.0;
cs = 5.0;
ka = 0.5;
kd = 0.4;
Expand Down

0 comments on commit 6fd3e4e

Please sign in to comment.