Skip to content

Commit 573ce05

Browse files
committed
Project restructure
1 parent 7fcc6db commit 573ce05

30 files changed

+290
-654
lines changed

CMakeLists.txt

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION "3.7.1")
22

33
# Solution
4-
project("driver_tutorial")
4+
project("driver_example")
55
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
66

77
# Deps
@@ -24,65 +24,34 @@ endif()
2424

2525
find_library(OPENVR_LIB openvr_api HINTS "${CMAKE_CURRENT_SOURCE_DIR}/libraries/openvr/lib/${PLATFORM_NAME}${PROCESSOR_ARCH}/" NO_DEFAULT_PATH )
2626

27-
# HMD Tutorial
28-
set(HMD_TUTORIAL_PROJECT "driver_tutorial_hmd")
29-
file(GLOB_RECURSE HEADERS "${HMD_TUTORIAL_PROJECT}/src/*.hpp")
30-
file(GLOB_RECURSE SOURCES "${HMD_TUTORIAL_PROJECT}/src/*.cpp")
31-
add_library("${HMD_TUTORIAL_PROJECT}" SHARED ${HEADERS} ${SOURCES})
32-
target_include_directories("${HMD_TUTORIAL_PROJECT}" PUBLIC ${OPENVR_INCLUDE_DIR})
33-
target_include_directories("${HMD_TUTORIAL_PROJECT}" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/libraries/linalg")
34-
target_include_directories("${HMD_TUTORIAL_PROJECT}" PUBLIC "${HMD_TUTORIAL_PROJECT}/src/")
35-
target_link_libraries("${HMD_TUTORIAL_PROJECT}" PUBLIC ${OPENVR_LIB})
36-
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/${HMD_TUTORIAL_PROJECT}/src" PREFIX "Header Files" FILES ${HEADERS})
37-
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/${HMD_TUTORIAL_PROJECT}/src" PREFIX "Source Files" FILES ${SOURCES})
38-
set_property(TARGET "${HMD_TUTORIAL_PROJECT}" PROPERTY CXX_STANDARD 17)
27+
# Example Driver
28+
set(EXAMPLE_PROJECT "driver_example")
29+
file(GLOB_RECURSE HEADERS "${EXAMPLE_PROJECT}/src/*.hpp")
30+
file(GLOB_RECURSE SOURCES "${EXAMPLE_PROJECT}/src/*.cpp")
31+
add_library("${EXAMPLE_PROJECT}" SHARED "${HEADERS}" "${SOURCES}")
3932

40-
# Copy driver assets to output folder
41-
add_custom_command(
42-
TARGET ${HMD_TUTORIAL_PROJECT}
43-
PRE_BUILD
44-
COMMAND ${CMAKE_COMMAND} -E copy_directory
45-
${CMAKE_SOURCE_DIR}/${HMD_TUTORIAL_PROJECT}/driver/
46-
$<TARGET_FILE_DIR:${HMD_TUTORIAL_PROJECT}>
47-
)
48-
49-
# Copy dll to right folder
50-
add_custom_command(
51-
TARGET ${HMD_TUTORIAL_PROJECT}
52-
POST_BUILD
53-
COMMAND ${CMAKE_COMMAND} -E copy
54-
$<TARGET_FILE:${HMD_TUTORIAL_PROJECT}>
55-
$<TARGET_FILE_DIR:${HMD_TUTORIAL_PROJECT}>/${HMD_TUTORIAL_PROJECT}/bin/${PLATFORM_NAME}${PROCESSOR_ARCH}/$<TARGET_FILE_NAME:${HMD_TUTORIAL_PROJECT}>
56-
)
57-
58-
59-
# Devices Tutorial
60-
set(DEVICES_TUTORIAL_PROJECT "driver_tutorial_devices")
61-
file(GLOB_RECURSE HEADERS "${DEVICES_TUTORIAL_PROJECT}/src/*.hpp")
62-
file(GLOB_RECURSE SOURCES "${DEVICES_TUTORIAL_PROJECT}/src/*.cpp")
63-
add_library("${DEVICES_TUTORIAL_PROJECT}" SHARED ${HEADERS} ${SOURCES})
64-
target_include_directories("${DEVICES_TUTORIAL_PROJECT}" PUBLIC ${OPENVR_INCLUDE_DIR})
65-
target_include_directories("${DEVICES_TUTORIAL_PROJECT}" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/libraries/linalg")
66-
target_include_directories("${DEVICES_TUTORIAL_PROJECT}" PUBLIC "${DEVICES_TUTORIAL_PROJECT}/src/")
67-
target_link_libraries("${DEVICES_TUTORIAL_PROJECT}" PUBLIC ${OPENVR_LIB})
68-
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/${DEVICES_TUTORIAL_PROJECT}/src" PREFIX "Header Files" FILES ${HEADERS})
69-
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/${DEVICES_TUTORIAL_PROJECT}/src" PREFIX "Source Files" FILES ${SOURCES})
70-
set_property(TARGET "${DEVICES_TUTORIAL_PROJECT}" PROPERTY CXX_STANDARD 17)
33+
target_include_directories("${EXAMPLE_PROJECT}" PUBLIC "${OPENVR_INCLUDE_DIR}")
34+
target_include_directories("${EXAMPLE_PROJECT}" PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/libraries/linalg")
35+
target_include_directories("${EXAMPLE_PROJECT}" PUBLIC "${EXAMPLE_PROJECT}/src/")
36+
target_link_libraries("${EXAMPLE_PROJECT}" PUBLIC "${OPENVR_LIB}")
37+
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/${EXAMPLE_PROJECT}/src" PREFIX "Header Files" FILES ${HEADERS})
38+
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/${EXAMPLE_PROJECT}/src" PREFIX "Source Files" FILES ${SOURCES})
39+
set_property(TARGET "${EXAMPLE_PROJECT}" PROPERTY CXX_STANDARD 17)
7140

7241
# Copy driver assets to output folder
7342
add_custom_command(
74-
TARGET ${DEVICES_TUTORIAL_PROJECT}
43+
TARGET ${EXAMPLE_PROJECT}
7544
PRE_BUILD
7645
COMMAND ${CMAKE_COMMAND} -E copy_directory
77-
${CMAKE_SOURCE_DIR}/${DEVICES_TUTORIAL_PROJECT}/driver/
78-
$<TARGET_FILE_DIR:${DEVICES_TUTORIAL_PROJECT}>
46+
${CMAKE_SOURCE_DIR}/${EXAMPLE_PROJECT}/driver/
47+
$<TARGET_FILE_DIR:${EXAMPLE_PROJECT}>
7948
)
8049

81-
# Copy dll to right folder
50+
# Copy dll to output folder
8251
add_custom_command(
83-
TARGET ${DEVICES_TUTORIAL_PROJECT}
52+
TARGET ${EXAMPLE_PROJECT}
8453
POST_BUILD
8554
COMMAND ${CMAKE_COMMAND} -E copy
86-
$<TARGET_FILE:${DEVICES_TUTORIAL_PROJECT}>
87-
$<TARGET_FILE_DIR:${DEVICES_TUTORIAL_PROJECT}>/${DEVICES_TUTORIAL_PROJECT}/bin/${PLATFORM_NAME}${PROCESSOR_ARCH}/$<TARGET_FILE_NAME:${DEVICES_TUTORIAL_PROJECT}>
55+
$<TARGET_FILE:${EXAMPLE_PROJECT}>
56+
$<TARGET_FILE_DIR:${EXAMPLE_PROJECT}>/${EXAMPLE_PROJECT}/bin/${PLATFORM_NAME}${PROCESSOR_ARCH}/$<TARGET_FILE_NAME:${EXAMPLE_PROJECT}>
8857
)

driver_tutorial_hmd/driver/driver_tutorial_hmd/driver.vrdrivermanifest renamed to driver_example/driver/driver_tutorial_hmd/driver.vrdrivermanifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"alwaysActivate": true,
3-
"name" : "tutorial_hmd",
3+
"name" : "example",
44
"directory" : "",
55
"resourceOnly" : false,
66
"prefersUpperDeviceIndices" : true,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
enum class DeviceType {
3+
HMD,
4+
CONTROLLER,
5+
TRACKER,
6+
TRACKING_REFERENCE
7+
};

driver_tutorial_hmd/src/Driver/HMDDevice.cpp renamed to driver_example/src/Driver/HMDDevice.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
#include "HMDDevice.hpp"
22
#include <Windows.h>
33

4-
TutorialDriver::HMDDevice::HMDDevice(std::string serial):m_serial(serial)
4+
ExampleDriver::HMDDevice::HMDDevice(std::string serial):m_serial(serial)
55
{
66
}
77

8-
std::string TutorialDriver::HMDDevice::serial()
8+
std::string ExampleDriver::HMDDevice::getSerial()
99
{
1010
return this->m_serial;
1111
}
1212

13-
void TutorialDriver::HMDDevice::update(std::vector<vr::VREvent_t> events)
13+
void ExampleDriver::HMDDevice::update()
1414
{
1515
if (this->m_deviceIndex == vr::k_unTrackedDeviceIndexInvalid)
1616
return;
1717

18-
// Get deltatime
19-
auto now = std::chrono::system_clock::now();
20-
double deltaTimeSeconds = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_lastFrameTime).count()/1000.0;
21-
this->m_lastFrameTime = now;
22-
2318
// Setup pose for this frame
2419
auto pose = this->GetPose();
2520

21+
float deltaSeconds = ExampleDriver::getDriver()->getLastFrameTime().count() / 1000.0;
22+
2623
// Get orientation
27-
this->m_yRot += (1.0 * (GetAsyncKeyState(VK_RIGHT) == 0) - 1.0 * (GetAsyncKeyState(VK_LEFT) == 0)) * deltaTimeSeconds;
28-
this->m_xRot += (-1.0 * (GetAsyncKeyState(VK_UP) == 0) + 1.0 * (GetAsyncKeyState(VK_DOWN) == 0)) * deltaTimeSeconds;
24+
this->m_yRot += (1.0 * (GetAsyncKeyState(VK_RIGHT) == 0) - 1.0 * (GetAsyncKeyState(VK_LEFT) == 0)) * deltaSeconds;
25+
this->m_xRot += (-1.0 * (GetAsyncKeyState(VK_UP) == 0) + 1.0 * (GetAsyncKeyState(VK_DOWN) == 0)) * deltaSeconds;
2926
this->m_xRot = std::fmax(this->m_xRot, -3.14159/2);
3027
this->m_xRot = std::fmin(this->m_xRot, 3.14159/2);
3128

@@ -45,7 +42,7 @@ void TutorialDriver::HMDDevice::update(std::vector<vr::VREvent_t> events)
4542
linalg::vec<float, 3> right_vec{0, 0, 1.0f * (GetAsyncKeyState(0x57) == 0) - 1.0f * (GetAsyncKeyState(0x53) == 0) };
4643
linalg::vec<float, 3> final_dir = forward_vec + right_vec;
4744
if (linalg::length(final_dir) > 0.01) {
48-
final_dir = linalg::normalize(final_dir) * (float)deltaTimeSeconds;
45+
final_dir = linalg::normalize(final_dir) * (float)deltaSeconds;
4946
final_dir = linalg::qrot(pose_rot, final_dir);
5047
this->m_x += final_dir.x;
5148
this->m_y += final_dir.y;
@@ -57,15 +54,20 @@ void TutorialDriver::HMDDevice::update(std::vector<vr::VREvent_t> events)
5754
pose.vecPosition[2] = this->m_z;
5855

5956
// Post pose
60-
vr::VRServerDriverHost()->TrackedDevicePoseUpdated(this->device_index(), pose, sizeof(vr::DriverPose_t));
57+
vr::VRServerDriverHost()->TrackedDevicePoseUpdated(this->getDeviceIndex(), pose, sizeof(vr::DriverPose_t));
58+
}
59+
60+
DeviceType ExampleDriver::HMDDevice::getDeviceType()
61+
{
62+
return DeviceType::HMD;
6163
}
6264

63-
vr::TrackedDeviceIndex_t TutorialDriver::HMDDevice::device_index()
65+
vr::TrackedDeviceIndex_t ExampleDriver::HMDDevice::getDeviceIndex()
6466
{
6567
return this->m_deviceIndex;
6668
}
6769

68-
vr::EVRInitError TutorialDriver::HMDDevice::Activate(uint32_t unObjectId)
70+
vr::EVRInitError ExampleDriver::HMDDevice::Activate(uint32_t unObjectId)
6971
{
7072
this->m_deviceIndex = unObjectId;
7173

@@ -94,36 +96,34 @@ vr::EVRInitError TutorialDriver::HMDDevice::Activate(uint32_t unObjectId)
9496
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceNotReady_String, "{tutorial_hmd}/icons/hmd_not_ready.png");
9597
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceStandby_String, "{tutorial_hmd}/icons/hmd_not_ready.png");
9698
vr::VRProperties()->SetStringProperty(props, vr::Prop_NamedIconPathDeviceAlertLow_String, "{tutorial_hmd}/icons/hmd_not_ready.png");
97-
98-
this->m_lastFrameTime = std::chrono::system_clock::now();
9999

100100
return vr::EVRInitError::VRInitError_None;
101101
}
102102

103-
void TutorialDriver::HMDDevice::Deactivate()
103+
void ExampleDriver::HMDDevice::Deactivate()
104104
{
105105
this->m_deviceIndex = vr::k_unTrackedDeviceIndexInvalid;
106106
}
107107

108-
void TutorialDriver::HMDDevice::EnterStandby()
108+
void ExampleDriver::HMDDevice::EnterStandby()
109109
{
110110
}
111111

112-
void* TutorialDriver::HMDDevice::GetComponent(const char* pchComponentNameAndVersion)
112+
void* ExampleDriver::HMDDevice::GetComponent(const char* pchComponentNameAndVersion)
113113
{
114114
if (!stricmp(pchComponentNameAndVersion, vr::IVRDisplayComponent_Version)) {
115115
return static_cast<vr::IVRDisplayComponent*>(this);
116116
}
117117
return nullptr;
118118
}
119119

120-
void TutorialDriver::HMDDevice::DebugRequest(const char* pchRequest, char* pchResponseBuffer, uint32_t unResponseBufferSize)
120+
void ExampleDriver::HMDDevice::DebugRequest(const char* pchRequest, char* pchResponseBuffer, uint32_t unResponseBufferSize)
121121
{
122122
if (unResponseBufferSize >= 1)
123123
pchResponseBuffer[0] = 0;
124124
}
125125

126-
vr::DriverPose_t TutorialDriver::HMDDevice::GetPose()
126+
vr::DriverPose_t ExampleDriver::HMDDevice::GetPose()
127127
{
128128
vr::DriverPose_t out_pose = { 0 };
129129

@@ -137,31 +137,31 @@ vr::DriverPose_t TutorialDriver::HMDDevice::GetPose()
137137
return out_pose;
138138
}
139139

140-
void TutorialDriver::HMDDevice::GetWindowBounds(int32_t* pnX, int32_t* pnY, uint32_t* pnWidth, uint32_t* pnHeight)
140+
void ExampleDriver::HMDDevice::GetWindowBounds(int32_t* pnX, int32_t* pnY, uint32_t* pnWidth, uint32_t* pnHeight)
141141
{
142142
*pnX = this->m_windowX;
143143
*pnY = this->m_windowY;
144144
*pnWidth = this->m_windowWidth;
145145
*pnHeight = this->m_windowHeight;
146146
}
147147

148-
bool TutorialDriver::HMDDevice::IsDisplayOnDesktop()
148+
bool ExampleDriver::HMDDevice::IsDisplayOnDesktop()
149149
{
150150
return true;
151151
}
152152

153-
bool TutorialDriver::HMDDevice::IsDisplayRealDisplay()
153+
bool ExampleDriver::HMDDevice::IsDisplayRealDisplay()
154154
{
155155
return false;
156156
}
157157

158-
void TutorialDriver::HMDDevice::GetRecommendedRenderTargetSize(uint32_t* pnWidth, uint32_t* pnHeight)
158+
void ExampleDriver::HMDDevice::GetRecommendedRenderTargetSize(uint32_t* pnWidth, uint32_t* pnHeight)
159159
{
160160
*pnWidth = this->m_windowWidth;
161161
*pnHeight = this->m_windowHeight;
162162
}
163163

164-
void TutorialDriver::HMDDevice::GetEyeOutputViewport(vr::EVREye eEye, uint32_t* pnX, uint32_t* pnY, uint32_t* pnWidth, uint32_t* pnHeight)
164+
void ExampleDriver::HMDDevice::GetEyeOutputViewport(vr::EVREye eEye, uint32_t* pnX, uint32_t* pnY, uint32_t* pnWidth, uint32_t* pnHeight)
165165
{
166166
*pnY = 0;
167167
*pnWidth = this->m_windowWidth / 2;
@@ -175,15 +175,15 @@ void TutorialDriver::HMDDevice::GetEyeOutputViewport(vr::EVREye eEye, uint32_t*
175175
}
176176
}
177177

178-
void TutorialDriver::HMDDevice::GetProjectionRaw(vr::EVREye eEye, float* pfLeft, float* pfRight, float* pfTop, float* pfBottom)
178+
void ExampleDriver::HMDDevice::GetProjectionRaw(vr::EVREye eEye, float* pfLeft, float* pfRight, float* pfTop, float* pfBottom)
179179
{
180180
*pfLeft = -1;
181181
*pfRight = 1;
182182
*pfTop = -1;
183183
*pfBottom = 1;
184184
}
185185

186-
vr::DistortionCoordinates_t TutorialDriver::HMDDevice::ComputeDistortion(vr::EVREye eEye, float fU, float fV)
186+
vr::DistortionCoordinates_t ExampleDriver::HMDDevice::ComputeDistortion(vr::EVREye eEye, float fU, float fV)
187187
{
188188
vr::DistortionCoordinates_t coordinates;
189189
coordinates.rfBlue[0] = fU;

driver_tutorial_hmd/src/Driver/HMDDevice.hpp renamed to driver_example/src/Driver/HMDDevice.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
#include <linalg.h>
77

88
#include <Driver/IVRDevice.hpp>
9+
#include <Native/DriverFactory.hpp>
910

10-
namespace TutorialDriver {
11+
namespace ExampleDriver {
1112
class HMDDevice : public IVRDevice, public vr::IVRDisplayComponent {
1213
public:
1314
HMDDevice(std::string serial);
1415
~HMDDevice() = default;
1516

1617
// Inherited via IVRDevice
17-
virtual std::string serial() override;
18-
virtual void update(std::vector<vr::VREvent_t> events) override;
19-
virtual vr::TrackedDeviceIndex_t device_index() override;
18+
virtual std::string getSerial() override;
19+
virtual void update() override;
20+
virtual vr::TrackedDeviceIndex_t getDeviceIndex() override;
21+
virtual DeviceType getDeviceType() override;
22+
2023
virtual vr::EVRInitError Activate(uint32_t unObjectId) override;
2124
virtual void Deactivate() override;
2225
virtual void EnterStandby() override;
@@ -42,10 +45,7 @@ namespace TutorialDriver {
4245
uint32_t m_windowHeight = 1080;
4346

4447
double m_x = 0, m_y = 0, m_z = 0;
45-
double m_xRot = 0, m_yRot = 0;
46-
47-
std::chrono::system_clock::time_point m_lastFrameTime;
48-
48+
double m_xRot = 0, m_yRot = 0;
4949

5050
};
5151
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#include <openvr_driver.h>
4+
#include <Driver/DeviceType.hpp>
5+
6+
namespace ExampleDriver {
7+
class IVRDevice : public vr::ITrackedDeviceServerDriver {
8+
public:
9+
/// <summary>
10+
/// Returns the serial string for this device
11+
/// </summary>
12+
/// <returns>Device serial</returns>
13+
virtual std::string getSerial() = 0;
14+
15+
/// <summary>
16+
/// Runs any update logic for this device.
17+
/// Called once per frame
18+
/// </summary>
19+
virtual void update() = 0;
20+
21+
/// <summary>
22+
/// Returns the OpenVR device index
23+
/// This should be 0 for HMDs
24+
/// </summary>
25+
/// <returns>OpenVR device index</returns>
26+
virtual vr::TrackedDeviceIndex_t getDeviceIndex() = 0;
27+
28+
/// <summary>
29+
/// Returns which type of device this device is
30+
/// </summary>
31+
/// <returns>The type of device</returns>
32+
virtual DeviceType getDeviceType() = 0;
33+
34+
// Inherited via ITrackedDeviceServerDriver
35+
virtual vr::EVRInitError Activate(uint32_t unObjectId) = 0;
36+
virtual void Deactivate() = 0;
37+
virtual void EnterStandby() = 0;
38+
virtual void* GetComponent(const char* pchComponentNameAndVersion) = 0;
39+
virtual void DebugRequest(const char* pchRequest, char* pchResponseBuffer, uint32_t unResponseBufferSize) = 0;
40+
virtual vr::DriverPose_t GetPose() = 0;
41+
42+
~IVRDevice() = default;
43+
};
44+
};

0 commit comments

Comments
 (0)