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 "reader.h"
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -417,7 +418,7 @@ int main() {
Vector3 point = (Vector3){0, 0, -10};
Tri internaltriarray[50];
static Tri internaltriarray[50000];
TriArray tarr;
tarr.arr = internaltriarray;
tarr.length = 0;
@ -427,12 +428,12 @@ int main() {
TriArrayAppend(&tarr, (Tri){(Vector3){0, 0, -2000}, (Vector3){0, 800, -2000},
(Vector3){800, 800, -2000}, BLUE});
Tri internaltransformedtriarray[50];
static Tri internaltransformedtriarray[50000];
TriArray TransformedTris;
TransformedTris.arr = internaltransformedtriarray;
TransformedTris.length = 0;
Tri2D internaltri2darray[50];
static Tri2D internaltri2darray[50000];
Tri2DArray Tri2Darr;
Tri2Darr.length = 0;
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};
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) {
float frametime = GetFrameTime();
CtrlLocalCam(&camera, frametime);

View file

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

View file

@ -1,30 +1,28 @@
#include "c3dtypes.h"
#include "arrayfuncs.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
int TestFunc(int x) {
x = x + 1;
return x;
}
Object ReadObjectFromFile() {
Object out;
out.VertexArray = malloc(10000*sizeof(Vector3));
out.VertexArrayLength = 0;
Object3D ReadObjectFromFile(char * fname) {
Object3D out;
Vector3 *VertexArray = malloc(10000 * sizeof(Vector3));
int VertexArrayLength = 0;
TriArray tarr;
tarr.arr = malloc(10000 * sizeof(Tri));
tarr.length = 0;
out.triangles = &tarr;
strncpy(out.name, "cube.obj", 100);
strncpy(out.name, fname, 100);
FILE * f = fopen("cube.obj", "r");
FILE *f = fopen(fname, "r");
while (true) {
char t[500];
@ -34,27 +32,37 @@ Object ReadObjectFromFile() {
}
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;
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(t, "f")) {
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];
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;
// 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
#define READER_HEADER
int TestFunc(int x);
Object3D ReadObjectFromFile(char * fname);
#endif

9965
teapot.obj Normal file

File diff suppressed because it is too large Load diff