loading from files

This commit is contained in:
InventorXtreme 2024-03-11 22:14:36 -04:00
parent 931f7e5c48
commit 11e9fae640
5 changed files with 10029 additions and 51 deletions

12
c3d.c
View file

@ -2,6 +2,7 @@
#include "raymath.h" #include "raymath.h"
#include "reader.h" #include "reader.h"
#include <math.h> #include <math.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -417,7 +418,7 @@ int main() {
Vector3 point = (Vector3){0, 0, -10}; Vector3 point = (Vector3){0, 0, -10};
Tri internaltriarray[50]; static Tri internaltriarray[50000];
TriArray tarr; TriArray tarr;
tarr.arr = internaltriarray; tarr.arr = internaltriarray;
tarr.length = 0; tarr.length = 0;
@ -427,12 +428,12 @@ int main() {
TriArrayAppend(&tarr, (Tri){(Vector3){0, 0, -2000}, (Vector3){0, 800, -2000}, TriArrayAppend(&tarr, (Tri){(Vector3){0, 0, -2000}, (Vector3){0, 800, -2000},
(Vector3){800, 800, -2000}, BLUE}); (Vector3){800, 800, -2000}, BLUE});
Tri internaltransformedtriarray[50]; static Tri internaltransformedtriarray[50000];
TriArray TransformedTris; TriArray TransformedTris;
TransformedTris.arr = internaltransformedtriarray; TransformedTris.arr = internaltransformedtriarray;
TransformedTris.length = 0; TransformedTris.length = 0;
Tri2D internaltri2darray[50]; static Tri2D internaltri2darray[50000];
Tri2DArray Tri2Darr; Tri2DArray Tri2Darr;
Tri2Darr.length = 0; Tri2Darr.length = 0;
Tri2Darr.arr = internaltri2darray; Tri2Darr.arr = internaltri2darray;
@ -458,6 +459,11 @@ int main() {
Tri2D norm = (Tri2D){(Vector2){500, 50}, (Vector2){0, 0}, (Vector2){250, 500}, 0, 0, 0, GREEN}; Tri2D norm = (Tri2D){(Vector2){500, 50}, (Vector2){0, 0}, (Vector2){250, 500}, 0, 0, 0, GREEN};
bool run3d = true; bool run3d = true;
Object3D t = ReadObjectFromFile("teapot.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]);
}
while (!WindowShouldClose() && run3d) { while (!WindowShouldClose() && run3d) {
float frametime = GetFrameTime(); float frametime = GetFrameTime();
CtrlLocalCam(&camera, frametime); CtrlLocalCam(&camera, frametime);

View file

@ -58,12 +58,10 @@ typedef struct Tri2DArray Tri2DArray;
struct Object { struct Object3D {
char name[100]; char name[100];
Vector3 *VertexArray;
int VertexArrayLength;
TriArray * triangles; TriArray * triangles;
}; };
typedef struct Object Object; typedef struct Object3D Object3D;
#endif #endif

View file

@ -1,60 +1,68 @@
#include "c3dtypes.h" #include "c3dtypes.h"
#include "arrayfuncs.h"
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
int TestFunc(int x) { int TestFunc(int x) {
x = x + 1; x = x + 1;
return x; return x;
} }
Object3D ReadObjectFromFile(char * fname) {
Object3D out;
Vector3 *VertexArray = malloc(10000 * sizeof(Vector3));
int VertexArrayLength = 0;
Object ReadObjectFromFile() { TriArray tarr;
Object out; tarr.arr = malloc(10000 * sizeof(Tri));
out.VertexArray = malloc(10000*sizeof(Vector3)); tarr.length = 0;
out.VertexArrayLength = 0;
TriArray tarr; out.triangles = &tarr;
tarr.arr = malloc(10000*sizeof(Tri));
tarr.length = 0; strncpy(out.name, fname, 100);
FILE *f = fopen(fname, "r");
while (true) {
char t[500];
char *fgetres = fgets(t, 500, f);
if (fgetres == NULL) {
break;
}
char objtype[10];
char v1[100];
char v2[100];
char v3[100];
sscanf(t, "%s %s %s %s", objtype, v1, v2, v3);
if (strcmp(objtype, "v") == 0) {
VertexArray[VertexArrayLength].x = atof(v1)*100;
VertexArray[VertexArrayLength].y = atof(v2)*100;
VertexArray[VertexArrayLength].z = atof(v3)*100;
VertexArrayLength = VertexArrayLength + 1;
}
if (strcmp(objtype, "f") == 0 ) {
// TODO: append face to triarry
Tri temptri;
out.triangles = &tarr; temptri.a = VertexArray[ atoi(v1) - 1];
temptri.b = VertexArray[ atoi(v2) - 1];
temptri.c = VertexArray[ atoi(v3) - 1];
strncpy(out.name, "cube.obj", 100); 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);
}
// need to bring in triarray functions into their own file
}
return out;
FILE * f = fopen("cube.obj", "r"); // TODO: use file object and read each line
// add verticies to the vertex list, +1 ing the length value each time
while (true) { // just fucking parse the obj file its not that hard
char t[500];
char * fgetres = fgets(t, 500, f);
if (fgetres == NULL) {
break;
}
char objtype[10];
double v1;
double v2;
double v3;
sscanf(t, "%s %lf %lf %lf", objtype, &v1,&v2,&v3);
if ( strcmp(t, "v")) {
out.VertexArray[out.VertexArrayLength].x = v1;
out.VertexArray[out.VertexArrayLength].x = v2;
out.VertexArray[out.VertexArrayLength].x = v3;
out.VertexArrayLength = out.VertexArrayLength + 1;
}
if ( strcmp(t, "f")) {
//TODO: append face to triarry
}
//need to bring in triarray functions into their own file
}
//TODO: use file object and read each line
// add verticies to the vertex list, +1 ing the length value each time
// just fucking parse the obj file its not that hard
} }

View file

@ -1,6 +1,7 @@
#include "c3dtypes.h"
#ifndef READER_HEADER #ifndef READER_HEADER
#define READER_HEADER #define READER_HEADER
int TestFunc(int x); int TestFunc(int x);
Object3D ReadObjectFromFile(char * fname);
#endif #endif

9965
teapot.obj Normal file

File diff suppressed because it is too large Load diff