Browse Source

HMD fixes.

Branimir Karadžić 11 years ago
parent
commit
c400562d61
3 changed files with 98 additions and 22 deletions
  1. 30 10
      examples/10-font/font.cpp
  2. 38 8
      examples/11-fontsdf/fontsdf.cpp
  3. 30 4
      examples/common/imgui/imgui.cpp

+ 30 - 10
examples/10-font/font.cpp

@@ -165,9 +165,6 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 
 
 	while (!entry::processEvents(width, height, debug, reset) )
 	while (!entry::processEvents(width, height, debug, reset) )
 	{
 	{
-		// Set view 0 default viewport.
-		bgfx::setViewRect(0, 0, 0, width, height);
-
 		// This dummy draw call is here to make sure that view 0 is cleared
 		// This dummy draw call is here to make sure that view 0 is cleared
 		// if no other draw calls are submitted to view 0.
 		// if no other draw calls are submitted to view 0.
 		bgfx::submit(0);
 		bgfx::submit(0);
@@ -195,19 +192,42 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 		textBufferManager->appendText(transientText, visitor10, L"text buffer\n");
 		textBufferManager->appendText(transientText, visitor10, L"text buffer\n");
 		textBufferManager->appendText(transientText, visitor10, fpsText);
 		textBufferManager->appendText(transientText, visitor10, fpsText);
 
 
-		float at[3] = { 0, 0, 0.0f };
+		float at[3]  = { 0, 0, 0.0f };
 		float eye[3] = {0, 0, -1.0f };
 		float eye[3] = {0, 0, -1.0f };
 
 
 		float view[16];
 		float view[16];
-		float proj[16];
 		bx::mtxLookAt(view, eye, at);
 		bx::mtxLookAt(view, eye, at);
 
 
-		// Setup a top-left ortho matrix for screen space drawing.
-		float centering = 0.5f;
-		bx::mtxOrtho(proj, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
+		const float centering = 0.5f;
 
 
-		// Set view and projection matrix for view 0.
-		bgfx::setViewTransform(0, view, proj);
+		// Setup a top-left ortho matrix for screen space drawing.
+		const bgfx::HMD* hmd = bgfx::getHMD();
+		if (NULL != hmd)
+		{
+			float proj[16];
+			bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
+
+			static float time = 0.0f;
+			time += 0.05f;
+
+			const float dist = 10.0f;
+			const float offset0 = -proj[8] + (hmd->eye[0].adjust[0] / dist * proj[0]);
+			const float offset1 = -proj[8] + (hmd->eye[1].adjust[0] / dist * proj[0]);
+
+			float ortho[2][16];
+			const float offsetx = width/2.0f;
+			bx::mtxOrtho(ortho[0], centering, offsetx + centering, height + centering, centering, -1.0f, 1.0f, offset0);
+			bx::mtxOrtho(ortho[1], centering, offsetx + centering, height + centering, centering, -1.0f, 1.0f, offset1);
+			bgfx::setViewTransform(0, view, ortho[0], BGFX_VIEW_STEREO, ortho[1]);
+			bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
+		}
+		else
+		{
+			float ortho[16];
+			bx::mtxOrtho(ortho, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
+			bgfx::setViewTransform(0, view, ortho);
+			bgfx::setViewRect(0, 0, 0, width, height);
+		}
 
 
 		// Submit the debug text.
 		// Submit the debug text.
 		textBufferManager->submitTextBuffer(transientText, 0);
 		textBufferManager->submitTextBuffer(transientText, 0);

+ 38 - 8
examples/11-fontsdf/fontsdf.cpp

@@ -144,7 +144,13 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 			, height
 			, height
 			);
 			);
 
 
-		imguiBeginScrollArea("Text Area", width - guiPanelWidth - 10, 10, guiPanelWidth, guiPanelHeight, &scrollArea);
+		imguiBeginScrollArea("Text Area"
+			, width - guiPanelWidth - 10
+			, 10
+			, guiPanelWidth
+			, guiPanelHeight
+			, &scrollArea
+			);
 		imguiSeparatorLine();
 		imguiSeparatorLine();
 
 
 		bool recomputeVisibleText = false;
 		bool recomputeVisibleText = false;
@@ -192,19 +198,43 @@ int _main_(int /*_argc*/, char** /*_argv*/)
 		bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Use a single distance field font to render text of various size.");
 		bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Use a single distance field font to render text of various size.");
 		bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime) * toMs);
 		bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime) * toMs);
 
 
-		float at[3] = { 0, 0, 0.0f };
+		float at[3]  = { 0, 0, 0.0f };
 		float eye[3] = {0, 0, -1.0f };
 		float eye[3] = {0, 0, -1.0f };
 
 
 		float view[16];
 		float view[16];
-		float proj[16];
 		bx::mtxLookAt(view, eye, at);
 		bx::mtxLookAt(view, eye, at);
-		float centering = 0.5f;
 
 
-		// Setup a top-left ortho matrix for screen space drawing.
-		bx::mtxOrtho(proj, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
+		const float centering = 0.5f;
 
 
-		// Set view and projection matrix for view 0.
-		bgfx::setViewTransform(0, view, proj);
+		// Setup a top-left ortho matrix for screen space drawing.
+		const bgfx::HMD* hmd = bgfx::getHMD();
+		if (NULL != hmd)
+		{
+			float proj[16];
+			bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
+
+			static float time = 0.0f;
+			time += 0.05f;
+
+			const float dist = 10.0f;
+			const float offset0 = -proj[8] + (hmd->eye[0].adjust[0] / dist * proj[0]);
+			const float offset1 = -proj[8] + (hmd->eye[1].adjust[0] / dist * proj[0]);
+
+			float ortho[2][16];
+			const float viewOffset = width/4.0f;
+			const float viewWidth  = width/2.0f;
+			bx::mtxOrtho(ortho[0], centering + viewOffset, centering + viewOffset + viewWidth, height + centering, centering, -1.0f, 1.0f, offset0);
+			bx::mtxOrtho(ortho[1], centering + viewOffset, centering + viewOffset + viewWidth, height + centering, centering, -1.0f, 1.0f, offset1);
+			bgfx::setViewTransform(0, view, ortho[0], BGFX_VIEW_STEREO, ortho[1]);
+			bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
+		}
+		else
+		{
+			float ortho[16];
+			bx::mtxOrtho(ortho, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
+			bgfx::setViewTransform(0, view, ortho);
+			bgfx::setViewRect(0, 0, 0, width, height);
+		}
 
 
 		//very crude approximation :(
 		//very crude approximation :(
 		float textAreaWidth = 0.5f * 66.0f * fontManager->getFontInfo(fontScaled).maxAdvanceWidth;
 		float textAreaWidth = 0.5f * 66.0f * fontManager->getFontInfo(fontScaled).maxAdvanceWidth;

+ 30 - 4
examples/common/imgui/imgui.cpp

@@ -775,11 +775,37 @@ struct Imgui
 		m_viewHeight = _height;
 		m_viewHeight = _height;
 		bgfx::setViewName(_view, "IMGUI");
 		bgfx::setViewName(_view, "IMGUI");
 		bgfx::setViewSeq(_view, true);
 		bgfx::setViewSeq(_view, true);
-		bgfx::setViewRect(_view, 0, 0, _width, _height);
 
 
-		float proj[16];
-		bx::mtxOrtho(proj, 0.0f, (float)_width, (float)_height, 0.0f, 0.0f, 1000.0f);
-		bgfx::setViewTransform(_view, NULL, proj);
+		const bgfx::HMD* hmd = bgfx::getHMD();
+		if (NULL != hmd)
+		{
+			m_viewWidth  = _width / 2;
+
+			float proj[16];
+			bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
+
+			static float time = 0.0f;
+			time += 0.05f;
+
+			const float dist = 10.0f;
+			const float offset0 = -proj[8] + (hmd->eye[0].adjust[0] / dist * proj[0]);
+			const float offset1 = -proj[8] + (hmd->eye[1].adjust[0] / dist * proj[0]);
+
+			float ortho[2][16];
+			const float viewOffset = _width/4.0f;
+			const float viewWidth  = _width/2.0f;
+			bx::mtxOrtho(ortho[0], viewOffset, viewOffset + viewWidth, (float)m_viewHeight, 0.0f, 0.0f, 1000.0f, offset0);
+			bx::mtxOrtho(ortho[1], viewOffset, viewOffset + viewWidth, (float)m_viewHeight, 0.0f, 0.0f, 1000.0f, offset1);
+			bgfx::setViewTransform(_view, NULL, ortho[0], BGFX_VIEW_STEREO, ortho[1]);
+			bgfx::setViewRect(_view, 0, 0, hmd->width, hmd->height);
+		}
+		else
+		{
+			float ortho[16];
+			bx::mtxOrtho(ortho, 0.0f, (float)m_viewWidth, (float)m_viewHeight, 0.0f, 0.0f, 1000.0f);
+			bgfx::setViewTransform(_view, NULL, ortho);
+			bgfx::setViewRect(_view, 0, 0, _width, _height);
+		}
 
 
 		updateInput(_mx, _my, _button, _scroll, _inputChar);
 		updateInput(_mx, _my, _button, _scroll, _inputChar);