diff --git a/Makefile b/Makefile index 8a8a46a..e7b9b52 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -CC = clang #Set compiler -CFLAGS = -pg -g -Wall -O2 -pg #set compiler flags +CC = gcc #Set compiler +CFLAGS = -pg -g -Wall -O3 -pg #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/arrayfuncs.c b/arrayfuncs.c index 345f1fe..fbfacbf 100644 --- a/arrayfuncs.c +++ b/arrayfuncs.c @@ -1,3 +1,4 @@ +#include "raylib.h" #include "c3dtypes.h" diff --git a/arrayfuncs.h b/arrayfuncs.h index 645172a..433bcb9 100644 --- a/arrayfuncs.h +++ b/arrayfuncs.h @@ -1,8 +1,7 @@ -#include "c3dtypes.h" #ifndef ARRAYFUNCS_HEADER #define ARRAYFUNCS_HEADER - +#include "c3dtypes.h" void TriArrayAppend(TriArray *tarr, Tri t); void Tri2DArrayAppend(Tri2DArray *tarr, Tri2D t); diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..a8f7e4b --- /dev/null +++ b/build.zig @@ -0,0 +1,27 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + const exe = b.addExecutable(.{ + .name = "c3d", + .target = target, + .optimize = optimize, +}); + exe.linkLibC(); + exe.addCSourceFiles(.{ + .files = &.{ "c3d.c", "arrayfuncs.c", "reader.c", "vecfunc.c" } + +}); + //exe.linkSystemLibrary("raylib"); + //exe.installLibraryHeaders(raylib) + const raylib_dep = b.dependency("raylib", .{ + .target = target, + .optimize = optimize, + .linux_display_backend = .Wayland, + }); + const rlib = raylib_dep.artifact("raylib"); + exe.linkLibrary(rlib); + b.installArtifact(exe); + +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..aa42ab4 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,16 @@ +.{ + .paths = .{""}, + .name = "c3d", + .version = "0.0.1", + .dependencies = .{ + .raylib =.{ + .url = "https://github.com/raysan5/raylib/archive/refs/heads/master.tar.gz", + .hash = "1220480ba605ef3fad3951565a45fce073c3d6fa506e96822d2e86d7a628ed88ec61", + + } + + } + + + +} diff --git a/c3d.c b/c3d.c index 80c7a61..c82336c 100644 --- a/c3d.c +++ b/c3d.c @@ -1,4 +1,5 @@ #include "raylib.h" +#include "c3dtypes.h" #include "raymath.h" #include "reader.h" #include @@ -8,7 +9,6 @@ #include #include "arrayfuncs.h" -#include "c3dtypes.h" #include "vecfunc.h" const int RENDERWIDTH = 1920; @@ -490,7 +490,7 @@ int main() { bool run3d = true; if (true) { - Object3D t = ReadObjectFromFile("mario.obj"); + Object3D t = ReadObjectFromFile("cube.obj"); for (int i = 0; i < t.triangles->length; i++) { // printf("t: %f\n", t.triangles->arr[i].a.x); TriArrayAppend(&tarr, t.triangles->arr[i]); diff --git a/c3dtypes.h b/c3dtypes.h index e2bb10c..a8e9056 100644 --- a/c3dtypes.h +++ b/c3dtypes.h @@ -1,8 +1,8 @@ -#include "raylib.h" #ifndef C3DTYPES_HEADER #define C3DTYPES_HEADER +#include "raylib.h" @@ -19,6 +19,7 @@ typedef struct LocalCam LocalCam; struct CMaterial { char * name; char * texturefile; + Color * tex; }; typedef struct CMaterial CMaterial; @@ -29,7 +30,7 @@ struct Tri { Color color; - Material material; + CMaterial material; Vector2 auv; Vector2 buv; @@ -71,6 +72,12 @@ struct TriArray { }; typedef struct TriArray TriArray; +struct CMaterialArray { + int length; + CMaterial *arr; +}; +typedef struct CMaterialArray CMaterialArray; + struct Tri2DArray { int length; Tri2D *arr; @@ -80,6 +87,7 @@ typedef struct Tri2DArray Tri2DArray; struct Object3D { char name[100]; TriArray * triangles; + CMaterialArray *mats; }; typedef struct Object3D Object3D; diff --git a/cube-tex.obj b/cube-tex.obj new file mode 100644 index 0000000..413db3e --- /dev/null +++ b/cube-tex.obj @@ -0,0 +1,2723 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 3d-cubes/cube-tex.obj at master · garykac/3d-cubes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+ Skip to content + + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + + + + + + + +
+ +
+ + + + + + + + +
+ + + + + +
+ + + + + + + + + + + +
+
+
+ + + + + + + + + + + + + + + +
+ Open in github.dev + Open in a new github.dev tab + Open in codespace + + + + + + + + + + + + + + + + + + +

Files

t

Latest commit

 

History

History
98 lines (85 loc) · 2.33 KB

cube-tex.obj

File metadata and controls

98 lines (85 loc) · 2.33 KB
+
+ + + + +
+ +
+ +
+
+ +
+ +
+

Footer

+ + + + +
+
+ + + + + © 2024 GitHub, Inc. + +
+ + +
+
+ + + + + + + + + + + + + + + + + + + + +
+ +
+
+ + + diff --git a/cube.mtl b/cube.mtl new file mode 100644 index 0000000..dcd20c5 --- /dev/null +++ b/cube.mtl @@ -0,0 +1,7 @@ +newmtl texture +Ka 0.0 0.0 0.0 +Kd 0.5 0.5 0.5 +Ks 0.0 0.0 0.0 +Ns 10.0 +illum 2 +map_Kd texture.png diff --git a/reader.c b/reader.c index cf3c9a3..bc60439 100644 --- a/reader.c +++ b/reader.c @@ -1,5 +1,6 @@ -#include "c3dtypes.h" #include "arrayfuncs.h" +#include "c3dtypes.h" +#include "raylib.h" #include #include #include @@ -9,24 +10,90 @@ int TestFunc(int x) { return x; } -Vector3 BreakDownObjTriElement(char * vert) { - int vertex = 0; - int vertextexture = 0; - int normal = 0; +Vector3 BreakDownObjTriElement(char *vert) { + int vertex = 0; + int vertextexture = 0; + int normal = 0; - sscanf(vert, "%i/%i/%i", &vertex, &vertextexture, &normal); - return (Vector3) {vertex, vertextexture, normal}; + sscanf(vert, "%i/%i/%i", &vertex, &vertextexture, &normal); + return (Vector3){vertex, vertextexture, normal}; } -Object3D ReadObjectFromFile(char * fname) { +CMaterial NewCmat(char *matname, char *fname) { + CMaterial cmat; + cmat.name = malloc(20 * sizeof(char)); + cmat.texturefile = malloc(20 * sizeof(char)); + strncpy(cmat.name, matname, 15); + strncpy(cmat.texturefile, fname, 15); + Image teximg = LoadImage(fname); + cmat.tex = LoadImageColors(teximg); + printf("Loaded Tex %s\n", matname); + return cmat; +} + +void LoadMats(char *fname, CMaterialArray *cmats) { + FILE *f = fopen(fname, "r"); + + char mtlname[50]; + strncpy(mtlname, "", 50); + char mtltex[50]; + strncpy(mtltex, "", 50); + printf("Loading mat of name: %s\n", fname); + while (true) { + char t[500]; + char *fgetres = fgets(t, 500, f); + + char objtype[30]; + char v1[100]; + char v2[100]; + char v3[100]; + sscanf(t, "%s %s %s %s", objtype, v1, v2, v3); + + printf("Entering comp for mtlname: %s, mtltex: %s\n", mtlname, mtltex); + + if (fgetres == NULL) { + if ((strcmp(mtlname, "") != 0) && (strcmp(mtltex, "") != 0)) { + cmats->arr[cmats->length] = NewCmat(mtlname, mtltex); + printf("Younki Sploinky\n"); + cmats->length = cmats->length + 1; + strncpy(mtltex, "", 50); + printf("ended and added\n"); + } + break; + } + + if (strcmp(objtype, "newmtl") == 0) { + if ((strcmp(mtlname, "") != 0) && (strcmp(mtltex, "") != 0)) { + cmats->arr[cmats->length] = NewCmat(mtlname, mtltex); + cmats->length = cmats->length + 1; + strncpy(mtltex, "", 50); + } + strncpy(mtlname, v1, 50); + } + if (strcmp(objtype, "map_Kd") == 0) { + strncpy(mtltex, v1, 50); + } + } + printf("Loaded Tex %s\n", fname); + fclose(f); +} + +Object3D ReadObjectFromFile(char *fname) { Object3D out; Vector3 *VertexArray = malloc(10000 * sizeof(Vector3)); int VertexArrayLength = 0; + Vector2 *UVArray = malloc(10000 * sizeof(Vector2)); + int UVArrayLength = 0; + TriArray tarr; tarr.arr = malloc(10000 * sizeof(Tri)); tarr.length = 0; + CMaterialArray cmats; + cmats.arr = malloc(10 * sizeof(CMaterial)); + cmats.length = 0; + out.triangles = &tarr; strncpy(out.name, fname, 100); @@ -45,34 +112,41 @@ Object3D ReadObjectFromFile(char * fname) { char v2[100]; char v3[100]; sscanf(t, "%s %s %s %s", objtype, v1, v2, v3); + if (strcmp(objtype, "mtllib") == 0) { + LoadMats(v1, &cmats); + } if (strcmp(objtype, "v") == 0) { - VertexArray[VertexArrayLength].x = atof(v1)*100; - VertexArray[VertexArrayLength].y = atof(v2)*100; - VertexArray[VertexArrayLength].z = atof(v3)*100; + VertexArray[VertexArrayLength].x = atof(v1) * 100; + VertexArray[VertexArrayLength].y = atof(v2) * 100; + VertexArray[VertexArrayLength].z = atof(v3) * 100; VertexArrayLength = VertexArrayLength + 1; } - if (strcmp(objtype, "vt") == 0) { - - } - if (strcmp(objtype, "f") == 0 ) { + if (strcmp(objtype, "vt") == 0) { + // printf("vt: (%s,%s)\n", v1, v2); + UVArray[UVArrayLength].x = atof(v1); + UVArray[UVArrayLength].y = atof(v2); + UVArrayLength = UVArrayLength + 1; + } + if (strcmp(objtype, "f") == 0) { // TODO: append face to triarry Tri temptri; + temptri.a = VertexArray[atoi(v1) - 1]; + temptri.b = VertexArray[atoi(v2) - 1]; + temptri.c = VertexArray[atoi(v3) - 1]; - temptri.a = VertexArray[ atoi(v1) - 1]; - temptri.b = VertexArray[ atoi(v2) - 1]; - temptri.c = VertexArray[ atoi(v3) - 1]; + // printf("OLINE: %s\n", t); + // printf("adding tri A: %d, B: %d, C: %d\n", atoi(v1), atoi(v2) , atoi(v3) ); - //printf("OLINE: %s\n", t); - //printf("adding tri A: %d, B: %d, C: %d\n", atoi(v1), atoi(v2) , atoi(v3) ); - - temptri.color = GREEN; - TriArrayAppend(&tarr, temptri); + temptri.color = GREEN; + TriArrayAppend(&tarr, temptri); } // need to bring in triarray functions into their own file } - printf("Loaded model %s with %d tris\n", fname, tarr.length); - return out; + + fclose(f); + printf("Loaded model %s with %d tris\n", fname, tarr.length); + return out; // TODO: use file object and read each line // add verticies to the vertex list, +1 ing the length value each time diff --git a/reader.h b/reader.h index 7719fa5..df3b4f7 100644 --- a/reader.h +++ b/reader.h @@ -1,4 +1,4 @@ -#include "c3dtypes.h" +//#include "c3dtypes.h" #ifndef READER_HEADER #define READER_HEADER diff --git a/texture.png b/texture.png new file mode 100644 index 0000000..3ba1183 Binary files /dev/null and b/texture.png differ