Restructure files
filereader: file operation, reading files, reading .obj parser: parses a string representing obj structure, converting to HE_obj print: various helper functions for printing the structures
This commit is contained in:
parent
2c1238d3be
commit
1218778145
4
Makefile
4
Makefile
@ -15,8 +15,8 @@ CFLAGS += -O0 -g3
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
TARGET = drow-engine
|
TARGET = drow-engine
|
||||||
HEADERS = err.h parser.h types.h
|
HEADERS = err.h parser.h types.h print.h filereader.h
|
||||||
OBJECTS = main.o parser.o
|
OBJECTS = main.o parser.o print.o filereader.o
|
||||||
INCS = -I.
|
INCS = -I.
|
||||||
|
|
||||||
CFLAGS += $(shell $(PKG_CONFIG) --cflags gl glu glib-2.0)
|
CFLAGS += $(shell $(PKG_CONFIG) --cflags gl glu glib-2.0)
|
||||||
|
96
filereader.c
Normal file
96
filereader.c
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2011-2014 hasufell
|
||||||
|
*
|
||||||
|
* This file is part of a hasufell project.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation version 2 of the License only.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "err.h"
|
||||||
|
#include "filereader.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read an obj file and return a HE_obj
|
||||||
|
* if parsing worked.
|
||||||
|
*
|
||||||
|
* @param filename file to open
|
||||||
|
* @return the HE_obj or NULL for failure
|
||||||
|
*/
|
||||||
|
HE_obj *read_obj_file(char const * const filename)
|
||||||
|
{
|
||||||
|
char *string = NULL; /* file content */
|
||||||
|
HE_obj *obj = NULL;
|
||||||
|
|
||||||
|
/* read the whole file into string */
|
||||||
|
string = read_file(filename);
|
||||||
|
if (!filename || !*filename || !string || !*string)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
obj = parse_obj(string);
|
||||||
|
free(string);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads a file and returns a newly allocated string.
|
||||||
|
*
|
||||||
|
* @param filename file to open
|
||||||
|
* @return newly allocated string, must be freed by the caller
|
||||||
|
*/
|
||||||
|
char *read_file(char const * const filename)
|
||||||
|
{
|
||||||
|
char buf[STD_FILE_BUF],
|
||||||
|
*string = NULL;
|
||||||
|
int objfile = 0;
|
||||||
|
size_t str_size = 0;
|
||||||
|
ssize_t n;
|
||||||
|
|
||||||
|
objfile = open(filename, O_RDONLY);
|
||||||
|
|
||||||
|
if (objfile) {
|
||||||
|
/* read and copy chunks */
|
||||||
|
while ((n = read(objfile, buf, STD_FILE_BUF)) > 0) {
|
||||||
|
char *tmp_ptr = NULL;
|
||||||
|
|
||||||
|
str_size += n; /* count total bytes read */
|
||||||
|
|
||||||
|
tmp_ptr = (char*) realloc( /* allocate correct size */
|
||||||
|
string, /* pointer to realloc */
|
||||||
|
str_size /* total bytes read */
|
||||||
|
+ 1); /* space for trailing NULL byte */
|
||||||
|
CHECK_PTR_VAL(tmp_ptr);
|
||||||
|
string = tmp_ptr;
|
||||||
|
|
||||||
|
/* append buffer to string */
|
||||||
|
memcpy(string + (str_size - n), buf, n);
|
||||||
|
}
|
||||||
|
/* add trailing NULL byte */
|
||||||
|
string[str_size] = '\0';
|
||||||
|
|
||||||
|
close(objfile);
|
||||||
|
|
||||||
|
return string;
|
||||||
|
} else {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
30
filereader.h
Normal file
30
filereader.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2011-2014 hasufell
|
||||||
|
*
|
||||||
|
* This file is part of a hasufell project.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation version 2 of the License only.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _DROW_ENGINE_FILEREADER_H
|
||||||
|
#define _DROW_ENGINE_FILEREADER_H
|
||||||
|
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
|
||||||
|
HE_obj *read_obj_file(char const * const filename);
|
||||||
|
char *read_file(char const * const filename);
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* _DROW_ENGINE_FILEREADER_H */
|
16
main.c
16
main.c
@ -16,7 +16,9 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "parser.h"
|
#include "filereader.h"
|
||||||
|
#include "print.h"
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
@ -28,10 +30,16 @@ char const * const helptext = "Usage: drow-engine <file.obj>\n";
|
|||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (argc > 1)
|
HE_obj *obj;
|
||||||
parse_obj(argv[1]);
|
|
||||||
else
|
if (argc > 1) {
|
||||||
|
obj = read_obj_file(argv[1]);
|
||||||
|
print_vertices(obj);
|
||||||
|
print_edges(obj);
|
||||||
|
print_faces(obj);
|
||||||
|
} else {
|
||||||
printf("%s", helptext);
|
printf("%s", helptext);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
135
parser.c
135
parser.c
@ -17,41 +17,28 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "err.h"
|
#include "err.h"
|
||||||
|
#include "filereader.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* static function declarations
|
|
||||||
*/
|
|
||||||
static char *read_file(char const * const filename);
|
|
||||||
static void print_edges(HE_obj *obj);
|
|
||||||
static void print_vertices(HE_obj *obj);
|
|
||||||
static void print_faces(HE_obj *obj);
|
|
||||||
static void print_plain_faces(FACE face, uint32_t fc);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse an .obj file and return a NULL terminated
|
* Parse an .obj string and return a HE_obj
|
||||||
* HE_face array that represents the object.
|
* that represents the whole object.
|
||||||
*
|
*
|
||||||
* @param filename .obj file
|
* @param obj_string the whole string from the .obj file
|
||||||
* @return the HE_face array that represents the object
|
* @return the HE_face array that represents the object
|
||||||
*/
|
*/
|
||||||
HE_obj *parse_obj(char const * const filename)
|
HE_obj *parse_obj(char const * const obj_string)
|
||||||
{
|
{
|
||||||
uint32_t vc = 0, /* vertices count */
|
uint32_t vc = 0, /* vertices count */
|
||||||
fc = 0, /* face count */
|
fc = 0, /* face count */
|
||||||
ec = 0; /* edge count */
|
ec = 0; /* edge count */
|
||||||
char *string = NULL, /* file content */
|
char *string = malloc(sizeof(char) * strlen(obj_string)),
|
||||||
*str_ptr_space = NULL, /* for strtok */
|
*str_ptr_space = NULL, /* for strtok */
|
||||||
*str_ptr_newline = NULL, /* for strtok */
|
*str_ptr_newline = NULL, /* for strtok */
|
||||||
*str_tmp_ptr = NULL; /* for strtok */
|
*str_tmp_ptr = NULL; /* for strtok */
|
||||||
@ -61,12 +48,7 @@ HE_obj *parse_obj(char const * const filename)
|
|||||||
HE_obj *obj = NULL;
|
HE_obj *obj = NULL;
|
||||||
FACE face_v = NULL;
|
FACE face_v = NULL;
|
||||||
|
|
||||||
/* read the whole file into string */
|
strcpy(string, obj_string);
|
||||||
string = read_file(filename);
|
|
||||||
if (!filename || !*filename || !string || !*string)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
printf("file content\n%s\n\n", string);
|
|
||||||
|
|
||||||
str_tmp_ptr = strtok_r(string, "\n", &str_ptr_newline);
|
str_tmp_ptr = strtok_r(string, "\n", &str_ptr_newline);
|
||||||
while (str_tmp_ptr && *str_tmp_ptr) {
|
while (str_tmp_ptr && *str_tmp_ptr) {
|
||||||
@ -153,7 +135,6 @@ HE_obj *parse_obj(char const * const filename)
|
|||||||
edges = tmp_edge_ptr;
|
edges = tmp_edge_ptr;
|
||||||
|
|
||||||
edges[ec].vert = &(vertices[face_v[i][j] - 1]);
|
edges[ec].vert = &(vertices[face_v[i][j] - 1]);
|
||||||
faces[j].edge = &(edges[ec]); /* last one will win */
|
|
||||||
edges[ec].face = &(faces[j]);
|
edges[ec].face = &(faces[j]);
|
||||||
edges[ec].pair = NULL; /* preliminary */
|
edges[ec].pair = NULL; /* preliminary */
|
||||||
|
|
||||||
@ -165,8 +146,9 @@ HE_obj *parse_obj(char const * const filename)
|
|||||||
ec++;
|
ec++;
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
faces[i].edge = &(edges[ec - 1]); /* "last" edge */
|
||||||
|
}
|
||||||
|
|
||||||
/* find pairs */
|
/* find pairs */
|
||||||
/* TODO: acceleration */
|
/* TODO: acceleration */
|
||||||
@ -177,6 +159,7 @@ HE_obj *parse_obj(char const * const filename)
|
|||||||
if (next_vert == edges[j].vert)
|
if (next_vert == edges[j].vert)
|
||||||
edges[i].pair = &(edges[j]);
|
edges[i].pair = &(edges[j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
obj = (HE_obj*) malloc(sizeof(HE_obj));
|
obj = (HE_obj*) malloc(sizeof(HE_obj));
|
||||||
CHECK_PTR_VAL(obj);
|
CHECK_PTR_VAL(obj);
|
||||||
|
|
||||||
@ -187,103 +170,7 @@ HE_obj *parse_obj(char const * const filename)
|
|||||||
obj->faces = faces;
|
obj->faces = faces;
|
||||||
obj->fc = fc;
|
obj->fc = fc;
|
||||||
|
|
||||||
print_plain_faces(face_v, fc);
|
|
||||||
print_vertices(obj);
|
|
||||||
print_edges(obj);
|
|
||||||
|
|
||||||
free(string);
|
free(string);
|
||||||
|
|
||||||
return NULL;
|
return obj;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads a file and returns a newly allocated string.
|
|
||||||
*
|
|
||||||
* @param filename file to open
|
|
||||||
* @return newly allocated string, must be freed by the caller
|
|
||||||
*/
|
|
||||||
static char *read_file(char const * const filename)
|
|
||||||
{
|
|
||||||
char buf[STD_FILE_BUF],
|
|
||||||
*string = NULL;
|
|
||||||
int objfile = 0;
|
|
||||||
size_t str_size = 0;
|
|
||||||
ssize_t n;
|
|
||||||
|
|
||||||
objfile = open(filename, O_RDONLY);
|
|
||||||
|
|
||||||
if (objfile) {
|
|
||||||
/* read and copy chunks */
|
|
||||||
while ((n = read(objfile, buf, STD_FILE_BUF)) > 0) {
|
|
||||||
char *tmp_ptr = NULL;
|
|
||||||
|
|
||||||
str_size += n; /* count total bytes read */
|
|
||||||
|
|
||||||
tmp_ptr = (char*) realloc( /* allocate correct size */
|
|
||||||
string, /* pointer to realloc */
|
|
||||||
str_size /* total bytes read */
|
|
||||||
+ 1); /* space for trailing NULL byte */
|
|
||||||
CHECK_PTR_VAL(tmp_ptr);
|
|
||||||
string = tmp_ptr;
|
|
||||||
|
|
||||||
/* append buffer to string */
|
|
||||||
memcpy(string + (str_size - n), buf, n);
|
|
||||||
}
|
|
||||||
/* add trailing NULL byte */
|
|
||||||
string[str_size] = '\0';
|
|
||||||
|
|
||||||
close(objfile);
|
|
||||||
|
|
||||||
return string;
|
|
||||||
} else {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void print_edges(HE_obj *obj)
|
|
||||||
{
|
|
||||||
for (uint32_t i = 0; i < obj->ec; i++) {
|
|
||||||
printf("edge vertices %i:\n", i);
|
|
||||||
printf(" x: %f\n", obj->edges[i].vert->x);
|
|
||||||
printf(" y: %f\n", obj->edges[i].vert->y);
|
|
||||||
printf(" z: %f\n", obj->edges[i].vert->z);
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void print_vertices(HE_obj *obj)
|
|
||||||
{
|
|
||||||
printf("vertices: %d\n", obj->vc);
|
|
||||||
for (uint32_t i = 0; i < obj->vc; i++) {
|
|
||||||
printf("x[%d]: %f\n", i, obj->vertices[i].x);
|
|
||||||
printf("y[%d]: %f\n", i, obj->vertices[i].y);
|
|
||||||
printf("z[%d]: %f\n", i, obj->vertices[i].z);
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void print_faces(HE_obj *obj)
|
|
||||||
{
|
|
||||||
for (uint32_t i = 0; i < obj->fc; i++) {
|
|
||||||
printf("face edge vertices %i:\n", i);
|
|
||||||
printf(" x: %f\n", obj->faces[i].edge->vert->x);
|
|
||||||
printf(" y: %f\n", obj->faces[i].edge->vert->y);
|
|
||||||
printf(" z: %f\n", obj->faces[i].edge->vert->z);
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void print_plain_faces(FACE face, uint32_t fc)
|
|
||||||
{
|
|
||||||
printf("plain faces:\n");
|
|
||||||
for (uint32_t i = 0; i < fc - 1; i++) {
|
|
||||||
uint32_t j = 0;
|
|
||||||
printf("f:");
|
|
||||||
while (face[i][j]) {
|
|
||||||
printf(" %d", face[i][j]);
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
}
|
||||||
|
94
print.c
Normal file
94
print.c
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2011-2014 hasufell
|
||||||
|
*
|
||||||
|
* This file is part of a hasufell project.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation version 2 of the License only.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print the coordinates of the starting vertices
|
||||||
|
* of all edges.
|
||||||
|
*
|
||||||
|
* @param obj the 3d object
|
||||||
|
*/
|
||||||
|
void print_edges(HE_obj *obj)
|
||||||
|
{
|
||||||
|
for (uint32_t i = 0; i < obj->ec; i++) {
|
||||||
|
printf("edge vertices %i:\n", i);
|
||||||
|
printf(" x: %f\n", obj->edges[i].vert->x);
|
||||||
|
printf(" y: %f\n", obj->edges[i].vert->y);
|
||||||
|
printf(" z: %f\n", obj->edges[i].vert->z);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print the coordinates of all vertices.
|
||||||
|
*
|
||||||
|
* @param obj the 3d object
|
||||||
|
*/
|
||||||
|
void print_vertices(HE_obj *obj)
|
||||||
|
{
|
||||||
|
printf("vertices: %d\n", obj->vc);
|
||||||
|
for (uint32_t i = 0; i < obj->vc; i++) {
|
||||||
|
printf("x[%d]: %f\n", i, obj->vertices[i].x);
|
||||||
|
printf("y[%d]: %f\n", i, obj->vertices[i].y);
|
||||||
|
printf("z[%d]: %f\n", i, obj->vertices[i].z);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print the coordinates of all edges
|
||||||
|
* that are saved in the HE_face structs.
|
||||||
|
*
|
||||||
|
* @param obj the 3d object
|
||||||
|
*/
|
||||||
|
void print_faces(HE_obj *obj)
|
||||||
|
{
|
||||||
|
for (uint32_t i = 0; i < obj->fc; i++) {
|
||||||
|
printf("face edge vertice %i:\n", i);
|
||||||
|
printf(" x: %f\n", obj->faces[i].edge->vert->x);
|
||||||
|
printf(" y: %f\n", obj->faces[i].edge->vert->y);
|
||||||
|
printf(" z: %f\n", obj->faces[i].edge->vert->z);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print all plain unconverted faces
|
||||||
|
* as they are saved in the .obj file.
|
||||||
|
*
|
||||||
|
* @param face the plain face struct
|
||||||
|
* @param fc the count of faces
|
||||||
|
*/
|
||||||
|
void print_plain_faces(FACE face, uint32_t fc)
|
||||||
|
{
|
||||||
|
printf("plain faces:\n");
|
||||||
|
for (uint32_t i = 0; i < fc - 1; i++) {
|
||||||
|
uint32_t j = 0;
|
||||||
|
printf("f:");
|
||||||
|
while (face[i][j]) {
|
||||||
|
printf(" %d", face[i][j]);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
32
print.h
Normal file
32
print.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2011-2014 hasufell
|
||||||
|
*
|
||||||
|
* This file is part of a hasufell project.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation version 2 of the License only.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _DROW_ENGINE_PRINT_H
|
||||||
|
#define _DROW_ENGINE_PRINT_H
|
||||||
|
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
|
||||||
|
void print_edges(HE_obj *obj);
|
||||||
|
void print_vertices(HE_obj *obj);
|
||||||
|
void print_faces(HE_obj *obj);
|
||||||
|
void print_plain_faces(FACE face, uint32_t fc);
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* _DROW_ENGINE_PRINT_H */
|
Loading…
Reference in New Issue
Block a user