Skip to content

Commit

Permalink
Rearranged File Structure
Browse files Browse the repository at this point in the history
  • Loading branch information
yghannam committed Feb 7, 2013
1 parent b9b328c commit d03b0c0
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 2,275 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
float3 N;
}Intersection;


float rayPlane(
Ray ray,
float3 normal,
Expand Down Expand Up @@ -154,18 +155,19 @@
return world;
}

float2 intersect(
Intersection intersect(
Ray ray,
bool isShadowRay,
int shapeType,
int numShapes,
__global float4 *shapeData,
__global float4 *shapeNormals,
__global uint4 *shapeIndices){

Intersection intersection;
intersection.t = INFINITY;

intersection.t = INFINITY;
intersection.shapeIndex = -1;

unsigned int i = 0;
if(shapeType == 1){
for( i = 0; i < numShapes; i++){
Expand All @@ -182,6 +184,8 @@
}
}
}
intersection.P = ray.o + intersection.t*ray.d;
intersection.N = normalize(intersection.P - shapeData[intersection.shapeIndex].xyz);
}
else if(shapeType == 2){
float alpha, beta;
Expand All @@ -195,65 +199,19 @@
alpha = rv.y;
beta = rv.z;
intersection.shapeIndex = i;
}
}
}
/*
if( t < INFINITY){
float epsilon = exp10(-2.f);
P = worldRay->o+epsilon + t*worldRay->d;
N = normalize(P-shapeData[shapeIndex].xyz);
Ray* shadowRay;
shadowRay->o = P;
shadowRay->d = lightDirection;
if( dot(N, L) > 0){
for( i = 0; i < numShapes; i++){
//debug[i] = sphereCenters[i];//(float3)(50,1000,27);
//float t = 0;
Sphere* sphere;
sphere->c = shapeData[i].xyz;
sphere->r = shapeData[i].w;
float rv = raySphere(shadowRay, sphere);
if ( rv < INFINITY ){
shadow = true;
break;
}
}
}
if( t < INFINITY){
float epsilon = 0;//exp10(-3.f);
P = worldRay->o+epsilon + t*worldRay->d;
float3 n0 = shapeNormals[shapeIndices[shapeIndex].x].xyz;
float3 n1 = shapeNormals[shapeIndices[shapeIndex].y].xyz;
float3 n2 = shapeNormals[shapeIndices[shapeIndex].z].xyz;
N = normalize( (1-alpha-beta)*n0 + alpha*n1 + beta*n2);
Ray* shadowRay;
shadowRay->o = P;
shadowRay->d = lightDirection;
if( dot(N, L) >= 0){
for( i = 0; i < numShapes; i++){
//debug[i] = sphereCenters[i];//(float3)(50,1000,27);
//float t = 0;
if(shapeIndex != i){
float3 p0 = shapeData[shapeIndices[i].x].xyz;
float3 p1 = shapeData[shapeIndices[i].y].xyz;
float3 p2 = shapeData[shapeIndices[i].z].xyz;
float3 rv = rayTriangle(shadowRay, p0, p1, p2);
if ( rv.x < INFINITY ){
shadow = true;
break;
}
}
}
}

if(isShadowRay){
break;
}
}
*/

return (float2)(intersection.t,intersection.shapeIndex);
}
intersection.P = ray.o + intersection.t*ray.d;
float3 n0 = shapeNormals[shapeIndices[intersection.shapeIndex].x].xyz;
float3 n1 = shapeNormals[shapeIndices[intersection.shapeIndex].y].xyz;
float3 n2 = shapeNormals[shapeIndices[intersection.shapeIndex].z].xyz;
intersection.N = normalize( (1-alpha-beta)*n0 + alpha*n1 + beta*n2);
}
return intersection;
}

__kernel void getPixelColor(
Expand Down Expand Up @@ -302,95 +260,56 @@
//debug[2] = (float4)(worldRay.o,0);
//debug[3] = (float4)(boundMax,0);

uchar4 color = (uchar4)(0, 0, 0, 255);
uchar4 color = (uchar4)(0, 255, 0, 255);

if(boundingBoxIntersect(worldRay, boundMin, boundMax)){

bool shadow = false;
bool reflectedShadow = false;
float3 P;
float3 N;
float3 L = (float3)(1.f,1.f,0.f);
float3 lightDirection = L;
float3 View = normalize( -worldRay.d);
float3 R;
float alpha;
float beta;

float4 lightIntensity = (float4)(1.f, 1.f, 1.f, 1.f);
float4 kd = (float4)(0.6f, 0.6f, 0.6f, 1.f);
float4 ks = (float4)(0.4f,0.4f,0.4f,1.f);
float4 ka = (float4)(0.1f, 0.1f, 0.1f, 1.f);
float shininess = 10;

float4 diffuse;
float4 reflectedDiffuse;
float epsilon = pow(10.f, -2.f);

float2 rv = intersect(worldRay, false, shapeType, numShapes, shapeData, shapeIndices);
float t = rv.x;
int shapeIndex = (int)rv.y;
P = worldRay.o + t*worldRay.d;
N = normalize(P - shapeData[shapeIndex].xyz);
/*
// TODO: Inter-reflection
if(true){
float3 reflectedRay = reflect(N, -transformedRay);
reflectedShadow = false;
float reflectedT = INFINITY;
for( i = 0; i < numShapes; i++){
//debug[i] = sphereCenters[i];//(float3)(50,1000,27);
//float t = 0;
float rv = raySphere(P, reflectedRay, shapeData[i].xyz, shapeData[i].w);
if ( rv < reflectedT ){
reflectedT = rv;
shapeIndex = i;
}
}
if( reflectedT < INFINITY){
float3 reflectedP = P+epsilon + reflectedT*reflectedRay;
float3 reflectedN = normalize(reflectedP-shapeData[shapeIndex].xyz);
if( dot(reflectedN, L) > 0){
for( i = 0; i < numShapes; i++){
//debug[i] = sphereCenters[i];//(float3)(50,1000,27);
//float t = 0;
float rv = raySphere(reflectedP, L, shapeData[i].xyz, shapeData[i].w);
if ( rv < INFINITY ){
reflectedShadow = true;
break;
}
}
}
reflectedDiffuse = kd * clamp(dot(reflectedN, normalize(L)), 0.0f, 1.f);
///*
if(reflectedShadow){
if(shadow){
diffuse = 0;
}
else{
diffuse = kd * clamp(dot(N, normalize(L)), 0.0f, 1.f);
}
}
else{
reflectedDiffuse = kd * clamp(dot(reflectedN, normalize(L)), 0.0f, 1.f);
diffuse = (1.f-0.6f)*(kd * clamp(dot(N, normalize(L)), 0.0f, 1.f)) + 0.4f*reflectedDiffuse;
}
//
}
}
*/
Intersection intersection
= intersect(worldRay, false, shapeType, numShapes, shapeData, shapeNormals, shapeIndices);

if (t < INFINITY){
if (intersection.t < INFINITY){
Ray shadowRay;
shadowRay.o = intersection.P + epsilon;
shadowRay.d = L;
Intersection shadowIntersection
= intersect(shadowRay, true, shapeType, numShapes, shapeData, shapeNormals, shapeIndices);
Ray reflectedRay;
reflectedRay.o = intersection.P + epsilon;
reflectedRay.d = reflect(intersection.N, -intersection.P);
Intersection reflectedIntersection
= intersect(reflectedRay, false, shapeType, numShapes, shapeData, shapeNormals, shapeIndices);

diffuse = kd * clamp(dot(N, normalize(L)), 0.0f, 1.f);
float4 diffuse
= (shadowIntersection.t < INFINITY) ?
(float4)(0,0,0,0) : (kd * clamp(dot(intersection.N, normalize(L)), 0.0f, 1.f));
if(reflectedIntersection.t < INFINITY){
Ray reflectedShadowRay;
reflectedShadowRay.o = reflectedIntersection.P + epsilon;
reflectedShadowRay.d = L;
Intersection reflectedShadowIntersection
= intersect(reflectedShadowRay, true, shapeType, numShapes, shapeData, shapeNormals, shapeIndices);
if(reflectedShadowIntersection.t < INFINITY){
float4 reflectedDiffuse = kd * clamp(dot(reflectedIntersection.N, normalize(L)), 0.0f, 1.f);
diffuse = (1.f - ks) * diffuse + ks * reflectedDiffuse;
}
}

R = reflect(N, L);

//float3 View = normalize( -worldRay.d);
//float3 R = reflect(intersection.N, L);
float4 specular = 0;//ks * pow(clamp(dot(View, R), 0.0f, 1.f), shininess);
//TODO: Ambient Lighting
float4 ambient = 0;//ka * lightIntensity;
float4 Intensity = lightIntensity * (diffuse+specular+ambient);
float4 Intensity = lightIntensity * (diffuse + specular + ambient);

uchar red = (uchar) round(255*Intensity.x);
uchar green = (uchar) round(255*Intensity.y);
Expand Down
File renamed without changes.
Loading

0 comments on commit d03b0c0

Please sign in to comment.