From 75f9f7fbb2e33064f6341b12b0ec99dc6af444f6 Mon Sep 17 00:00:00 2001 From: InventorXtreme Date: Mon, 4 Dec 2023 12:15:12 -0500 Subject: [PATCH] rendering tri, still need to cull when behind --- c3d.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 147 insertions(+), 11 deletions(-) diff --git a/c3d.c b/c3d.c index 086e220..2baff56 100644 --- a/c3d.c +++ b/c3d.c @@ -1,5 +1,6 @@ #include #include +#include #include "raylib.h" const int RENDERWIDTH = 1920; @@ -35,9 +36,72 @@ struct LocalCam { Vector3 angleVelocity; Vector3 angleAcceleration; }; - typedef struct LocalCam LocalCam; +struct Tri { + Vector3 a; + Vector3 b; + Vector3 c; + + Color color; +}; +typedef struct Tri Tri; + +struct Tri2D{ + Vector2 a; + Vector2 b; + Vector2 c; + + Color color; +}; +typedef struct Tri2D Tri2D; + +struct Zee { + int depth; + Tri2D * triangle; +}; +typedef struct Zee Zee; + +struct TriArray { + int length; + Tri* arr; +}; +typedef struct TriArray TriArray; + +struct Tri2DArray { + int length; + Tri2D* arr; +}; +typedef struct Tri2DArray Tri2DArray; + +float Sign(Vector2 v1, Vector2 v2, Vector2 v3) { + return (v1.x - v3.x) * (v2.y - v3.y) - (v2.x - v3.x) * (v1.y - v3.y); +} + +bool IsInTri(Tri2D tri, Vector2 p) { + float d1, d2, d3; + bool has_neg, has_pos; + + d1 = Sign(p, tri.a, tri.b); + d2 = Sign(p, tri.b, tri.c); + d3 = Sign(p, tri.c, tri.a); + + has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0); + has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0); + + return !(has_neg && has_pos); +} + +void TriArrayAppend(TriArray * tarr, Tri t) { + tarr->arr[tarr->length] = t; + tarr->length = tarr->length + 1; +} + +void Tri2DArrayAppend(Tri2DArray * tarr, Tri2D t) { + tarr->arr[tarr->length] = t; + tarr->length = tarr->length + 1; +} + Vector3 Vector3Sum(Vector3 v1, Vector3 v2) { Vector3 retvec; retvec.x = v1.x + v2.x; @@ -188,11 +252,31 @@ Vector3 TransformWithCam(Vector3 v, LocalCam * cam) { return returnvector; } +Tri TriTransformWithCam(Tri *t, LocalCam * cam) { + Tri rettri; + rettri.a = TransformWithCam(t->a,cam); + rettri.b = TransformWithCam(t->b,cam); + rettri.c = TransformWithCam(t->c,cam); + + rettri.color = t->color; + return rettri; +} + +Tri2D ConvertTriToTri2D(Tri* t){ + Tri2D rettri2d; + rettri2d.a = Conv3Dto2D(t->a); + rettri2d.b = Conv3Dto2D(t->b); + rettri2d.c = Conv3Dto2D(t->c); + + rettri2d.a = Conv2DCenteredToScreen(rettri2d.a); + rettri2d.b = Conv2DCenteredToScreen(rettri2d.b); + rettri2d.c = Conv2DCenteredToScreen(rettri2d.c); + + rettri2d.color = t->color; + return rettri2d; +} int main() { proj = HALFWIDTH / tan(half_fov); - - - SetConfigFlags(FLAG_WINDOW_UNDECORATED); InitWindow(SCREENWIDTH, SCREENHEIGHT, "raylib [core] example - basic window"); @@ -223,6 +307,23 @@ int main() { Vector3 point = (Vector3){0,0,-10}; + Tri internaltriarray[50]; + TriArray tarr; + tarr.arr = internaltriarray; + tarr.length = 0; + TriArrayAppend(&tarr,(Tri){(Vector3){0,0,-1000},(Vector3){0,800,-1000},(Vector3){800,800,-1000},WHITE}); + + Tri internaltransformedtriarray[50]; + TriArray TransformedTris; + TransformedTris.arr = internaltransformedtriarray; + TransformedTris.length = 0; + + Tri2D internaltri2darray[50]; + Tri2DArray Tri2Darr; + Tri2Darr.length=0; + Tri2Darr.arr = internaltri2darray; + + static Zee ZBuff[1920][1080] = {{(Zee){10000,NULL}}}; //FIXME: Stupid static makes the file 32 Megs because pog while (!WindowShouldClose()) { @@ -230,16 +331,51 @@ int main() { CtrlLocalCam(&camera,frametime); LocalCamApplyVelo(&camera,frametime); BeginTextureMode(renderTexture); - ClearBackground(WHITE); + ClearBackground(BLACK); DrawText("Congrats! You created your first window!", 190, 200, 20, BLACK); - Vector3 TransVector = TransformWithCam(point,&camera); - if (TransVector.z < 0) { - Vector2 MPos = Conv3Dto2D(TransVector); - Vector2 FinPos = Conv2DCenteredToScreen(MPos); - DrawCircleV(FinPos,100,BLACK); + /* Vector3 TransVector = TransformWithCam(point,&camera); */ + /* if (TransVector.z < 0) { */ + /* Vector2 MPos = Conv3Dto2D(TransVector); */ + /* Vector2 FinPos = Conv2DCenteredToScreen(MPos); */ + /* DrawCircleV(FinPos,100,BLACK); */ + /* } */ + /* EndTextureMode(); */ + + TransformedTris.length = 0; + Tri2Darr.length = 0; + + for (int i = 0; i < tarr.length; i++) { + TriArrayAppend(&TransformedTris,TriTransformWithCam(&tarr.arr[i],&camera)); } - EndTextureMode(); + + for(int i = 0; i < TransformedTris.length; i++) { + Tri2DArrayAppend(&Tri2Darr,ConvertTriToTri2D(&TransformedTris.arr[i])); + } + + memset(ZBuff,0,sizeof(ZBuff)); + + for(int i = 0; i < Tri2Darr.length; i++) { + for(int y = 0; y < RENDERHEIGHT; y++){ + for(int x = 0; x< RENDERWIDTH; x++) { + if (IsInTri(Tri2Darr.arr[i],(Vector2){x,y})) { + ZBuff[x][y].triangle = &Tri2Darr.arr[i]; + } + } + } + } + + for(int y = 0; y < RENDERHEIGHT; y++) { + for(int x = 0; x < RENDERWIDTH; x++) { + if (ZBuff[x][y].triangle != NULL){ + DrawPixel(x,y,ZBuff[x][y].triangle->color); + } + + } + + } + + EndTextureMode(); //Copytexture to main display :0