gonna push this cause its kinda there. but not really.
This commit is contained in:
@@ -12,11 +12,11 @@
|
||||
#include "../stb/stb_image.h"
|
||||
|
||||
struct defaults {
|
||||
int outWidth = 1024;
|
||||
int outHeight = 1024;
|
||||
size_t gridWidth = 1024;
|
||||
size_t gridHeight = 1024;
|
||||
size_t gridDepth = 1024;
|
||||
int outWidth = 512;
|
||||
int outHeight = 512;
|
||||
int gridWidth = 64;
|
||||
int gridHeight = 64;
|
||||
int gridDepth = 64;
|
||||
float fps = 30.0f;
|
||||
PNoise2 noise = PNoise2(42);
|
||||
};
|
||||
@@ -30,20 +30,20 @@ struct Shared {
|
||||
VoxelGrid grid;
|
||||
};
|
||||
|
||||
VoxelGrid setup(defaults config) {
|
||||
void setup(defaults config, VoxelGrid& grid) {
|
||||
float threshold = 0.3 * 255;
|
||||
VoxelGrid grid(config.gridWidth, config.gridHeight, config.gridDepth);
|
||||
grid.resize(config.gridWidth, config.gridHeight, config.gridDepth);
|
||||
std::cout << "Generating grid of size " << config.gridWidth << "x" << config.gridHeight << "x" << config.gridDepth << std::endl;
|
||||
for (size_t z = 0; z < config.gridDepth; ++z) {
|
||||
for (int z = 0; z < config.gridDepth; ++z) {
|
||||
if (z % 64 == 0) {
|
||||
std::cout << "Processing layer " << z << " of " << config.gridDepth << std::endl;
|
||||
}
|
||||
|
||||
for (size_t y = 0; y < config.gridWidth; ++y) {
|
||||
for (size_t x = 0; x < config.gridHeight; ++x) {
|
||||
for (int y = 0; y < config.gridWidth; ++y) {
|
||||
for (int x = 0; x < config.gridHeight; ++x) {
|
||||
Vec4ui8 noisecolor = config.noise.permuteColor(Vec3f( x / 64, y / 64, z / 64));
|
||||
if (noisecolor.a > threshold) {
|
||||
grid.set(Vec3T(x,y,z), true, Vec3ui8(noisecolor.xyz()));
|
||||
grid.set(Vec3i(x,y,z), true, Vec3ui8(noisecolor.xyz()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,7 @@ VoxelGrid setup(defaults config) {
|
||||
}
|
||||
|
||||
void livePreview(VoxelGrid& grid, defaults config, Camera cam) {
|
||||
updatePreview = false;
|
||||
std::lock_guard<std::mutex> lock(PreviewMutex);
|
||||
|
||||
currentPreviewFrame = grid.renderFrame(cam.posfor.origin, cam.posfor.direction, cam.up, cam.fov, config.outWidth, config.outHeight, frame::colormap::BGRA);
|
||||
@@ -113,13 +114,16 @@ int main() {
|
||||
|
||||
bool application_not_closed = true;
|
||||
GLFWwindow* window = glfwCreateWindow((int)(1280), (int)(800), "voxelgrid live renderer", nullptr, nullptr);
|
||||
if (window == nullptr)
|
||||
if (window == nullptr) {
|
||||
glfwTerminate();
|
||||
return 1;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSwapInterval(1);
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
(void)io;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
||||
ImGui::StyleColorsDark();
|
||||
ImGuiStyle& style = ImGui::GetStyle();
|
||||
@@ -133,6 +137,13 @@ int main() {
|
||||
bool show_demo_window = true;
|
||||
bool show_another_window = false;
|
||||
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
||||
|
||||
defaults config;
|
||||
VoxelGrid grid;
|
||||
bool gridInitialized = false;
|
||||
|
||||
Camera cam(config.gridWidth, Vec3f(0,0,0), Vec3f(0,1,0), 80);
|
||||
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
glfwPollEvents();
|
||||
|
||||
@@ -142,7 +153,43 @@ int main() {
|
||||
ImGui::NewFrame();
|
||||
{
|
||||
ImGui::Begin("settings");
|
||||
|
||||
if(ImGui::CollapsingHeader("output", ImGuiTreeNodeFlags_DefaultOpen)) {
|
||||
ImGui::SliderInt("Width", &config.outWidth, 256, 4096);
|
||||
ImGui::SliderInt("Height", &config.outHeight, 256, 4096);
|
||||
}
|
||||
|
||||
if (ImGui::CollapsingHeader("Grid Settings", ImGuiTreeNodeFlags_DefaultOpen)) {
|
||||
ImGui::SliderInt("#Width", &config.gridWidth, 64, 512);
|
||||
ImGui::SliderInt("#Height", &config.gridHeight, 64, 512);
|
||||
ImGui::SliderInt("#Depth", &config.gridDepth, 64, 512);
|
||||
}
|
||||
|
||||
ImGui::Separator();
|
||||
|
||||
if (ImGui::Button("Generate Grid")) {
|
||||
setup(config, grid);
|
||||
gridInitialized = true;
|
||||
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
{
|
||||
ImGui::Begin("Preview");
|
||||
|
||||
if (gridInitialized) {
|
||||
ImGui::Image((void*)(intptr_t)textu,ImVec2(config.outWidth, config.outHeight));
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
if (gridInitialized && updatePreview == false) {
|
||||
livePreview(grid, config, cam);
|
||||
}
|
||||
|
||||
// std::cout << "ending frame" << std::endl;
|
||||
ImGui::Render();
|
||||
int display_w, display_h;
|
||||
@@ -173,8 +220,8 @@ int main() {
|
||||
textu = 0;
|
||||
}
|
||||
glfwTerminate();
|
||||
// std::cout << "printing" << std::endl;
|
||||
FunctionTimer::printStats(FunctionTimer::Mode::ENHANCED);
|
||||
|
||||
// std::cout << "printing" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -29,8 +29,8 @@ struct Camera {
|
||||
class VoxelGrid {
|
||||
private:
|
||||
double binSize = 1;
|
||||
Vec3T gridSize;
|
||||
//size_t width, height, depth;
|
||||
Vec3i gridSize;
|
||||
//int width, height, depth;
|
||||
std::vector<Voxel> voxels;
|
||||
float radians(float rads) {
|
||||
return rads * (M_PI / 180);
|
||||
@@ -69,49 +69,60 @@ private:
|
||||
}
|
||||
|
||||
public:
|
||||
VoxelGrid(size_t w, size_t h, size_t d) : gridSize(w,h,d) {
|
||||
VoxelGrid() : gridSize(0,0,0) {
|
||||
std::cout << "creating empty grid." << std::endl;
|
||||
}
|
||||
|
||||
VoxelGrid(int w, int h, int d) : gridSize(w,h,d) {
|
||||
voxels.resize(w * h * d);
|
||||
}
|
||||
|
||||
Voxel& get(size_t x, size_t y, size_t z) {
|
||||
Voxel& get(int x, int y, int z) {
|
||||
return voxels[z * gridSize.x * gridSize.y + y * gridSize.x + x];
|
||||
}
|
||||
|
||||
const Voxel& get(size_t x, size_t y, size_t z) const {
|
||||
const Voxel& get(int x, int y, int z) const {
|
||||
return voxels[z * gridSize.x * gridSize.y + y * gridSize.x + x];
|
||||
}
|
||||
|
||||
Voxel& get(const Vec3T& xyz) {
|
||||
Voxel& get(const Vec3i& xyz) {
|
||||
return voxels[xyz.z * gridSize.x * gridSize.y + xyz.y * gridSize.x + xyz.x];
|
||||
}
|
||||
|
||||
void resize() {
|
||||
//TODO: proper resizing
|
||||
void resize(int newW, int newH, int newD) {
|
||||
std::vector<Voxel> newVoxels(newW * newH * newD);
|
||||
int copyW = std::min(static_cast<int>(gridSize.x), newW);
|
||||
int copyH = std::min(static_cast<int>(gridSize.y), newH);
|
||||
int copyD = std::min(static_cast<int>(gridSize.z), newD);
|
||||
for (int z = 0; z < copyD; ++z) {
|
||||
for (int y = 0; y < copyH; ++y) {
|
||||
int oldRowStart = z * gridSize.x * gridSize.y + y * gridSize.x;
|
||||
int newRowStart = z * newW * newH + y * newW;
|
||||
std::copy(
|
||||
voxels.begin() + oldRowStart,
|
||||
voxels.begin() + oldRowStart + copyW,
|
||||
newVoxels.begin() + newRowStart
|
||||
);
|
||||
}
|
||||
}
|
||||
voxels = std::move(newVoxels);
|
||||
gridSize = Vec3i(newW, newH, newD);
|
||||
}
|
||||
|
||||
void set(size_t x, size_t y, size_t z, bool active, Vec3ui8 color) {
|
||||
set(Vec3T(x,y,z), active, color);
|
||||
void set(int x, int y, int z, bool active, Vec3ui8 color) {
|
||||
set(Vec3i(x,y,z), active, color);
|
||||
}
|
||||
|
||||
void set(Vec3T pos, bool active, Vec3ui8 color) {
|
||||
void set(Vec3i pos, bool active, Vec3ui8 color) {
|
||||
if (pos.x >= 0 || pos.y >= 0 || pos.z >= 0) {
|
||||
if (!(pos.x < gridSize.x)) {
|
||||
//until resizing added:
|
||||
return;
|
||||
gridSize.x = pos.x;
|
||||
resize();
|
||||
resize(pos.x, gridSize.y, gridSize.z);
|
||||
}
|
||||
else if (!(pos.y < gridSize.y)) {
|
||||
//until resizing added:
|
||||
return;
|
||||
gridSize.y = pos.y;
|
||||
resize();
|
||||
resize(gridSize.x, pos.y, gridSize.z);
|
||||
}
|
||||
else if (!(pos.z < gridSize.z)) {
|
||||
//until resizing added:
|
||||
return;
|
||||
gridSize.z = pos.z;
|
||||
resize();
|
||||
resize(gridSize.x, gridSize.y, pos.z);
|
||||
}
|
||||
|
||||
Voxel& v = get(pos);
|
||||
@@ -120,7 +131,7 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void set(Vec3T pos, Vec4ui8 rgbaval) {
|
||||
void set(Vec3i pos, Vec4ui8 rgbaval) {
|
||||
set(pos, static_cast<float>(rgbaval.a / 255), rgbaval.toVec3());
|
||||
}
|
||||
|
||||
@@ -129,9 +140,9 @@ public:
|
||||
return (voxl >= 0 && voxl.x < gridSize.x && voxl.y < gridSize.y && voxl.z < gridSize.z);
|
||||
}
|
||||
|
||||
void voxelTraverse(const Vec3d& origin, const Vec3d& end, std::vector<Vec3T>& visitedVoxel) {
|
||||
Vec3T cv = (origin / binSize).floorToT();
|
||||
Vec3T lv = (end / binSize).floorToT();
|
||||
void voxelTraverse(const Vec3d& origin, const Vec3d& end, std::vector<Vec3i>& visitedVoxel) {
|
||||
Vec3i cv = (origin / binSize).floorToI();
|
||||
Vec3i lv = (end / binSize).floorToI();
|
||||
Vec3d ray = end - origin;
|
||||
Vec3f step = Vec3f(ray.x >= 0 ? 1 : -1, ray.y >= 0 ? 1 : -1, ray.z >= 0 ? 1 : -1);
|
||||
Vec3d nextVox = cv.toDouble() + step * binSize;
|
||||
@@ -142,7 +153,7 @@ public:
|
||||
ray.y != 0 ? binSize / ray.y * step.y : INF,
|
||||
ray.z != 0 ? binSize / ray.z * step.z : INF);
|
||||
|
||||
Vec3T diff(0,0,0);
|
||||
Vec3i diff(0,0,0);
|
||||
bool negRay = false;
|
||||
if (cv.x != lv.x && ray.x < 0) {
|
||||
diff.x = diff.x--;
|
||||
@@ -187,17 +198,17 @@ public:
|
||||
return; // &&visitedVoxel;
|
||||
}
|
||||
|
||||
size_t getWidth() const {
|
||||
int getWidth() const {
|
||||
return gridSize.x;
|
||||
}
|
||||
size_t getHeight() const {
|
||||
int getHeight() const {
|
||||
return gridSize.y;
|
||||
}
|
||||
size_t getDepth() const {
|
||||
int getDepth() const {
|
||||
return gridSize.z;
|
||||
}
|
||||
|
||||
frame renderFrame(const Vec3f& CamPos, const Vec3f& dir, const Vec3f& up, float fov, size_t outW, size_t outH, frame::colormap colorformat = frame::colormap::RGB) {
|
||||
frame renderFrame(const Vec3f& CamPos, const Vec3f& dir, const Vec3f& up, float fov, int outW, int outH, frame::colormap colorformat = frame::colormap::RGB) {
|
||||
TIME_FUNCTION;
|
||||
Vec3f forward = (dir - CamPos).normalized();
|
||||
Vec3f right = forward.cross(up).normalized();
|
||||
@@ -210,10 +221,10 @@ public:
|
||||
frame outFrame(outH, outW, frame::colormap::RGB);
|
||||
std::vector<uint8_t> colorBuffer(outW * outH * 3);
|
||||
#pragma omp parallel for
|
||||
for (size_t y = 0; y < outH; y++) {
|
||||
for (int y = 0; y < outH; y++) {
|
||||
float v = (static_cast<float>(y) / static_cast<float>(outH - 1)) - 0.5f;
|
||||
for (size_t x = 0; x < outW; x++) {
|
||||
std::vector<Vec3T> hitVoxels;
|
||||
for (int x = 0; x < outW; x++) {
|
||||
std::vector<Vec3i> hitVoxels;
|
||||
float u = (static_cast<float>(x) / static_cast<float>(outW - 1)) - 0.5f;
|
||||
Vec3f rayDirWorld = (forward + right * (u * viewW) + upCor * (v * viewH)).normalized();
|
||||
Vec3f rayEnd = CamPos + rayDirWorld * maxDist;
|
||||
@@ -221,7 +232,7 @@ public:
|
||||
Vec3d rayEndGrid = rayEnd.toDouble() / binSize;
|
||||
voxelTraverse(rayStartGrid, rayEndGrid, hitVoxels);
|
||||
Vec3ui8 hitColor(10, 10, 255);
|
||||
for (const Vec3T& voxelPos : hitVoxels) {
|
||||
for (const Vec3i& voxelPos : hitVoxels) {
|
||||
if (inGrid(voxelPos)) {
|
||||
const Voxel& voxel = get(voxelPos);
|
||||
if (voxel.active) {
|
||||
@@ -236,14 +247,14 @@ public:
|
||||
// Set pixel color in buffer
|
||||
switch (colorformat) {
|
||||
case frame::colormap::RGB: {
|
||||
size_t idx = (y * outW + x) * 3;
|
||||
int idx = (y * outW + x) * 3;
|
||||
colorBuffer[idx + 0] = hitColor.x;
|
||||
colorBuffer[idx + 1] = hitColor.y;
|
||||
colorBuffer[idx + 2] = hitColor.z;
|
||||
break;
|
||||
}
|
||||
case frame::colormap::BGRA: {
|
||||
size_t idx = (y * outW + x) * 4;
|
||||
int idx = (y * outW + x) * 4;
|
||||
colorBuffer[idx + 3] = hitColor.x;
|
||||
colorBuffer[idx + 2] = hitColor.y;
|
||||
colorBuffer[idx + 1] = hitColor.z;
|
||||
|
||||
Reference in New Issue
Block a user