Skip to content

Commit

Permalink
Major
Browse files Browse the repository at this point in the history
  • Loading branch information
nbtm committed Sep 17, 2021
1 parent 98c0902 commit 7e70674
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 1 deletion.
Binary file added cube.ssv
Binary file not shown.
18 changes: 18 additions & 0 deletions cube_q.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Blender v2.92.0 OBJ File: ''
# www.blender.org
o Cube
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 1.000000
s off
f 1 5 7 3
f 4 3 7 8
f 8 7 5 6
f 6 2 4 8
f 2 1 3 4
f 6 5 1 2
Binary file added obj
Binary file not shown.
116 changes: 116 additions & 0 deletions obj.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include "obj.h"

enum type {
COL = 1,
NOCOL = 2
};

struct quad {
int x[4];
int y[4];
int z[4];

enum type type;
unsigned int texture;
};

struct vertice {
int x;
int y;
int z;
};

int next_char_pointer(char *content, int current_offset, int lim, char delim) {
// Returns the index of the next newline char
int i = 0;

while ((*(content + current_offset + i) != delim) && i + current_offset < lim) {
i += 1;
}

return current_offset + i;
};

int main(int argc, char *argv[]) {
if (argc < 3) {
printf("No infile or outfile supplied");
return 0;
}
in_file = argv[1];
out_file = argv[2];

printf("Read: %s. Write: %s\n", in_file, out_file);

// Allocate buffer for file size
FILE *fp;
fp = fopen(in_file, "r");

if (fp == NULL) {
perror("Failed to open infile");
return 0;
}

fseek(fp, 0L, SEEK_END);
int file_size = ftell(fp);
fseek(fp, 0L, SEEK_SET);

printf("%s %d bytes long\n", in_file, file_size);
char fp_content[file_size];

fread(&fp_content, file_size, 1, fp);

// Count vertexes
int vertex_count = 0;
int offset = 0;

while (offset < file_size) {
int line_size = next_char_pointer((char *) &fp_content, offset, file_size, (char) *"\n");
line_size -= offset;

char *line = (char*) malloc(line_size + 2);
memset(line, '\x00', line_size);
memcpy(line, (char *) &fp_content[offset], line_size);

if (line[0] == *"v" && line[1] == *" ") {
vertex_count += 1;
}

free(line);
offset += line_size + 1;
}
printf("%d\n", vertex_count);

// Allocate the array for the vertices
struct vertice object_vertices[vertex_count];
offset = 0;
int vertex_i = 0;

while (offset < file_size) {
int line_size = next_char_pointer((char *) &fp_content, offset, file_size, (char) *"\n");
line_size -= offset;

char *line = (char*) malloc(line_size + 2);
memset(line, '\x00', line_size);
memcpy(line, (char *) &fp_content[offset], line_size);

if (line[0] == *"v" && line[1] == *" ") {
// Interperate vertex
int col_offset = 2;
int v_size;

v_size = next_char_pointer((char*) &fp_content, offset + col_offset, file_size, (char) *" ");
v_size -= offset;
v_size -= col_offset;

char *x_str = (char*) malloc(v_size + 2);
memset(x_str, '\x00', v_size);
memcpy(x_str, (char*) &fp_content[offset + col_offset], v_size);
object_vertices[0].x = atoi(x_str);
free(x_str);
printf("%d\n", object_vertices[0].x);
}

free(line);
offset += line_size + 1;
}
}
15 changes: 15 additions & 0 deletions obj.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

enum type;

struct quad;
struct vertice;

int next_char_pointer(char *content, int current_offset, int lim, char delim);

char *out_file;
char *in_file;
int main(int argc, char *argv[]);
Binary file added out.bin
Binary file not shown.
Binary file added read
Binary file not shown.
2 changes: 2 additions & 0 deletions read.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ int main() {

// Object is now loaded into memory.
printf("First vertex: (%d, %d, %d)", object[0].x[0], object[0].y[0], object[0].z[0]);


}
Binary file added sr
Binary file not shown.
7 changes: 7 additions & 0 deletions thing.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
jo_vertice plane[] = {
{object[0].x[0], object[0].y[0], object[0].z[0]},
{object[0].x[1], object[0].y[1], object[0].z[1]},
{object[0].x[2], object[0].y[2], object[0].z[2]},
{object[0].x[3], object[0].y[3], object[0].z[3]},
};

Binary file added write
Binary file not shown.
2 changes: 1 addition & 1 deletion write.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct quad {
int main() {
// Create cube
struct quad cube_object[6];

// 1, 1
cube_object[0].x[0] = 1;
cube_object[0].x[1] = -1;
cube_object[0].x[2] = -1;
Expand Down

0 comments on commit 7e70674

Please sign in to comment.