color index generation for rasterizer

This commit is contained in:
wgroeneveld 2020-07-12 09:29:54 +02:00
parent 2ee8fe4e4a
commit 54b857bd9b
4 changed files with 28 additions and 9 deletions

View File

@ -6,9 +6,11 @@ This is (very) loosely based on David's 3D soft engine in C#/JS: https://www.dav
Engine blueprint: a stripped-down version of []( combined with more _tonc_ library functions.
#### Show me the money
### Show me the money!
Sure thing. **Demo 1**: without wires
Sure thing.
#### **Demo 1**: without wires
@ -17,7 +19,7 @@ You 'should' see a cube forming based on 8 vertices. It's a simple example to sh
60 FPS. Yay.
**Demo 1b**: with wires
#### **Demo 1b**: with wires
@ -28,7 +30,11 @@ MODE4's weird byte write problems are causing trouble. Time to consult _tonc_ an
30 FPS, winning 10 frames using `bmp8_line()`.
**Demo 2**: load actual vertex content from Blender/Babylon based on [this](
#### **Demo 2**: load actual vertex content
From Blender/Babylon based on [this](
A JS script has been provided that generates the needed C++ code. It's not great, and not very performant.
@ -41,7 +47,16 @@ Use the `gba-sprite-engine` instead.
I am aware of countless optimization opportunities but even thinking about that makes me sleepy.
This is a high-level C++ engine, meaning redundant stack objects could also cause problems. And I'm okay with that - it's a proof-of-concept!
#### GBA-Specific problems
#### **Demo 3**: rasterization
The `RasterizerRenderer` class draws triangles as 'fast' as possible, using horizontal scanlines.
There is a fast way to lines into VRAM. I tried implementing Z-buffering, but the buffer was too big and too slow as z-coords also had to be interpolated...
At this point, I do not think it's that interesting to go on to texture mapping other than the fun of it. Even with a lot of haxx and tricks, the colored monkey won't ever spin at 30FPS...
### GBA-Specific problems
**Fixed-point math** sums up things nicely.
@ -78,3 +93,4 @@ More details in `math.h`.
In any case, lots of rounding errors occur. It is luckily not a problem due to GBA's limited screen dimensions.
**Limited iWRAM** size is another problem. Use `const` arrays as much as possible!

View File

@ -5,8 +5,9 @@
#include <libgba-sprite-engine/background/text_stream.h>
#include "monkey.h"
const unsigned short pal[4] __attribute__((aligned(4))) = {
0x0000, 0xFFFF, 0x3AE2
// BLACK, [white->black] from 0xFFFF / 252
const unsigned short pal[253] __attribute__((aligned(4))) = {
0x0000, 65535,32767,21845,16383,13107,10922,9362,8191,7281,6553,5957,5461,5041,4681,4369,4095,3855,3640,3449,3276,3120,2978,2849,2730,2621,2520,2427,2340,2259,2184,2114,2047,1985,1927,1872,1820,1771,1724,1680,1638,1598,1560,1524,1489,1456,1424,1394,1365,1337,1310,1285,1260,1236,1213,1191,1170,1149,1129,1110,1092,1074,1057,1040,1023,1008,992,978,963,949,936,923,910,897,885,873,862,851,840,829,819,809,799,789,780,771,762,753,744,736,728,720,712,704,697,689,682,675,668,661,655,648,642,636,630,624,618,612,606,601,595,590,585,579,574,569,564,560,555,550,546,541,537,532,528,524,520,516,511,508,504,500,496,492,489,485,481,478,474,471,468,464,461,458,455,451,448,445,442,439,436,434,431,428,425,422,420,417,414,412,409,407,404,402,399,397,394,392,390,387,385,383,381,378,376,374,372,370,368,366,364,362,360,358,356,354,352,350,348,346,344,343,341,339,337,336,334,332,330,329,327,326,324,322,321,319,318,316,315,313,312,310,309,307,306,304,303,302,300,299,297,296,295,293,292,291,289,288,287,286,284,283,282,281,280,278,277,276,275,274,273,271,270,269,268,267,266,265,264,263,262,261,260
std::vector<Mesh*> MonkeyScene::meshes() {

View File

@ -66,6 +66,7 @@ void RasterizerRenderer::plotTriangle(const VectorFx& pt1, const VectorFx& pt2,
void RasterizerRenderer::render(const MatrixFx &transformationMatrix, const Mesh *mesh) {
bool colorSwitch = false;
int i = 0;
for (auto &face : mesh->faces()) {
auto &vertexA = mesh->vertices()[face.a];
auto &vertexB = mesh->vertices()[face.b];
@ -75,8 +76,9 @@ void RasterizerRenderer::render(const MatrixFx &transformationMatrix, const Mesh
auto pixelB = engine->project(*vertexB.get(), transformationMatrix);
auto pixelC = engine->project(*vertexC.get(), transformationMatrix);
plotTriangle(pixelA, pixelB, pixelC, colorSwitch ? 2 : 1);
COLOR cI = ONE + fxmul(fxdiv(int2fx(i), int2fx(mesh->faces().size())), int2fx(250));
plotTriangle(pixelA, pixelB, pixelC, fx2int(cI));
colorSwitch = !colorSwitch;

img/raster.gif Normal file

Binary file not shown.


Width:  |  Height:  |  Size: 66 KiB