diff options
author | StapleButter <thetotalworm@gmail.com> | 2017-02-11 04:10:59 +0100 |
---|---|---|
committer | StapleButter <thetotalworm@gmail.com> | 2017-02-11 04:10:59 +0100 |
commit | 15c8d59e2b0f1aada8804b363515dcd146c702d7 (patch) | |
tree | fca130f0b1af771d4de0037bc43f380d20e27e10 | |
parent | 050ba5dfbe823672bbb1fdd45afa3b3b5f16f7b3 (diff) |
vertex colors, with Gouraud shading and all
has yet to be checked for whether it's accurate, optimized, etc...
-rw-r--r-- | GPU3D.cpp | 20 | ||||
-rw-r--r-- | GPU3D_Soft.cpp | 50 | ||||
-rw-r--r-- | melonDS.depend | 2 |
3 files changed, 44 insertions, 28 deletions
@@ -157,6 +157,7 @@ void UpdateClipMatrix(); u32 PolygonMode; s16 CurVertex[3]; +u8 VertexColor[3]; Vertex TempVertexBuffer[4]; u32 VertexNum; @@ -690,10 +691,9 @@ void SubmitVertex() vertextrans->Position[1] = (vertextrans->Position[1] * w_inv) >> 12; vertextrans->Position[2] = (vertextrans->Position[2] * w_inv) >> 12; - // boo - vertextrans->Color[0] = 63; - vertextrans->Color[1] = 0; - vertextrans->Color[2] = 63; + vertextrans->Color[0] = VertexColor[0]; + vertextrans->Color[1] = VertexColor[1]; + vertextrans->Color[2] = VertexColor[2]; /*printf("vertex trans: %f %f %f %f\n", vertextrans->Position[0]/4096.0f, @@ -1059,6 +1059,18 @@ void ExecuteCommand() } break; + case 0x20: // vertex color + { + u32 c = ExecParams[0]; + u32 r = c & 0x1F; + u32 g = (c >> 5) & 0x1F; + u32 b = (c >> 10) & 0x1F; + VertexColor[0] = r ? (r<<1)+1 : 0; + VertexColor[1] = g ? (g<<1)+1 : 0; + VertexColor[2] = b ? (b<<1)+1 : 0; + } + break; + case 0x21: // TODO: more cycles if lights are enabled break; diff --git a/GPU3D_Soft.cpp b/GPU3D_Soft.cpp index 9c057e9..4155751 100644 --- a/GPU3D_Soft.cpp +++ b/GPU3D_Soft.cpp @@ -127,6 +127,11 @@ void RenderPolygon(Polygon* polygon) //xmax = scrcoords[rcur][0] << 12; } + Vertex* vlcur = polygon->Vertices[lcur]; + Vertex* vlnext = polygon->Vertices[lnext]; + Vertex* vrcur = polygon->Vertices[rcur]; + Vertex* vrnext = polygon->Vertices[rnext]; + s32 lfactor, rfactor; if (scrcoords[lnext][1] == scrcoords[lcur][1]) @@ -139,35 +144,34 @@ void RenderPolygon(Polygon* polygon) 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); + s32 xl = scrcoords[lcur][0] + (((scrcoords[lnext][0] - scrcoords[lcur][0]) * lfactor) >> 12); + s32 xr = scrcoords[rcur][0] + (((scrcoords[rnext][0] - scrcoords[rcur][0]) * rfactor) >> 12); + + u8 rl = vlcur->Color[0] + (((vlnext->Color[0] - vlcur->Color[0]) * lfactor) >> 12); + u8 gl = vlcur->Color[1] + (((vlnext->Color[1] - vlcur->Color[1]) * lfactor) >> 12); + u8 bl = vlcur->Color[2] + (((vlnext->Color[2] - vlcur->Color[2]) * lfactor) >> 12); + + u8 rr = vrcur->Color[0] + (((vrnext->Color[0] - vrcur->Color[0]) * rfactor) >> 12); + u8 gr = vrcur->Color[1] + (((vrnext->Color[1] - vrcur->Color[1]) * rfactor) >> 12); + u8 br = vrcur->Color[2] + (((vrnext->Color[2] - vrcur->Color[2]) * rfactor) >> 12); + + s32 xdiv; + if (xr == xl) + xdiv = 0; + else + xdiv = 0x1000 / (xr - xl); - for (s32 x = xmin; x <= xmax; x++) + for (s32 x = xl; x <= xr; x++) { + s32 xfactor = (x - xl) * xdiv; + u8* pixel = &ColorBuffer[((256*y) + x) * 4]; - pixel[0] = 0; - pixel[1] = 63; - pixel[2] = 0; + pixel[0] = rl + (((rr - rl) * xfactor) >> 12); + pixel[1] = gl + (((gr - gl) * xfactor) >> 12); + pixel[2] = bl + (((br - bl) * xfactor) >> 12); 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) diff --git a/melonDS.depend b/melonDS.depend index 7c0308e..8fffc74 100644 --- a/melonDS.depend +++ b/melonDS.depend @@ -148,7 +148,7 @@ 1486777933 c:\documents\sources\melonds\gpu3d.h -1486777278 source:c:\documents\sources\melonds\gpu3d.cpp +1486782030 source:c:\documents\sources\melonds\gpu3d.cpp <stdio.h> <string.h> "NDS.h" |