Zig build and Loading textures from mtl

This commit is contained in:
InventorXtreme 2024-07-19 14:36:33 -04:00
parent 16e1e6a3a8
commit 97a53e49f7
12 changed files with 2889 additions and 34 deletions

View file

@ -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

View file

@ -1,3 +1,4 @@
#include "raylib.h"
#include "c3dtypes.h"

View file

@ -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);

27
build.zig Normal file
View file

@ -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);
}

16
build.zig.zon Normal file
View file

@ -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",
}
}
}

4
c3d.c
View file

@ -1,4 +1,5 @@
#include "raylib.h"
#include "c3dtypes.h"
#include "raymath.h"
#include "reader.h"
#include <math.h>
@ -8,7 +9,6 @@
#include <string.h>
#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]);

View file

@ -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;

2723
cube-tex.obj Normal file

File diff suppressed because one or more lines are too long

7
cube.mtl Normal file
View file

@ -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

View file

@ -1,5 +1,6 @@
#include "c3dtypes.h"
#include "arrayfuncs.h"
#include "c3dtypes.h"
#include "raylib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -18,15 +19,81 @@ Vector3 BreakDownObjTriElement(char * vert) {
return (Vector3){vertex, vertextexture, normal};
}
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,6 +112,9 @@ 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;
@ -52,13 +122,15 @@ Object3D ReadObjectFromFile(char * fname) {
VertexArrayLength = VertexArrayLength + 1;
}
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];
@ -71,6 +143,8 @@ Object3D ReadObjectFromFile(char * fname) {
}
// need to bring in triarray functions into their own file
}
fclose(f);
printf("Loaded model %s with %d tris\n", fname, tarr.length);
return out;

View file

@ -1,4 +1,4 @@
#include "c3dtypes.h"
//#include "c3dtypes.h"
#ifndef READER_HEADER
#define READER_HEADER

BIN
texture.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 294 B