pushing some stuff home
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include "../util/grid/grid3.hpp"
|
||||
#include "../util/output/bmpwriter.hpp"
|
||||
#include "../util/noise/pnoise2.hpp"
|
||||
@@ -24,30 +25,28 @@ void generateNoiseGrid(VoxelGrid& grid, PNoise2& noise) {
|
||||
std::cout << "Processing layer " << z << " of " << GRID_SIZE << std::endl;
|
||||
}
|
||||
|
||||
#pragma omp parallel for
|
||||
//#pragma omp parallel for
|
||||
for (size_t y = 0; y < GRID_SIZE; ++y) {
|
||||
#pragma omp parallel for
|
||||
//#pragma omp parallel for
|
||||
for (size_t x = 0; x < GRID_SIZE; ++x) {
|
||||
// Create 3D noise coordinates (scaled for better frequency)
|
||||
float scale = 0.05f; // Controls noise frequency
|
||||
float noiseVal = noise.permute(Vec3f(x * scale, y * scale, z * scale));
|
||||
|
||||
// Convert from [-1, 1] to [0, 1] range
|
||||
float normalizedNoise = (noiseVal + 1.0f) * 0.5f;
|
||||
//float normalizedNoise = (noiseVal + 1.0f) * 0.5f;
|
||||
|
||||
// Apply threshold to make some voxels "active"
|
||||
// Higher threshold = sparser voxels
|
||||
float threshold = 0.3f;
|
||||
float active = (normalizedNoise > threshold) ? normalizedNoise : 0.0f;
|
||||
//float active = 1;
|
||||
float active = (noiseVal > threshold) ? noiseVal : 0.0f;
|
||||
|
||||
// Create grayscale color based on noise value
|
||||
uint8_t grayValue = static_cast<uint8_t>(normalizedNoise * 255);
|
||||
uint8_t grayValue = static_cast<uint8_t>(noiseVal * 255);
|
||||
Vec3ui8 color(grayValue, grayValue, grayValue);
|
||||
#pragma omp critical
|
||||
if (active > threshold) {
|
||||
grid.set(x, y, z, active, color);
|
||||
//std::cout << "setting a voxel to color: " << color << " with alpha of " << active << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -60,7 +59,7 @@ void generateNoiseGrid(VoxelGrid& grid, PNoise2& noise) {
|
||||
bool renderView(const std::string& filename, VoxelGrid& grid, const Vec3f& position,
|
||||
const Vec3f& direction, const Vec3f& up = Vec3f(0, 1, 0)) {
|
||||
TIME_FUNCTION;
|
||||
Camera cam(position, direction, up);
|
||||
Camera cam(position, direction, up, 40);
|
||||
|
||||
std::vector<uint8_t> renderBuffer;
|
||||
size_t width = RENDER_WIDTH;
|
||||
@@ -81,85 +80,81 @@ bool renderView(const std::string& filename, VoxelGrid& grid, const Vec3f& posit
|
||||
return success;
|
||||
}
|
||||
|
||||
// Function to rotate a vector around the Y axis
|
||||
Vec3f rotateY(const Vec3f& vec, float angle) {
|
||||
TIME_FUNCTION;
|
||||
float cosA = cos(angle);
|
||||
float sinA = sin(angle);
|
||||
return Vec3f(
|
||||
vec.x * cosA + vec.z * sinA,
|
||||
vec.y,
|
||||
-vec.x * sinA + vec.z * cosA
|
||||
);
|
||||
}
|
||||
|
||||
int main() {
|
||||
try {
|
||||
std::cout << "=== Noise Grid Generator and Renderer ===" << std::endl;
|
||||
std::cout << "Grid Size: 1024x1024x1024 voxels" << std::endl;
|
||||
std::cout << "Render Size: 512x512 pixels" << std::endl;
|
||||
TIME_FUNCTION;
|
||||
std::cout << "=== Noise Grid Generator and Renderer ===" << std::endl;
|
||||
std::cout << "Grid Size: 1024x1024x1024 voxels" << std::endl;
|
||||
std::cout << "Render Size: 512x512 pixels" << std::endl;
|
||||
|
||||
// Initialize Perlin noise generator
|
||||
PNoise2 noise;
|
||||
|
||||
// Create voxel grid
|
||||
VoxelGrid grid(GRID_SIZE, GRID_SIZE, GRID_SIZE);
|
||||
|
||||
// Generate noise grid
|
||||
generateNoiseGrid(grid, noise);
|
||||
|
||||
// Define center of the grid for camera positioning
|
||||
Vec3f gridCenter(GRID_SIZE / 2.0f, GRID_SIZE / 2.0f, GRID_SIZE / 2.0f);
|
||||
|
||||
// Camera distance from center (outside the grid)
|
||||
float cameraDistance = GRID_SIZE * 2.0f;
|
||||
|
||||
// Create 180-degree rotation around the Y axis
|
||||
int numFrames = 2;
|
||||
|
||||
// Base camera position (looking from front)
|
||||
Vec3f basePosition(0, 0, cameraDistance);
|
||||
Vec3f baseDirection(0, 0, -1); // Looking towards negative Z (towards center)
|
||||
Vec3f up(0, 1, 0);
|
||||
|
||||
// Render frames around 180 degrees
|
||||
for (int i = 0; i <= numFrames; i++) {
|
||||
float angle = (float)i / numFrames * M_PI; // 0 to π (180 degrees)
|
||||
|
||||
// Initialize Perlin noise generator
|
||||
PNoise2 noise;
|
||||
// Rotate camera position around Y axis
|
||||
Vec3f rotatedPos = rotateY(basePosition, angle);
|
||||
Vec3f finalPos = gridCenter + rotatedPos;
|
||||
//Vec3f rotatedDir = rotateY(baseDirection, angle);
|
||||
Vec3f rotatedDir = (gridCenter - finalPos).normalized();
|
||||
|
||||
// Create voxel grid
|
||||
VoxelGrid grid(GRID_SIZE, GRID_SIZE, GRID_SIZE);
|
||||
// Create filename with frame number
|
||||
char filename[256];
|
||||
snprintf(filename, sizeof(filename), "output/frame_%03d.bmp", i);
|
||||
|
||||
// Generate noise grid
|
||||
generateNoiseGrid(grid, noise);
|
||||
std::cout << "Rendering frame " << i << "/" << numFrames
|
||||
<< " (angle: " << (angle * 360.0f / M_PI) << " degrees)" << std::endl;
|
||||
|
||||
// Define center of the grid for camera positioning
|
||||
Vec3f gridCenter(GRID_SIZE / 2.0f, GRID_SIZE / 2.0f, GRID_SIZE / 2.0f);
|
||||
|
||||
// Camera distance from center (outside the grid)
|
||||
float cameraDistance = GRID_SIZE * 2.0f;
|
||||
float cameraDir = GRID_SIZE * 0.5f;
|
||||
|
||||
// Render from 4 cardinal directions (looking at center)
|
||||
std::cout << "\nRendering cardinal views..." << std::endl;
|
||||
|
||||
// North view (looking from positive Z towards center)
|
||||
renderView("output/north_view.bmp", grid,
|
||||
gridCenter + Vec3f(0, 0, cameraDistance),
|
||||
Vec3f(0, 0, -1)); // Look towards negative Z
|
||||
|
||||
// South view (looking from negative Z towards center)
|
||||
renderView("output/south_view.bmp", grid,
|
||||
gridCenter + Vec3f(0, 0, -cameraDistance),
|
||||
Vec3f(0, 0, 1)); // Look towards positive Z
|
||||
|
||||
// East view (looking from positive X towards center)
|
||||
renderView("output/east_view.bmp", grid,
|
||||
gridCenter + Vec3f(cameraDistance, 0, 0),
|
||||
Vec3f(-1, 0, 0)); // Look towards negative X
|
||||
|
||||
// West view (looking from negative X towards center)
|
||||
renderView("output/west_view.bmp", grid,
|
||||
gridCenter + Vec3f(-cameraDistance, 0, 0),
|
||||
Vec3f(1, 0, 0)); // Look towards positive X
|
||||
|
||||
// Zenith view (looking from above)
|
||||
std::cout << "\nRendering zenith and nadir views..." << std::endl;
|
||||
renderView("output/zenith_view.bmp", grid,
|
||||
gridCenter + Vec3f(0, cameraDistance, 0),
|
||||
Vec3f(0, -1, 0), // Look down
|
||||
Vec3f(0, 0, -1)); // Adjust up vector for proper orientation
|
||||
|
||||
// Nadir view (looking from below)
|
||||
renderView("output/nadir_view.bmp", grid,
|
||||
gridCenter + Vec3f(0, -cameraDistance, 0),
|
||||
Vec3f(0, 1, 0), // Look up
|
||||
Vec3f(0, 0, 1)); // Adjust up vector
|
||||
|
||||
// Optional: Create a front view (alternative to north)
|
||||
renderView("output/front_view.bmp", grid,
|
||||
gridCenter + Vec3f(0, 0, cameraDistance),
|
||||
Vec3f(0, 0, -1),
|
||||
Vec3f(0, 1, 0));
|
||||
|
||||
std::cout << "\n=== All renders complete! ===" << std::endl;
|
||||
std::cout << "Generated BMP files:" << std::endl;
|
||||
std::cout << "1. north_view.bmp - Looking from positive Z" << std::endl;
|
||||
std::cout << "2. south_view.bmp - Looking from negative Z" << std::endl;
|
||||
std::cout << "3. east_view.bmp - Looking from positive X" << std::endl;
|
||||
std::cout << "4. west_view.bmp - Looking from negative X" << std::endl;
|
||||
std::cout << "5. zenith_view.bmp - Looking from above" << std::endl;
|
||||
std::cout << "6. nadir_view.bmp - Looking from below" << std::endl;
|
||||
std::cout << "7. front_view.bmp - Alternative front view" << std::endl;
|
||||
|
||||
return 0;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return 1;
|
||||
renderView(filename, grid, finalPos, rotatedDir, up);
|
||||
}
|
||||
|
||||
std::cout << "\nRendering zenith and nadir views..." << std::endl;
|
||||
renderView("output/zenith_view.bmp", grid,
|
||||
gridCenter + Vec3f(0, cameraDistance, 0),
|
||||
Vec3f(0, -1, 0), // Look down
|
||||
Vec3f(0, 0, -1)); // Adjust up vector for proper orientation
|
||||
|
||||
// Nadir view (looking from below)
|
||||
renderView("output/nadir_view.bmp", grid,
|
||||
gridCenter + Vec3f(0, -cameraDistance, 0),
|
||||
Vec3f(0, 1, 0), // Look up
|
||||
Vec3f(0, 0, 1)); // Adjust up vector
|
||||
|
||||
std::cout << "\n=== All renders complete! ===" << std::endl;
|
||||
|
||||
FunctionTimer::printStats(FunctionTimer::Mode::ENHANCED);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user