From 1459b08bc10180f4189fd11b7cf013649ba462b8 Mon Sep 17 00:00:00 2001 From: Jim McCann Date: Fri, 3 Nov 2023 15:53:50 -0400 Subject: [PATCH] revised readme and starting on basic OpenXR stuff --- Maekfile.js | 5 +++- README.md | 81 +++++++++++++++++++++++++++++++++++++++++++++++------ main.cpp | 59 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 134 insertions(+), 11 deletions(-) diff --git a/Maekfile.js b/Maekfile.js index 1ff9924..43aea5c 100644 --- a/Maekfile.js +++ b/Maekfile.js @@ -21,6 +21,7 @@ const maek = init_maek(); //====================================================================== const NEST_LIBS = `../nest-libs/${maek.OS}`; +const OPENXR_SDK = `../openxr-sdk-source`; //set compile flags (these can also be overridden per-task using the "options" parameter): if (maek.OS === "windows") { @@ -49,7 +50,9 @@ if (maek.OS === "windows") { //include paths for nest libraries: `-I${NEST_LIBS}/SDL2/include/SDL2`, `-D_THREAD_SAFE`, //the output of sdl-config --cflags `-I${NEST_LIBS}/glm/include`, - `-I${NEST_LIBS}/libpng/include` + `-I${NEST_LIBS}/libpng/include`, + //OpenXR: + `-I${OPENXR_SDK}/include` ); maek.options.LINKLibs.push( //linker flags for nest libraries: diff --git a/README.md b/README.md index 1fa0dca..a63864e 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,80 @@ -# (TODO: your game's title) +# 15-466 Demonstration Code - NDK + OpenXR -Author: (TODO: your name) +This demonstration code shows how to use a `Maekfile.js` to build with the NDK for Android devices, and how to use OpenXR on such devices. -Design: (TODO: In two sentences or fewer, describe what is new and interesting about your game.) +Particularly, this code is designed to build for Meta Quest 3. -Screen Shot: +Based on [15-466-f23-base2](https://github.com/15-466/15-466-f23-base2) with modifications inspired by both the [Oculus OpenXR Mobile SDK](https://developer.oculus.com/downloads/package/oculus-openxr-mobile-sdk/) samples and the [OpenXR Source SDK](https://github.com/KhronosGroup/OpenXR-SDK-Source) samples. -![Screen Shot](screenshot.png) +## Setup -How To Play: +You will need to jump through some hoops to get your development environment set up, and to get your hardware ready for development. -(TODO: describe the controls and (if needed) goals/strategy.) +### Software -This game was built with [NEST](NEST.md). +Get the android sdk set up in a directory which is a sibling of this one using the `sdkmanager` utility. (Note, these instructions are based on https://developer.android.com/tools/sdkmanager .) + + +Start with a "command line tools only" package from https://developer.android.com/studio (scroll to the bottom). Now extract it and get it working: + +``` +$ mkdir android-sdk +$ cd android-sdk +$ unzip ../downloads/commandlinetools-linux-10406996_latest.zip +$ cd cmdline-tools +$ mkdir latest +#will complain about moving latest into itself, which is fine: +$ mv * latest + +``` + +Now get the things that the ovr SDK says we need (as per https://developer.oculus.com/documentation/native/android/mobile-studio-setup-android/ ): +``` +#from android-sdk: +$ ./cmdline-tools/latest/bin/sdkmanager --install 'platforms;android-26' +# "version 28.0.3 or later"; latest as of --list is build-tools;34.0.0-rc3 but: +$ ./cmdline-tools/latest/bin/sdkmanager --install 'build-tools;28.0.3' +# android NDK (no version given, this is latest as of this writing): +$ ./cmdline-tools/latest/bin/sdkmanager --install 'ndk;26.1.10909125' +``` + +Also download Meta's OpenXR SDK (from https://developer.oculus.com/downloads/package/oculus-openxr-mobile-sdk/) and extract into a sibling of this directory: +``` +$ mkdir ovr-openxr-sdk +$ cd ovr-openxr-sdk +$ unzip ../downloads/ovr_openxr_mobile_sdk_57.0.zip #current version as of this writing +``` + +Once all of that is done you should have these three as siblings: +``` +15466-f23-ndk-openxr #this folder +android-sdk #android sdk, tools, etc +ovr-openxr-sdk #Meta's OpenXR SDK stuff +``` + +### Hardware + +Set up your Quest 3 for development, as per https://developer.oculus.com/documentation/native/android/mobile-device-setup/ . +- create a developer acct: https://developer.oculus.com/manage/organizations/create/ + - might require setting up 2FA at: https://developer.oculus.com/manage/verify/ +- from oculus phone app: headset -> settings -> developer mode [set to: on] +- connect via USB C (the included white cable works, though is annoyingly short) and allow data (notification will pop up in headset) + + +You can now connect to the headset via the `adb` tool: +``` +#from android-sdk: +$ ./platform-tools/adb devices +# shows "unauthorized" and causes the "allow debugging" dialog to pop on the headset +# if dialog accepted, says "device" next to the device (maybe that's the device name?) +$ ./platform-tools/adb devices +# if you like to explore a bit, get a shell on your device: +$ ./platform-tools/adb shell +# interesting things include `top` (see what's running), `cat /proc/cpuinfo` (learn more about the cpu) +#(ctrl-d to exit) +``` + +## Building + + +... diff --git a/main.cpp b/main.cpp index 9d21350..9205ff5 100644 --- a/main.cpp +++ b/main.cpp @@ -13,8 +13,17 @@ //for screenshots: #include "load_save_png.hpp" +#ifdef __ANDROID__ +//libSDL not used on android(!) +#else //Includes for libSDL: #include +#endif + +//for OpenXR: +#include +#define XR_USE_GRAPHICS_API_OPENGL +#include //...and for c++ standard library functions: #include @@ -22,6 +31,7 @@ #include #include #include +#include #ifdef _WIN32 extern "C" { uint32_t GetACP(); } @@ -42,7 +52,52 @@ int main(int argc, char **argv) { try { #endif - //------------ initialization ------------ + //------------ OpenXR init ------------ + + { + //OpenXR needs an "instance" to store global state: + XrInstance instance{XR_NULL_HANDLE}; + XrInstanceCreateInfo create_info{XR_TYPE_INSTANCE_CREATE_INFO}; + + //with reference to openxr_program.cpp from openxr-sdk-source + the openxr specification + + std::strcpy(create_info.applicationInfo.applicationName, "gp23 openxr demo"); + create_info.applicationInfo.apiVersion = XR_CURRENT_API_VERSION; + + //TODO: ON ANROID --> set create_info->next to point to an XrInstanceCreateInfoAndroidKHR structure + // filled in as per openxr-sdk-source's platformplugin_android.cpp + + std::vector< const char * > extensions{ + XR_KHR_OPENGL_ENABLE_EXTENSION_NAME, + //TOOD: ON ANROID --> XR_KHR_ANDROID_CREATE_INSTANCE_EXTENSION_NAME + }; + + create_info.enabledExtensionCount = uint32_t(extensions.size()); + create_info.enabledExtensionNames = extensions.data(); + + if (XrResult res = xrCreateInstance( &create_info, &instance); + res != XR_SUCCESS) { + std::cerr << "Failed to create OpenXR instance: " << res << std::endl; + return 1; + } + + //Query the instance to learn what runtime this is using: + XrInstanceProperties properties{XR_TYPE_INSTANCE_PROPERTIES}; + if (XrResult res = xrGetInstanceProperties(instance, &properties); + res != XR_SUCCESS) { + std::cerr << "Failed to get OpenXR instance properties: " << res << std::endl; + return 1; + } + std::cout << "OpenXR Runtime is '" << properties.runtimeName << "', version " + << (properties.runtimeVersion >> 48) + << "." << ((properties.runtimeVersion >> 32) & 0xffff) + << "." << (properties.runtimeVersion & 0xffffffff) + << std::endl; + + //TODO, later: xrDestroyInstance ! + } + + //------------ initialization ------------ //Initialize SDL library: SDL_Init(SDL_INIT_VIDEO); @@ -63,7 +118,7 @@ int main(int argc, char **argv) { //create window: SDL_Window *window = SDL_CreateWindow( - "gp23 game2: enter the matr... virtual world", //TODO: remember to set a title for your game! + "gp23: openxr demo code", //TODO: remember to set a title for your game! SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, //TODO: modify window size if you'd like SDL_WINDOW_OPENGL