Tag Archives: illumination

Dot Product

Dot product is a cheap and useful function in many situations. It calculate the cosAngle to two unit vectors which is 1 when acute, 0 to -1 for obtuse angle. These are some applications which I found very useful.

Gameplay
Check if two unit are facing each other by dot product their forwardVec, for 3D you can optionally ignore the height component for the vectors.

For airplane game, prevent a plane from over pitching by checking the planeDir dot worldUpVec and worldDownVec.

Test if an unit has passed a waypoint, assuming the unit is moving forward. Do a distance check to ensure the unit is near the waypoint. do a dot product between the unit forward vector and the direction to the waypoint, if the angle is obtuse, mean the unit has pass the waypoint.

For terrain dot product normals of two face to find out the steepness of a slope.

Graphics
NdotL for lambert diffuse
NdotH for blinn specular
RdotV for phong specular

NdotV to detect edges of the model.

Lerp

Linear Interpolation

It would commonly take in 3 scaler or 2 vector and a scaler, and return the result of the formula

x + s(y-x).

Gameplay

I often find this function useful for cheap generation of points along a relatively straight line. It is also very useful to calculate the display of health/power/magic bar, eg

healthBarLength = lerp(healthBarStart, healthBarMaxLength, %healthLeft)

In UI it is a very good helper function to convert relative screencoord to absolute screencoord

Graphics

It is frequently used for alpha blending, blending of normal maps, terrain splatting. I like to use it for signal scaling,

eg getting value max(0, NdotL) / max(0, NdotV) to scale to some other values for various effects.

Power

A Power function raised x to specific power.

It has an interesting property when x is between 0 – 1, the result will be always between 0 – 1. Do take note Power function will not preserve the sign of x, it may be necessary to calculate the sign beforehand.

What Power does is to change the curve of x to favor certain values.

This is the curve for using a joystick to aim at something. Typically when gamers want to have a fast movement on analog stick, They would push the analog stick to max. Using the Power function we can allocate 80% of the analog controls to slower movement, so gamers would have more control over aiming, yet allow the quick movement by pushing the analog stick to max.

Power is also very commonly used in graphics, We could use it to change color curves, contrast of images, fade off of lighting equation/fog/glows and many more.

Light Attenuation

Point Light Distance Attenuation

This is the commonly used formula to attenuate light by a constant, linear and quadric factor.

1 / (constantFactor + (linearFactor * disFactor) + (quadricFactor * disFactor * disFactor));

personally I dislike this formula as it is very difficult to isolate light to a particular area only.

 Spherical Light Attenuation

This is something I use more frequently.

saturate(1.0 – smoothstep(lightAttMinRange, lightMaxRange, lightDis));

This will only start attenuate at a specific distance and will have a smooth fall off to the lightMaxRange. It really make lights placement in room simpler.

Spot Light Attenuation

float spot = dot(spotlightDir, lightDir);

float outerAngleFactor = cos(outerAngleFactor / 2);

float innerAngleFactor  = cos(innerAngleFactor / 2);

float attenuation = (spot >= outerAngleFactor) * pow((spot – outerAngleFactor) / (innerAngleFactor – outerAngleFactor), falloff);

if you are using ogre, outerAngleFactor and attenuation  is calculated for you. if you are using lightVec from NdotL, do remember to inverse the direction.