Master the fundamental concepts of software rasterizer through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksIn 1975, Bui Tuong Phong published a model that broke real-time lighting into three terms. It is empirical, not physically based, but fast and convincing enough to dominate games for three decades before PBR took over.
max(0, N dot L) where N is surface normal, L is light directionThe Blinn-Phong specular term uses a half-angle vector instead of a reflection vector:
cLoading…
For example, a surface with normal (0,0,1), light at (2,2,5), and eye at (0,0,5) produces a bright specular lobe when N dot H is near 1. Diffuse depends only on light angle; specular depends on both light and view angles.
shininess controls highlight tightness (32 is moderately glossy)You will implement Blinn-Phong lighting for a single surface point with given normal, light position, eye position, and material constants. This task asks you to compute L, V, H, and print the final RGB color. Even PBR renderers retain the same N dot L diffuse structure introduced here.
Shade surface points with the Phong and Blinn-Phong reflection models, using point lights with colour and distance attenuation. For each light, print the geometric terms (distance, N·L, and the specular cosine R·V or N·H) and the resulting diffuse and specular contributions. Then print the final colour, both unclamped and as 8-bit RGB.
You may use <math.h>: the task compiles with -lm.
# starts a comment line.
cLoading…
With N normalized, V = normalize(camera - P), and for each light L = normalize(light - P) and d = |light - P|:
max(0, N·L).N·L > 0:
H = normalize(L + V) and s = max(0, N·H)^shininess.R = 2(N·L)N - L and s = max(0, R·V)^shininess.N·L ≤ 0, the cosine is reported as 0.ka·albedoₖ + Σ att·lightₖ·(kd·diffuse·albedoₖ + ks·s). Specular highlights take the light's colour, not the surface's.(int)(c·255 + 0.5).cLoading…
%.4f, and a value with |x| < 0.00005 prints 0.0000.kd·diffuse and "specular" is ks·s, both before attenuation and colour. The label is R.V under Phong. A light with N·L ≤ 0 adds (facing away).material: KA KD KS SHININESS(1-256), albedo: R G B (0-1)light: NAME X Y Z R G B, light: at most 4 lightsattenuation: CONSTANT LINEAR QUADRATIC (not all zero)camera: X Y Z, model: phong|blinnshade: PX PY PZ NX NY NZ, shade: the normal must not be zerounknown command XInput:
cLoading…
Output:
cLoading…
N·L > 0, so a surface facing away from a light gets no highlight from it.Hidden tests cover two coloured lights with attenuation (one behind the surface), a tilted normal, Phong versus Blinn-Phong on the same point, over-exposure clamping, a zero normal, and argument errors.