{"id":13531532,"url":"https://github.com/ands/lightmapper","last_synced_at":"2025-05-16T05:04:13.868Z","repository":{"id":37774012,"uuid":"58688426","full_name":"ands/lightmapper","owner":"ands","description":"A C/C++ single-file library for drop-in lightmap baking. Just use your existing OpenGL renderer to bounce light!","archived":false,"fork":false,"pushed_at":"2023-03-24T08:48:21.000Z","size":1844,"stargazers_count":1474,"open_issues_count":7,"forks_count":135,"subscribers_count":67,"default_branch":"master","last_synced_at":"2025-05-16T05:03:58.698Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ands.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-05-13T01:12:46.000Z","updated_at":"2025-04-19T13:07:05.000Z","dependencies_parsed_at":"2024-01-03T04:00:54.508Z","dependency_job_id":"d8f0fb86-98cf-4933-84a9-ba88a22b5f05","html_url":"https://github.com/ands/lightmapper","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ands%2Flightmapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ands%2Flightmapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ands%2Flightmapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ands%2Flightmapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ands","download_url":"https://codeload.github.com/ands/lightmapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254471061,"owners_count":22076585,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T07:01:03.766Z","updated_at":"2025-05-16T05:04:13.844Z","avatar_url":"https://github.com/ands.png","language":"C","readme":"﻿# Lightmapper\nlightmapper.h is a C/C++ single-file library for lightmap baking by using your existing OpenGL renderer.\n\nTo paste the implementation into your project, insert the following lines:\n```\n#define LIGHTMAPPER_IMPLEMENTATION\n#include \"lightmapper.h\"\n```\n\nWhile the simple example application demonstrates the baking of ambient occlusion only, the library can also be used to precompute global illumination for static scene geometry by doing multiple bounces.\n\nAny light shapes and emissive surfaces are supported as long as you draw them with their corresponding emissive light colors.\n\nYou may want to use [thekla_atlas](https://github.com/Thekla/thekla_atlas) / [xatlas](https://github.com/jpcy/xatlas) or [uvatlas](https://github.com/microsoft/UVAtlas) to prepare your geometry for lightmapping.\n\n# Example application\n![Lightmapper Example Screenshot](https://github.com/ands/lightmapper/raw/master/example_images/example.png)\nThe provided [example application](https://github.com/ands/lightmapper/blob/master/example/example.c) should build on Windows/Linux/MacOSX.\n\nLinux dependencies for glfw: xorg-dev libgl1-mesa-dev\n```\ngit clone --recursive https://github.com/ands/lightmapper.git\ncd lightmapper/example\ncmake .\nmake\n./example\n```\n\n# Example usage\n```\nlm_context *ctx = lmCreate(\n\t64,               // hemicube rendering resolution/quality\n\t0.001f, 100.0f,   // zNear, zFar\n\t1.0f, 1.0f, 1.0f, // sky/clear color\n\t2, 0.01f          // hierarchical selective interpolation for speedup (passes, threshold)\n\t0.0f);            // modifier for camera-to-surface distance for hemisphere rendering.\n\t                  // tweak this to trade-off between interpolated vertex normal quality and other artifacts (see declaration).\nif (!ctx)\n{\n\tprintf(\"Could not initialize lightmapper.\\n\");\n\texit(-1);\n}\n\nfor (int b = 0; b \u003c bounces; b++)\n{\n\t// render all geometry to their lightmaps\n\tfor (int i = 0; i \u003c meshes; i++)\n\t{\n\t\tmemset(mesh[i].lightmap, 0, sizeof(float) * mesh[i].lightmapWidth * mesh[i].lightmapHeight * 3); // clear lightmap to black\n\t\tlmSetTargetLightmap(ctx, mesh[i].lightmap, mesh[i].lightmapWidth, mesh[i].lightmapHeight, 3);\n\t\t\n\t\tlmSetGeometry(ctx, mesh[i].modelMatrix,\n\t\t\tLM_FLOAT, (uint8_t*)mesh[i].vertices + positionOffset, vertexSize,\n\t\t\tLM_NONE , NULL, 0, // optional vertex normals for smooth surfaces\n\t\t\tLM_FLOAT, (uint8_t*)mesh[i].vertices + lightmapUVOffset, vertexSize,\n\t\t\tmesh[i].indexCount, LM_UNSIGNED_SHORT, mesh[i].indices);\n\t\n\t\tint vp[4];\n\t\tmat4 view, proj;\n\t\twhile (lmBegin(\u0026ctx, vp, \u0026view[0][0], \u0026proj[0][0]))\n\t\t{\n\t\t\t// don't glClear on your own here!\n\t\t\tglViewport(vp[0], vp[1], vp[2], vp[3]);\n\t\t\tdrawScene(view, proj);\n\t\t\tprintf(\"\\r%6.2f%%\", lmProgress(ctx) * 100.0f); // don't actually call printf that often ;) it's slow.\n\t\t\tlmEnd(\u0026ctx);\n\t\t}\n\t}\n\n\t// postprocess and upload all lightmaps to the GPU now to use them for indirect lighting during the next bounce.\n\tfor (int i = 0; i \u003c meshes; i++)\n\t{\n\t\t// you can also call other lmImage* here to postprocess the lightmap.\n\t\tlmImageDilate(mesh[i].lightmap, temp, mesh[i].lightmapWidth, mesh[i].lightmapHeight, 3);\n\t\tlmImageDilate(temp, mesh[i].lightmap, mesh[i].lightmapWidth, mesh[i].lightmapHeight, 3);\n\n\t\tglBindTexture(GL_TEXTURE_2D, mesh[i].lightmapHandle);\n\t\tglTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, mesh[i].lightmapWidth, mesh[i].lightmapHeight, 0, GL_RGB, GL_FLOAT, data);\n\t}\n}\n\nlmDestroy(\u0026ctx);\n\n// gamma correct and save lightmaps to disk\nfor (int i = 0; i \u003c meshes; i++)\n{\n\tlmImagePower(mesh[i].lightmap, mesh[i].lightmapWidth, mesh[i].lightmapHeight, 3, 1.0f / 2.2f);\n\tlmImageSaveTGAf(mesh[i].lightmapFilename, mesh[i].lightmap, mesh[i].lightmapWidth, mesh[i].lightmapHeight, 3);\n}\n```\n\n# Quality improvement\nTo improve the lightmapping quality on closed meshes it is recommended to disable backface culling and to write `(gl_FrontFacing ? 1.0 : 0.0)` into the alpha channel during scene rendering to mark valid and invalid geometry (look at [example.c](https://github.com/ands/lightmapper/blob/master/example/example.c) for more details). The lightmapper will use this information to discard lightmap texel results with too many invalid samples. These texels can then be filled in by calls to `lmImageDilate` during postprocessing.\n\n# Selective hierarchical interpolation for speedup\nThe lightmapper can do several passes traversing the geometry to render to the lightmap.\nIf interpolation passes are turned on, only every (1 \u003c\u003c interpolationPasses) texel gets rendered in the first pass.\nConsecutive passes either render or interpolate the values in between based on the interpolationThreshold value.\nIf the neighbours deviate too much or don't exist yet, the texel gets also rendered, otherwise it is simply interpolated between the neighbours.\nIf interpolationPasses or interpolationThreshold are set too high, lighting variations can be missed.\nIf they are very low, very many lightmap texels get rendered, which is very expensive compared to interpolating them.\nOn the example above this technique gives about a ~3.3x speedup without degrading the quality.\nLarger lightmaps can even get a ~10x speedup depending on the scene.\nThis image shows which texels get rendered (red) and which get interpolated (green) in the above example:\n\n![Interpolated texels](https://github.com/ands/lightmapper/raw/master/example_images/debug_interpolation.png)\n\nThis technique is also described by Hugo Elias over [here](http://web.archive.org/web/20160311085440/http://freespace.virgin.net/hugo.elias/radiosity/radiosity.htm).\n\n# Example media\nThe following video shows several lighting effects. The static/stationary and indirect lighting were precomputed with lightmapper.h:\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.youtube.com/watch?v=ukiTqj8JBKY\"\u003e\n    \u003cimg width=\"100%\" src=\"https://img.youtube.com/vi/ukiTqj8JBKY/0.jpg\" alt=\"Showing Some Editor Features (Master Thesis)\"/\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n![Sphere Light](https://github.com/ands/lightmapper/raw/master/example_images/gazebo_light.png)\n![Skybox](https://github.com/ands/lightmapper/raw/master/example_images/gazebo_skybox.png)\n![Shadows and Reflections](https://github.com/ands/lightmapper/raw/master/example_images/gazebo_shadows_reflections.png)\n\n# Thanks\n- To Ignacio Castaño and Jonathan Blow for blogging about their lightmapping system for The Witness.\n- To Hugo Elias who documented his radiosity light mapper and acceleration technique a long time ago.\n- To Sean Barrett for his library design philosophy.\n- To Nyamyam Ltd. for letting me use their engine and assets during my master's thesis for which I started my work on this library.\n- To Apoorva Joshi for trying to find a graphics driver performance anomaly.\n- To Teh_Bucket for this public domain gazebo model on OpenGameArt.org.\n- To everybody else that I forgot :)\n","funding_links":[],"categories":["Graphics","ComputerGraphics \u0026\u0026 Shadingv","图形","Libraries"],"sub_categories":["Google Analytics","C"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fands%2Flightmapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fands%2Flightmapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fands%2Flightmapper/lists"}