Implement screenshots and add images to the README

This commit is contained in:
2024-10-10 12:18:57 +02:00
parent 6cee9c77a7
commit 94b1fd1ca6
10 changed files with 1784 additions and 88 deletions
+1
View File
@@ -1,3 +1,4 @@
*.exe *.exe
*.exr *.exr
./*.png
path_trace path_trace
+6 -1
View File
@@ -1 +1,6 @@
![Image](assets/path_tracing.png) This is the implementation of the ray tracing algorithm. Here are some pics:
![scene 0](assets/screenshot_0.png)
![scene 0](assets/screenshot_1.png)
![scene 1](assets/screenshot_2.png)
![scene 2](assets/screenshot_3.png)
Binary file not shown.

Before

Width:  |  Height:  |  Size: 564 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB

-85
View File
@@ -1,85 +0,0 @@
"""
# Import the library using the alias "mi"
import mitsuba as mi
# Set the variant of the renderer
mi.set_variant('scalar_rgb')
# Load a scene
scene = mi.load_dict(mi.cornell_box())
# Render the scene
img = mi.render(scene)
# Write the rendered image to an EXR file
mi.Bitmap(img).write('cbox.exr')
"""
import mitsuba as mi
import cv2
import numpy as np
mi.set_variant('scalar_rgb')
# Define a scene with a sphere and a cube
scene_dict = {
'type': 'scene',
'integrator': {
'type': 'path'
},
'sensor': {
'type': 'perspective',
'film': {
'type': 'hdrfilm',
'width': 800,
'height': 600,
},
'sampler': {
'type': 'independent',
'sample_count': 64
},
'to_world': mi.ScalarTransform4f.look_at(
origin=[10, 10, 10], # Camera position
target=[0, 0, 0], # Camera looks at the origin
up=[0, 1, 0] # Up direction for the camera
)
},
'light': {
'type': 'point',
'position': [0, 5, 0],
'intensity': {'type': 'spectrum', 'value': 30.0}
},
'sphere': {
'type': 'sphere',
'center': [0, 0, 0],
'radius': 1,
'bsdf': {
'type': 'diffuse',
'reflectance': {'type': 'rgb', 'value': [0.8, 0.3, 0.3]}
}
},
'cube': {
'type': 'cube',
'to_world': mi.ScalarTransform4f.translate([2, 0, 0]).scale(1),
'bsdf': {
'type': 'diffuse',
'reflectance': {'type': 'rgb', 'value': [0.3, 0.8, 0.3]}
}
},
'floor': {
'type': 'cube',
'to_world': mi.ScalarTransform4f.translate([0, -0.5, 0]).scale([5, 0.01, 5]),
'bsdf': {
'type': 'diffuse',
'reflectance': {'type': 'rgb', 'value': [0.8, 0.8, 0.8]} # Light grey floor
}
}
}
# Load and render the scene
scene = mi.load_dict(scene_dict)
img = mi.render(scene)
# Convert the rendered image to a numpy array
bitmap = mi.Bitmap(img)
image_np = np.array(bitmap.convert(mi.Bitmap.PixelFormat.RGB, mi.Struct.Type.UInt8))
# Display the image using OpenCV
cv2.imshow('Rendered Image', image_np)
cv2.waitKey(0) # Press any key to close the window
cv2.destroyAllWindows()
+53 -2
View File
@@ -13,6 +13,9 @@
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include "clock.h" #include "clock.h"
#include "utils.h" #include "utils.h"
#include "camera.h" #include "camera.h"
@@ -302,10 +305,14 @@ static void error_callback(int error, const char* description)
fprintf(stderr, "Error: %s\n", description); fprintf(stderr, "Error: %s\n", description);
} }
void screenshot(void);
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{ {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE); glfwSetWindowShouldClose(window, GLFW_TRUE);
if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
screenshot();
} }
void framebuffer_size_callback(GLFWwindow* window, int width, int height) void framebuffer_size_callback(GLFWwindow* window, int width, int height)
@@ -876,6 +883,50 @@ void update_frame_texture(void)
os_mutex_unlock(&frame_mutex); os_mutex_unlock(&frame_mutex);
} }
// Must be executed on the main thread
void screenshot(void)
{
char file[1<<12];
int i = 0;
while (i < 1000) {
int k = snprintf(file, sizeof(file), "screenshot_%d.png", i);
if (k < 0 || k >= (int) sizeof(file)) {
fprintf(stderr, "Couldn't take screenshot (path buffer too small)\n");
return;
}
FILE *stream = fopen(file, "rb");
if (stream == NULL) {
if (errno == ENOENT)
break;
fprintf(stderr, "Couldn't take screenshot (%s)\n", strerror(errno));
return;
}
fclose(stream);
i++;
}
uint8_t *converted = malloc(frame_w * frame_h * 3 * sizeof(uint8_t));
if (converted == NULL) {
fprintf(stderr, "Couldn't take screenshot (out of memory)\n");
}
for (int i = 0; i < frame_w * frame_h; i++) {
converted[i * 3 + 0] = frame[i].x * 255;
converted[i * 3 + 1] = frame[i].y * 255;
converted[i * 3 + 2] = frame[i].z * 255;
}
stbi_flip_vertically_on_write(1);
int ok = stbi_write_png(file, frame_w, frame_h, 3, converted, 0);
free(converted);
if (!ok)
fprintf(stderr, "Could not take screenshot (write error)\n");
else
fprintf(stderr, "Took screenshot! (%s)\n", file);
}
bool parse_scene_file(char *file, Scene *scene); bool parse_scene_file(char *file, Scene *scene);
int main(int argc, char **argv) int main(int argc, char **argv)
File diff suppressed because it is too large Load Diff