OpenGL, Xlib, and OpenVR Programming

For dumb java web developers


2020-10-25

What are all these crazy names?

As you can tell from my beautifully polished and modern website, I'm a web developer. I know virtually nothing about OpenGL or writing modern C++ but I have a VR headset and I want to draw things in it. This page is me documenting the process I'm currently going through to try and learn VR development with absolutely no experience.

Step 1: Look up vr programming

Results: nothing interesting. I don't really care about VR games so Unity/Unreal Engine tutorials (especially those in video form) aren't interesting to me but I've been using vr-video-player which works great and is open source so I decided to start there. The code is about 2.5k lines over basically 2 files and mostly meaningless to me. I see it's based on some OpenVR sample code that valve provided so I diff it and look at the changes. XOpenDisplay, XGetWindowAttributes, XFixesSelectCursorInput, XCompositeRedirectWindow, glXBindTextImageExt, it's all foreign to me. A quick search shows these are Xlib and OpenGL functions so I start with the Xlib documentation and try to reduce this application to its most basic parts ignoring VR for the moment.

Step 0: Give up on VR and learn some Xlib

XOpenDisplay(nullptr) is pretty much the first line of code and also the first roadblock I hit. I'm not completely useless so I man xopendisplay and see that it's to connect to the X server. It's about this point in time where I stumble upon this seemingly better Xlib manual and also decide that intellij isn't well suited for C++ development. I have never written a single line of C/C++ in an IDE before (only vim) so I download CLion and open the project in that after sorting out my color scheme and enabling IdeaVim. I'm immediately inundated with warnings and errors across my screen and CLion is asking me about some CMakeLists.txt file so I guess I have to address that issue before moving on to the actual code.

Step -1: Sidetracked by tooling

I vaguely know that cmake is a build tool. It might generate makefiles or something like that I don't really know. vr-video-player only has a simple build script but I need all the balls in my court to learn all this stuff at once so I'm feeling motivated to get this IDE working. The OpenVR hellovr_opengl sample code has a small 26 line CMakeLists.txt that I used as a reference for my next 26-step plan to fix my IDE project.

set(TARGET_NAME hellovr_opengl)

This must be setting an environment variable? Cross-referencing the cmake official reference I see that I only need a few items to create a basic project.

if(APPLE)

ignored

add_executable(${TARGET_NAME}
  ${SHARED_SRC_FILES}
  hellovr_opengl_main.cpp
)

I guess that you reference variables similarly to bash. This function looks like it's important so I substituted my source files in there. Going back to the code I see that CLion has only a few errors about missing header files but I can ctrl+click to go to function declarations now so we're making good progress. I know I need to add libraries and link them so I find the conveniently placed [add a library][5] step, search for link, then end up with:

cmake_minimum_required(VERSION 3.10)
project(XLib)

add_library(XLibDeps main.cpp)
target_link_libraries(XLibDeps GL X11 Xcomposite)

add_executable(Main main.cpp)
target_link_libraries(Main XLibDeps)

Astonishingly this works and I can compile/run some stuff. Back to step 0 with my newfound mastery of cmake.

Step 0: Give up on VR and learn some Xlib again

We can officially connect to the default X server and nothing else. The monumental challenge of learning all this is crumbling away by the minute. Now what do I do? I decided that the simplest place to start would be to learn how to create a window, draw some stuff on it, then project that window into VR. This is parallel to what vr-video-player does so I thought it would be a good way to learn while referencing something that actually works. Quick search shows XCreateWindow, perfect. Then XCreateSimpleWindow, perfecter. Now I have a Display but I need a parent Window so I got the root window with DefaultRootWindow.

-To be continued