gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Mode 7 Graphics

#12942 - GunterPete - Mon Dec 01, 2003 9:52 pm

Hi there.

I'm trying to implement Mode 7 3D graphics based around DarkFader's tutorial at http://users.raketnet.nl/darkfader/http://darkfader.net/gba/files/Mode%207%20tutorial.txt. I'm using C++, and have implemented Vector and Matrix classes using fixed point maths.
I can determine the map coordinates for any given ray projected through the frustum. The only bit I don't understand is the final part, where this information is loaded into the GBA background registers.
Quote:

Put the left/right results (X/Z) into StartX, StartY, H_DiffX & H_DiffY into a BgAffineDestData array:

d->StartX = V2AFF(left3[0]); // simply does a divide by 256 (16:16 -> 24:8 fixed point)
d->StartY = V2AFF(left3[2]); // "
d->H_DiffX = V2AFF2(right3[0] - left3[0]); // same, but also includes a division by 240
d->H_DiffY = V2AFF2(right3[2] - left3[2]); // "

Interrupt & DMA the rotation/scaling values
-------------------------------------------
Set up a V-count interrupt at scanline 0 or something...

void VCountIntr()
{
rDMA0CNT_H = 0; // stop DMA
rDMA0SAD = bgAffineDestData;
rDMA0DAD = (void *)&rBg2Affine;
rDMA0CNT = DMA_ENABLE | DMA_TIMMING_H_BLANK | DMA_16BIT_BUS | DMA_CONTINUOUS_ON | DMA_SRC_INC | DMA_DEST_RELOAD | 8;
}



Can anyone help explain this to me? Or are there any other usefull tutorials on Mode 7 graphics for the GBA (I can't seem to find any other than this one).

Thanks in advance (No pun intended).
-Pete Gunter

#12944 - poslundc - Mon Dec 01, 2003 11:40 pm

It might help if you were a bit more specific about what part you don't understand... do you not understand the principle behind the Mode7 technique, do you not understand DMA, do you not understand the HBlank-reloading aspect of DMA, etc.

Basically that chunk of code sets up an automated hardware feature in the GBA to copy your precalculated rot/scale values into the GBA's background control registers on every HBlank.

If you are unfamiliar with DMA, I suggest you check out Day 5 of The Pern Project Tutorials for a good explanation of how it works.

Dan.

#12946 - GunterPete - Mon Dec 01, 2003 11:53 pm

You're right, I should have made myself clearer.

My problem is more that I don't understand how the rotation and scale values are calculated. I can calculate the 2Dmap coordinates of the left and right pixels of the scanline (Is it best to calculate these whenever the camera moves, and store them in arrays?. I just don't understand how to get the values for REG_BG2PA-D and REG_BG2X-Y from these coordinates.

In the code I'm not sure what V2AFF or V2AFF2 do. Also, what kind of structure is a BgAffineDetData?

Thanks,
-Pete Gunter

#12948 - poslundc - Tue Dec 02, 2003 12:38 am

The tricky part with answering your questions is that the tutorial you've used clearly has its own way of going about the process of calculating the register values. The basic Mode7 technique doesn't require anything more than dividing "y" by "x" to obtain a "z" value you can use on each scanline for the scale data.

I can't tell you what V2AFF or V2AFF2 do. From the name they likely do a vector-to-affine-register conversion, but how that conversion takes place is anyone's guess; presumably there is a code listing in the tutorial somewhere.

BgAffineDestData is most likely a structure that mimics the layout of the background registers in memory: four half-words and two words. An array of 160 of these could then be used to represent values you want to use for every scanline of the screen.

I am going to suggest that you instead have a look at the Pern Project's Mode7 tutorial instead; it is more focussed on the actual Mode7 technique and doesn't try to implement raycasting. Raycasting tutorials for the GBA are a bit of a tease: they spend 90% of the time on raycasting and make you think you've accomplished everything once you understanding how their system works, but when it finally comes time to mixing it with Mode7 it turns out that the raycasting is the easy part. :)

Once you've got a better grasp of Mode7 you can give raycasting a shot, but I'll warn you right now that when I tried it I found that I couldn't really wrap my head around anyone else's tutorial, and eventually needed to combine the two on my own.

Good luck,

Dan.