Trace the Ray – Part 4 (Textures, Perlin Noise, Bump Mapping)

I am back again, this time with textures!

In my fourth assignment, my task was to implement:

  • Texture mapping (Triangle and Sphere)
  • Perlin noise implementation
  • Bump mapping

Apart from those capabilities, I have also added support for backgroundTexture, where a color read from that background texture is used to color the current pixel when a ray cast from that pixel does not hit any object in the scene.

Texture Mapping

From now on, we have the Textures tag in our input scene files. A texture has:

  • ImageName: path to the image. Use perlin noise instead of an image if the value of this tag is “perlin”.
  • Interpolation: nearest and bilinear.
    • Nearest: If hit (1.2, 3.4), round to nearest pixel integer coordinate (1, 3) on the texture and use that color.
    • Bilinear: Do not round, but average four neighbour pixels’ texture color values.
  • DecalMode: replace_kd, replace_all, blend_kd
    • replace_kd: While calculating the diffuse color, replace kd constant with the texture color normalized to [0..1].
    • replace_all: Replace all color with texture color.
    • blend_kd: Half weight texture color, half weight diffuse color.
  • Normalizer: Normalize texture color value by that much.
  • Appearance and ScalingFactor: Used in perlin. “patch” appearance is a smooth-lined perlin, “vein” appearance is a hard-lined perlin.
  • bumpmap=”true” if bump map would be used; bumpmapMultiplier=”<float_value>” to boost the bump effect.

In Spheres, we calculate texture u, v coordinates such that the texture could map the whole surface. In Triangles, we use barycentric coordinates to calculate u and v. Here are some outputs:


sphere_texture_blend_bilinear.xml
(800x800)
/w 8 thrd, 1MSAA

sphere_texture_replace_bilinear.xml
(800x800)
/w 8 thrd, 1MSAA

sphere_texture_replace_nearest.xml
(800x800)
/w 8 thrd, 1MSAA

skybox.xml
(1600x800)
/w 8 thrd, 100MSAA
Ground is triangle,
Scene is inside a sphere (sky).

killeroo_diffuse_specular_texture.xml
(800x800)
/w 8 thrd, 16MSAA
Walls are grid-textured.

galactica_static.xml
(1280x720)
/w 8 thrd, 36MSAA
METU logo on wings,
My name, CENK, on the tips of wings.

galactica_dynamic.xml
(1280x720)
/w 8 thrd, 64MSAA, MB
METU logo on wings,
My name, CENK, on the tips of wings.
Had some BBox vs. MB problems,
will solve them.

Perlin Noise Implementation

Please check Ken Perlin’s webpage for detailed information on Perlin Noise.

This part is where I spent most of my time for this assignement. I wrote a Perlin class which initially creates the G array, and a shuffled P array. Then for each hit point, I floored the hitpoint coordinates, built up the 3D grid cube, assigned e vectors to the vertices by selecting from G, using index array to choose an index, and finally weighted those 8 corners of the cube to get a Perlin value between [0..1].

If the appearance is chosen as “patch”, then (perlinValue + 1) / 2 is sent to the replace_kd operation as normalized texture color.

If the appearance is chosen as “vein”, then abs(perlinValue) is sent to the replace_kd operation as normalized texture color.

Below are my outputs:

ellipsoids_texture.xml
(800x800)
/w 8 thrd, 1MSAA
Left sphere is replace_all,
Right sphere is blend_kd (reddish diffuse),
Second right sphere is replace_kd,
Second left sphere is "patch" perlin.

perlin_types.xml (800x800)
/w 8 thrd, 1MSAA
Ground is blend_kd scalingFactor 0.03 
"patch" perlin triangle,
Closer left-right are scalingFactor 5
"patch" and "vein", respectively,
Middle left-right are scalingFactor 1
"patch" and "vein", respectively,
Far left-right are scalingFactor 1
"patch" and "vein", respectively.

Bump Mapping

Bump mapping is used to give the effect of bumpings on the surfaces, without actually disturbing the geometry of the surface. It is done by changing the pointing direction of the surface normal by an amount of the difference in texture color between hitpoint u, v and neightbour u, v (as if taking the derivative) when a point on the surface is hit by a ray. The more the difference we have, the more the bump is seen. Below are my outputs of Triangle and Sphere bump mappings. Some of the outputs include the use of Perlin noise and bump mapping together, where we use the difference between the perlin values of neighbour coordinates as the bump amount, and also the color.

bump_mapping_basic.xml
(800x800)
/w 8 thrd, 36MSAA
Ground triangle bumped by grid texture,
Sphere bumped by leather texture.

bump_mapping_transformed.xml
(800x800)
/w 8 thrd, 36MSAA
Ground triangle bumped by cobblestone
texture, Sphere bumped by leather
texture.

sphere_bump_nobump.xml
(800x800)
/w 8 thrd, 36MSAA
Left nobump,
Right bump.

killeroo_bump_walls.xml
(800x800)
/w 8 thrd, 16MSAA
Walls are grid bumped,
No texture & bump on killeroo.

spheres_perlin.xml
(800x800)
/w 8 thrd, 36MSAA
Left "patch" perlin,
Right "vein" perlin.

perlin_types_bump.xml
(800x800)
/w 8 thrd, 1MSAA
Same setup as perlin_types.xml above,
but with bump mapping now.

I would like to end this post by a video of sequential renders of a slightly edited (METU logo on the wings and my name, CENK, on the tips of the wings) version of Galactica spaceship Oguz hoca has shared with us, together with The Imperial March!

See you in the next post!

Happy tracing!