diff --git a/Makefile b/Makefile index e7b9b52..5a43f61 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC = gcc #Set compiler -CFLAGS = -pg -g -Wall -O3 -pg #set compiler flags +CFLAGS = -pg -g -Wall -O3 #set compiler flags LDFLAGS = -pg -g #set linker flags LDLIBS = -lraylib -lm #set libraries to use objects = c3d.o reader.o arrayfuncs.o vecfunc.o #list all object files diff --git a/c3d.c b/c3d.c index d006cc1..4717ad4 100644 --- a/c3d.c +++ b/c3d.c @@ -179,6 +179,16 @@ void V2Print(Vector2 v) { printf("%f, %f\n", v.x, v.y); } +Vector2 GetUVFromTriAndBary(Vector3 bary, Tri2D * tri, float z){ + Vector2 ta = Vector2Scale(tri->auv, (1.0 / tri->adepth )); + Vector2 tb = Vector2Scale(tri->buv, (1.0 / tri->bdepth )); + Vector2 tc = Vector2Scale(tri->cuv, (1.0 / tri->cdepth )); + + Vector2 AB = Vector2Add( Vector2Scale(ta, bary.x), Vector2Scale(tb, bary.y)) ; + Vector2 ABC = Vector2Add(AB, Vector2Scale(tc, bary.z)); + return Vector2Scale(ABC, z); +} + void DrawScanline(Zee *zee, Tri2D *trianglepointer, double start, double end, int scanline) { float depth = 0; Vector2 UVpos; @@ -192,17 +202,16 @@ void DrawScanline(Zee *zee, Tri2D *trianglepointer, double start, double end, in /* V2Print(UVRangeTri.b); */ /* V2Print(UVRangeTri.c); */ - Vector3 f1baryatpoint = Tri2DBaryAtPoint(trianglepointer, (Vector2){Max(start, 0), scanline}); Vector3 f1baryatpoint2 = Tri2DBaryAtPoint(trianglepointer, (Vector2){Max(start, 0) + 1, scanline}); - Vector2 tex1point = BaryAndTritoPoint(&UVRangeTri, f1baryatpoint); - Vector2 tex2point = BaryAndTritoPoint(&UVRangeTri, f1baryatpoint2); double f1invdepth = InvDepthAtBary(trianglepointer, f1baryatpoint); double f1invdepth2 = InvDepthAtBary(trianglepointer, f1baryatpoint2); + Vector2 tex1point = GetUVFromTriAndBary(f1baryatpoint, trianglepointer, 1.0 / f1invdepth); + Vector2 tex2point = GetUVFromTriAndBary(f1baryatpoint2, trianglepointer, 1.0 / f1invdepth2); double dinvslope = f1invdepth2 - f1invdepth; // change in store_depth per pixel @@ -212,22 +221,23 @@ void DrawScanline(Zee *zee, Tri2D *trianglepointer, double start, double end, in // UNCOMMENT TO DO TRUE NON SLOPE BASED DEPTH CALC: //Vector3 baryatpoint = Tri2DBaryAtPoint(trianglepointer, ( (Vector2){i, scanline})); + depth = 1.0 / (f1invdepth + (dinvslope * ((float)i - (float)(Max(start, 0))))); //depth = 1.0 / InvDepthAtBary(trianglepointer, baryatpoint); //UNCOMMENT TO DO REAL UV CALC //UVpos = BaryAndTritoPoint(&UVRangeTri, baryatpoint); + //UVpos = GetUVFromTriAndBary(baryatpoint, trianglepointer, depth); //V2Print(UVpos); UVpos = Vector2Add(tex1point, Vector2Scale(UVSlope, ((float)i - (float)(Max(start, 0))))); - depth = 1.0 / (f1invdepth + (dinvslope * ((float)i - (float)(Max(start, 0))))); if (depth < zee[IndexOfZBuff(i, scanline)].depth) { CMaterial *mat = trianglepointer->material; - + if (mat != NULL) { - int y = (int)(UVpos.y * (float)mat->h) % mat->h ; // keep within range because of bug + int y = (int)(UVpos.y * (float)mat->h) % mat->h; // keep within range because of bug int x = (int)(UVpos.x * (float)mat->w) % mat->w; // printf("plotting point x: %f, y: %d\n", UVpos.x,y); // int pitch = @@ -340,6 +350,22 @@ void FillBottomFlatZbuffer(Zee *zee, Tri2D *t, Tri2D *tp) { /* } */ + + +void AnotateTri(Tri2D *tex) { + char linea[500]; + char lineb[500]; + char linec[500]; + sprintf(linea, "UV: %f, %f;", tex->auv.x, tex->auv.y); + sprintf(lineb, "UV: %f, %f;", tex->buv.x, tex->buv.y); + sprintf(linec, "UV: %f, %f;", tex->cuv.x, tex->cuv.y); + + DrawText(linea, tex->a.x, tex->a.y, 60, WHITE); + DrawText(lineb, tex->b.x, tex->b.y, 60, WHITE); + DrawText(linec, tex->c.x, tex->c.y, 60, WHITE); + +} + //the issues is the tt pointer changes, so the thing does not reference properly for debugging void DrawTriZuff(Zee *zbuf, Tri2D *tex) { Tri2D tt = *tex; @@ -353,6 +379,8 @@ void DrawTriZuff(Zee *zbuf, Tri2D *tex) { Vector2Integate(&t->b); Vector2Integate(&t->c); + //AnotateTri(tex); + CMaterial *mat = tex->material; /* int y = (int) UVpos.y % mat->h; // keep within range because of bug */ /* int x = (int) UVpos.x % mat->w; */ @@ -453,6 +481,12 @@ void CtrlLocalCam(LocalCam *cam, float time) { if (IsKeyDown(KEY_E)) { cam->angleVelocity.y = cam->angleVelocity.y - time * 30; } + if (IsKeyDown(KEY_R)) { + cam->angleVelocity.x = cam->angleVelocity.x + time * 30; + } + if (IsKeyDown(KEY_F)) { + cam->angleVelocity.x = cam->angleVelocity.x - time * 30; + } if (IsKeyDown(KEY_G)) { printf("PosX: %f PosY: %f PosZ: %f\n", cam->position.x, cam->position.y, cam->position.z); @@ -606,7 +640,7 @@ int main() { bool run3d = true; - Object3D t = ReadObjectFromFile("mario.obj"); + Object3D t = ReadObjectFromFile("IronMan.obj"); if (true) { for (int i = 0; i < t.triangles->length; i++) { // printf("t: %f\n", t.triangles->arr[i].a.x); @@ -643,6 +677,8 @@ int main() { ZBuff[i].depth = 10000000; } + BeginTextureMode(uiraylibtexture); + ClearBackground(BLANK); for (int i = 0; i < Tri2Darr.length; i++) { DrawTriZuff(ZBuff, &Tri2Darr.arr[i]); } @@ -686,7 +722,6 @@ int main() { /* } */ /* } */ - BeginTextureMode(uiraylibtexture); // gui stuff EndTextureMode(); diff --git a/reader.c b/reader.c index 3dc189d..2b8fdb5 100644 --- a/reader.c +++ b/reader.c @@ -27,6 +27,7 @@ CMaterial NewCmat(char *matname, char *fname) { strncpy(cmat.name, matname, 15); strncpy(cmat.texturefile, fname, 15); Image teximg = LoadImage(fname); + ImageFlipVertical(&teximg); cmat.h = teximg.height; cmat.w = teximg.width; cmat.tex = LoadImageColors(teximg); @@ -92,18 +93,18 @@ CMaterial * FindCmat(CMaterialArray * cmats, char * name){ Object3D ReadObjectFromFile(char *fname) { Object3D out; - Vector3 *VertexArray = malloc(10000 * sizeof(Vector3)); + Vector3 *VertexArray = malloc(1000000 * sizeof(Vector3)); int VertexArrayLength = 0; - Vector2 *UVArray = malloc(10000 * sizeof(Vector2)); + Vector2 *UVArray = malloc(1000000 * sizeof(Vector2)); int UVArrayLength = 0; TriArray * tarr = malloc(sizeof(TriArray)); - tarr->arr = malloc(10000 * sizeof(Tri)); + tarr->arr = malloc(1000000 * sizeof(Tri)); tarr->length = 0; CMaterialArray * cmats = malloc(sizeof(CMaterialArray)); - cmats->arr = malloc(50 * sizeof(CMaterial)); + cmats->arr = malloc(500 * sizeof(CMaterial)); cmats->length = 0; out.triangles = tarr; diff --git a/singletri.mtl b/singletri.mtl new file mode 100644 index 0000000..ce56451 --- /dev/null +++ b/singletri.mtl @@ -0,0 +1,12 @@ +# Blender 4.2.0 MTL File: 'singletri.blend' +# www.blender.org + +newmtl Material.001 +Ns 250.000000 +Ka 1.000000 1.000000 1.000000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.500000 +d 1.000000 +illum 2 +map_Kd /home/inventorx/Downloads/0.png diff --git a/singletri.obj b/singletri.obj new file mode 100644 index 0000000..d43fb8a --- /dev/null +++ b/singletri.obj @@ -0,0 +1,36 @@ +# Blender 4.2.0 +# www.blender.org +mtllib singletri.mtl +o Cube +v -1.000000 -1.000000 1.000000 +v -1.000000 1.000000 1.000000 +v -1.000000 -1.000000 -1.000000 +v -1.000000 1.000000 -1.000000 +v 1.000000 -1.000000 1.000000 +v 1.000000 1.000000 1.000000 +v 1.000000 -1.000000 -1.000000 +v 1.000000 1.000000 -1.000000 +vn -1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 -1.0000 +vn 1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 1.0000 +vn -0.0000 -1.0000 -0.0000 +vn -0.0000 1.0000 -0.0000 +vt 0.000000 1.000000 +vt 1.000000 0.000000 +vt 0.000000 0.000000 +vt 1.000000 1.000000 +s 0 +usemtl Material.001 +f 2/1/1 3/2/1 1/3/1 +f 4/1/2 7/2/2 3/3/2 +f 8/4/3 5/3/3 7/2/3 +f 6/4/4 1/3/4 5/2/4 +f 7/4/5 1/3/5 3/1/5 +f 4/1/6 6/2/6 8/4/6 +f 2/1/1 4/4/1 3/2/1 +f 4/1/2 8/4/2 7/2/2 +f 8/4/3 6/1/3 5/3/3 +f 6/4/4 2/1/4 1/3/4 +f 7/4/5 5/2/5 1/3/5 +f 4/1/6 2/3/6 6/2/6