added lighting, stole a terrain generator and ported it.
This commit is contained in:
218
include/Map.h
218
include/Map.h
@@ -3,26 +3,160 @@
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/Graphics/Color.hpp>
|
||||
#include <random>
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <cmath>
|
||||
|
||||
class Map {
|
||||
public:
|
||||
Map(sf::Vector3i dim) {
|
||||
dimensions = dim;
|
||||
std::mt19937 gen;
|
||||
std::uniform_real_distribution<double> dis(-1.0, 1.0);
|
||||
auto f_rand = std::bind(dis, gen);
|
||||
|
||||
|
||||
list = new char[dim.x * dim.y * dim.z];
|
||||
//for (int i = 0; i < dim.x * dim.y * dim.x; i++) {
|
||||
// list[i] = 0;
|
||||
|
||||
height_map = new double[dim.x * dim.y];
|
||||
|
||||
for (int i = 0; i < dim.x * dim.y; i++) {
|
||||
height_map[i] = 0;
|
||||
}
|
||||
|
||||
//int featuresize = 2;
|
||||
|
||||
//for (int y = 0; y < dim.y; y += featuresize)
|
||||
// for (int x = 0; x < dim.x; x += featuresize) {
|
||||
// double t = dis(gen);
|
||||
// setSample(x, y, t); //IMPORTANT: frand() is a random function that returns a value between -1 and 1.
|
||||
// }
|
||||
|
||||
for (int x = 0; x < dim.x / 10; x++) {
|
||||
for (int y = 0; y < dim.y / 10; y++) {
|
||||
for (int z = 0; z < dim.z; z++) {
|
||||
if (rand() % 1000 < 1)
|
||||
list[x + dim.x * (y + dim.z * z)] = rand() % 6;
|
||||
//int samplesize = featuresize;
|
||||
|
||||
//double scale = 10.0;
|
||||
|
||||
//while (samplesize > 1) {
|
||||
|
||||
// DiamondSquare(samplesize, scale);
|
||||
|
||||
// samplesize /= 2;
|
||||
// scale /= 2.0;
|
||||
//}
|
||||
|
||||
|
||||
|
||||
|
||||
//size of grid to generate, note this must be a
|
||||
//value 2^n+1
|
||||
int DATA_SIZE = dim.x + 1;
|
||||
//an initial seed value for the corners of the data
|
||||
double SEED = 50;
|
||||
|
||||
//seed the data
|
||||
setSample(0, 0, SEED);
|
||||
setSample(0, dim.y, SEED);
|
||||
setSample(dim.x, 0, SEED);
|
||||
setSample(dim.x, dim.y, SEED);
|
||||
|
||||
double h = 30.0;//the range (-h -> +h) for the average offset
|
||||
//for the new value in range of h
|
||||
//side length is distance of a single square side
|
||||
//or distance of diagonal in diamond
|
||||
for (int sideLength = DATA_SIZE - 1;
|
||||
//side length must be >= 2 so we always have
|
||||
//a new value (if its 1 we overwrite existing values
|
||||
//on the last iteration)
|
||||
sideLength >= 2;
|
||||
//each iteration we are looking at smaller squares
|
||||
//diamonds, and we decrease the variation of the offset
|
||||
sideLength /= 2, h /= 2.0) {
|
||||
//half the length of the side of a square
|
||||
//or distance from diamond center to one corner
|
||||
//(just to make calcs below a little clearer)
|
||||
int halfSide = sideLength / 2;
|
||||
|
||||
//generate the new square values
|
||||
for (int x = 0; x < DATA_SIZE - 1; x += sideLength) {
|
||||
for (int y = 0; y < DATA_SIZE - 1; y += sideLength) {
|
||||
//x, y is upper left corner of square
|
||||
//calculate average of existing corners
|
||||
double avg = sample(x, y) + //top left
|
||||
sample(x + sideLength,y) +//top right
|
||||
sample(x,y + sideLength) + //lower left
|
||||
sample(x + sideLength,y + sideLength);//lower right
|
||||
avg /= 4.0;
|
||||
|
||||
//center is average plus random offset
|
||||
setSample(x + halfSide,y + halfSide,
|
||||
//We calculate random value in range of 2h
|
||||
//and then subtract h so the end value is
|
||||
//in the range (-h, +h)
|
||||
avg + (f_rand() * 2 * h) - h);
|
||||
}
|
||||
}
|
||||
|
||||
//generate the diamond values
|
||||
//since the diamonds are staggered we only move x
|
||||
//by half side
|
||||
//NOTE: if the data shouldn't wrap then x < DATA_SIZE
|
||||
//to generate the far edge values
|
||||
for (int x = 0; x < DATA_SIZE - 1; x += halfSide) {
|
||||
//and y is x offset by half a side, but moved by
|
||||
//the full side length
|
||||
//NOTE: if the data shouldn't wrap then y < DATA_SIZE
|
||||
//to generate the far edge values
|
||||
for (int y = (x + halfSide) % sideLength; y < DATA_SIZE - 1; y += sideLength) {
|
||||
//x, y is center of diamond
|
||||
//note we must use mod and add DATA_SIZE for subtraction
|
||||
//so that we can wrap around the array to find the corners
|
||||
double avg =
|
||||
sample((x - halfSide + DATA_SIZE) % DATA_SIZE,y) + //left of center
|
||||
sample((x + halfSide) % DATA_SIZE,y) + //right of center
|
||||
sample(x,(y + halfSide) % DATA_SIZE) + //below center
|
||||
sample(x,(y - halfSide + DATA_SIZE) % DATA_SIZE); //above center
|
||||
avg /= 4.0;
|
||||
|
||||
//new value = average plus random offset
|
||||
//We calculate random value in range of 2h
|
||||
//and then subtract h so the end value is
|
||||
//in the range (-h, +h)
|
||||
avg = avg + (f_rand() * 2 * h) - h;
|
||||
//update value for center of diamond
|
||||
setSample(x,y, avg);
|
||||
|
||||
//wrap values on the edges, remove
|
||||
//this and adjust loop condition above
|
||||
//for non-wrapping values.
|
||||
if (x == 0) setSample(DATA_SIZE - 1,y, avg);
|
||||
if (y == 0) setSample(x, DATA_SIZE - 1, avg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dimensions = dim;
|
||||
global_light = sf::Vector3f(0.2, 0.4, 1);
|
||||
|
||||
for (int x = 0; x < dim.x; x++) {
|
||||
for (int y = 0; y < dim.y; y++) {
|
||||
|
||||
if (height_map[x + y * dim.x] > 0) {
|
||||
int z = height_map[x + y * dim.x];
|
||||
list[x + dim.x * (y + dim.z * z)] = 5;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// for (int x = 0; x < dim.x / 10; x++) {
|
||||
// for (int y = 0; y < dim.y / 10; y++) {
|
||||
// for (int z = 0; z < dim.z; z++) {
|
||||
// if (rand() % 1000 < 1)
|
||||
// list[x + dim.x * (y + dim.z * z)] = rand() % 6;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
||||
~Map() {
|
||||
@@ -44,7 +178,73 @@ public:
|
||||
protected:
|
||||
|
||||
private:
|
||||
double* height_map;
|
||||
|
||||
double sample(int x, int y) {
|
||||
return height_map[(x & (dimensions.x - 1)) + (y & (dimensions.y - 1)) * dimensions.x];
|
||||
}
|
||||
|
||||
void setSample(int x, int y, double value) {
|
||||
height_map[(x & (dimensions.x - 1)) + (y & (dimensions.y - 1)) * dimensions.x] = value;
|
||||
}
|
||||
|
||||
void sampleSquare(int x, int y, int size, double value) {
|
||||
int hs = size / 2;
|
||||
|
||||
// a b
|
||||
//
|
||||
// x
|
||||
//
|
||||
// c d
|
||||
|
||||
double a = sample(x - hs, y - hs);
|
||||
double b = sample(x + hs, y - hs);
|
||||
double c = sample(x - hs, y + hs);
|
||||
double d = sample(x + hs, y + hs);
|
||||
|
||||
setSample(x, y, ((a + b + c + d) / 4.0) + value);
|
||||
|
||||
}
|
||||
|
||||
void sampleDiamond(int x, int y, int size, double value) {
|
||||
int hs = size / 2;
|
||||
|
||||
// c
|
||||
//
|
||||
//a x b
|
||||
//
|
||||
// d
|
||||
|
||||
double a = sample(x - hs, y);
|
||||
double b = sample(x + hs, y);
|
||||
double c = sample(x, y - hs);
|
||||
double d = sample(x, y + hs);
|
||||
|
||||
setSample(x, y, ((a + b + c + d) / 4.0) + value);
|
||||
}
|
||||
|
||||
void DiamondSquare(int stepsize, double scale) {
|
||||
|
||||
std::mt19937 generator;
|
||||
std::uniform_real_distribution<double> uniform_distribution(-1.0, 1.0);
|
||||
auto f_rand = std::bind(uniform_distribution, std::ref(generator));
|
||||
|
||||
int halfstep = stepsize / 2;
|
||||
|
||||
for (int y = halfstep; y < dimensions.y + halfstep; y += stepsize) {
|
||||
for (int x = halfstep; x < dimensions.x + halfstep; x += stepsize) {
|
||||
sampleSquare(x, y, stepsize, f_rand() * scale);
|
||||
}
|
||||
}
|
||||
|
||||
for (int y = 0; y < dimensions.y; y += stepsize) {
|
||||
for (int x = 0; x < dimensions.x; x += stepsize) {
|
||||
sampleDiamond(x + halfstep, y, stepsize, f_rand() * scale);
|
||||
sampleDiamond(x, y + halfstep, stepsize, f_rand() * scale);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
33
src/main.cpp
33
src/main.cpp
@@ -35,9 +35,9 @@
|
||||
const int WINDOW_X = 1000;
|
||||
const int WINDOW_Y = 1000;
|
||||
|
||||
const int MAP_X = 1000;
|
||||
const int MAP_Y = 1000;
|
||||
const int MAP_Z = 1000;
|
||||
const int MAP_X = 1024;
|
||||
const int MAP_Y = 1024;
|
||||
const int MAP_Z = 256;
|
||||
|
||||
float elap_time(){
|
||||
static std::chrono::time_point<std::chrono::system_clock> start;
|
||||
@@ -157,11 +157,26 @@ int main() {
|
||||
|
||||
|
||||
sf::Vector3f cam_pos(55, 50, 50);
|
||||
|
||||
cl_mem cam_pos_buff = clCreateBuffer(
|
||||
c.getContext(), CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,
|
||||
sizeof(float) * 4, &cam_pos, NULL
|
||||
);
|
||||
|
||||
// {r, g, b, i, x, y, z, x', y', z'}
|
||||
float light[] = { 0.4, 0.8, 0.1, 1, 50, 50, 50, 1.1, 0.4, 0.7};
|
||||
|
||||
cl_mem light_buff = clCreateBuffer(
|
||||
c.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
|
||||
sizeof(float) * 10, light, NULL
|
||||
);
|
||||
|
||||
int light_count = 1;
|
||||
|
||||
cl_mem light_cnt_buff = clCreateBuffer(
|
||||
c.getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
|
||||
sizeof(int), &light_count, NULL
|
||||
);
|
||||
|
||||
unsigned char* pixel_array = new sf::Uint8[WINDOW_X * WINDOW_Y * 4];
|
||||
|
||||
@@ -186,16 +201,14 @@ int main() {
|
||||
if (c.assert(error, "clCreateFromGLTexture"))
|
||||
return -1;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
c.store_buffer(map_buff, "map_buffer");
|
||||
c.store_buffer(dim_buff, "dim_buffer");
|
||||
c.store_buffer(res_buff, "res_buffer");
|
||||
c.store_buffer(view_matrix_buff, "view_matrix_buffer");
|
||||
c.store_buffer(cam_dir_buff, "cam_dir_buffer");
|
||||
c.store_buffer(cam_pos_buff, "cam_pos_buffer");
|
||||
c.store_buffer(light_buff, "light_buffer");
|
||||
c.store_buffer(light_cnt_buff, "light_count_buffer");
|
||||
c.store_buffer(image_buff, "image_buffer");
|
||||
|
||||
c.set_kernel_arg("min_kern", 0, "map_buffer");
|
||||
@@ -204,12 +217,12 @@ int main() {
|
||||
c.set_kernel_arg("min_kern", 3, "view_matrix_buffer");
|
||||
c.set_kernel_arg("min_kern", 4, "cam_dir_buffer");
|
||||
c.set_kernel_arg("min_kern", 5, "cam_pos_buffer");
|
||||
c.set_kernel_arg("min_kern", 6, "image_buffer");
|
||||
c.set_kernel_arg("min_kern", 6, "light_buffer");
|
||||
c.set_kernel_arg("min_kern", 7, "light_count_buffer");
|
||||
c.set_kernel_arg("min_kern", 8, "image_buffer");
|
||||
|
||||
const int size = WINDOW_X * WINDOW_Y;
|
||||
|
||||
|
||||
|
||||
s.setTexture(t);
|
||||
|
||||
// The step size in milliseconds between calls to Update()
|
||||
|
||||
Reference in New Issue
Block a user