The ‘globe.cpp’ demo An introduction to the application of basic raytracing principles Setting the scene • Our example depicts a sphere and a plane • A single monochromatic point light-source • Objects are: – Globe: a sphere of radius 100, center=(0,0,0) – Table: a plane tangent to sphere at (0,-100,0) – Light: a point source located at (600,800,-800) – Eye: a viewer who is positioned at (0,0,-1800) – Poly: a square, for table’s edges (side = 670) Positioning the screen-image • Graphics display-mode is 259 (= 0x103) – int hres = 800, vres = 600; – unsigned char *vram; (256 colors) • Move coordinate-origin to screen’s center: – float transX = hres/2, transY = vres/2; • Magnify image by 25% (i.e., divide by 0.8) – float scaleX = 0.8, transY = -0.8; • scale-factor for Y: negative “flips” up/down Illumination coefficients • 8bpp color: allows 64 different intensities • We apportioned these as follows: – Iambient = 16.0 – Idiffuse = 24 .0 – Ispecular = 24.0 (= 1/4 of range 64) (= 3/8 of range 64) (= 3/8 of range 64) • We decided proportions by “trial-and-error” ambient diffuse specular Maximum of 64 distinct intensity-levels Front view light-source y-axis globe x-axis table Top view light globe x-axis table eye z-axis Light-beam hits table light eye table screen-pixel view-plane Light-beam hits globe light screen-pixel eye table view-plane shadow Ambient intensity • The idea of ‘ambient light’ is that it’s a low level of background illumination, traveling in all directions with equal intensity • We model this component as a constant Diffuse reradiation • The idea is that the light rays which aren’t absorbed when they hit a matte surface are redirected away from that surface in a manner which obeys Lambert’s Law: • Intensity is proportional to area subtended Illustration of Law this angle is smaller (so less energy reaches pixel) light-energy radiates outward in all directions pixels of equal area this angle is larger (so more energy reaches pixel) Area proportional to cosine surface-normal ray-direction pixel-area vector-angle A bigger vector-angle would have a smaller cosine Diffuse reflectivity Fewer rays are incident with a given surface-area if the surface is tilted at an angle with respect to the incoming light’s direction A Ө A cos Ө So where there is less light-energy that reaches a surface, there will be less light-energy for this surface to reradiate Specular reflection • The idea is that a surface that’s “shiny” will reflect light-rays in roughly the same way as a mirror does, obeying Fermat’s Law (but also allowing for some “scallering”) • Angle-of-incidence = Angle-of-reflection mirror-like surface angle-of-incidence angle-of-reflection “Reflecting” a vector r + s = 2p n surface-normal vector toward light-source s p r vector reflected off shiny surface surface p is projection of s along n Vector-projection • From elementary linear algebra: p = projn( s ) = ( s•n / n•n )n • So we can compute r from s and n : r = -s + 2( s•n / n•n )n “Scattering” semi-shiny surface very shiny suface Use a power of the cosine • Remember that (for acute angles) we have 0 < cos(θ) < 1 • So powers of cos(θ) are between 0 and 1 • But higher powers have sharper graphs! • We can “model” the shinyness of surfaces by the power of the cosine we choose: the higher the power, the shininer the surface Summing up • Intensity of achromatic light at a pixel is a sum of three illumination components: intensity = ambient + diffuse + specular • ambient: (this will just be a constant) • diffuse: (proportional to dot-product: s•n) • specular: (proportional to cos5(θ)=(rr•se)5) In-class experimentation • The ‘globe.cpp’ demo-program produces a monochromatic (i.e., gray-scale) image • A range of the DAC controller’s 256 color registers is programmed for 64 different intensities of “white” illunimation • But we can also create varying intensities of some other RGB color-combinations • Exercise: Add color to the globe and table