LJYC

2023

A multi-user online viewer for 3D scenes

When you discuss a 3D model with someone remotely, screenshots alone often make it hard to say which angle you mean; if several people could join the same room, sync straight to each other’s camera viewpoint and talk in text alongside, communication would go much more smoothly.

Albedo is my undergraduate thesis project (the thesis was rated a university-level outstanding undergraduate thesis). I did not build it directly on an existing game engine; I wanted to use the thesis to write both parts, graphics rendering and network communication, myself: underneath I wrapped a Vulkan rendering library and an ASIO-based asynchronous network library, and on top of them built four programs that work together, the render client, the lobby client, the lobby server and the room server.

Chapter 1

View sync and chat inside a room

The core features of working together are view sync and text chat. After joining a room, each render client sends its current camera’s view matrix (16 floats describing the camera’s position and orientation) to the room server up to about 120 times a second, and the server forwards it unchanged to every member of the room. Check Sync Online Camera in the UI, choose the target member under Who's View, and the local image simply uses the view matrix that person sent. Text chat and the room’s member list are forwarded through the room server in the same way.

Drag a camera on the stage, or drag one of the three views; either way you turn that person’s own camera.

Red Field of view 60°
Green Field of view 80°
Blue Field of view 45°

The view matrix red sends this frame: 16 floats

Goes through the room server

  • Everyone’s view matrix
  • Text chat
  • The list of people in the room

In the interactive demo above, drag the three cameras and sync green and blue to red, and the three beams shine from the same position and add up to white. While synced, a member’s own camera stays where it was; rendering just temporarily uses the other person’s view matrix. And because each keeps its own field of view, the same viewpoint still covers a different extent in the three windows. The two screenshots below, from chapter 6 of the thesis, show two real clients syncing camera viewpoints after talking it over in the chat bar.

Thesis figure 6-11: the full interface of an Albedo render client. On the left are Toolkit and Net State (two people in the room), in the middle is a cube in Main Scene with a photo of a watchtower on it, on the right are Camera Parameters and Light Parameters, and along the bottom are Vulkan Context, Console and User Inputs. The Console shows two chat lines: Please sync my camera :) and Ok.

Thesis figure 6-11 · Chat

The first person’s render client in full. The Console at the bottom is the chat: they sent “Please sync my camera :)” and the second person replied “Ok”. Net State on the left lists the two people in the room, and the Online Functions section sits at the very bottom of Camera Parameters on the right. The Vulkan Context panel shows that the GPU is an RTX 2070 and the swapchain (the set of images the window displays) is 1920 × 1017.

Thesis figure 6-12: two Albedo render client windows overlapping. The window behind has Sync Online Camera checked, with the other account selected under Who's View, and its camera position is 0, 0, -5; the window in front has its camera at 1.373, 1.081, -4.165. The cubes with the watchtower photo in the two Main Scene views are at the same angle.

Thesis figure 6-12 · Synced viewpoint

The window behind is the second person’s, with Sync Online Camera checked and the first person selected under Who's View. Its own camera parameters still read (0, 0, −5), yet the cube in its Main Scene is at the same angle as in the window in front (camera at 1.373, 1.081, −4.165).

Chapter 2

Vulkan rendering and AlbedoRHI

To put the 3D scene together with camera parameters, lighting controls, network status and the chat bar in one working interface, the client uses the docking branch of Dear ImGui for dockable windows. Each frame first draws the glTF model in the forward render pass (parsed with tinygltf; the loading and drawing code is adapted from Sascha Willems’s Vulkan examples), then copies it with vkCmdBlitImage into a texture embedded in the Main Scene window, and finally draws the panels in the UI pass and presents. The render loop lets the CPU run at most two frames ahead of the GPU, and each frame has one fence, four semaphores and two command buffers.

  1. Forward render pass

    Draws the glTF scene into a swapchain image

  2. vkCmdBlitImage

    Copied every frame into a texture for ImGui

  3. UI pass

    Puts the texture in the Main Scene window, then draws the panels

  4. Present

    Handed to the window

The path of one frame.

To keep the tedious raw Vulkan calls contained, I split the rendering wrapper out into its own library, AlbedoRHI, which mainly handles three kinds of low-level work:

Working out pipeline layouts with SPIRV-Reflect

When writing a graphics pipeline, the descriptor set layouts (the buffers and textures the shaders need) and push constants (a small block of data passed directly with each draw) do not have to be written by hand every time. If a subclass does not declare them explicitly, the base class uses SPIRV-Reflect to parse the compiled SPIR-V bytecode and automatically extract descriptor set 0 (the uniform buffers for the camera and lights), descriptor set 1 (material textures) and the push constant (the model matrix, already multiplied down the node tree), and from them generates the complete pipeline layout. The client’s PresentPipeline is built through this mechanism.

Image objects manage their own layout

An image object records its current memory layout internally and, when the layout needs to change, works out the matching barrier and pipeline stages automatically; GPU memory allocation is handled throughout by VMA (Vulkan Memory Allocator).

Per-thread pools and asynchronous loading

Command pools and descriptor pools are created lazily per thread; at startup the program reads the glTF model file on a background thread through std::async.

Chapter 3

ASIO asynchronous networking and a custom message protocol

Underneath, the four programs share one network library called AlbedoNet, built on standalone ASIO. async_accept, async_connect, async_read and async_write are all handled asynchronously by an io_context running on its own thread, without blocking rendering or the main loop. Once a connection is established there is a simple handshake: the server sends a random number, the client returns “random number XOR version number”, and the connection only counts if the check matches. A complete message that has been read is written into a queue protected by a lock and a condition variable; the render client consumes messages on a separate worker thread, while the lobby and room servers process them on the main thread, which blocks waiting. This basic skeleton of Session, Server, Client and a thread-safe queue follows javidx9’s olc::net tutorial framework; on top of it I added a checksum in the message header, HandlerPool for dispatching by message ID, and modules that hash with Crypto++ and send email with libcurl.

Each network message consists of a fixed 12-byte header and a variable-length body. The header holds three 32-bit integers: the message ID, the body length and the body’s Adler32 checksum (computed with Crypto++); the body is serialized with Protocol Buffers, which avoids byte-order problems between platforms. On receiving data, the receiver recomputes Adler32 and drops the message if it does not match the header (the lobby server replies with RESEND). Messages that pass are dispatched by message ID: the lobby server’s HandlerPool locates the handler by “message ID ÷ 100” (sign-up in the 100 range, login in the 200 range, room registration in the 1000 range); in the room protocol, joining a room is 2, a view matrix is 100 and chat is 201. In the packet demo alongside, you can click “Flip one bit in transit” to try how the checksum catches it.

Sent

Message ID
Length
Adler32

Received

Recomputed Adler32

The integers in the header are shown in little-endian order.

Chapter 4

The full connection flow of the four programs

The whole online viewing flow is handled in stretches by four programs: the lobby server takes care of account management and room registration, with user accounts kept in MySQL’s user table; at sign-up it sends a 6-digit verification code in an HTML email through libcurl over SMTPS, and writes to the database once the code checks out. The lobby client handles login and shows the room list, refreshed every 3 seconds; clicking Launch writes the target address, port, passphrase and user information into a one-time config.json and starts the render client. The room server first registers with the lobby server when it starts, then forwards the view matrices and chat of everyone in the room on port 5201. Step through the connection diagram below to see the whole sequence of interactions as two users go from sign-up and login to joining the same room and syncing viewpoints.

1/ 11

    Every message starts with the 12-byte header from the previous chapter.