# HG changeset patch
# User John Tsiombikas <nuclear@member.fsf.org>
# Date 1414537718 -7200
# Node ID 62a37bd5bc7477e6a9d131059b7afbd9f68c3471
# Parent  256d8fcf02f1d2ae796e45bb13e433c7ba330148
ok oculus2 test now works on linux with SDK 0.4.3 experimental.

diff -r 256d8fcf02f1 -r 62a37bd5bc74 src/main.c
--- a/src/main.c	Mon Oct 27 04:20:07 2014 +0200
+++ b/src/main.c	Wed Oct 29 01:08:38 2014 +0200
@@ -54,6 +54,9 @@
 static ovrSizei eyeres[2];
 static ovrEyeRenderDesc eye_rdesc[2];
 static ovrGLTexture fb_ovr_tex[2];
+static union ovrGLConfig glcfg;
+static unsigned int distort_caps;
+static unsigned int hmd_caps;
 
 static unsigned int chess_tex;
 
@@ -83,8 +86,7 @@
 int init(void)
 {
 	int i, x, y;
-	unsigned int flags, dcaps;
-	union ovrGLConfig glcfg;
+	unsigned int flags;
 
 	/* libovr must be initialized before we create the OpenGL context */
 	ovr_Initialize();
@@ -137,7 +139,7 @@
 		fb_ovr_tex[i].OGL.Header.TextureSize.h = fb_tex_height;
 		/* this next field is the only one that differs between the two eyes */
 		fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.x = i == 0 ? 0 : fb_width / 2.0;
-		fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.y = fb_tex_height - fb_height;
+		fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.y = 0;
 		fb_ovr_tex[i].OGL.Header.RenderViewport.Size.w = fb_width / 2.0;
 		fb_ovr_tex[i].OGL.Header.RenderViewport.Size.h = fb_height;
 		fb_ovr_tex[i].OGL.TexId = fb_tex;	/* both eyes will use the same texture id */
@@ -175,15 +177,16 @@
 	}
 
 	/* enable low-persistence display and dynamic prediction for lattency compensation */
-	ovrHmd_SetEnabledCaps(hmd, ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction);
+	hmd_caps = ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction;
+	ovrHmd_SetEnabledCaps(hmd, hmd_caps);
 
 	/* configure SDK-rendering and enable chromatic abberation correction, vignetting, and
 	 * timewrap, which shifts the image before drawing to counter any lattency between the call
 	 * to ovrHmd_GetEyePose and ovrHmd_EndFrame.
 	 */
-	dcaps = ovrDistortionCap_Chromatic | ovrDistortionCap_Vignette | ovrDistortionCap_TimeWarp |
+	distort_caps = ovrDistortionCap_Chromatic | ovrDistortionCap_Vignette | ovrDistortionCap_TimeWarp |
 		ovrDistortionCap_Overdrive;
-	if(!ovrHmd_ConfigureRendering(hmd, &glcfg.Config, dcaps, hmd->DefaultEyeFov, eye_rdesc)) {
+	if(!ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc)) {
 		fprintf(stderr, "failed to configure distortion renderer\n");
 	}
 
@@ -225,10 +228,28 @@
 		SDL_GetWindowPosition(win, &prev_x, &prev_y);
 		SDL_SetWindowPosition(win, hmd->WindowsPos.x, hmd->WindowsPos.y);
 		SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
+
+#ifdef OVR_OS_LINUX
+		/* on linux for now we have to deal with screen rotation during rendering. The docs are promoting
+		 * not rotating the DK2 screen globally
+		 */
+		glcfg.OGL.Header.RTSize.w = hmd->Resolution.h;
+		glcfg.OGL.Header.RTSize.h = hmd->Resolution.w;
+
+		distort_caps |= ovrDistortionCap_LinuxDevFullscreen;
+		ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
+#endif
 	} else {
 		/* return to windowed mode and move the window back to its original position */
 		SDL_SetWindowFullscreen(win, 0);
 		SDL_SetWindowPosition(win, prev_x, prev_y);
+
+#ifdef OVR_OS_LINUX
+		glcfg.OGL.Header.RTSize = hmd->Resolution;
+
+		distort_caps &= ~ovrDistortionCap_LinuxDevFullscreen;
+		ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
+#endif
 	}
 }
 
@@ -246,6 +267,8 @@
 	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
+	glUseProgram(0);
+
 	/* for each eye ... */
 	for(i=0; i<2; i++) {
 		ovrEyeType eye = hmd->EyeRenderOrder[i];
@@ -294,13 +317,18 @@
 	 * compensated for lens distortion and chromatic abberation onto the HMD screen.
 	 */
 	glBindFramebuffer(GL_FRAMEBUFFER, 0);
-	glViewport(0, 0, win_width, win_height);
 
 	ovrHmd_EndFrame(hmd, pose, &fb_ovr_tex[0].Texture);
 
 	assert(glGetError() == GL_NO_ERROR);
 }
 
+void reshape(int x, int y)
+{
+	win_width = x;
+	win_height = y;
+}
+
 void draw_scene(void)
 {
 	int i;
@@ -459,6 +487,12 @@
 		}
 		break;
 
+	case SDL_WINDOWEVENT:
+		if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
+			reshape(ev->window.data1, ev->window.data2);
+		}
+		break;
+
 	default:
 		break;
 	}
@@ -482,6 +516,7 @@
 			return -1;
 
 		case ' ':
+		case 'r':
 			/* allow the user to recenter by pressing space */
 			ovrHmd_RecenterPose(hmd);
 			break;
@@ -491,6 +526,30 @@
 			toggle_hmd_fullscreen();
 			break;
 
+		case 'v':
+			distort_caps ^= ovrDistortionCap_Vignette;
+			printf("Vignette: %s\n", distort_caps & ovrDistortionCap_Vignette ? "on" : "off");
+			ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
+			break;
+
+		case 't':
+			distort_caps ^= ovrDistortionCap_TimeWarp;
+			printf("Time-warp: %s\n", distort_caps & ovrDistortionCap_TimeWarp ? "on" : "off");
+			ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
+			break;
+
+		case 'o':
+			distort_caps ^= ovrDistortionCap_Overdrive;
+			printf("OLED over-drive: %s\n", distort_caps & ovrDistortionCap_Overdrive ? "on" : "off");
+			ovrHmd_ConfigureRendering(hmd, &glcfg.Config, distort_caps, hmd->DefaultEyeFov, eye_rdesc);
+			break;
+
+		case 'l':
+			hmd_caps ^= ovrHmdCap_LowPersistence;
+			printf("Low-persistence display: %s\n", hmd_caps & ovrHmdCap_LowPersistence ? "on" : "off");
+			ovrHmd_SetEnabledCaps(hmd, hmd_caps);
+			break;
+
 		default:
 			break;
 		}