diff options
author | StapleButter <thetotalworm@gmail.com> | 2017-02-11 03:54:08 +0100 |
---|---|---|
committer | StapleButter <thetotalworm@gmail.com> | 2017-02-11 03:54:08 +0100 |
commit | 050ba5dfbe823672bbb1fdd45afa3b3b5f16f7b3 (patch) | |
tree | ca4a647d5a3ef67d7fb5e4c81361f2e2f68f4e77 | |
parent | 78f49d061a1c5d1c3056244db42f2f00bed29c6b (diff) |
some debuts of 3D drawing.
it can draw non-strip polygons, filled with a fixed color. oh and it doesn't work if they're facing back.
-rw-r--r-- | GPU2D.cpp | 21 | ||||
-rw-r--r-- | GPU2D.h | 1 | ||||
-rw-r--r-- | GPU3D.cpp | 5 | ||||
-rw-r--r-- | GPU3D.h | 4 | ||||
-rw-r--r-- | GPU3D_Soft.cpp | 143 | ||||
-rw-r--r-- | melonDS.depend | 18 |
6 files changed, 183 insertions, 9 deletions
@@ -290,7 +290,7 @@ void GPU2D::DrawScanlineBGMode(u32 line, u32* spritebuf, u16* dst) if (DispCnt & 0x0100) { if ((!Num) && (DispCnt & 0x8)) - {} // TODO + DrawBG_3D(line, dst); else DrawBG_Text(line, dst, 0); } @@ -333,6 +333,25 @@ void GPU2D::DrawScanline_Mode1(u32 line, u16* dst) } +void GPU2D::DrawBG_3D(u32 line, u16* dst) +{ + // TODO: scroll, etc + + u8* src = GPU3D::GetLine(line); + for (int i = 0; i < 256; i++) + { + // TODO: color buffer should be 18bit!! + + u8 r = *src++; + u8 g = *src++; + u8 b = *src++; + u8 a = *src++; + if (a == 0) continue; + + dst[i] = (r >> 1) | ((g >> 1) << 5) | ((b >> 1) << 10); + } +} + void GPU2D::DrawBG_Text(u32 line, u16* dst, u32 bgnum) { u16 bgcnt = BGCnt[bgnum]; @@ -59,6 +59,7 @@ private: template<u32 bgmode> void DrawScanlineBGMode(u32 line, u32* spritebuf, u16* dst); void DrawScanline_Mode1(u32 line, u16* dst); + void DrawBG_3D(u32 line, u16* dst); void DrawBG_Text(u32 line, u16* dst, u32 num); void DrawBG_Extended(u32 line, u16* dst, u32 bgnum); @@ -1169,6 +1169,11 @@ void VBlank() NumPolygons = 0; } +u8* GetLine(int line) +{ + return SoftRenderer::GetLine(line); +} + u8 Read8(u32 addr) { @@ -36,6 +36,8 @@ typedef struct } Polygon; +extern s32 Viewport[4]; + bool Init(); void DeInit(); void Reset(); @@ -45,6 +47,7 @@ void CheckFIFOIRQ(); void CheckFIFODMA(); void VBlank(); +u8* GetLine(int line); u8 Read8(u32 addr); u16 Read16(u32 addr); @@ -61,6 +64,7 @@ void DeInit(); void Reset(); void RenderFrame(Vertex* vertices, Polygon* polygons, int npolys); +u8* GetLine(int line); } diff --git a/GPU3D_Soft.cpp b/GPU3D_Soft.cpp index 38d3539..9c057e9 100644 --- a/GPU3D_Soft.cpp +++ b/GPU3D_Soft.cpp @@ -32,7 +32,7 @@ u8 ColorBuffer[256*192 * 4]; bool Init() { - // + return true; } void DeInit() @@ -46,9 +46,148 @@ void Reset() } +void RenderPolygon(Polygon* polygon) +{ + int nverts = polygon->NumVertices; + + int vtop, vbot; + s32 ytop = 191, ybot = 0; + s32 scrcoords[10][3]; + + // find the topmost and bottommost vertices of the polygon + + for (int i = 0; i < nverts; i++) + { + Vertex* vtx = polygon->Vertices[i]; + + s32 scrX = (((vtx->Position[0] + 0x1000) * Viewport[2]) >> 13) + Viewport[0]; + s32 scrY = (((vtx->Position[1] + 0x1000) * Viewport[3]) >> 13) + Viewport[1]; + if (scrX > 255) scrX = 255; + if (scrY > 191) scrY = 191; + + scrcoords[i][0] = scrX; + scrcoords[i][1] = 191 - scrY; + scrcoords[i][2] = 0; // TODO: Z + + if (scrcoords[i][1] < ytop) + { + ytop = scrcoords[i][1]; + vtop = i; + } + if (scrcoords[i][1] > ybot) + { + ybot = scrcoords[i][1]; + vbot = i; + } + } + + // draw, line per line + + int lcur = vtop, rcur = vtop; + int lnext, rnext; + s32 lstep, rstep; + //s32 xmin, xmax; + + lnext = lcur + 1; + if (lnext >= nverts) lnext = 0; + rnext = rcur - 1; + if (rnext < 0) rnext = nverts - 1; + + /*if ((scrcoords[lnext][1] - scrcoords[lcur][1]) == 0) lstep = 0; else + lstep = ((scrcoords[lnext][0] - scrcoords[lcur][0]) << 12) / (scrcoords[lnext][1] - scrcoords[lcur][1]); + if ((scrcoords[rnext][1] - scrcoords[rcur][1]) == 0) rstep = 0; else + rstep = ((scrcoords[rnext][0] - scrcoords[rcur][0]) << 12) / (scrcoords[rnext][1] - scrcoords[rcur][1]);*/ + + //xmin = scrcoords[lcur][0] << 12; + //xmax = scrcoords[rcur][0] << 12; + + for (s32 y = ytop; y <= ybot; y++) + { + if (y == scrcoords[lnext][1] && y < ybot) + { + lcur++; + if (lcur >= nverts) lcur = 0; + + lnext = lcur + 1; + if (lnext >= nverts) lnext = 0; + + //lstep = ((scrcoords[lnext][0] - scrcoords[lcur][0]) << 12) / (scrcoords[lnext][1] - scrcoords[lcur][1]); + //xmin = scrcoords[lcur][0] << 12; + } + + if (y == scrcoords[rnext][1] && y < ybot) + { + rcur--; + if (rcur < 0) rcur = nverts - 1; + + rnext = rcur - 1; + if (rnext < 0) rnext = nverts - 1; + + //rstep = ((scrcoords[rnext][0] - scrcoords[rcur][0]) << 12) / (scrcoords[rnext][1] - scrcoords[rcur][1]); + //xmax = scrcoords[rcur][0] << 12; + } + + s32 lfactor, rfactor; + + if (scrcoords[lnext][1] == scrcoords[lcur][1]) + lfactor = 0; + else + lfactor = ((y - scrcoords[lcur][1]) << 12) / (scrcoords[lnext][1] - scrcoords[lcur][1]); + + if (scrcoords[rnext][1] == scrcoords[rcur][1]) + rfactor = 0; + else + rfactor = ((y - scrcoords[rcur][1]) << 12) / (scrcoords[rnext][1] - scrcoords[rcur][1]); + + s32 xmin = scrcoords[lcur][0] + (((scrcoords[lnext][0] - scrcoords[lcur][0]) * lfactor) >> 12); + s32 xmax = scrcoords[rcur][0] + (((scrcoords[rnext][0] - scrcoords[rcur][0]) * rfactor) >> 12); + + for (s32 x = xmin; x <= xmax; x++) + { + u8* pixel = &ColorBuffer[((256*y) + x) * 4]; + pixel[0] = 0; + pixel[1] = 63; + pixel[2] = 0; + pixel[3] = 31; + } + } + + // test + /*for (int i = 0; i < nverts; i++) + { + Vertex* vtx = polygon->Vertices[i]; + + s32 scrX = (((vtx->Position[0] + 0x1000) * Viewport[2]) >> 13) + Viewport[0]; + s32 scrY = (((vtx->Position[1] + 0x1000) * Viewport[3]) >> 13) + Viewport[1]; + if (scrX > 255) scrX = 255; + if (scrY > 191) scrY = 191; + + u8* pixel = &ColorBuffer[((256*scrY) + scrX) * 4]; + pixel[0] = 0; + pixel[1] = 63; + pixel[2] = 0; + pixel[3] = 31; + }*/ +} + void RenderFrame(Vertex* vertices, Polygon* polygons, int npolys) { - // + // TODO: render translucent polygons last + + for (int i = 0; i < 256*192; i++) + { + ((u32*)ColorBuffer)[i] = 0x1F000000; + } + + for (int i = 0; i < npolys; i++) + { + RenderPolygon(&polygons[i]); + } +} + +u8* GetLine(int line) +{ + return &ColorBuffer[line * 256 * 4]; } } diff --git a/melonDS.depend b/melonDS.depend index c13ca03..7c0308e 100644 --- a/melonDS.depend +++ b/melonDS.depend @@ -10,7 +10,7 @@ 1481161027 c:\documents\sources\melonds\types.h -1486730943 source:c:\documents\sources\melonds\nds.cpp +1486778178 source:c:\documents\sources\melonds\nds.cpp <stdio.h> <string.h> "NDS.h" @@ -24,7 +24,7 @@ "RTC.h" "Wifi.h" -1486512922 source:c:\documents\sources\melonds\arm.cpp +1486736660 source:c:\documents\sources\melonds\arm.cpp <stdio.h> "NDS.h" "ARM.h" @@ -87,13 +87,13 @@ "NDS.h" "SPI.h" -1486736522 source:c:\documents\sources\melonds\gpu2d.cpp +1486778220 source:c:\documents\sources\melonds\gpu2d.cpp <stdio.h> <string.h> "NDS.h" "GPU.h" -1486736507 c:\documents\sources\melonds\gpu2d.h +1486777351 c:\documents\sources\melonds\gpu2d.h 1481040524 c:\documents\sources\melonds\wifi.h @@ -146,12 +146,18 @@ "NDS.h" "NDSCart.h" -1486736374 c:\documents\sources\melonds\gpu3d.h +1486777933 c:\documents\sources\melonds\gpu3d.h -1486736648 source:c:\documents\sources\melonds\gpu3d.cpp +1486777278 source:c:\documents\sources\melonds\gpu3d.cpp <stdio.h> <string.h> "NDS.h" "GPU.h" "FIFO.h" +1486781263 source:c:\documents\sources\melonds\gpu3d_soft.cpp + <stdio.h> + <string.h> + "NDS.h" + "GPU3D.h" + |