Tags

, , , , , , , , , , , , ,

Arkwood stood with a doorknob in his hand. It was not attached to a door.

‘Is that my gold knob!’ I exclaimed, tossing the broadsheet onto the carpet.

‘It is your gold knob, yes. I wanted to get a closer look at it. At the material.’

He was referring to my post, Materials and light on the Oculus Rift, where I had turned a plain old cube into sparkling gold.

My C++ Microsoft Visual Studio application makes use of OpenGL graphics library and the Oculus SDK for Windows to render objects onto my Oculus Rift virtual reality headset. We can employ a Phong lighting model of ambient, diffuse and specular components to turn each object into yellow rubber or gold or whatever material we choose.

It blew Arkwood’s tiny mind. With a screwdriver I reintroduced the knob to the door.

‘Never mind that. I have something more amazing for you!’

The Learn OpenGL Lighting maps article explains that an object does not usually consist of a single material, but of many. We can use a diffuse map to provide each fragment of our object with a diffuse (and ambient) colour. And we can use a specular map to determine which parts of our object are shiny.

I am loading objects into my virtual world via wavefront obj files, spat out by Blender (the 3D creation suite). I add the diffuse map to my cube in Blender as a simple texture. As for the specular map, I will simply edit the associated mtl file to place it alongside the diffuse map:

# Blender MTL File: 'mycube_maps_ds.blend'
# Material Count: 1

newmtl CubeFace
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd container2.png
map_Ks container2_specular.png

With both the diffuse and specular maps at my disposal in the C++ app, I simply update the code as per the Learn OpenGL article. My fragment shader samples the maps for the lighting calculations of each fragment.

No longer do I have a crappy gold cube (it wasn’t real gold anyway). The diffuse map has transformed my cube into a detailed wooden container. And the specular map has given the steel borders a rather charming shine.

See how the light source near the ceiling is sparkling off the steel borders of the container. The specular map allows us to treat the steel as shiny and the wood as not.

Our material shininess is set to 256, and our light has ambient 0.2, diffuse 0.5 and specular 1.0. Note that the diffuse and specular maps are available from the Learn OpenGL article.

I told Arkwood the good news. I wish I hadn’t. For twenty minutes later he came back into the lounge, this time holding the entire front door under his arm.

‘I want to see its steel border and wooden surface shine under the light bulb,’ he said, grinning wildly.

I took the door from him, turned it so I was holding it like a huge cricket bat in my hands, and promptly swung it smack against his face. He fell like a sack of potatoes.

Ciao!

Advertisements