added lighting, stole a terrain generator and ported it.
This commit is contained in:
@@ -1,3 +1,18 @@
|
||||
|
||||
|
||||
float4 white_light(float4 input, float3 light, int3 mask) {
|
||||
|
||||
input.w = input.w + acos(
|
||||
dot(
|
||||
normalize(light),
|
||||
normalize(fabs(convert_float3(mask)))
|
||||
)
|
||||
) / 2;
|
||||
|
||||
return input;
|
||||
|
||||
}
|
||||
|
||||
__kernel void min_kern(
|
||||
global char* map,
|
||||
global int3* map_dim,
|
||||
@@ -5,6 +20,8 @@ __kernel void min_kern(
|
||||
global float3* projection_matrix,
|
||||
global float3* cam_dir,
|
||||
global float3* cam_pos,
|
||||
global float* lights,
|
||||
global int* light_count,
|
||||
__write_only image2d_t image
|
||||
){
|
||||
|
||||
@@ -26,43 +43,30 @@ __kernel void min_kern(
|
||||
|
||||
// Setup the voxel step based on what direction the ray is pointing
|
||||
int3 voxel_step = {1, 1, 1};
|
||||
voxel_step.x *= (ray_dir.x > 0) - (ray_dir.x < 0);
|
||||
voxel_step *= (ray_dir > 0) - (ray_dir < 0);
|
||||
|
||||
/*voxel_step.x *= (ray_dir.x > 0) - (ray_dir.x < 0);
|
||||
voxel_step.y *= (ray_dir.y > 0) - (ray_dir.y < 0);
|
||||
voxel_step.z *= (ray_dir.z > 0) - (ray_dir.z < 0);
|
||||
voxel_step.z *= (ray_dir.z > 0) - (ray_dir.z < 0);*/
|
||||
|
||||
// Setup the voxel coords from the camera origin
|
||||
int3 voxel = {
|
||||
floor(cam_pos->x),
|
||||
floor(cam_pos->y),
|
||||
floor(cam_pos->z)
|
||||
};
|
||||
int3 voxel = convert_int3(*cam_pos);
|
||||
|
||||
// Delta T is the units a ray must travel along an axis in order to
|
||||
// traverse an integer split
|
||||
float3 delta_t = {
|
||||
fabs(1.0f / ray_dir.x),
|
||||
fabs(1.0f / ray_dir.y),
|
||||
fabs(1.0f / ray_dir.z)
|
||||
};
|
||||
float3 delta_t = fabs(1.0f / ray_dir);
|
||||
|
||||
// Intersection T is the collection of the next intersection points
|
||||
// for all 3 axis XYZ.
|
||||
float3 intersection_t = {
|
||||
delta_t.x,
|
||||
delta_t.y,
|
||||
delta_t.z
|
||||
};
|
||||
float3 intersection_t = delta_t;
|
||||
|
||||
int2 randoms = { 3, 7 };
|
||||
int2 randoms = { 3, 14 };
|
||||
uint seed = randoms.x + id;
|
||||
uint t = seed ^ (seed << 11);
|
||||
uint result = randoms.y ^ (randoms.y >> 19) ^ (t ^ (t >> 8));
|
||||
|
||||
int max_dist = 500 + result % 50;
|
||||
int dist = 0;
|
||||
int face = -1;
|
||||
// X:0, Y:1, Z:2
|
||||
|
||||
int dist = 0;
|
||||
|
||||
int3 mask = { 0, 0, 0 };
|
||||
|
||||
@@ -74,20 +78,16 @@ __kernel void min_kern(
|
||||
intersection_t += delta_t * fabs(convert_float3(mask.xyz));
|
||||
voxel.xyz += voxel_step.xyz * mask.xyz;
|
||||
|
||||
|
||||
|
||||
// If the ray went out of bounds
|
||||
int3 overshoot = voxel.xyz <= map_dim->xyz;
|
||||
int3 overshoot = voxel <= *map_dim;
|
||||
int3 undershoot = voxel > 0;
|
||||
|
||||
|
||||
|
||||
if (overshoot.x == 0 || overshoot.y == 0 || overshoot.z == 0){
|
||||
write_imagef(image, pixel, (float4)(.50 * abs(overshoot.x), .50 * abs(overshoot.y), .50 * abs(overshoot.z), 1));
|
||||
if (overshoot.x == 0 || overshoot.y == 0 || overshoot.z == 0 || undershoot.x == 0 || undershoot.y == 0){
|
||||
write_imagef(image, pixel, (float4)(.73, .81, .89, 1.0));
|
||||
return;
|
||||
}
|
||||
if (undershoot.x == 0 || undershoot.y == 0 || undershoot.z == 0) {
|
||||
write_imagef(image, pixel, (float4)(.1 * abs(undershoot.x), .80 * abs(undershoot.y), .20 * abs(undershoot.z), 1));
|
||||
if (undershoot.z == 0) {
|
||||
write_imagef(image, pixel, (float4)(.14, .30, .50, 1.0));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -95,8 +95,6 @@ __kernel void min_kern(
|
||||
int index = voxel.x + map_dim->x * (voxel.y + map_dim->z * voxel.z);
|
||||
int voxel_data = map[index];
|
||||
|
||||
|
||||
|
||||
if (voxel_data != 0) {
|
||||
switch (voxel_data) {
|
||||
case 1:
|
||||
@@ -104,8 +102,6 @@ __kernel void min_kern(
|
||||
return;
|
||||
case 2:
|
||||
write_imagef(image, pixel, (float4)(.00, .50, .40, 1.00));
|
||||
//if (id == 249000)
|
||||
// printf("%i\n", voxel_data);
|
||||
return;
|
||||
case 3:
|
||||
write_imagef(image, pixel, (float4)(.00, .00, .50, 1.00));
|
||||
@@ -114,7 +110,8 @@ __kernel void min_kern(
|
||||
write_imagef(image, pixel, (float4)(.25, .00, .25, 1.00));
|
||||
return;
|
||||
case 5:
|
||||
write_imagef(image, pixel, (float4)(.10, .30, .80, 1.00));
|
||||
//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));
|
||||
return;
|
||||
case 6:
|
||||
write_imagef(image, pixel, (float4)(.30, .80, .10, 1.00));
|
||||
@@ -125,6 +122,6 @@ __kernel void min_kern(
|
||||
dist++;
|
||||
} while (dist < max_dist);
|
||||
|
||||
write_imagef(image, pixel, (float4)(.00, .00, .00, .00));
|
||||
write_imagef(image, pixel, (float4)(.73, .81, .89, 1.0));
|
||||
return;
|
||||
}
|
||||
Reference in New Issue
Block a user