pushing cause its kinda working.
This commit is contained in:
@@ -38,7 +38,7 @@ void generateNoiseGrid(VoxelGrid& grid, PNoise2& noise) {
|
|||||||
|
|
||||||
// Apply threshold to make some voxels "active"
|
// Apply threshold to make some voxels "active"
|
||||||
// Higher threshold = sparser voxels
|
// Higher threshold = sparser voxels
|
||||||
float threshold = 0.01;
|
float threshold = 0.3;
|
||||||
float active = (noiseVal > threshold) ? noiseVal : 0.0f;
|
float active = (noiseVal > threshold) ? noiseVal : 0.0f;
|
||||||
|
|
||||||
// Create grayscale color based on noise value
|
// Create grayscale color based on noise value
|
||||||
@@ -155,12 +155,16 @@ int main() {
|
|||||||
char filename[256];
|
char filename[256];
|
||||||
snprintf(filename, sizeof(filename), "output/framey_%03d.bmp", i);
|
snprintf(filename, sizeof(filename), "output/framey_%03d.bmp", i);
|
||||||
|
|
||||||
std::cout << "Rendering frame " << i << "/" << numFrames
|
// std::cout << "Rendering frame " << i << "/" << numFrames
|
||||||
<< " (angle: " << (angle * 360.0f / M_PI) << " degrees)" << std::endl;
|
// << " (angle: " << (angle * 360.0f / M_PI) << " degrees)" << std::endl;
|
||||||
|
|
||||||
renderView(filename, grid, finalPos, rotatedDir, up);
|
renderView(filename, grid, finalPos, rotatedDir, up);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
basePosition = Vec3f(0, 0, cameraDistance);
|
||||||
|
baseDirection = Vec3f(0, 0, -1);
|
||||||
|
up = Vec3f(0, 1, 0);
|
||||||
|
|
||||||
for (int i = 0; i <= numFrames; i++) {
|
for (int i = 0; i <= numFrames; i++) {
|
||||||
float angle = (float)i / numFrames * M_PI; // 0 to π (180 degrees)
|
float angle = (float)i / numFrames * M_PI; // 0 to π (180 degrees)
|
||||||
|
|
||||||
@@ -174,12 +178,16 @@ int main() {
|
|||||||
char filename[256];
|
char filename[256];
|
||||||
snprintf(filename, sizeof(filename), "output/framez_%03d.bmp", i);
|
snprintf(filename, sizeof(filename), "output/framez_%03d.bmp", i);
|
||||||
|
|
||||||
std::cout << "Rendering frame " << i << "/" << numFrames
|
// std::cout << "Rendering frame " << i << "/" << numFrames
|
||||||
<< " (angle: " << (angle * 360.0f / M_PI) << " degrees)" << std::endl;
|
// << " (angle: " << (angle * 360.0f / M_PI) << " degrees)" << std::endl;
|
||||||
|
|
||||||
renderView(filename, grid, finalPos, rotatedDir, up);
|
renderView(filename, grid, finalPos, rotatedDir, up);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
basePosition = Vec3f(0, 0, cameraDistance);
|
||||||
|
baseDirection = Vec3f(0, 0, -1);
|
||||||
|
up = Vec3f(0, 1, 0);
|
||||||
|
|
||||||
for (int i = 0; i <= numFrames; i++) {
|
for (int i = 0; i <= numFrames; i++) {
|
||||||
float angle = (float)i / numFrames * M_PI; // 0 to π (180 degrees)
|
float angle = (float)i / numFrames * M_PI; // 0 to π (180 degrees)
|
||||||
|
|
||||||
@@ -193,8 +201,8 @@ int main() {
|
|||||||
char filename[256];
|
char filename[256];
|
||||||
snprintf(filename, sizeof(filename), "output/framex_%03d.bmp", i);
|
snprintf(filename, sizeof(filename), "output/framex_%03d.bmp", i);
|
||||||
|
|
||||||
std::cout << "Rendering frame " << i << "/" << numFrames
|
// std::cout << "Rendering frame " << i << "/" << numFrames
|
||||||
<< " (angle: " << (angle * 360.0f / M_PI) << " degrees)" << std::endl;
|
// << " (angle: " << (angle * 360.0f / M_PI) << " degrees)" << std::endl;
|
||||||
|
|
||||||
renderView(filename, grid, finalPos, rotatedDir, up);
|
renderView(filename, grid, finalPos, rotatedDir, up);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ private:
|
|||||||
return rads * (M_PI / 180);
|
return rads * (M_PI / 180);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Mat4f lookAt(Vec3f const& eye, Vec3f const& center, Vec3f const& up) {
|
static Mat4f lookAt(const Vec3f& eye, const Vec3f& center, const Vec3f& up) {
|
||||||
Vec3f const f = (center - eye).normalized();
|
Vec3f const f = (center - eye).normalized();
|
||||||
Vec3f const s = f.cross(up).normalized();
|
Vec3f const s = f.cross(up).normalized();
|
||||||
Vec3f const u = s.cross(f);
|
Vec3f const u = s.cross(f);
|
||||||
@@ -65,7 +65,7 @@ private:
|
|||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<float,float> rayBoxIntersect(Vec3f origin, Vec3f direction) {
|
std::pair<float,float> rayBoxIntersect(const Vec3f& origin, const Vec3f& direction) {
|
||||||
Vec3f tBMin = Vec3f(0,0,0);
|
Vec3f tBMin = Vec3f(0,0,0);
|
||||||
Vec3f tBMax = Vec3f(width, height, depth);
|
Vec3f tBMax = Vec3f(width, height, depth);
|
||||||
float tmin = 0;
|
float tmin = 0;
|
||||||
@@ -86,7 +86,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
//used to prevent division by 0 issues
|
//used to prevent division by 0 issues
|
||||||
bool specialCases(Vec3f origin, Vec3f direction, float maxDist, Vec3f hitColor) {
|
bool specialCases(const Vec3f& origin, const Vec3f& direction, float maxDist, Vec3f& hitColor) {
|
||||||
float stepSize = 0.5;
|
float stepSize = 0.5;
|
||||||
int maxSteps = maxDist/stepSize;
|
int maxSteps = maxDist/stepSize;
|
||||||
for (int step = 0; step < maxSteps; ++step) {
|
for (int step = 0; step < maxSteps; ++step) {
|
||||||
@@ -159,7 +159,7 @@ public:
|
|||||||
return (voxl >= 0 && voxl.x < width && voxl.y < height && voxl.z < depth);
|
return (voxl >= 0 && voxl.x < width && voxl.y < height && voxl.z < depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Vec3f> genPixelDirs(Vec3f pos, Vec3f dir, size_t imgWidth, size_t imgHeight, float fov) {
|
std::vector<Vec3f> genPixelDirs(const Vec3f& pos, const Vec3f& dir, size_t imgWidth, size_t imgHeight, float fov) {
|
||||||
std::vector<Vec3f> dirs(imgWidth * imgHeight);
|
std::vector<Vec3f> dirs(imgWidth * imgHeight);
|
||||||
float fovRad = radians(fov);
|
float fovRad = radians(fov);
|
||||||
float tanFov = tan(fovRad * 0.5);
|
float tanFov = tan(fovRad * 0.5);
|
||||||
@@ -168,14 +168,18 @@ public:
|
|||||||
Vec3f camRight = worldUp.cross(dir).normalized();
|
Vec3f camRight = worldUp.cross(dir).normalized();
|
||||||
Vec3f camUp = dir.cross(camRight).normalized();
|
Vec3f camUp = dir.cross(camRight).normalized();
|
||||||
|
|
||||||
for (int y = 0; y < height; ++y) {
|
float imgWidthInv = 1 / (imgWidth - 1);
|
||||||
float ndcY = 1 - (2 * y / (height - 1));
|
float imgHeightInv = 1 / (imgHeight - 1);
|
||||||
|
float aspectTanFov = aspect * tanFov;
|
||||||
|
|
||||||
|
for (int y = 0; y < imgHeight; ++y) {
|
||||||
|
float ndcY = 1 - (2 * y * imgHeightInv);
|
||||||
float screenY = ndcY * tanFov;
|
float screenY = ndcY * tanFov;
|
||||||
for (int x = 0; x < width; ++x) {
|
for (int x = 0; x < imgWidth; ++x) {
|
||||||
float ndcX = (2 * x / (width - 1)) - 1;
|
float ndcX = (2 * x * imgWidthInv) - 1;
|
||||||
float screenX = ndcX * aspect*tanFov;
|
float screenX = ndcX * aspectTanFov;
|
||||||
Vec3f dir = (camRight * screenX + camUp * screenY + dir).normalized();
|
Vec3f dir = (camRight * screenX + camUp * screenY + dir).normalized();
|
||||||
dirs[y*width+x] = dir;
|
dirs[y*imgWidth+x] = dir;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return dirs;
|
return dirs;
|
||||||
@@ -197,8 +201,7 @@ public:
|
|||||||
return rayDirWorld.normalized();
|
return rayDirWorld.normalized();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool rayCast(Vec3f origin, Vec3f direction, float maxDist, Vec3f hitColor) {
|
bool rayCast(const Vec3f& origin, const Vec3f& direction, float maxDist, Vec3f& hitColor) {
|
||||||
TIME_FUNCTION;
|
|
||||||
direction.normalized();
|
direction.normalized();
|
||||||
if (abs(direction.length()) < EPSILON) return false;
|
if (abs(direction.length()) < EPSILON) return false;
|
||||||
|
|
||||||
@@ -216,41 +219,43 @@ public:
|
|||||||
if (tEntry < EPSILON || tExit < EPSILON) return false;
|
if (tEntry < EPSILON || tExit < EPSILON) return false;
|
||||||
float tStart = std::max(0.0f, tEntry);
|
float tStart = std::max(0.0f, tEntry);
|
||||||
if (tStart > maxDist) return false;
|
if (tStart > maxDist) return false;
|
||||||
Vec3f gridOrig = Vec3f(origin+direction*tStart);
|
Vec3f gridOrig = origin + direction * tStart;
|
||||||
currentVoxel = gridOrig.floorToT();
|
currentVoxel = gridOrig.floorToT();
|
||||||
}
|
}
|
||||||
|
|
||||||
Vec3i8 step = Vec3i8(direction.x >= 0 ? 1 : -1, direction.y >= 0 ? 1 : -1, direction.z >= 0 ? 1 : -1);
|
Vec3i8 step = Vec3i8(direction.x >= 0 ? 1 : -1, direction.y >= 0 ? 1 : -1, direction.z >= 0 ? 1 : -1);
|
||||||
Vec3f nextVoxel = Vec3f(currentVoxel.x + (step.x>0 ? 1 : 0) - origin.x,
|
Vec3f tMax;
|
||||||
currentVoxel.y + (step.y>0 ? 1 : 0) - origin.y,
|
for (int i = 0; i < 3; i++) {
|
||||||
currentVoxel.z + (step.z>0 ? 1 : 0) - origin.z);
|
if (step[i] > 0) {
|
||||||
Vec3f tMax = nextVoxel*invDir;
|
tMax[i] = ((currentVoxel[i] + 1) - origin[i]) * invDir[i];
|
||||||
|
} else {
|
||||||
|
tMax[i] = (currentVoxel[i] - origin[i]) * invDir[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
Vec3f tDelta = invDir.abs();
|
Vec3f tDelta = invDir.abs();
|
||||||
|
|
||||||
float aalpha = 0;
|
float aalpha = 0;
|
||||||
|
|
||||||
while (inGrid(currentVoxel)) {
|
while (inGrid(currentVoxel) && aalpha < 1) {
|
||||||
Voxel cv = get(currentVoxel);
|
Voxel cv = get(currentVoxel);
|
||||||
if (cv.active > EPSILON) {
|
if (cv.active > EPSILON) {
|
||||||
hitColor = (hitColor * aalpha) + (cv.color * cv.active);
|
float alpha = cv.active * (1.0f - aalpha);
|
||||||
|
Vec3f voxelColor = cv.color.toFloat() / 255.0f;
|
||||||
|
hitColor = hitColor + voxelColor * alpha;
|
||||||
aalpha += cv.active;
|
aalpha += cv.active;
|
||||||
}
|
}
|
||||||
if (aalpha >= 1 ) {
|
|
||||||
//std::cout << "hit in normal case at: " << currentVoxel << " due to alpha overload" << std::endl;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tMax.x < tMax.y && tMax.x < tMax.z) {
|
if (tMax.x < tMax.y && tMax.x < tMax.z) {
|
||||||
if (tMax.x > maxDist) break;
|
if (tMax.x > maxDist) break;
|
||||||
currentVoxel = (currentVoxel.x + step.x, currentVoxel.y, currentVoxel.z);
|
currentVoxel.x += step.x;
|
||||||
tMax.x += tDelta.x;
|
tMax.x += tDelta.x;
|
||||||
} else if (tMax.y < tMax.z)
|
} else if (tMax.y < tMax.z)
|
||||||
{
|
{
|
||||||
currentVoxel = (currentVoxel.x, currentVoxel.y + step.y, currentVoxel.z);
|
currentVoxel.y += step.y;
|
||||||
tMax.y += tDelta.y;
|
tMax.y += tDelta.y;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
currentVoxel = (currentVoxel.x, currentVoxel.y, currentVoxel.z + step.z);
|
currentVoxel.z += step.z;
|
||||||
tMax.z += tDelta.z;
|
tMax.z += tDelta.z;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -271,12 +276,17 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void renderOut(std::vector<uint8_t>& output, size_t& outwidth, size_t& outheight, const Camera& cam) {
|
void renderOut(std::vector<uint8_t>& output, size_t& outwidth, size_t& outheight, const Camera& cam) {
|
||||||
|
TIME_FUNCTION;
|
||||||
output.resize(outwidth * outheight * 3);
|
output.resize(outwidth * outheight * 3);
|
||||||
Vec3f backgroundColor(0.1f, 0.1f, 1.0f);
|
Vec3f backgroundColor(0.1f, 0.1f, 1.0f);
|
||||||
float maxDistance = std::sqrt(width*width + height*height + depth*depth) * 2.0f;
|
float maxDistance = std::sqrt(width*width + height*height + depth*depth) * 2.0f;
|
||||||
std::vector<Vec3f> dirs = genPixelDirs(cam.posfor.origin, cam.posfor.direction, outwidth, outheight, cam.fov);
|
// std::vector<Vec3f> dirs = genPixelDirs(cam.posfor.origin, cam.posfor.direction, outwidth, outheight, cam.fov);
|
||||||
|
|
||||||
for (size_t y = 0; y < outheight; y++) {
|
for (size_t y = 0; y < outheight; y++) {
|
||||||
|
float yout = y * outwidth;
|
||||||
for (size_t x = 0; x < outwidth; x++) {
|
for (size_t x = 0; x < outwidth; x++) {
|
||||||
|
// Vec3f rayDir = dirs[y*outwidth + x];
|
||||||
|
// Vec3f hitColor = Vec3f(0,0,0);
|
||||||
Vec3f rayDir = perPixelRayDir(x, y, outwidth, outheight, cam);
|
Vec3f rayDir = perPixelRayDir(x, y, outwidth, outheight, cam);
|
||||||
Ray3f ray(cam.posfor.origin, rayDir);
|
Ray3f ray(cam.posfor.origin, rayDir);
|
||||||
Vec3f hitPos;
|
Vec3f hitPos;
|
||||||
@@ -292,7 +302,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
finalColor = finalColor.clamp(0, 1);
|
finalColor = finalColor.clamp(0, 1);
|
||||||
size_t pixelIndex = (y * outwidth + x) * 3;
|
size_t pixelIndex = (yout + x) * 3;
|
||||||
output[pixelIndex + 0] = static_cast<uint8_t>(finalColor.x * 255);
|
output[pixelIndex + 0] = static_cast<uint8_t>(finalColor.x * 255);
|
||||||
output[pixelIndex + 1] = static_cast<uint8_t>(finalColor.y * 255);
|
output[pixelIndex + 1] = static_cast<uint8_t>(finalColor.y * 255);
|
||||||
output[pixelIndex + 2] = static_cast<uint8_t>(finalColor.z * 255);
|
output[pixelIndex + 2] = static_cast<uint8_t>(finalColor.z * 255);
|
||||||
|
|||||||
@@ -164,6 +164,29 @@ public:
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vec4ui8 permuteColor(const Vec3<float>& point) {
|
||||||
|
TIME_FUNCTION;
|
||||||
|
|
||||||
|
float noiseR = permute(point);
|
||||||
|
float noiseG = permute(point + Vec3<float>(100.0f, 100.0f, 100.0f));
|
||||||
|
float noiseB = permute(point + Vec3<float>(200.0f, 200.0f, 200.0f));
|
||||||
|
float noiseA = permute(point + Vec3<float>(300.0f, 300.0f, 300.0f));
|
||||||
|
float rNormalized = (noiseR + 1.0f) * 0.5f;
|
||||||
|
float gNormalized = (noiseG + 1.0f) * 0.5f;
|
||||||
|
float bNormalized = (noiseB + 1.0f) * 0.5f;
|
||||||
|
float aNormalized = (noiseA + 1.0f) * 0.5f;
|
||||||
|
rNormalized = std::clamp(rNormalized, 0.0f, 1.0f);
|
||||||
|
gNormalized = std::clamp(gNormalized, 0.0f, 1.0f);
|
||||||
|
bNormalized = std::clamp(bNormalized, 0.0f, 1.0f);
|
||||||
|
aNormalized = std::clamp(aNormalized, 0.0f, 1.0f);
|
||||||
|
uint8_t r = static_cast<uint8_t>(rNormalized * 255.0f);
|
||||||
|
uint8_t g = static_cast<uint8_t>(gNormalized * 255.0f);
|
||||||
|
uint8_t b = static_cast<uint8_t>(bNormalized * 255.0f);
|
||||||
|
uint8_t a = static_cast<uint8_t>(aNormalized * 255.0f);
|
||||||
|
|
||||||
|
return Vec4ui8(r, g, b, a);
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user