Zig build and Loading textures from mtl
This commit is contained in:
parent
16e1e6a3a8
commit
97a53e49f7
4
Makefile
4
Makefile
|
@ -1,5 +1,5 @@
|
||||||
CC = clang #Set compiler
|
CC = gcc #Set compiler
|
||||||
CFLAGS = -pg -g -Wall -O2 -pg #set compiler flags
|
CFLAGS = -pg -g -Wall -O3 -pg #set compiler flags
|
||||||
LDFLAGS = -pg -g #set linker flags
|
LDFLAGS = -pg -g #set linker flags
|
||||||
LDLIBS = -lraylib -lm #set libraries to use
|
LDLIBS = -lraylib -lm #set libraries to use
|
||||||
objects = c3d.o reader.o arrayfuncs.o vecfunc.o #list all object files
|
objects = c3d.o reader.o arrayfuncs.o vecfunc.o #list all object files
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include "raylib.h"
|
||||||
#include "c3dtypes.h"
|
#include "c3dtypes.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#include "c3dtypes.h"
|
|
||||||
|
|
||||||
#ifndef ARRAYFUNCS_HEADER
|
#ifndef ARRAYFUNCS_HEADER
|
||||||
#define ARRAYFUNCS_HEADER
|
#define ARRAYFUNCS_HEADER
|
||||||
|
#include "c3dtypes.h"
|
||||||
void TriArrayAppend(TriArray *tarr, Tri t);
|
void TriArrayAppend(TriArray *tarr, Tri t);
|
||||||
|
|
||||||
void Tri2DArrayAppend(Tri2DArray *tarr, Tri2D t);
|
void Tri2DArrayAppend(Tri2DArray *tarr, Tri2D t);
|
||||||
|
|
27
build.zig
Normal file
27
build.zig
Normal 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
16
build.zig.zon
Normal 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
4
c3d.c
|
@ -1,4 +1,5 @@
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
#include "c3dtypes.h"
|
||||||
#include "raymath.h"
|
#include "raymath.h"
|
||||||
#include "reader.h"
|
#include "reader.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
@ -8,7 +9,6 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "arrayfuncs.h"
|
#include "arrayfuncs.h"
|
||||||
#include "c3dtypes.h"
|
|
||||||
#include "vecfunc.h"
|
#include "vecfunc.h"
|
||||||
|
|
||||||
const int RENDERWIDTH = 1920;
|
const int RENDERWIDTH = 1920;
|
||||||
|
@ -490,7 +490,7 @@ int main() {
|
||||||
bool run3d = true;
|
bool run3d = true;
|
||||||
|
|
||||||
if (true) {
|
if (true) {
|
||||||
Object3D t = ReadObjectFromFile("mario.obj");
|
Object3D t = ReadObjectFromFile("cube.obj");
|
||||||
for (int i = 0; i < t.triangles->length; i++) {
|
for (int i = 0; i < t.triangles->length; i++) {
|
||||||
// printf("t: %f\n", t.triangles->arr[i].a.x);
|
// printf("t: %f\n", t.triangles->arr[i].a.x);
|
||||||
TriArrayAppend(&tarr, t.triangles->arr[i]);
|
TriArrayAppend(&tarr, t.triangles->arr[i]);
|
||||||
|
|
12
c3dtypes.h
12
c3dtypes.h
|
@ -1,8 +1,8 @@
|
||||||
#include "raylib.h"
|
|
||||||
|
|
||||||
#ifndef C3DTYPES_HEADER
|
#ifndef C3DTYPES_HEADER
|
||||||
#define C3DTYPES_HEADER
|
#define C3DTYPES_HEADER
|
||||||
|
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ typedef struct LocalCam LocalCam;
|
||||||
struct CMaterial {
|
struct CMaterial {
|
||||||
char * name;
|
char * name;
|
||||||
char * texturefile;
|
char * texturefile;
|
||||||
|
Color * tex;
|
||||||
};
|
};
|
||||||
typedef struct CMaterial CMaterial;
|
typedef struct CMaterial CMaterial;
|
||||||
|
|
||||||
|
@ -29,7 +30,7 @@ struct Tri {
|
||||||
|
|
||||||
Color color;
|
Color color;
|
||||||
|
|
||||||
Material material;
|
CMaterial material;
|
||||||
|
|
||||||
Vector2 auv;
|
Vector2 auv;
|
||||||
Vector2 buv;
|
Vector2 buv;
|
||||||
|
@ -71,6 +72,12 @@ struct TriArray {
|
||||||
};
|
};
|
||||||
typedef struct TriArray TriArray;
|
typedef struct TriArray TriArray;
|
||||||
|
|
||||||
|
struct CMaterialArray {
|
||||||
|
int length;
|
||||||
|
CMaterial *arr;
|
||||||
|
};
|
||||||
|
typedef struct CMaterialArray CMaterialArray;
|
||||||
|
|
||||||
struct Tri2DArray {
|
struct Tri2DArray {
|
||||||
int length;
|
int length;
|
||||||
Tri2D *arr;
|
Tri2D *arr;
|
||||||
|
@ -80,6 +87,7 @@ typedef struct Tri2DArray Tri2DArray;
|
||||||
struct Object3D {
|
struct Object3D {
|
||||||
char name[100];
|
char name[100];
|
||||||
TriArray * triangles;
|
TriArray * triangles;
|
||||||
|
CMaterialArray *mats;
|
||||||
};
|
};
|
||||||
typedef struct Object3D Object3D;
|
typedef struct Object3D Object3D;
|
||||||
|
|
||||||
|
|
2723
cube-tex.obj
Normal file
2723
cube-tex.obj
Normal file
File diff suppressed because one or more lines are too long
7
cube.mtl
Normal file
7
cube.mtl
Normal 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
|
80
reader.c
80
reader.c
|
@ -1,5 +1,6 @@
|
||||||
#include "c3dtypes.h"
|
|
||||||
#include "arrayfuncs.h"
|
#include "arrayfuncs.h"
|
||||||
|
#include "c3dtypes.h"
|
||||||
|
#include "raylib.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -18,15 +19,81 @@ Vector3 BreakDownObjTriElement(char * vert) {
|
||||||
return (Vector3){vertex, vertextexture, normal};
|
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 ReadObjectFromFile(char *fname) {
|
||||||
Object3D out;
|
Object3D out;
|
||||||
Vector3 *VertexArray = malloc(10000 * sizeof(Vector3));
|
Vector3 *VertexArray = malloc(10000 * sizeof(Vector3));
|
||||||
int VertexArrayLength = 0;
|
int VertexArrayLength = 0;
|
||||||
|
|
||||||
|
Vector2 *UVArray = malloc(10000 * sizeof(Vector2));
|
||||||
|
int UVArrayLength = 0;
|
||||||
|
|
||||||
TriArray tarr;
|
TriArray tarr;
|
||||||
tarr.arr = malloc(10000 * sizeof(Tri));
|
tarr.arr = malloc(10000 * sizeof(Tri));
|
||||||
tarr.length = 0;
|
tarr.length = 0;
|
||||||
|
|
||||||
|
CMaterialArray cmats;
|
||||||
|
cmats.arr = malloc(10 * sizeof(CMaterial));
|
||||||
|
cmats.length = 0;
|
||||||
|
|
||||||
out.triangles = &tarr;
|
out.triangles = &tarr;
|
||||||
|
|
||||||
strncpy(out.name, fname, 100);
|
strncpy(out.name, fname, 100);
|
||||||
|
@ -45,6 +112,9 @@ Object3D ReadObjectFromFile(char * fname) {
|
||||||
char v2[100];
|
char v2[100];
|
||||||
char v3[100];
|
char v3[100];
|
||||||
sscanf(t, "%s %s %s %s", objtype, v1, v2, v3);
|
sscanf(t, "%s %s %s %s", objtype, v1, v2, v3);
|
||||||
|
if (strcmp(objtype, "mtllib") == 0) {
|
||||||
|
LoadMats(v1, &cmats);
|
||||||
|
}
|
||||||
if (strcmp(objtype, "v") == 0) {
|
if (strcmp(objtype, "v") == 0) {
|
||||||
VertexArray[VertexArrayLength].x = atof(v1) * 100;
|
VertexArray[VertexArrayLength].x = atof(v1) * 100;
|
||||||
VertexArray[VertexArrayLength].y = atof(v2) * 100;
|
VertexArray[VertexArrayLength].y = atof(v2) * 100;
|
||||||
|
@ -52,13 +122,15 @@ Object3D ReadObjectFromFile(char * fname) {
|
||||||
VertexArrayLength = VertexArrayLength + 1;
|
VertexArrayLength = VertexArrayLength + 1;
|
||||||
}
|
}
|
||||||
if (strcmp(objtype, "vt") == 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) {
|
if (strcmp(objtype, "f") == 0) {
|
||||||
// TODO: append face to triarry
|
// TODO: append face to triarry
|
||||||
Tri temptri;
|
Tri temptri;
|
||||||
|
|
||||||
|
|
||||||
temptri.a = VertexArray[atoi(v1) - 1];
|
temptri.a = VertexArray[atoi(v1) - 1];
|
||||||
temptri.b = VertexArray[atoi(v2) - 1];
|
temptri.b = VertexArray[atoi(v2) - 1];
|
||||||
temptri.c = VertexArray[atoi(v3) - 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
|
// need to bring in triarray functions into their own file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
printf("Loaded model %s with %d tris\n", fname, tarr.length);
|
printf("Loaded model %s with %d tris\n", fname, tarr.length);
|
||||||
return out;
|
return out;
|
||||||
|
|
||||||
|
|
2
reader.h
2
reader.h
|
@ -1,4 +1,4 @@
|
||||||
#include "c3dtypes.h"
|
//#include "c3dtypes.h"
|
||||||
#ifndef READER_HEADER
|
#ifndef READER_HEADER
|
||||||
#define READER_HEADER
|
#define READER_HEADER
|
||||||
|
|
||||||
|
|
BIN
texture.png
Normal file
BIN
texture.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 294 B |
Loading…
Reference in a new issue