Home Creators Posts Import Register Favorites Logout

Content

CHAPTER 93: What Hides and What Changes

George asked Daniel a question one Monday in May:

"If the player is looking north and there's a 3,000-triangle building behind him, how many triangles should the engine render?"

"Zero," Daniel said. "You don't render what isn't seen."

"Correct. That's frustum culling and we already have it. Now: if the player is looking north and there's a 3,000-triangle skyscraper in front of him, and behind the skyscraper there's a street with 30,000 triangles of buildings, cars, and NPCs the player can't see because the skyscraper blocks them, how many triangles should the engine render?"

"3,000. Just the skyscraper."

"Exactly. But the engine doesn't know that. The engine knows those 30,000 triangles are inside the camera's frustum. They pass the frustum culling because they're technically in front of the player. But they aren't visible because something covers them. The engine processes them, calculates their lighting, projects them to the screen, and only at the end, when it goes to write the pixels, does it discover they're behind other pixels and discards them. 30,000 triangles of wasted work."

"We need to know before rendering what's covered," Daniel said.

"We need occlusion culling."

*

Frustum culling was the first filter: it discarded everything outside the camera's field of view. It was fast and eliminated 50-60% of the world's geometry. But everything that survived the frustum culling was rendered, including what was covered by other objects.

George implemented a second filter: the hierarchical Z-buffer.

The normal Z-buffer was an image the size of the screen where each pixel stored the distance of the nearest object. Each time the renderer drew a triangle, it compared the distance of each pixel of the triangle with the distance stored in the Z-buffer. If the triangle's pixel was farther, it was discarded. If it was closer, it was written and the Z-buffer was updated. It was the standard mechanism that prevented distant objects from being drawn on top of near ones.

The hierarchical Z-buffer was a reduced version: a pyramid of Z-buffers of decreasing resolution. The highest level was the full 640x480 Z-buffer. The next was one of 320x240. Then 160x120. Then 80x60. Each level stored the maximum distance of each group of pixels of the lower level.

Before rendering an object, the engine calculated the rectangle the object would occupy on screen and compared it with the appropriate level of the hierarchical Z-buffer. If the nearest distance of the object was greater than the maximum distance stored in that region of the Z-buffer, the object was completely covered. It was discarded whole, without processing a single triangle. Without calculating lighting. Without projecting to the screen. Without wasting a single cycle of GPU.

The trick was the render order. For the hierarchical Z-buffer to work, the near objects had to be rendered first so their distances were in the Z-buffer when the far objects were evaluated. George rendered the scene from front to back: first the nearest objects, then the medium ones, then the far ones. The near ones filled the Z-buffer with short distances, and the far ones that were behind the near ones were discarded without being rendered.

The result was brutal. On a Downtown street with skyscrapers, the occlusion culling discarded 40% of the triangles that had survived the frustum culling. 40% less geometry the renderer didn't have to process. 40% less work for the GPU. 40% more margin for details, NPCs, vehicles, effects.

George measured the impact in a test scene that represented a Downtown intersection: without occlusion culling, the renderer processed 180,000 triangles. With occlusion culling, 108,000. Seventy-two thousand triangles eliminated by a system that asked a single question before rendering each object: is someone already there?

*

The occlusion culling for exteriors worked with the hierarchical Z-buffer. The interiors needed something else.

When CJ entered a building, the exterior world kept existing: the cars circulated, the NPCs walked, the sun moved. But none of that was visible from inside the building. Rendering it was waste. The engine needed to know that when the player was inside a building, everything outside shouldn't be rendered.

George implemented portals: invisible surfaces that marked the transitions between interior and exterior. A door was a portal. A window was a portal. Each portal had a property that told the engine what was on each side: if the player was on the interior side, the engine only rendered the interior geometry plus whatever was visible through the portals (the view out the window, the street through the open door). Everything else was discarded.

The portal system was conceptually simple but implementing it correctly was a labyrinth of special cases. What happened if the player was standing in the doorway, half inside half outside? The engine rendered both sides. What happened if a window showed another window of another building that showed the interior of that other building? George limited the portal depth to 2 levels: the view through a window rendered what the window showed, but what was seen through a second window within that view wasn't rendered. The player never noticed the limit because at two levels of depth the objects were too small to distinguish detail.

Elena tested the portal system with the Grove Street houses she was designing as interior spaces. She went into CJ's house, closed the door, and the framerate rose 40% because the engine stopped rendering the whole street. She opened the door and the framerate went back to normal: the street appeared framed by the rectangle of the portal, like a living painting inside the frame of the door.

"It's like a real window," Elena said. "I see the street through the door but only the part the door frames. Everything else doesn't exist."

"Everything else exists," George corrected. "It just isn't rendered."

"For the player it's the same thing."

"For the player it's the same thing," George conceded. Because in games, the difference between existing and being rendered was the difference between truth and perception, and the engine only needed to handle the perception.

*

With the occlusion culling working, George had a margin of triangles to spend. And the first thing he built with that margin was the sky.

Los Santos needed a complete day and night cycle. A real cycle, beyond the simple color-filter change of other games where the screen darkened and was called "night": the sun moving across the sky, the clouds drifting, the stars appearing, the moon with its phases, and the world's light changing every minute so that the same street looked different at eight in the morning, at noon, at five in the afternoon, and at midnight.

George implemented the sky dome: a giant sphere that wrapped the whole world and that served as a canvas for the sky. The sphere didn't have complex geometry: 32 horizontal segments and 16 vertical, enough for it to look curved without wasting triangles. The texture of the sky dome was a procedurally generated gradient that changed with the position of the sun.

The sun was a direction vector that rotated around the east-west axis at a speed of 15 degrees per game minute (360 degrees in 24 real minutes, which were 24 hours of the game). The position of the sun determined three things: the direction of the directional light of the lighting system, the color of the sky gradient, and the intensity of the ambient light.

George designed the sky gradient with 6 key points that corresponded to the most visually distinct hours of the cycle:

Six in the morning: the horizon orange-pink transitioning to pale blue at the zenith. The dawn light coming in horizontally, with very long shadows that shortened with each minute.

Ten in the morning: clean blue sky, high sun, medium shadows. The Los Santos morning light George remembered from the daytime missions of the original he carried in his memory: clear, sharp, without the smog that would come later.

Two in the afternoon: the sun started its descent. The LA smog (which George implemented as a haze that increased with the time of day) tinted the horizon a dirty yellow that blurred the distant buildings. It was the visual touch that separated Los Santos from any other city: the smog as an aesthetic element, the pollution as texture.

Five in the afternoon: the golden hour. The low sun painting everything orange. The shadows lengthening again. The Downtown buildings reflecting the sun in their glass facades. It was the moment Marcus had described as the visual soul of Los Santos, the moment that went on the cover and in the trailer and in every promotional screenshot: the LA sunset compressed into a sky gradient that went from orange to purple to dark blue.

Eight at night: the purple sky of late twilight. The stars starting to appear as white points with a gradual fade from 0 to 100% opacity in 5 real minutes. The lampposts lit. The neon of the stores and the bars activating. The city transforming from a place of work into a place of something else.

Midnight: black sky with stars. The moon (which George implemented as a textured disc with a phase that changed every 7 day/night cycles) lighting the city with a faint bluish light. The lampposts as the only sources of artificial light. The sharp shadows under the lampposts and the total darkness between them.

Six points. The engine interpolated between them to produce a continuous transition with no cuts or jumps. The player who sat on a bench in Santa Monica and looked at the sky saw the sun go down, the clouds tinge orange, the stars appear, the moon rise, and the lights of the city turn on, all in a continuum that lasted 24 clock minutes and that felt like a complete day compressed into the length of a television episode.

*

The lampposts turned on with the random delay of the lighting system: each streetlamp detected when the ambient light dropped below a threshold and activated with a delay of 0 to 5 seconds. The effect was that the lampposts didn't turn on like a master switch that lights everything at the same time but like a wave that ran through the city, each lamppost lighting at its own rhythm, some seconds before its neighbors, others seconds after, as if each lamppost had its own person inside who looked at the sky and decided when it was dark enough.

At night, the city transformed.

Not only visually. The NPCs changed behavior. The AI module George and the team were developing in parallel had a time parameter that modified the routines: during the day, the South Central NPCs were workers who went to their jobs and gang members who gathered on the corners. At night, the workers disappeared and the gang members multiplied. The Ballas patrolled more aggressively. The Grove Street Families concentrated on the corners of their territory. The civilian NPCs walked faster, looked over their shoulders, avoided the alleys.

The same street that during the day was a functional neighborhood with people going to work became at night a tense space where every shadow could contain a threat. Only the light and the behavior of the NPCs changed. The same buildings, the same streets, the same streetlights. But different because the hour was different, and the hour changed everything.

Lena took advantage of the cycle to design the dynamic audio she was planning: each hour of the day would have its own sound profile. Birds at dawn. Heavy traffic at eight. Construction at ten. Calm at three. Children playing at four. Crickets at dusk. Sirens at two in the morning. Each layer activated and deactivated according to the time of day in the game, blending with the other layers in a soundscape that evolved continuously.

"The city breathes," Lena said when George showed her the cycle working. "It doesn't just look different. It sounds different. It smells different, if I could smell. It's the same city but it's a different city every hour."

*

George played a complete day and night cycle in the test scene one Friday at seven in the evening.

Twenty-four minutes. Sitting in front of the monitor, looking at colored cubes with the sun of the lighting system traveling across the sky dome, the shadows rotating with the hour, the lampposts turning on at sunset, the stars appearing, the moon rising, and the bluish light of midnight turning the cubes into dark silhouettes against a sky of white points.

They were cubes, not Los Santos. But the cubes existed in a world that had time, that changed with the hours, that wasn't the same space at ten in the morning as at ten at night. And that difference was more important than any texture or any model because it meant that Los Santos wasn't going to be a static place. It was going to be a living place. A place where the player could do the same mission at noon or at midnight and where the experience would be different because the city changed around the mission.

George looked at the counters. The day/night cycle cost less than he expected: the sky dome gradient was procedural (no texture load), the rotation of the sun was a vector that updated every frame (negligible cost), and the change of color of the lighting was a linear interpolation between 6 points (less than a millisecond per frame). The most visible system in the engine was also one of the cheapest.

Daniel peeked over from his desk.

"How much does the day/night cycle cost?"

"Less than one percent of the frame time."

"One percent. For a system that makes the city look completely different every hour."

"The rotation of a vector and the interpolation of six colors. The light we already had does it. The day/night cycle only moves the sun. The light does the rest."

Daniel nodded with the approval of an engineer who appreciates when a complex system is built on top of simple systems instead of being complex in itself. The day/night cycle was a new way of using the lighting system that already existed. And that was the philosophy of NGN: each new system should be built on top of the previous ones, not next to them. The modularity was an architecture and also a way of thinking that made each new piece cheaper because it took advantage of the pieces that were already there.

Seven weeks of work. Six engine systems completed: renderer, lighting, streaming, LOD with impostors, occlusion culling, and day/night cycle. Seven more remained. But the six that were working already produced something George could see when he closed his eyes: a city of cubes that lit and darkened and that existed in a space that had depth and time and the promise that someday, when Marcus finished the buildings and Lena finished the sounds and Elena finished the streets, the cubes were going to be replaced by something that looked like a real place.

Six systems. Seven to build.

End of Chapter 93

Comments

No comments found for this post.