summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/3d/renderer.cc19
-rw-r--r--src/3d/renderer.h1
-rw-r--r--src/procedural/generator.cc36
-rw-r--r--src/tests/test_3d_render.cc7
4 files changed, 26 insertions, 37 deletions
diff --git a/src/3d/renderer.cc b/src/3d/renderer.cc
index 45caf14..778509f 100644
--- a/src/3d/renderer.cc
+++ b/src/3d/renderer.cc
@@ -111,7 +111,12 @@ void Renderer3D::create_skybox_pipeline() {
desc.primitive.frontFace = WGPUFrontFace_CCW;
desc.multisample.count = 1;
desc.multisample.mask = 0xFFFFFFFF;
- desc.depthStencil = nullptr; // Explicitly no depth for skybox
+
+ WGPUDepthStencilState depth_stencil = {};
+ depth_stencil.format = WGPUTextureFormat_Depth24Plus;
+ depth_stencil.depthWriteEnabled = WGPUOptionalBool_False;
+ depth_stencil.depthCompare = WGPUCompareFunction_Always;
+ desc.depthStencil = &depth_stencil;
skybox_pipeline_ = wgpuDeviceCreateRenderPipeline(device_, &desc);
wgpuBindGroupLayoutRelease(bgl);
@@ -295,6 +300,7 @@ void Renderer3D::update_uniforms(const Scene& scene, const Camera& camera,
float time) {
GlobalUniforms globals;
globals.view_proj = camera.get_projection_matrix() * camera.get_view_matrix();
+ globals.inv_view_proj = globals.view_proj.inverse();
globals.camera_pos_time =
vec4(camera.position.x, camera.position.y, camera.position.z, time);
globals.params =
@@ -416,6 +422,7 @@ void Renderer3D::render(const Scene& scene, const Camera& camera, float time,
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device_, nullptr);
// --- Pass 1: Render Skybox (Clears color, clears and stores depth) ---
+ bool skybox_rendered = false;
if (sky_texture_view_ && skybox_pipeline_) {
WGPURenderPassColorAttachment sky_color_attachment = {};
gpu_init_color_attachment(sky_color_attachment, target_view);
@@ -464,20 +471,24 @@ void Renderer3D::render(const Scene& scene, const Camera& camera, float time,
wgpuRenderPassEncoderDraw(sky_pass, 3, 1, 0, 0); // Draw a full-screen quad
wgpuRenderPassEncoderEnd(sky_pass);
wgpuRenderPassEncoderRelease(sky_pass);
+ skybox_rendered = true;
}
// --- Pass 2: Render Scene Objects (Loads depth, writes depth) ---
WGPURenderPassColorAttachment obj_color_attachment = {};
gpu_init_color_attachment(obj_color_attachment, target_view);
- obj_color_attachment.loadOp = WGPULoadOp_Load; // Load skybox color
+ obj_color_attachment.loadOp =
+ skybox_rendered ? WGPULoadOp_Load : WGPULoadOp_Clear; // Load or Clear
obj_color_attachment.storeOp = WGPUStoreOp_Store;
+ obj_color_attachment.clearValue = {0.05f, 0.05f, 0.05f,
+ 1.0f}; // Dark gray if no sky
WGPURenderPassDepthStencilAttachment obj_depth_attachment = {};
obj_depth_attachment.view = depth_view;
obj_depth_attachment.depthLoadOp =
- WGPULoadOp_Load; // Load cleared depth from skybox pass
+ skybox_rendered ? WGPULoadOp_Load : WGPULoadOp_Clear; // Load or Clear
obj_depth_attachment.depthStoreOp = WGPUStoreOp_Store; // Store object depth
- obj_depth_attachment.depthClearValue = 1.0f; // Not used when loadOp is Load
+ obj_depth_attachment.depthClearValue = 1.0f;
WGPURenderPassDescriptor obj_pass_desc = {};
obj_pass_desc.colorAttachmentCount = 1;
diff --git a/src/3d/renderer.h b/src/3d/renderer.h
index 148a521..57ad671 100644
--- a/src/3d/renderer.h
+++ b/src/3d/renderer.h
@@ -16,6 +16,7 @@
// Matches the GPU struct layout
struct GlobalUniforms {
mat4 view_proj;
+ mat4 inv_view_proj; // Added for skybox/raymarching
vec4 camera_pos_time; // xyz = camera_pos, w = time
vec4 params; // x = num_objects, yzw = padding
vec2 resolution;
diff --git a/src/procedural/generator.cc b/src/procedural/generator.cc
index 0eb8b03..f6d4e02 100644
--- a/src/procedural/generator.cc
+++ b/src/procedural/generator.cc
@@ -33,7 +33,8 @@ bool gen_perlin(uint8_t* buffer, int w, int h, const float* params,
// Pre-allocate temporary float buffer for accumulating noise
float* accum = (float*)calloc((size_t)w * h, sizeof(float));
- if (!accum) return false;
+ if (!accum)
+ return false;
float current_freq = base_freq;
float current_amp = base_amp;
@@ -42,7 +43,8 @@ bool gen_perlin(uint8_t* buffer, int w, int h, const float* params,
for (int o = 0; o < octaves; ++o) {
const int lattice_w = (int)ceil(current_freq) + 1;
const int lattice_h = (int)ceil(current_freq) + 1;
- float* lattice = (float*)malloc((size_t)lattice_w * lattice_h * sizeof(float));
+ float* lattice =
+ (float*)malloc((size_t)lattice_w * lattice_h * sizeof(float));
if (!lattice) {
free(accum);
return false;
@@ -110,43 +112,33 @@ bool gen_perlin(uint8_t* buffer, int w, int h, const float* params,
bool gen_noise(uint8_t* buffer, int w, int h, const float* params,
int num_params) {
-
float freq = (num_params > 1) ? params[1] : 4.0f;
if (num_params > 0 && params[0] != 0) {
-
srand((unsigned int)params[0]);
-
}
-
-
// Create a small lattice of random values
const int lattice_w = (int)ceil(freq);
const int lattice_h = (int)ceil(freq);
- float* lattice = (float*)malloc((size_t)lattice_w * lattice_h * sizeof(float));
-
- if (!lattice) return false;
-
+ float* lattice =
+ (float*)malloc((size_t)lattice_w * lattice_h * sizeof(float));
+ if (!lattice)
+ return false;
for (int i = 0; i < lattice_w * lattice_h; ++i) {
-
lattice[i] = (float)rand() / RAND_MAX;
-
}
const float scale_u = 1.f * (lattice_w - 1) / w;
const float scale_v = 1.f * (lattice_h - 1) / h;
-
-
for (int y = 0; y < h; ++y) {
-
const float v = scale_v * y;
const int ly = (int)floor(v);
@@ -162,7 +154,6 @@ bool gen_noise(uint8_t* buffer, int w, int h, const float* params,
uint8_t* const dst = &buffer[y * w * 4];
for (int x = 0; x < w; ++x) {
-
float u = scale_u * x;
const int lx = (int)floor(u);
@@ -171,8 +162,6 @@ bool gen_noise(uint8_t* buffer, int w, int h, const float* params,
float fu = smooth(u - lx);
-
-
float n00 = n0[lx];
float n10 = n0[lx_next];
@@ -181,12 +170,8 @@ bool gen_noise(uint8_t* buffer, int w, int h, const float* params,
float n11 = n1[lx_next];
-
-
const float noise = mix(mix(n00, n10, fu), mix(n01, n11, fu), fv);
-
-
const uint8_t val = (uint8_t)(noise * 255.0f);
dst[4 * x + 0] = val; // R
@@ -196,19 +181,14 @@ bool gen_noise(uint8_t* buffer, int w, int h, const float* params,
dst[4 * x + 2] = val; // B
dst[4 * x + 3] = 255; // A
-
}
-
}
free(lattice);
return true;
-
}
-
-
// Simple grid generator
// Params[0]: Grid Size (pixels)
// Params[1]: Line Thickness (pixels)
diff --git a/src/tests/test_3d_render.cc b/src/tests/test_3d_render.cc
index 5ae8b3a..2e9b663 100644
--- a/src/tests/test_3d_render.cc
+++ b/src/tests/test_3d_render.cc
@@ -120,11 +120,6 @@ void setup_scene() {
g_scene.clear();
srand(12345); // Fixed seed
- // Skybox object (always drawn first by renderer)
- Object3D skybox(ObjectType::SKYBOX);
- skybox.scale = vec3(1000.0f, 1000.0f, 1000.0f); // Large sphere
- g_scene.add_object(skybox);
-
// Large floor, use BOX type (SDF) at index 0
Object3D floor(ObjectType::BOX);
floor.position = vec3(0, -2.0f, 0);
@@ -222,6 +217,8 @@ int main(int argc, char** argv) {
sky_def.params = {42.0f, 4.0f, 1.0f, 0.5f, 6.0f};
g_textures.create_procedural_texture("sky", sky_def);
+ g_renderer.set_sky_texture(g_textures.get_texture_view("sky"));
+
setup_scene();
g_camera.position = vec3(0, 5, 10);