Tags

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

I cranked open the Oculus Rift PC SDK in my last post, and used OpenGL to render Blender shapes into a virtual world. The virtual world in question is the SDK’s OculusRoomTiny(GL) Visual Studio project.

Let’s try adding some texture to those shapes! Again, our C++ code will make use of Assimp to import a Blender 3D model into the project – but the code will be extended to handle textures for each mesh of the model.

Blender

First up, I add a cube and a cone mesh in Blender. With the help of Sri Harsha Chilakapati’s video I wrap the cube in a texture, using an image from The Inscrutable Diaries Of Rodger Saltwash. The cone I will not texture.

oculusrift_cubecone_blender

I export the meshes from Blender using the Wavefront .obj file format.

Assimp

Okay, so now we want to extend the C++ code from the previous post, so as to handle the texture we have wrapped around our cube.

I refactor my model struct to hold an array of new mesh structs, as instructed in the Learn OpenGL Mesh and Model tutorials.

We can map our cube’s texture UV coordinates from Assimp to our mesh struct. The cone does not have any texture UV coordinates, so we apply a colour to it instead:

if (mesh->mTextureCoords[0])
{
	vertex.U = mesh->mTextureCoords[0][i].x;
	vertex.V = mesh->mTextureCoords[0][i].y;
}
else
{
	vertex.U = 0.0f;
	vertex.V = 0.0f;
	vertex.C = 0xff202050;
}

We loop through all our Assimp textures by type, using SOIL (Simple OpenGL Image Library) to load and bind each image.

Oculus Rift

With the Blender exported .obj file imported into our project using Assimp, and the texture handled as per the Learn OpenGL tutorials, we are ready to run the OculusRoomTiny(GL) project.

As we render each mesh, we will bind its textures to texture units used by the fragment shader samplers.

I put on the Oculus Rift virtual reality headset and wonder at the textured cube.

oculusrift_cubecone_sdk

I can walk around the cube and admire Rodger Saltwash in his decomposed state. The ginger beard will never die! Of course, the cone hanging in the air has no texture so is simply a reddish colour.

Let’s try loading the nanosuit model mentioned in the Learn OpenGL tutorials. Our fragment shader was decorating our cone with colour:

FragColor = oColor * texture2D(Texture0, oTexCoord);

But we can drop the colour when splashing on our nanosuit diffuse texture:

FragColor = texture2D(texture_diffuse1, oTexCoord);

There stands the dude in all his glory, crushing our utilitarian furniture:

oculusrift_cubecone_nanosuit

Ciao!

Advertisements