Coder Social home page Coder Social logo

miloyip / light2d Goto Github PK

View Code? Open in Web Editor NEW
844.0 62.0 123.0 10.76 MB

Light rendering in 2D

Makefile 0.88% C++ 9.54% C 76.02% TeX 13.56%
graphics-programming graphics2d global-illumination monte-carlo-integration signed-distance-field constructive-solid-geometries

light2d's Introduction

light2d

This project illustrates light rendering in 2D with C.

All samples output PNGs with svpng.

License: public domain.

Basic

Use Monte Carol integration and ray marching of signed distance field (SDF) to render a emissive circle.

Source code: basic.c

Uniform sampling (64 samples per pixel):

Uniform sampling with different number of samples per pixel:

Stratified sampling (64 samples per pixel):

Jittered sampling (64 samples per pixel):

Various sampling method side-by-side comparison (64 samples per pixel)::

Constructive Solid Geometry

Source code: csg.c

Use union operation for creating multiple shapes:

Various CSG operations on two circles:

Shapes

Source code: shapes.c

Examples of various shapes defined by SDF:

Reflection

Source code: reflection.c

Test scene with two boxes:

Visualization of SDF gradient, which is approximated by central difference:

Reflection via recursive tracing:

Concave mirror scene generates caustics effect:

Refraction

Source code: refraction.c

Applying Snell's law to compute refraction direction. Total internal reflection is also handled.

Test scenes:

Fresnel Reflectance

Source code: fresnel.c

Applying Fresnel equation to compute reflectance of dielectric medium.

Without Fresnel:

With Fresnel term:

Beer-Lambert

Source code: beerlambert.c beerlambert_color.c

Applying Beer-Lambert law to simulate absorption of light in medimum.

light2d's People

Contributors

miloyip avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

light2d's Issues

Makefile无法正常工作

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: $(TARGETS) $(OUTPUTS)

%: %.c
gcc -lm -Wall -O3 -o $@ $&lt;

%.png: %
sh -c "time ./$<"

clean:
rm $(TARGETS) *.png

Lack of proper gamma compression for the output

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:
refraction size 512 N=4096 aa=0 4

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.