You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
3.9 KiB
139 lines
3.9 KiB
8 years ago
|
|
||
|
|
||
8 years ago
|
float4 white_light(float4 input, float3 light, int3 mask) {
|
||
8 years ago
|
|
||
|
input.w = input.w + acos(
|
||
|
dot(
|
||
|
normalize(light),
|
||
|
normalize(fabs(convert_float3(mask)))
|
||
|
)
|
||
8 years ago
|
) / 2;
|
||
8 years ago
|
|
||
8 years ago
|
return input;
|
||
8 years ago
|
|
||
|
}
|
||
|
|
||
8 years ago
|
__kernel void min_kern(
|
||
|
global char* map,
|
||
8 years ago
|
global int3* map_dim,
|
||
|
global int2* resolution,
|
||
8 years ago
|
global float3* projection_matrix,
|
||
8 years ago
|
global float2* cam_dir,
|
||
8 years ago
|
global float3* cam_pos,
|
||
8 years ago
|
global float* lights,
|
||
|
global int* light_count,
|
||
8 years ago
|
__write_only image2d_t image
|
||
8 years ago
|
){
|
||
|
|
||
8 years ago
|
size_t id = get_global_id(0);
|
||
8 years ago
|
int2 pixel = {id % (*resolution).x, id / (*resolution).x};
|
||
|
float3 ray_dir = projection_matrix[pixel.x + (*resolution).x * pixel.y];
|
||
8 years ago
|
|
||
|
ray_dir = (float3)(
|
||
8 years ago
|
ray_dir.z * sin((*cam_dir).x) + ray_dir.x * cos((*cam_dir).x),
|
||
8 years ago
|
ray_dir.y,
|
||
8 years ago
|
ray_dir.z * cos((*cam_dir).x) - ray_dir.x * sin((*cam_dir).x)
|
||
8 years ago
|
);
|
||
|
|
||
|
ray_dir = (float3)(
|
||
8 years ago
|
ray_dir.x * cos((*cam_dir).y) - ray_dir.y * sin((*cam_dir).y),
|
||
|
ray_dir.x * sin((*cam_dir).y) + ray_dir.y * cos((*cam_dir).y),
|
||
8 years ago
|
ray_dir.z
|
||
8 years ago
|
);
|
||
|
|
||
|
// Setup the voxel step based on what direction the ray is pointing
|
||
|
int3 voxel_step = {1, 1, 1};
|
||
8 years ago
|
voxel_step *= (ray_dir > 0) - (ray_dir < 0);
|
||
|
|
||
8 years ago
|
// Setup the voxel coords from the camera origin
|
||
8 years ago
|
int3 voxel = convert_int3(*cam_pos);
|
||
8 years ago
|
|
||
|
// Delta T is the units a ray must travel along an axis in order to
|
||
|
// traverse an integer split
|
||
8 years ago
|
float3 delta_t = fabs(1.0f / ray_dir);
|
||
8 years ago
|
|
||
8 years ago
|
// offset is how far we are into a voxel, enables sub voxel movement
|
||
|
float3 offset = ((*cam_pos) - floor(*cam_pos)) * convert_float3(voxel_step);
|
||
|
|
||
|
//offset.x += delta_t.x * convert_float((voxel_step.x < 0));
|
||
|
//offset -= delta_t * floor(offset / delta_t);
|
||
|
|
||
|
// Intersection T is the collection of the next intersection points
|
||
8 years ago
|
// for all 3 axis XYZ.
|
||
8 years ago
|
float3 intersection_t = delta_t * offset;
|
||
|
|
||
|
if (intersection_t.x < 0) {
|
||
|
intersection_t.x += delta_t.x;
|
||
|
}
|
||
|
if (intersection_t.y < 0) {
|
||
|
intersection_t.y += delta_t.y;
|
||
|
}
|
||
|
if (intersection_t.z < 0) {
|
||
|
intersection_t.z += delta_t.z;
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
int2 randoms = { 3, 14 };
|
||
8 years ago
|
uint seed = randoms.x + id;
|
||
|
uint t = seed ^ (seed << 11);
|
||
|
uint result = randoms.y ^ (randoms.y >> 19) ^ (t ^ (t >> 8));
|
||
8 years ago
|
|
||
8 years ago
|
int max_dist = 500 + result % 50;
|
||
8 years ago
|
int dist = 0;
|
||
8 years ago
|
|
||
|
int3 mask = { 0, 0, 0 };
|
||
|
|
||
8 years ago
|
// Andrew Woo's raycasting algo
|
||
|
do {
|
||
8 years ago
|
|
||
|
mask = intersection_t.xyz <= min(intersection_t.yzx, intersection_t.zxy);
|
||
8 years ago
|
float3 thing = delta_t * fabs(convert_float3(mask.xyz));
|
||
8 years ago
|
intersection_t += delta_t * fabs(convert_float3(mask.xyz));
|
||
|
voxel.xyz += voxel_step.xyz * mask.xyz;
|
||
|
|
||
8 years ago
|
// If the ray went out of bounds
|
||
8 years ago
|
int3 overshoot = voxel <= *map_dim;
|
||
8 years ago
|
int3 undershoot = voxel > 0;
|
||
|
|
||
8 years ago
|
if (overshoot.x == 0 || overshoot.y == 0 || overshoot.z == 0 || undershoot.x == 0 || undershoot.y == 0){
|
||
8 years ago
|
write_imagef(image, pixel, (float4)(.73, .81, .89, 1.0));
|
||
8 years ago
|
return;
|
||
|
}
|
||
8 years ago
|
if (undershoot.z == 0) {
|
||
8 years ago
|
write_imagef(image, pixel, (float4)(.14, .30, .50, 1.0));
|
||
8 years ago
|
return;
|
||
|
}
|
||
8 years ago
|
|
||
|
// If we hit a voxel
|
||
8 years ago
|
int index = voxel.x + (*map_dim).x * (voxel.y + (*map_dim).z * voxel.z);
|
||
8 years ago
|
int voxel_data = map[index];
|
||
|
|
||
8 years ago
|
if (voxel_data != 0) {
|
||
|
switch (voxel_data) {
|
||
|
case 1:
|
||
8 years ago
|
write_imagef(image, pixel, (float4)(.50, .00, .00, 1));
|
||
8 years ago
|
return;
|
||
|
case 2:
|
||
8 years ago
|
write_imagef(image, pixel, (float4)(.00, .50, .40, 1.00));
|
||
8 years ago
|
return;
|
||
|
case 3:
|
||
8 years ago
|
write_imagef(image, pixel, (float4)(.00, .00, .50, 1.00));
|
||
8 years ago
|
return;
|
||
|
case 4:
|
||
8 years ago
|
write_imagef(image, pixel, (float4)(.25, .00, .25, 1.00));
|
||
8 years ago
|
return;
|
||
|
case 5:
|
||
8 years ago
|
//write_imagef(image, pixel, (float4)(.25, .00, .25, 1.00));
|
||
|
write_imagef(image, pixel, white_light((float4)(.25, .32, .14, 0.2), (float3)(lights[7], lights[8], lights[9]), mask));
|
||
8 years ago
|
return;
|
||
|
case 6:
|
||
8 years ago
|
write_imagef(image, pixel, (float4)(.30, .80, .10, 1.00));
|
||
8 years ago
|
return;
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
|
dist++;
|
||
8 years ago
|
} while (dist < max_dist);
|
||
8 years ago
|
|
||
8 years ago
|
write_imagef(image, pixel, (float4)(.73, .81, .89, 1.0));
|
||
8 years ago
|
return;
|
||
8 years ago
|
}
|