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

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

Now that I actually have the Rift virtual reality headset in my paws, let’s see if we can write some C++ code to add a texture to that triangle! (Okay, at least pretend to be excited).


When I think of soil, I think of those unfortunate moments I’ve failed to reach the toilet on time. But, for the purpose of this article, SOIL stands for Simple OpenGL Image Library. It will help us load our texture image into the OculusRoomTiny(GL) project.

SanD on Stack Overflow provides excellent instruction on adding SOIL to Visual Studio.

Once done, add to following to the top of the OculusRoomTiny(GL) Win32_GLAppUtil.h file:

#include <SOIL.h>

And test out SOIL by loading an image e.g.

int width, height;
unsigned char* image = SOIL_load_image("rodger_saltwash.png", &width, &height, 0, SOIL_LOAD_RGB);

Place the image you want to load in the project folder e.g. OculusSDK\Samples\OculusRoomTiny\OculusRoomTiny (GL)\Projects\Windows\VS2015

If the image does not load, Gogi says that you can use the following code to find out why:

const char* errX = SOIL_last_result();

Also, if you get the error LNK4098 defaultlib ‘MSVCRTD’ conflicts with use of other libs; use /NODEFAULTLIB:library when building OculusRoomTiny(GL) with SOIL, try changing the Runtime Library to Multi-threaded Debug (/MTd) when building SOIL, as described in David’s Blog.


Okay, so we can now load an image into the OculusRoomTiny(GL) project. Let’s create a texture with the image, and add the texture to our triangle.

The superb Learn OpenGL site by Joey de Vries has a section on textures, to help us put the code together. As a side note, there is an offline book of the Learn OpenGL site which I am currently reading through on train journeys etc. Get it. Read it. Praise it.

Back in the OculusRoomTiny(GL) Win32_GLAppUtil.h file, we add the following code to the Scene’s Init function:

GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);


int width, height;
unsigned char* image = SOIL_load_image("rodger_saltwash.png", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glBindTexture(GL_TEXTURE_2D, 0);

Lovely. Our image has been loaded and added to a texture.

I’ve done some refactoring of the Win32_GLAppUtil.h file, so as to add my own model struct for the triangle. I can simply pass the new texture into the model’s constructor, so the model can make use of it when rendering to the Rift.

Here’s the model’s Render function binding the texture:

glBindTexture(GL_TEXTURE_2D, Texture);

Note that if the image is upside down when rending, we can rework the triangle’s UV coordinates from the previous post:

Vector3f TriangleVertices[][2] =
	Vector3f(1.0f, 0.0f, -2.0f), Vector3f(0.0f, 1.0f),
	Vector3f(2.0f, 0.0f, -2.0f), Vector3f(1.0f, 1.0f),
	Vector3f(1.5f, 1.0f, -2.0f), Vector3f(0.5f, 0.0f)

Also, we may want to drop the vertex colour:

//vertex.C = 0xff202050;


Great. With our texture in place, let’s run the OculusRoomTiny(GL) project, put on our Rift headset, and eyeball our triangle in all its textured glory:


Ah, there be the inscrutable Rodger Saltwash adorning our triangle!

And there you have it. We have added an OpenGL texture to the Oculus Rift PC SDK with some C++ code. We can walk around the triangle in a virtual world and salivate.