Skip to content

Instantly share code, notes, and snippets.

@neozero
Last active November 19, 2023 18:31
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save neozero/50213e1176ede686066502c6658397ab to your computer and use it in GitHub Desktop.
Save neozero/50213e1176ede686066502c6658397ab to your computer and use it in GitHub Desktop.
OculusHomeless - Use Oculus Dash without Home 2.0
/************************************************************************************
Content : First-person view test application for Oculus Rift
Created : 19th June 2015
Authors : Tom Heath
Copyright : Copyright 2015 Oculus, Inc. All Rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************************/
#include "d3d11.h"
#include "d3dcompiler.h"
#include "OVR_CAPI_D3D.h"
#include "DirectXMath.h"
#include <cstdio>
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>
#include <fstream>
#include <Shlwapi.h>
using namespace DirectX;
#pragma comment(lib, "d3dcompiler.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "Shlwapi.lib")
bool ProcessRunning(const wchar_t* name);
//-------------------------------------------------------------------------------------------------
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR, int)
{
// Init Rift and device
ovrResult result = ovr_Initialize(0);
ovrGraphicsLuid luid;
ovrSession session;
ovr_Create(&session, &luid);
ID3D11DeviceContext * Context;
IDXGIFactory * DXGIFactory; CreateDXGIFactory1(__uuidof(IDXGIFactory), (void**)(&DXGIFactory));
IDXGIAdapter * DXGIAdapter; DXGIFactory->EnumAdapters(0, &DXGIAdapter);
ID3D11Device * Device; D3D11CreateDevice(DXGIAdapter, D3D_DRIVER_TYPE_UNKNOWN, 0, 0, 0, 0, D3D11_SDK_VERSION, &Device, 0, &Context);
// Create eye render buffers
ID3D11RenderTargetView * eyeRenderTexRtv[2][10];
ovrLayerEyeFov ld = { { ovrLayerType_EyeFov } };
for (int i = 0; i < 2; i++)
{
ld.Fov[i] = ovr_GetHmdDesc(session).DefaultEyeFov[i];
ld.Viewport[i].Size = ovr_GetFovTextureSize(session, (ovrEyeType)i, ld.Fov[i], 1.0f);
ovrTextureSwapChainDesc dsDesc = { ovrTexture_2D, OVR_FORMAT_R8G8B8A8_UNORM_SRGB, 1, ld.Viewport[i].Size.w, ld.Viewport[i].Size.h,
1, 1, ovrFalse, ovrTextureMisc_DX_Typeless, ovrTextureBind_DX_RenderTarget };
ovr_CreateTextureSwapChainDX(session, Device, &dsDesc, &ld.ColorTexture[i]);
int textureCount = 0; ovr_GetTextureSwapChainLength(session, ld.ColorTexture[i], &textureCount);
for (int j = 0; j < textureCount; j++)
{
ID3D11Texture2D* tex; ovr_GetTextureSwapChainBufferDX(session, ld.ColorTexture[i], j, IID_PPV_ARGS(&tex));
D3D11_RENDER_TARGET_VIEW_DESC rtvd = { DXGI_FORMAT_R8G8B8A8_UNORM, D3D11_RTV_DIMENSION_TEXTURE2D };
Device->CreateRenderTargetView(tex, &rtvd, &eyeRenderTexRtv[i][j]);
}
}
// Default background color to gray
float clearColor[4] = { 0.5f, 0.5f, 0.5f, 1.0f };
// Try reading color from file
CHAR path[MAX_PATH];
DWORD length = GetModuleFileNameA(NULL, path, MAX_PATH);
PathRemoveFileSpecA(path);
StrCatA(path, "\\background_color.txt");
std::ifstream file;
file.open(path);
if (file)
{
float r = -1, g = -1, b = -1;
file >> r >> g >> b;
if (r >= 0 && r <= 1 &&
g >= 0 && g <= 1 &&
b >= 0 && b <= 1)
{
clearColor[0] = r;
clearColor[1] = g;
clearColor[2] = b;
}
file.close();
}
DWORD lastCheckTime = GetTickCount();
long long frameIndex = 0;
for(;;)
{
// Quit if Oculus Client is not running anymore
if (GetTickCount() - lastCheckTime > 1500)
{
if (!ProcessRunning(L"OculusClient.exe"))
break;
lastCheckTime = GetTickCount();
}
ovrSessionStatus sessionStatus;
ovr_GetSessionStatus(session, &sessionStatus);
if (sessionStatus.DisplayLost || sessionStatus.ShouldQuit)
break;
// Pause rendering when showing Dash
if (!sessionStatus.IsVisible || !sessionStatus.HmdMounted)
{
Sleep(20);
continue;
}
// Get pose using a default IPD
ovrPosef HmdToEyePose[2] = {{{0,0,0,1}, {-0.032f, 0, 0}},
{{0,0,0,1}, {+0.032f, 0, 0}}};
ovrPosef pose[2]; ovr_GetEyePoses(session, frameIndex, ovrTrue, HmdToEyePose, pose, &ld.SensorSampleTime);
for (int i = 0; i < 2; i++) ld.RenderPose[i] = pose[i];
// Render to each eye
for (int i = 0; i < 2; i++)
{
// Set and clear current render target, and set viewport
int index = 0; ovr_GetTextureSwapChainCurrentIndex(session, ld.ColorTexture[i], &index);
Context->OMSetRenderTargets(1, &eyeRenderTexRtv[i][index], 0);
Context->ClearRenderTargetView(eyeRenderTexRtv[i][index], clearColor);
D3D11_VIEWPORT D3Dvp = { 0, 0, (float)ld.Viewport[i].Size.w, (float)ld.Viewport[i].Size.h };
Context->RSSetViewports(1, &D3Dvp);
// Commit frame
ovr_CommitTextureSwapChain(session, ld.ColorTexture[i]);
}
// Send rendered eye buffers to HMD, and increment the frame if we're visible
ovrLayerHeader* layers[1] = { &ld.Header };
if (ovrSuccess == ovr_SubmitFrame(session, frameIndex, nullptr, layers, 1))
frameIndex++;
}
ovr_Shutdown();
}
//-------------------------------------------------------------------------------------------------
bool ProcessRunning(const wchar_t* name)
{
HANDLE SnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (SnapShot == INVALID_HANDLE_VALUE)
return false;
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(SnapShot, &procEntry))
return false;
do
{
if (_wcsicmp(procEntry.szExeFile, name) == 0)
return true;
} while (Process32Next(SnapShot, &procEntry));
return false;
}
@mathieu2em
Copy link

i have the oculus loading sign on loop is it normal ?

@Kzitold
Copy link

Kzitold commented Feb 26, 2019

Would it be possible for you to put an option to run another program, such as SteamVR?

@jonathanHardy
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment