Skip to content

Commit d695ff9

Browse files
committed
update notebook to say xeus-cpp instead of xeus-cpp-lite
1 parent 353438b commit d695ff9

File tree

2 files changed

+107
-7
lines changed

2 files changed

+107
-7
lines changed

notebooks/dlopen.ipynb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"metadata": {
3+
"kernelspec": {
4+
"name": "xcpp20",
5+
"display_name": "C++20",
6+
"language": "cpp"
7+
},
8+
"language_info": {
9+
"codemirror_mode": "text/x-c++src",
10+
"file_extension": ".cpp",
11+
"mimetype": "text/x-c++src",
12+
"name": "C++",
13+
"version": "20"
14+
}
15+
},
16+
"nbformat_minor": 5,
17+
"nbformat": 4,
18+
"cells": [
19+
{
20+
"id": "cc5e6dc8-a9ef-4142-9a43-42b3b177849e",
21+
"cell_type": "code",
22+
"source": "#include <dlfcn.h>\n#include <iostream>\n\ntypedef void (*func_t)();\n\nint main() {\n void* handle = dlopen(NULL, RTLD_NOW); // NULL means access symbols from the main module\n if (!handle) {\n std::cerr << \"Failed to access main module\" << std::endl;\n return 1;\n }\n \n func_t myFunc = (func_t) dlsym(handle, \"my_function\");\n if (!myFunc) {\n std::cerr << \"Function not found in main module\" << std::endl;\n } else {\n std::cout << \"Function found! Calling my_function()\" << std::endl;\n myFunc(); // If found, call it directly\n }\n return 0;\n}\n",
23+
"metadata": {
24+
"trusted": true
25+
},
26+
"outputs": [],
27+
"execution_count": 1
28+
},
29+
{
30+
"id": "d2ff6c27-b158-4f87-b8f0-4fb78237b7d3",
31+
"cell_type": "code",
32+
"source": "main();",
33+
"metadata": {
34+
"trusted": true
35+
},
36+
"outputs": [
37+
{
38+
"name": "stderr",
39+
"output_type": "stream",
40+
"text": "Function not found in main module\n"
41+
}
42+
],
43+
"execution_count": 2
44+
},
45+
{
46+
"id": "5def964d-1331-4390-b72c-219773014e66",
47+
"cell_type": "code",
48+
"source": "void* handle = dlopen(\"custom_module.wasm\", RTLD_NOW);\nif (!handle) {\n std::cerr << \"Failed to load custom module\" << std::endl;\n return 1;\n}\n\nfunc_t myFunc = (func_t) dlsym(handle, \"my_custom_function\");\nif (!myFunc) {\n std::cerr << \"Function not found in custom module\" << std::endl;\n} else {\n std::cout << \"Function found! Calling my_custom_function()\" << std::endl;\n myFunc(); \n}",
49+
"metadata": {
50+
"trusted": true
51+
},
52+
"outputs": [
53+
{
54+
"name": "stdout",
55+
"output_type": "stream",
56+
"text": "Function found! Calling my_custom_function()\nHello from my_custom_function!\n"
57+
}
58+
],
59+
"execution_count": 3
60+
},
61+
{
62+
"id": "d4cff916-7906-4e5c-b4fe-390fdf8fa8aa",
63+
"cell_type": "code",
64+
"source": "myFunc();",
65+
"metadata": {
66+
"trusted": true
67+
},
68+
"outputs": [
69+
{
70+
"name": "stdout",
71+
"output_type": "stream",
72+
"text": "Hello from my_custom_function!\n"
73+
}
74+
],
75+
"execution_count": 4
76+
},
77+
{
78+
"id": "2ac3a00b-03d8-4d5e-a20e-172daabcfc7e",
79+
"cell_type": "code",
80+
"source": "",
81+
"metadata": {
82+
"trusted": true
83+
},
84+
"outputs": [],
85+
"execution_count": null
86+
}
87+
]
88+
}

notebooks/tinyraytracer.ipynb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"metadata": {
33
"kernelspec": {
4-
"name": "xcpp23",
54
"display_name": "C++23",
6-
"language": "cpp"
5+
"language": "cpp",
6+
"name": "xcpp23"
77
},
88
"language_info": {
99
"codemirror_mode": "text/x-c++src",
@@ -19,15 +19,18 @@
1919
{
2020
"id": "08da9111-d635-47d0-8fec-d9490704964e",
2121
"cell_type": "markdown",
22-
"source": "# Tiny Ray Tracer in xeus-cpp-lite\n\nAn educational ray tracer based on Dmitry Sokolov’s “tinyraytracer” project\n\nThis notebook walks through the core ideas behind a minimal ray tracer, following the excellent educational series by Dmitry Sokolov (ssloy):\n\n* GitHub project: https://github.com/ssloy/tinyraytracer \n* Tutorial series: https://github.com/ssloy/tinyraytracer/wiki\n\nThe tutorial builds a ray tracer step-by-step, starting from basic vector math and gradually introducing spheres, materials, shadows, reflections, refractions, and finally global illumination.\n\nThe implementation in this notebook corresponds to **Step 9** of the tutorial series, and can be extended further as an assignment toward **Step 10**, producing a more advanced final render.",
22+
"source": "# Tiny Ray Tracer in xeus-cpp\n\nAn educational ray tracer based on Dmitry Sokolov’s “tinyraytracer” project\n\nThis notebook walks through the core ideas behind a minimal ray tracer, following the excellent educational series by Dmitry Sokolov (ssloy):\n\n* GitHub project: https://github.com/ssloy/tinyraytracer \n* Tutorial series: https://github.com/ssloy/tinyraytracer/wiki\n\nThe tutorial builds a ray tracer step-by-step, starting from basic vector math and gradually introducing spheres, materials, shadows, reflections, refractions, and finally global illumination.\n\nThe implementation in this notebook corresponds to **Step 9** of the tutorial series, and can be extended further as an assignment toward **Step 10**, producing a more advanced final render.",
2323
"metadata": {}
2424
},
2525
{
2626
"id": "b7d674bf-ac5a-4344-8e59-3e692f2be561",
2727
"cell_type": "code",
2828
"source": "#include <tuple>\n#include <vector>\n#include <algorithm>\n#include <cmath>\n\n// -------------------- Math & scene types --------------------\n\nstruct vec3 {\n float x = 0, y = 0, z = 0;\n\n float& operator[](const int i) { return i==0 ? x : (1==i ? y : z); }\n const float& operator[](const int i) const { return i==0 ? x : (1==i ? y : z); }\n\n vec3 operator*(const float v) const { return {x*v, y*v, z*v}; }\n float operator*(const vec3& v) const { return x*v.x + y*v.y + z*v.z; }\n vec3 operator+(const vec3& v) const { return {x+v.x, y+v.y, z+v.z}; }\n vec3 operator-(const vec3& v) const { return {x-v.x, y-v.y, z-v.z}; }\n vec3 operator-() const { return {-x, -y, -z}; }\n\n float norm() const { return std::sqrt(x*x + y*y + z*z); }\n vec3 normalized() const { return (*this) * (1.f / norm()); }\n};\n\nvec3 cross(const vec3 v1, const vec3 v2) {\n return {\n v1.y*v2.z - v1.z*v2.y,\n v1.z*v2.x - v1.x*v2.z,\n v1.x*v2.y - v1.y*v2.x\n };\n}\n\nstruct Material {\n float refractive_index = 1.f;\n float albedo[4] = {2.f, 0.f, 0.f, 0.f};\n vec3 diffuse_color = {0.f, 0.f, 0.f};\n float specular_exponent = 0.f;\n};\n\nstruct Sphere {\n vec3 center;\n float radius;\n Material material;\n};\n\n// -------------------- Scene definition --------------------\n\nconstexpr Material ivory = {1.0f, {0.9f, 0.5f, 0.1f, 0.0f}, {0.4f, 0.4f, 0.3f}, 50.f};\nconstexpr Material glass = {1.5f, {0.0f, 0.9f, 0.1f, 0.8f}, {0.6f, 0.7f, 0.8f}, 125.f};\nconstexpr Material red_rubber = {1.0f, {1.4f, 0.3f, 0.0f, 0.0f}, {0.3f, 0.1f, 0.1f}, 10.f};\nconstexpr Material mirror = {1.0f, {0.0f, 16.0f, 0.8f, 0.0f}, {1.0f, 1.0f, 1.0f}, 1425.f};\n\nconstexpr Sphere spheres[] = {\n {{-3.f, 0.f, -16.f}, 2.f, ivory},\n {{-1.0f, -1.5f, -12.f}, 2.f, glass},\n {{ 1.5f, -0.5f, -18.f}, 3.f, red_rubber},\n {{ 7.f, 5.f, -18.f}, 4.f, mirror}\n};\n\nconstexpr vec3 lights[] = {\n {-20.f, 20.f, 20.f},\n { 30.f, 50.f, -25.f},\n { 30.f, 20.f, 30.f}\n};\n\n// -------------------- Ray / intersection helpers --------------------\n\nvec3 reflect(const vec3 &I, const vec3 &N) {\n return I - N * (2.f * (I * N));\n}\n\nvec3 refract(const vec3 &I, const vec3 &N, const float eta_t, const float eta_i = 1.f) {\n float cosi = - std::max(-1.f, std::min(1.f, I * N));\n if (cosi < 0.f)\n return refract(I, -N, eta_i, eta_t);\n\n float eta = eta_i / eta_t;\n float k = 1.f - eta*eta*(1.f - cosi*cosi);\n if (k < 0.f) {\n return {1.f, 0.f, 0.f}; // same hack as original code\n }\n return I*eta + N*(eta*cosi - std::sqrt(k));\n}\n\nstd::tuple<bool, float> ray_sphere_intersect(const vec3 &orig, const vec3 &dir, const Sphere &s) {\n vec3 L = s.center - orig;\n float tca = L * dir;\n float d2 = L * L - tca*tca;\n float r2 = s.radius * s.radius;\n if (d2 > r2) return {false, 0.f};\n float thc = std::sqrt(r2 - d2);\n float t0 = tca - thc;\n float t1 = tca + thc;\n if (t0 > 0.001f) return {true, t0};\n if (t1 > 0.001f) return {true, t1};\n return {false, 0.f};\n}\n\nstd::tuple<bool, vec3, vec3, Material> scene_intersect(const vec3 &orig, const vec3 &dir) {\n vec3 pt, N;\n Material material;\n\n float nearest_dist = 1e10f;\n\n // Checkerboard plane y = -4\n if (std::abs(dir.y) > 0.001f) {\n float d = -(orig.y + 4.f) / dir.y;\n vec3 p = orig + dir * d;\n if (d > 0.001f && d < nearest_dist && std::abs(p.x) < 10.f && p.z < -10.f && p.z > -30.f) {\n nearest_dist = d;\n pt = p;\n N = {0.f, 1.f, 0.f};\n material.diffuse_color =\n ( (int(0.5f*pt.x + 1000.f) + int(0.5f*pt.z)) & 1 )\n ? vec3{0.3f, 0.3f, 0.3f}\n : vec3{0.3f, 0.2f, 0.1f};\n }\n }\n\n for (const Sphere &s : spheres) {\n auto [intersection, d] = ray_sphere_intersect(orig, dir, s);\n if (!intersection || d > nearest_dist)\n continue;\n nearest_dist = d;\n pt = orig + dir * nearest_dist;\n N = (pt - s.center).normalized();\n material = s.material;\n }\n\n return {nearest_dist < 1000.f, pt, N, material};\n}\n\nvec3 cast_ray(const vec3 &orig, const vec3 &dir, const int depth = 0) {\n auto [hit, point, N, material] = scene_intersect(orig, dir);\n if (depth > 4 || !hit)\n return {0.2f, 0.7f, 0.8f};\n\n vec3 reflect_dir = reflect(dir, N).normalized();\n vec3 refract_dir = refract(dir, N, material.refractive_index).normalized();\n vec3 reflect_color = cast_ray(point, reflect_dir, depth + 1);\n vec3 refract_color = cast_ray(point, refract_dir, depth + 1);\n\n float diffuse_light_intensity = 0.f;\n float specular_light_intensity = 0.f;\n\n for (const vec3 &light : lights) {\n vec3 light_dir = (light - point).normalized();\n auto [shadow_hit, shadow_pt, trashnrm, trashmat] = scene_intersect(point, light_dir);\n if (shadow_hit && (shadow_pt - point).norm() < (light - point).norm())\n continue;\n\n diffuse_light_intensity += std::max(0.f, light_dir * N);\n specular_light_intensity += std::pow(std::max(0.f, -reflect(-light_dir, N) * dir),\n material.specular_exponent);\n }\n\n vec3 diffuse = material.diffuse_color * (diffuse_light_intensity * material.albedo[0]);\n vec3 specular = vec3{1.f, 1.f, 1.f} * (specular_light_intensity * material.albedo[1]);\n vec3 reflectc = reflect_color * material.albedo[2];\n vec3 refractc = refract_color * material.albedo[3];\n\n return diffuse + specular + reflectc + refractc;\n}\n\n// -------------------- Render into framebuffer (no I/O) --------------------\n\nconstexpr int IMAGE_WIDTH = 1024;\nconstexpr int IMAGE_HEIGHT = 768;\nconstexpr float FOV = 1.05f; // ~60° in radians\n\nstd::vector<vec3> render_frame()\n{\n std::vector<vec3> framebuffer(IMAGE_WIDTH * IMAGE_HEIGHT);\n\n for (int pix = 0; pix < IMAGE_WIDTH * IMAGE_HEIGHT; ++pix) {\n float dir_x = (pix % IMAGE_WIDTH + 0.5f) - IMAGE_WIDTH / 2.f;\n float dir_y = -(pix / IMAGE_WIDTH + 0.5f) + IMAGE_HEIGHT / 2.f;\n float dir_z = -IMAGE_HEIGHT / (2.f * std::tan(FOV / 2.f));\n\n vec3 dir = vec3{dir_x, dir_y, dir_z}.normalized();\n framebuffer[pix] = cast_ray(vec3{0.f, 0.f, 0.f}, dir);\n }\n\n return framebuffer;\n}\n",
2929
"metadata": {
30-
"trusted": true
30+
"trusted": true,
31+
"vscode": {
32+
"languageId": "c++"
33+
}
3134
},
3235
"outputs": [],
3336
"execution_count": 1
@@ -37,7 +40,10 @@
3740
"cell_type": "code",
3841
"source": "#include <string>\n#include <fstream>\n#include <sstream>\n#include <vector>\n#include <cstdint>\n#include <iostream>\n\n#include \"nlohmann/json.hpp\"\n#include \"xeus/xbase64.hpp\"\n#include \"xcpp/xdisplay.hpp\"\n\nnamespace nl = nlohmann;\n\n// ---------- Jupyter display helper: BMP file -> notebook ----------\n\nnamespace im\n{\n struct image\n {\n explicit image(const std::string& filename)\n {\n std::ifstream fin(filename, std::ios::binary);\n m_buffer << fin.rdbuf();\n }\n std::stringstream m_buffer;\n };\n\n nl::json mime_bundle_repr(const image& i)\n {\n nl::json bundle = nl::json::object();\n bundle[\"image/bmp\"] = xeus::base64encode(i.m_buffer.str());\n return bundle;\n }\n}\n\nvoid display_bmp_in_notebook(const std::string& filename)\n{\n im::image img(filename);\n xcpp::display(img);\n}\n\n// ---------- Minimal 24-bit BMP writer ----------\n\nvoid write_bmp(const std::string& filename, const std::vector<vec3>& framebuffer)\n{\n if (framebuffer.size() != static_cast<std::size_t>(IMAGE_WIDTH * IMAGE_HEIGHT)) {\n std::cerr << \"[ERROR] framebuffer size mismatch\\n\";\n return;\n }\n\n const int width = IMAGE_WIDTH;\n const int height = IMAGE_HEIGHT;\n const int bytes_per_pixel = 3; // 24-bit RGB\n const int row_stride = width * bytes_per_pixel;\n const int padded_row_stride = (row_stride + 3) & ~3; // rows padded to multiple of 4 bytes\n\n const std::uint32_t file_size = 54 + padded_row_stride * height;\n\n unsigned char file_header[14] = {\n 'B','M', // signature\n 0,0,0,0, // file size\n 0,0, 0,0, // reserved\n 54,0,0,0 // pixel data offset\n };\n\n unsigned char dib_header[40] = {\n 40,0,0,0, // DIB header size\n 0,0,0,0, // width\n 0,0,0,0, // height\n 1,0, // color planes\n 24,0, // bits per pixel\n 0,0,0,0, // compression (none)\n 0,0,0,0, // image size (can be 0 for BI_RGB)\n 0x13,0x0B,0,0, // horizontal resolution (2835 px/m)\n 0x13,0x0B,0,0, // vertical resolution\n 0,0,0,0, // colors in palette\n 0,0,0,0 // important colors\n };\n\n // fill file size\n file_header[ 2] = (unsigned char)( file_size );\n file_header[ 3] = (unsigned char)( file_size >> 8 );\n file_header[ 4] = (unsigned char)( file_size >> 16 );\n file_header[ 5] = (unsigned char)( file_size >> 24 );\n\n // fill width & height\n int w = width;\n int h = height;\n dib_header[ 4] = (unsigned char)( w );\n dib_header[ 5] = (unsigned char)( w >> 8 );\n dib_header[ 6] = (unsigned char)( w >> 16 );\n dib_header[ 7] = (unsigned char)( w >> 24 );\n\n dib_header[ 8] = (unsigned char)( h );\n dib_header[ 9] = (unsigned char)( h >> 8 );\n dib_header[10] = (unsigned char)( h >> 16 );\n dib_header[11] = (unsigned char)( h >> 24 );\n\n std::ofstream out(filename, std::ios::binary);\n if (!out) {\n std::cerr << \"[ERROR] cannot open \" << filename << \" for writing\\n\";\n return;\n }\n\n out.write(reinterpret_cast<char*>(file_header), sizeof(file_header));\n out.write(reinterpret_cast<char*>(dib_header), sizeof(dib_header));\n\n std::vector<unsigned char> row(padded_row_stride);\n\n // BMP stores rows bottom-up\n for (int y = 0; y < height; ++y) {\n int by = height - 1 - y; // flip vertically\n for (int x = 0; x < width; ++x) {\n const vec3& c = framebuffer[by * width + x];\n\n float maxc = std::max(1.f, std::max(c[0], std::max(c[1], c[2])));\n unsigned char r = static_cast<unsigned char>(255.f * c[0] / maxc);\n unsigned char g = static_cast<unsigned char>(255.f * c[1] / maxc);\n unsigned char b = static_cast<unsigned char>(255.f * c[2] / maxc);\n\n // BMP is BGR\n int idx = x * 3;\n row[idx + 0] = b;\n row[idx + 1] = g;\n row[idx + 2] = r;\n }\n // zero padding already in vector's default-initialized bytes\n out.write(reinterpret_cast<char*>(row.data()), padded_row_stride);\n }\n}\n\n// ---------- Glue: framebuffer -> BMP -> notebook ----------\n\nvoid display_framebuffer_as_bmp(const std::vector<vec3>& framebuffer,\n const std::string& filename = \"raytrace.bmp\")\n{\n write_bmp(filename, framebuffer);\n display_bmp_in_notebook(filename);\n std::remove(filename.c_str());\n}",
3942
"metadata": {
40-
"trusted": true
43+
"trusted": true,
44+
"vscode": {
45+
"languageId": "c++"
46+
}
4147
},
4248
"outputs": [],
4349
"execution_count": 2
@@ -47,7 +53,10 @@
4753
"cell_type": "code",
4854
"source": "auto fb = render_frame();\ndisplay_framebuffer_as_bmp(fb);",
4955
"metadata": {
50-
"trusted": true
56+
"trusted": true,
57+
"vscode": {
58+
"languageId": "c++"
59+
}
5160
},
5261
"outputs": [
5362
{
@@ -65,7 +74,10 @@
6574
"cell_type": "code",
6675
"source": "",
6776
"metadata": {
68-
"trusted": true
77+
"trusted": true,
78+
"vscode": {
79+
"languageId": "c++"
80+
}
6981
},
7082
"outputs": [],
7183
"execution_count": null

0 commit comments

Comments
 (0)