How do I implement the Oculus Rift distortion shader?

AKA: How do I use ofxOculusRiftDK2 with stereo video cameras instead of a rendered 3D scene?

The ofxOculusRiftDK2 addon usually works by rendering a single scene from two distinct camera points, then compositing those views into a single window after applying a shader to fix barrel distortion and vignetting.

I’m trying to render two video feeds, one to each eye, while simultaneously getting head position data from the Rift. I’ve accomplished this by pretty much ignoring the addon’s built-in leftEye.begin(), leftEye.end() funtions, but now I need to use the distortion shader to fix my video.

I’ve tried fighting my way through the Introducing Shaders tutorial a number of times, but the information there doesn’t quite sync up with what’s going on in the addon’s source. How might I go about using the shaders on my own?

MORE INFO:

The addon is doing this to apply the shader to both eyes (there might be more to it):

ofDisableDepthTest();
ofEnableAlphaBlending();
distortionShader.begin();
distortionShader.setUniformTexture("Texture", renderTarget.getTextureReference(), 1);
distortionShader.setUniform2f("TextureScale", renderTarget.getTextureReference().getWidth(), renderTarget.getTextureReference().getHeight());

for (int eyeIndex = 0; eyeIndex < 2; eyeIndex++) {
	// Setup shader constants
	distortionShader.setUniform2f("EyeToSourceUVScale", UVScaleOffset[eyeIndex][0].x, UVScaleOffset[eyeIndex][0].y);
	distortionShader.setUniform2f("EyeToSourceUVOffset", UVScaleOffset[eyeIndex][1].x, UVScaleOffset[eyeIndex][1].y);
	ovrMatrix4f timeWarpMatrices[2];
	ovrHmd_GetEyeTimewarpMatrices(hmd, (ovrEyeType) eyeIndex, headPose[eyeIndex], timeWarpMatrices);
	distortionShader.setUniformMatrix4f("EyeRotationStart", toOf(timeWarpMatrices[0]) );
	distortionShader.setUniformMatrix4f("EyeRotationEnd", toOf(timeWarpMatrices[1]) );

	eyeMesh[eyeIndex].draw();
}
distortionShader.end();

As of now, I’m drawing each eye by drawing to an FBO in ofApp::update(), then drawing the FBO in ofApp::draw():

leftEyeVideo.update();

if (leftEyeVideo.isFrameNew()) {
	leftFbo.begin();
	{
		ofTranslate(leftFbo.getWidth() / 2, leftFbo.getHeight() / 2);
		ofRotate(-90);
		ofScale(leftFbo.getWidth() / leftEyeVideo.getHeight(), leftFbo.getWidth() / leftEyeVideo.getHeight());
		leftEyeVideo.draw(0, 0);
	}
	leftFbo.end();
}
1 Like

I have the same question here!
As far as I know there is also a color aberration correction implemented in the oculus SDK. I haven’t’ found how to apply the distortions to the fbos os I am using the oculus sdk instead.
I am working on a similar thing as you for the Machine to be Another