planet texture mapping rewrite

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andrew Davie


I've rewritten the texture mapping on the planet drawing, and also significantly optimised that code.  The planet can now zoom to full-screen - with zooming around the centerpoint instead of the top edge as in previous implementations.

Further, I discovered a bug in the mapping -- a 5-trixel wide vs 4-trixel wide discrepancy in my logic.  So now the textures are much nicer.

And finally this version implements a mathematically correct globe mapping instead of the hand-wrangled estimate that I had before.

Overall effect -- much less blockiness when zoomed close; at least the smoothness of the movement is better, albeit low resolution. I might improve the resolution -we shall see.

In any case, a big improvement. In this video I go through all 10 of the planets to show them off.


Andrew Davie

I've added an "anti-shimmer" filter on the texture mapping.

Essentialy, the planet looks far smoother at distance; we don't get a lot of pixels popping on and off as it rotates. This is probably about as far as I care to take these optimisations.


Andrew Davie

Some significant updates to this

1) Prior versions used a character-based source format. That was because when I originally wrote this code years ago, it was intended to display the playing board -- that is the M*N playing area.  The board would literally be wrapped around the surface of a sphere.  That all worked splendidly but set limitations, most particularly on the complexity of the textures that could be displayed.  Since this game didn't actually use the board, but instead planet textures, those textures had to be converted into a character set.  I'd tried a whole lot of combinations of shared character sets, dropping N pixels as a sort of approximation, individual character sets with set character limits, and more. They all worked to various degrees.

But I got to thinkning - well, why not bitmaps?  And so I tried that and it worked, and it actually saved ROM space (because characters only used 4 bits/byte) whereas a bitmap uses all 8.  Then I got to thinking - well, what sort of image compression can I apply to these bitmaps to make them smaller. Some investigation later and I'm now decompressing the bitmaps to RAM and then displaying from RAM.

2) I've added a rotating "terminator" and using stippling for the planet shadow.  It's not perfect, but it's different and to some degree gives the planet a more "solid body" feel. To me, anyway.

3) I've changed the sequence so it's not just a planet oscillating back and forth. Now there's a definite "approach" and a definite "depart" feeling.

Overall, much to my surprise, these changes have combined to save of 16 KB of ROM.


Andrew Davie

#3
Here's a demo showing all the planets.

Use left/right to change rotation speed. Button will skip to next planet.

I've improved, even more, the smoothness of the texture. Given the low resolution of the PF it's quite amazing.  Also, there's a night hemisphere and terminator which shows up just before the planet leaves. We effectively do an orbit of the planet - even the star-field moves!



Andrew Davie

Here's the planet sequence running through all 10 planets.

Although hard to believe, there is ***ONLY ONE COLOUR ON ANY SCANLINE ***.  The game uses complex multiplexing to achieve the perceptual trick giving more colours.

The shadow on the hemisphere opposite the sun is/was extremely tricky to draw. You see it as the planet is receding. I've gone through many many ideas to get what you see here. Not perfect, but good enough I think.


Bomberman94

Wow - very smooth movement of the planets surface. Regarding the "shadow scenery" - maybe instead of black and a dark tone of the planet, you can use a very dark tone and a dark tone?

Andrew Davie

Quote from: Bomberman94 on 13 Oct 2026, 10:04 PMWow - very smooth movement of the planets surface. Regarding the "shadow scenery" - maybe instead of black and a dark tone of the planet, you can use a very dark tone and a dark tone?

There are *ZERO* tone colours available. I can only get the shadow by not-drawing some pixels/scanlines.  It's a very tricky challenge.

Andrew Davie

I managed to sneak in a "decent" moon orbiting the planet.


Andrew Davie

Here's even more work on the moon. It sticks with the planet during the zoom in/out. It rotates independently. Speeds of rotation and orbit are randomised.  It's really pushing the limits now.


Binary for you to play with!  Spin the planet faster with left/right.  Skip to the next planet with the fire button.


Andrew Davie

HOW THE GLOBE SCREEN DRAWS THE PLANET AND MOON

THE DISPLAY AND CHRONOCOLOUR

The globe kernel shows a playfield image built from four byte-column buffers in display RAM: PF1 and PF2 for the left half, and PF1 and PF2 for the right half. PF0 isn't used, so the picture is 32 columns wide. The kernel mirrors the right half in hardware with the CTRLPF reflect bit, so the ARM stores the right-hand buffers bit-reversed, and the hardware reverses them back. Each buffer has one byte per scanline. The ARM writes the buffers and the kernel streams them out through the data streams. Text sits in a separate set of player graphics buffers.

One picture row (a "trixel" row) is three scanlines tall, and that's how colour works. Each of a pixel's three scanlines shows one colour plane, and the playfield colour is set per scanline from a buffer filled each frame with the planet's three primary colours. A pixel's colour is therefore which of its three scanlines are lit: a 3-bit index into the eight mixtures of the three primaries. Each frame a value called the roller moves on, rotating which plane lands on which scanline, and the palette buffer is filled in the same rotated order. Over a few frames each scanline cycles through all three primaries, and the eye blends them into the mixture. How many phases the roller has (fixed, two or three) depends on the chronocolour mode chosen.

Everything that puts pixels on this screen follows the same rule: plane p goes on scanline (p minus roller) mod 3. That applies to the planet, the stars and the moon, and it's what lets them all share the same palette and the same three scanlines.

THE PLANET

Each planet's texture is stored compressed (LZSS) in ROM and decoded into ARM RAM at the start of a visit. The decode is spread over the first few frames: decompress a slice per frame, then expand each row so that it wraps round past its own end, which means a texel window read never has to deal with the wrap. The planet isn't drawn at all until the whole texture is ready, so a frame never shows half a planet. Texture rows are pre-warped: row i holds exactly the latitude that disc row i shows at full size, so only longitude has to be projected at run time. Each texel has three bit-planes, matching the chronocolour planes directly.

The planet is drawn one half at a time, row by row:
1. Find the texture row for this screen row by stepping through the texture at the current zoom, anchored on the disc's centre row so the planet grows and shrinks about its middle.
2. Find the row's silhouette (its half-chord) from a table.
3. Project each output column onto the sphere: its longitude is the arcsine of its distance from the centre over the half-chord, taken from tables. This builds a gather map from source texel to output column, rebuilt only when the half-chord changes from the row before.
4. Read a window of texels around the current scroll position and gather each plane through that map.
5. When zoomed out far, filter each plane with a three-texel majority vote, horizontally and between rows, to stop shimmer.
6. If a row would repeat the previous row's result, copy it instead of recomputing.
7. Clear the rows above and below the disc every frame, since the disc's height changes with zoom.

Rotation is a scroll position advanced each frame by a speed scaled to the texture's width. The joystick nudges that speed. The zoom eases the planet in from far away on arrival. On departure it holds at full size for a while, then accelerates away.

SHADOWS (THE DAY AND NIGHT LINE)

The sun has a direction around the planet. It sits behind the viewer, so the planet is fully lit, for most of the visit, and starts circling once the departure begins. The cosine of the sun's angle gives a single threshold for the frame. For each pixel, its signed position across its row's half-chord, mirrored according to which side the sun is on, is compared with that threshold; anything at or below it is night. There's no per-pixel trigonometry: each row's positions come from the same projection already used to fetch the texture.

Night pixels are darkened by blanking one of each trixel's three scanlines. It's always the same scanline position, so the plane removed rotates with the roller. This darkens the pixel without the flicker you'd get from a separate blanking pattern beating against the colour roll. A twilight band and a checkerboard stipple also exist as options; both are off.

While drawing, the planet also records for each row which output columns are inside the disc (the disc mask), separately for each half. This is what keeps stars and a moon behind the planet hidden even where the planet's night side is dark and looks empty.

The stars drift as the sun circles, so the sky appears to turn with it. They're drawn last. A star is never drawn over a lit pixel or inside the disc mask, so it always appears behind both the planet and the moon.

THE MOON: ITS ORBIT

The moon's orbit position is a high-resolution phase counter, advanced by the orbit speed every frame. Its position comes from sine and cosine of that phase, blended between entries of a quarter-wave table, so it moves smoothly at any speed; only the final screen position is rounded to whole columns and rows.

1. Sine gives the left and right offset. Cosine gives the up and down offset, because the orbit is tilted towards the viewer: the moon passes low in front of the planet and high behind it. Cosine also says which half of the orbit it's on.
2. Both offsets, and the moon's size, are multiplied by a perspective factor for a viewer at a fixed distance. The factor comes from a table, interpolated the same way. That's why the moon looks bigger and sweeps faster when it crosses in front, and is smaller and slower behind.
3. Both offsets and the size are also multiplied by the reciprocal of the planet's zoom, so the moon follows the planet in and out during the arrival and departure. The reciprocal is found without division: shift the zoom value down into a fixed range, interpolate a reciprocal table, then shift back.

Each visit picks the moon's orbit direction (it follows the planet's spin), its orbit speed, its spin speed and direction, its starting rotation, and which of the five moons it is. During the arrival the zoom spreads the orbit outward, so the moon starts half of the arrival's arc before its front crossing. That way the zoom and the orbit carry it the same way and it never appears to change direction. During the zoom the orbit speed is capped, and it eases to the visit's own speed afterwards.

When the moon is in front, it's drawn over the planet. When it's behind, it's held back by the disc mask and never drawn over a lit pixel, the same rule the stars follow.

THE MOON: ITS APPEARANCE

Each moon type is a small texture: one bit per texel (surface or marking), a fixed number of texels around, with rows pre-warped by latitude the same way as the planet's, plus its own surface and marking colours. For every disc diameter from one column up to sixteen, a projection table built ahead of time lists, for each row, the first column inside the disc, how many columns are inside, and which texture row holds that latitude. For each pixel it gives a shift amount that picks the pixel's texel out of a 32-texel window.

Each frame, the moon's apparent diameter (perspective times zoom) picks the nearest disc. Its rotation comes from its own spin counter, independent of the orbit, so the spin can be any speed or direction while still stepping one texel at a time.

THE MOON: REAL-TIME RENDERING WITH TWO BUFFERS

The moon image is rendered at run time into one of two buffers. Both live in the globe's overlay RAM: memory that the game screen otherwise uses for particles. The globe only borrows it, and everything in it is set up again at the start of each visit.

One buffer is the image on screen. The other is the one being built. A build starts whenever the wanted size or rotation differs from the image on screen, and it uses the size and rotation wanted at the moment it starts. Rendering one row takes two steps:
1. Funnel-shift the texture row by the rotation into a 32-texel window, a single operation per row.
2. For each pixel, one shift and one AND pick out its texel, using the pixel's shift from the projection table.

The rendered rows are just masks. When every row is done, the finished buffer swaps in at the start of the next frame, never in the middle of one. That way both halves of the screen always show the same image within a frame.

When there's little time to spare, a build simply takes more frames. The moon goes on showing the previous image a little longer, so rotation and size change in bigger steps. It never overruns.

Drawing the on-screen image (compositing) takes, for each row:
1. Shift the inside mask and marking mask to the moon's screen column.
2. Split them into the playfield bytes, reversing bits for the buffers that need it.
3. For each byte, do a read-modify-write of its three scanlines, putting in the surface or marking colour's planes in rolled order.

In front, the moon's pixels simply replace whatever is there. Behind, they're first masked by the disc mask and by any pixels already lit.

THE OVERSCAN AND VERTICAL BLANK SPLIT

Each displayed frame is built across one overscan (OS) and the following vertical blank (VB). The kernel then shows the result.

In OS, in this order:
1. Move the chronocolour roller on and update the fade level.
2. planetAdvance, which runs exactly once per frame: rotation, zoom (stepped twice to keep the old speeds), the sun and stars during the departure, the moon's orbit and spin, and a slice of texture decoding while that's still in progress.
3. moonPrepare: swap in a finished moon image if there is one, work out the moon's position, front or back, wanted size and rotation, and start the next build if needed.
4. Choose which screen half to draw in OS: the half the moon's centre is on.
5. Draw that half of the planet.
6. Run the text: typing the next character, and the information-phase sequence.
7. Last of all, drawMoonOS:
   - Draw the OS half of the moon, if there's time; otherwise leave it for VB.
   - Spend whatever OS time is left rendering rows of the next moon image.
8. The general scheduled tasks run after the globe code.

In VB, in this order:
1. Set up the data streams and fill the colour buffer in rolled order.
2. Draw the other half of the planet.
3. Draw the moon's VB half, plus the OS half if it was left over.
4. Draw the stars.
5. Read the joystick.

Why it's split like this:
- Everything that changes from frame to frame advances only once, in OS, so both halves of a frame use exactly the same rotation and zoom. Advancing in both halves used to put a seam down the middle.
- Each half of the moon has to be written after the planet half beneath it, or the planet would overwrite it. That's why the moon is split by screen half and not by rows.
- VB has less time than OS, and each planet half nearly fills a VB on its own. Drawing whichever planet half the moon is mostly over in OS means most of the moon's cost lands in OS. VB is left with one planet half, the moon's smaller share and the stars.

TIMING AND HOW IT ADAPTS

The fixed costs are the two planet halves, which vary with zoom and how many rows can be copied. The variable costs are the text (the information-phase switch has a one-frame spike) and the moon.

Every optional step decides whether to run by checking the frame timer against the frame's available time, minus an estimate of its own cost and a reserve for what still has to run after it, mainly the scheduled tasks:
- The OS half of the moon checks using its row count times a per-row cost.
- The moon renderer checks before each row.

Both checks come after the text has run, so they see the time actually left rather than a guess. On a heavy frame the OS moon half moves to VB and fewer moon rows get rendered. On a light frame the renderer takes up the slack. The overall result is that the moon's update rate adjusts itself to whatever time is left, while the fixed work is always protected.

Other timing details:
- Texture decoding is spread over several frames rather than done at the hand-over to the globe screen, because doing it all at once used to overrun that overscan.
- SaveKey writes are held off while the planet is large, because a SaveKey write takes a big bite out of overscan and the full-size planet can't absorb it. They go through while the planet is small or still decoding.

When GLOBE_TIMING is on, it records:
- the worst time for each segment of each half;
- the smallest headroom left in each half;
- the moon's drawing time in OS and in VB;
- the most frames any moon image took to build;
- full snapshots of any call that overran.

That's how the current balance was measured, and how to re-check it after future changes.

HOW MEMORY IS USED

Display RAM holds what the kernel streams: the four playfield column buffers, the per-scanline colour buffer and the text buffers. The ARM rewrites all of these every frame.

ARM RAM holds the current planet's decoded texture, the disc masks for each row and half, and the moon's state: the orbit and spin counters, the image on screen and the build in progress. The two moon image buffers and the star list are in the globe overlay, which is borrowed from memory only the game screen uses and set up again for each visit, so they cost almost no extra RAM.

ROM holds the compressed planet textures, the moon textures and their colours, the moon's projection tables, and the small tables used to avoid division: quarter-wave sine, perspective and reciprocal.

There's no division anywhere at run time. Wherever a ratio is needed, it's a table plus interpolation, or a shift.

Andrew Davie

Trying an alternate shadow. It's complex, as I have just a single colour on any scanline with this graphics system.  I've tried a lot of alternaties.  In this demo, the moon now has 1 of 4 different textures, and also has a shadow applied.

There's a sun in the background -- well actually for most of the planet view it's directly behind you - which is why you see full disc with no shadows on the moon/planet.  But during the exit sequence, you start orbiting the planet (as opposed to the planet spinning below you while you're stationary), and so you see the sun and the stars move.

It's all mathematically correct, insofar as the shadows accurately reflect the direction sunwards. 

I've pushed this system really far - probably about time to start working on something else.  But it's been fun.