Created
December 4, 2022 02:41
-
-
Save Sgeo/6e8ea12da48f3714c9bf0218811428e9 to your computer and use it in GitHub Desktop.
This code was written by ChatGPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Windows.h> | |
#include <MinHook.h> | |
#include <GL/gl.h> | |
#include <GL/glext.h> | |
#include <OVR_CAPI_GL.h> | |
// Declaration of the original function. | |
typedef void (__stdcall *glBegin_t)(GLenum); | |
glBegin_t original_glBegin; | |
// Replacement function that adds VR support. | |
void __stdcall VR_glBegin(GLenum mode) | |
{ | |
// Get the current VR pose. | |
ovrPosef pose = ovrHmd_BeginEyeRender(hmd, ovrEye_Left); | |
// Set up the projection and modelview matrices for the left eye. | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glMultMatrixf(pose.ProjectionDesc.LeftProjection.M[0]); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
glMultMatrixf(pose.LeftView.M[0]); | |
// Call the original glBegin function. | |
original_glBegin(mode); | |
// Set up the projection and modelview matrices for the right eye. | |
pose = ovrHmd_BeginEyeRender(hmd, ovrEye_Right); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glMultMatrixf(pose.ProjectionDesc.RightProjection.M[0]); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
glMultMatrixf(pose.RightView.M[0]); | |
// Call the original glBegin function again. | |
original_glBegin(mode); | |
} | |
int main(int argc, char** argv) | |
{ | |
// Initialize the VR system. | |
ovr_Initialize(); | |
hmd = ovrHmd_Create(0); | |
ovrHmd_ConfigureTracking(hmd, ovrTrackingCap_Orientation | | |
ovrTrackingCap_Position, 0); | |
// Install the hook for the glBegin function. | |
MH_Initialize(); | |
MH_CreateHook(&glBegin, &VR_glBegin, (void**)&original_glBegin); | |
MH_EnableHook(&glBegin); | |
// Run the game loop. | |
while (true) | |
{ | |
// Get the VR pose and update the game state. | |
ovrTrackingState state = ovrHmd_GetTrackingState(hmd, 0.0); | |
UpdateGameState(state.HeadPose.ThePose); | |
// Render the game world. | |
ovrHmd_BeginFrame(hmd, 0); | |
RenderGame(); | |
ovrHmd_EndFrame(hmd); | |
} | |
// Uninstall the hook and shutdown the VR system. | |
MH_DisableHook(&glBegin); | |
MH_RemoveHook(&glBegin); | |
MH_Uninitialize(); | |
ovrHmd_Destroy(hmd); | |
ovr_Shutdown(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment