miloyip / light2d Goto Github PK
View Code? Open in Web Editor NEWLight rendering in 2D
Light rendering in 2D
There's a major but common mistake which makes all the images look wrong. The images are naturally calculated using linear light values but saved as 8-bit sRGB values without any correct conversion from linear values to sRGB (gamma compressed) values. Here's how it should look once that problem is fixed:
This is quite radically different, and you can now see how the beams that cross into the shadows appear as bright as they should. The way to do it is to replace this:
p[0] = p[1] = p[2] = (int)(fminf(sample((float)x / W, (float)y / H) * 255.0f, 255.0f));
with this:
p[0] = p[1] = p[2] = (int)(255. * lsrgb(fminf(sample((float)x / W, (float)y / H), 1.f)));
and add somewhere a function lsrgb
that does this:
double lsrgb(double linear) // converts a [0.0, 1.0] linear value into a [0.0, 1.0] sRGB value
{
if (linear <= 0.0031308)
return linear * 12.92;
else
return 1.055 * pow(linear, 1.0/2.4) - 0.055;
}
Btw in the image above I also added Gaussian antialiasing which is achieved by adding a Gaussian jitter in the sample()
function to x
and y
independently.
在Manjaro上会报错:
[wurui@computer light2d]$ make
cc basic.c -o basic
/tmp/ccb1Fj9Q.o:在函数‘circleSDF’中:
basic.c:(.text+0x104e):对‘sqrtf’未定义的引用
/tmp/ccb1Fj9Q.o:在函数‘sample’中:
basic.c:(.text+0x1189):对‘sinf’未定义的引用
basic.c:(.text+0x119e):对‘cosf’未定义的引用
/tmp/ccb1Fj9Q.o:在函数‘main’中:
basic.c:(.text+0x1264):对‘fminf’未定义的引用
collect2: 错误:ld 返回 1
make: *** [<内置>:basic] 错误 1
必须要改成这样,才能正常工作:
TARGETS=basic csg
OUTPUTS=$(addsuffix .png, $(TARGETS))
all: $(TARGETS)
test:
%: %.c
gcc -lm -Wall -O3 -o
%.png: %
sh -c "time ./$<"
clean:
rm $(TARGETS) *.png
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.