diff --git a/src/Makefile b/src/Makefile index c7ff8ca..d7cf6c5 100644 --- a/src/Makefile +++ b/src/Makefile @@ -15,8 +15,8 @@ CFLAGS += -O0 -g3 endif TARGET = drow-engine -HEADERS = err.h parser.h common.h print.h filereader.h gl_draw.h vector.h half_edge.h -OBJECTS = main.o parser.o print.o filereader.o gl_draw.o vector.o half_edge.o +HEADERS = err.h common.h print.h filereader.h gl_draw.h vector.h half_edge.h +OBJECTS = main.o print.o filereader.o gl_draw.o vector.o half_edge.o INCS = -I. CFLAGS += $(shell $(PKG_CONFIG) --cflags gl glu glib-2.0) diff --git a/src/common.h b/src/common.h index c31f25b..1a7a765 100644 --- a/src/common.h +++ b/src/common.h @@ -20,7 +20,7 @@ * @file common.h * Header for common types, macros and constants * shared amongst the codebase. Error macros go in err.h. - * @brief common.h common types, macros, constants + * @brief common types, macros, constants */ #ifndef _DROW_ENGINE_TYPES_H diff --git a/src/err.h b/src/err.h index d84dea4..3ea6f24 100644 --- a/src/err.h +++ b/src/err.h @@ -16,17 +16,18 @@ * along with this program. If not, see . */ -#ifndef _DROW_ENGINE_ERR_H -#define _DROW_ENGINE_ERR_H - -#include - /** * @file err.h * Holds error handling macros. * @brief error handling */ +#ifndef _DROW_ENGINE_ERR_H +#define _DROW_ENGINE_ERR_H + +#include + + /** * Abort the program with a given error message. */ diff --git a/src/filereader.c b/src/filereader.c index 11fbf3c..a98fd81 100644 --- a/src/filereader.c +++ b/src/filereader.c @@ -16,9 +16,16 @@ * along with this program. If not, see . */ +/** + * @file filereader.c + * Reading of arbitrary files into strings as well as + * reading specific ones via various parsers. + * @brief reading of different filetypes + */ + #include "err.h" #include "filereader.h" -#include "parser.h" +#include "half_edge.h" #include "common.h" #include diff --git a/src/filereader.h b/src/filereader.h index 220b996..9bcf79e 100644 --- a/src/filereader.h +++ b/src/filereader.h @@ -16,6 +16,12 @@ * along with this program. If not, see . */ +/** + * @file filereader.h + * Header for the external API of filereader.c + * @brief header of filereader.c + */ + #ifndef _DROW_ENGINE_FILEREADER_H #define _DROW_ENGINE_FILEREADER_H diff --git a/src/gl_draw.c b/src/gl_draw.c index 12bd4b5..6705a56 100644 --- a/src/gl_draw.c +++ b/src/gl_draw.c @@ -16,6 +16,14 @@ * along with this program. If not, see . */ +/** + * @file gl_draw.c + * This file does the actual OpenGL and GLUT logic, + * drawing the objects, handling keyboard input and + * various GLUT-related callback functions. + * @brief OpenGL drawing + */ + #include "err.h" #include "filereader.h" #include "half_edge.h" diff --git a/src/gl_draw.h b/src/gl_draw.h index 2840b1a..2f08cc6 100644 --- a/src/gl_draw.h +++ b/src/gl_draw.h @@ -16,6 +16,12 @@ * along with this program. If not, see . */ +/** + * @file gl_draw.h + * Header for the external API of gl_draw.c + * @brief header of gl_draw.c + */ + #ifndef _DROW_ENGINE_DRAW_H #define _DROW_ENGINE_DRAW_H diff --git a/src/half_edge.c b/src/half_edge.c index cfb43bc..93bef5b 100644 --- a/src/half_edge.c +++ b/src/half_edge.c @@ -16,9 +16,22 @@ * along with this program. If not, see . */ +/** + * @file half_edge.c + * This file provides operations on half-edge data structures + * which are defined in half_edge.h, as well as assembling + * such a struct. + * @brief operations on half-edge data structs + */ + +#include "err.h" +#include "filereader.h" #include "half_edge.h" +#include +#include #include +#include /** @@ -31,3 +44,154 @@ HE_edge **get_all_emanating_edges(HE_vert *vertice) { return NULL; } + +/** + * Parse an .obj string and return a HE_obj + * that represents the whole object. + * + * @param obj_string the whole string from the .obj file + * @return the HE_face array that represents the object + */ +HE_obj *parse_obj(char const * const obj_string) +{ + uint32_t vc = 0, /* vertices count */ + fc = 0, /* face count */ + ec = 0; /* edge count */ + char *string = malloc(sizeof(char) * strlen(obj_string) + 1), + *str_ptr_space = NULL, /* for strtok */ + *str_ptr_newline = NULL, /* for strtok */ + *str_tmp_ptr = NULL; /* for strtok */ + HE_vert *vertices = NULL; + HE_edge *edges = NULL; + HE_face *faces = NULL; + HE_obj *obj = NULL; + FACE face_v = NULL; + + strcpy(string, obj_string); + + str_tmp_ptr = strtok_r(string, "\n", &str_ptr_newline); + while (str_tmp_ptr && *str_tmp_ptr) { + + str_tmp_ptr = strtok_r(str_tmp_ptr, " ", &str_ptr_space); + + /* parse vertices */ + if (!strcmp(str_tmp_ptr, "v")) { + char *myfloat = NULL; + HE_vert *tmp_ptr; + + tmp_ptr = (HE_vert*) realloc(vertices, + sizeof(HE_vert) * (vc + 1)); + CHECK_PTR_VAL(tmp_ptr); + vertices = tmp_ptr; + + /* fill x */ + myfloat = strtok_r(NULL, " ", &str_ptr_space); + CHECK_PTR_VAL(myfloat); + vertices[vc].x = atof(myfloat); + + /* fill y */ + myfloat = strtok_r(NULL, " ", &str_ptr_space); + CHECK_PTR_VAL(myfloat); + vertices[vc].y = atof(myfloat); + + /* fill z */ + myfloat = strtok_r(NULL, " ", &str_ptr_space); + CHECK_PTR_VAL(myfloat); + vertices[vc].z = atof(myfloat); + + /* set edge NULL */ + vertices[vc].edge = NULL; + + vc++; + + /* exceeds 3 dimensions, malformed vertice */ + if (strtok_r(NULL, " ", &str_ptr_space)) + return NULL; + + /* parse faces */ + } else if (!strcmp(str_tmp_ptr, "f")) { + char *myint = NULL; + uint8_t i = 0; + FACE tmp_ptr = NULL; + + /* fill FACE */ + tmp_ptr = (FACE) realloc(face_v, sizeof(FACE*) * (fc + 1)); + CHECK_PTR_VAL(tmp_ptr); + face_v = tmp_ptr; + face_v[fc] = NULL; + while ((myint = strtok_r(NULL, " ", &str_ptr_space))) { + uint32_t *tmp_ptr = NULL; + + i++; + ec++; + + tmp_ptr = (uint32_t*) realloc(face_v[fc], + sizeof(FACE**) * (i + 1)); + CHECK_PTR_VAL(tmp_ptr); + tmp_ptr[i - 1] = (uint32_t) atoi(myint); + tmp_ptr[i] = 0; /* so we can iterate over it */ + face_v[fc] = tmp_ptr; + } + fc++; + } + + str_tmp_ptr = strtok_r(NULL, "\n", &str_ptr_newline); + } + + + faces = (HE_face*) malloc(sizeof(HE_face) * fc); + CHECK_PTR_VAL(faces); + edges = (HE_edge*) malloc(sizeof(HE_edge) * ec); + CHECK_PTR_VAL(edges); + + ec = 0; + /* create HE_edges and real HE_faces */ + for (uint32_t i = 0; i < fc; i++) { + uint32_t j = 0; + + /* for all vertices of the face */ + while (face_v[i][j]) { + edges[ec].vert = &(vertices[face_v[i][j] - 1]); + edges[ec].face = &(faces[j]); + edges[ec].pair = NULL; /* preliminary */ + vertices[face_v[i][j] - 1].edge = &(edges[ec]); /* last one wins */ + + if (face_v[i][j + 1]) /* connect to next vertice */ + edges[ec].next = &(edges[ec + 1]); + else /* no vertices left, connect to first vertice */ + edges[ec].next = &(edges[ec - j]); + + ec++; + j++; + } + + faces[i].edge = &(edges[ec - 1]); /* "last" edge */ + } + + /* find pairs */ + /* TODO: acceleration */ + for (uint32_t i = 0; i < ec; i++) { + HE_vert *next_vert = edges[i].next->vert; + + for (uint32_t j = 0; j < ec; j++) + if (next_vert == edges[j].vert) + edges[i].pair = &(edges[j]); + } + + obj = (HE_obj*) malloc(sizeof(HE_obj)); + CHECK_PTR_VAL(obj); + + obj->vertices = vertices; + obj->vc = vc; + obj->edges = edges; + obj->ec = ec; + obj->faces = faces; + obj->fc = fc; + + free(string); + for (uint32_t i = 0; i < fc; i++) + free(face_v[i]); + free(face_v); + + return obj; +} diff --git a/src/half_edge.h b/src/half_edge.h index ca6925f..c64f6d2 100644 --- a/src/half_edge.h +++ b/src/half_edge.h @@ -16,6 +16,14 @@ * along with this program. If not, see . */ +/** + * @file half_edge.h + * Header for the external API of half_edge.c, + * primarily holding the common half-edge data + * structures. + * @brief header of half_edge.c + */ + #ifndef _DROW_ENGINE_HE_OPERATIONS_H #define _DROW_ENGINE_HE_OPERATIONS_H @@ -23,6 +31,12 @@ #include +/** + * Typedef for the plain faces + * that are not yet converted to real HE_face. + */ +typedef uint32_t** FACE; + typedef struct HE_edge HE_edge; typedef struct HE_vert HE_vert; typedef struct HE_face HE_face; @@ -110,6 +124,7 @@ struct HE_obj { HE_edge **get_all_emanating_edges(HE_vert *vertice); +HE_obj *parse_obj(char const * const filename); #endif /* _DROW_ENGINE_HE_OPERATIONS_H */ diff --git a/src/parser.c b/src/parser.c deleted file mode 100644 index 249c54d..0000000 --- a/src/parser.c +++ /dev/null @@ -1,178 +0,0 @@ -/* - * 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 . - */ - -#include "err.h" -#include "filereader.h" -#include "parser.h" - -#include -#include -#include -#include - - -/** - * Parse an .obj string and return a HE_obj - * that represents the whole object. - * - * @param obj_string the whole string from the .obj file - * @return the HE_face array that represents the object - */ -HE_obj *parse_obj(char const * const obj_string) -{ - uint32_t vc = 0, /* vertices count */ - fc = 0, /* face count */ - ec = 0; /* edge count */ - char *string = malloc(sizeof(char) * strlen(obj_string) + 1), - *str_ptr_space = NULL, /* for strtok */ - *str_ptr_newline = NULL, /* for strtok */ - *str_tmp_ptr = NULL; /* for strtok */ - HE_vert *vertices = NULL; - HE_edge *edges = NULL; - HE_face *faces = NULL; - HE_obj *obj = NULL; - FACE face_v = NULL; - - strcpy(string, obj_string); - - str_tmp_ptr = strtok_r(string, "\n", &str_ptr_newline); - while (str_tmp_ptr && *str_tmp_ptr) { - - str_tmp_ptr = strtok_r(str_tmp_ptr, " ", &str_ptr_space); - - /* parse vertices */ - if (!strcmp(str_tmp_ptr, "v")) { - char *myfloat = NULL; - HE_vert *tmp_ptr; - - tmp_ptr = (HE_vert*) realloc(vertices, - sizeof(HE_vert) * (vc + 1)); - CHECK_PTR_VAL(tmp_ptr); - vertices = tmp_ptr; - - /* fill x */ - myfloat = strtok_r(NULL, " ", &str_ptr_space); - CHECK_PTR_VAL(myfloat); - vertices[vc].x = atof(myfloat); - - /* fill y */ - myfloat = strtok_r(NULL, " ", &str_ptr_space); - CHECK_PTR_VAL(myfloat); - vertices[vc].y = atof(myfloat); - - /* fill z */ - myfloat = strtok_r(NULL, " ", &str_ptr_space); - CHECK_PTR_VAL(myfloat); - vertices[vc].z = atof(myfloat); - - /* set edge NULL */ - vertices[vc].edge = NULL; - - vc++; - - /* exceeds 3 dimensions, malformed vertice */ - if (strtok_r(NULL, " ", &str_ptr_space)) - return NULL; - - /* parse faces */ - } else if (!strcmp(str_tmp_ptr, "f")) { - char *myint = NULL; - uint8_t i = 0; - FACE tmp_ptr = NULL; - - /* fill FACE */ - tmp_ptr = (FACE) realloc(face_v, sizeof(FACE*) * (fc + 1)); - CHECK_PTR_VAL(tmp_ptr); - face_v = tmp_ptr; - face_v[fc] = NULL; - while ((myint = strtok_r(NULL, " ", &str_ptr_space))) { - uint32_t *tmp_ptr = NULL; - - i++; - ec++; - - tmp_ptr = (uint32_t*) realloc(face_v[fc], - sizeof(FACE**) * (i + 1)); - CHECK_PTR_VAL(tmp_ptr); - tmp_ptr[i - 1] = (uint32_t) atoi(myint); - tmp_ptr[i] = 0; /* so we can iterate over it */ - face_v[fc] = tmp_ptr; - } - fc++; - } - - str_tmp_ptr = strtok_r(NULL, "\n", &str_ptr_newline); - } - - - faces = (HE_face*) malloc(sizeof(HE_face) * fc); - CHECK_PTR_VAL(faces); - edges = (HE_edge*) malloc(sizeof(HE_edge) * ec); - CHECK_PTR_VAL(edges); - - ec = 0; - /* create HE_edges and real HE_faces */ - for (uint32_t i = 0; i < fc; i++) { - uint32_t j = 0; - - /* for all vertices of the face */ - while (face_v[i][j]) { - edges[ec].vert = &(vertices[face_v[i][j] - 1]); - edges[ec].face = &(faces[j]); - edges[ec].pair = NULL; /* preliminary */ - vertices[face_v[i][j] - 1].edge = &(edges[ec]); /* last one wins */ - - if (face_v[i][j + 1]) /* connect to next vertice */ - edges[ec].next = &(edges[ec + 1]); - else /* no vertices left, connect to first vertice */ - edges[ec].next = &(edges[ec - j]); - - ec++; - j++; - } - - faces[i].edge = &(edges[ec - 1]); /* "last" edge */ - } - - /* find pairs */ - /* TODO: acceleration */ - for (uint32_t i = 0; i < ec; i++) { - HE_vert *next_vert = edges[i].next->vert; - - for (uint32_t j = 0; j < ec; j++) - if (next_vert == edges[j].vert) - edges[i].pair = &(edges[j]); - } - - obj = (HE_obj*) malloc(sizeof(HE_obj)); - CHECK_PTR_VAL(obj); - - obj->vertices = vertices; - obj->vc = vc; - obj->edges = edges; - obj->ec = ec; - obj->faces = faces; - obj->fc = fc; - - free(string); - for (uint32_t i = 0; i < fc; i++) - free(face_v[i]); - free(face_v); - - return obj; -} diff --git a/src/parser.h b/src/parser.h deleted file mode 100644 index 6d97ea9..0000000 --- a/src/parser.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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 . - */ - -#ifndef _DROW_ENGINE_PARSER_H -#define _DROW_ENGINE_PARSER_H - -#include "half_edge.h" - -#include - - -/** - * Typedef for the plain faces - * that are not yet converted to real HE_face. - */ -typedef uint32_t** FACE; - -HE_obj *parse_obj(char const * const filename); - - -#endif /* _DROW_ENGINE_PARSER_H */ diff --git a/src/print.c b/src/print.c index d4f1e25..5432805 100644 --- a/src/print.c +++ b/src/print.c @@ -16,8 +16,15 @@ * along with this program. If not, see . */ +/** + * @file print.c + * Various verbose print functions that help + * debugging code or provide more detailed + * error messages. + * @brief various print functions + */ + #include "half_edge.h" -#include "parser.h" #include "vector.h" #include diff --git a/src/print.h b/src/print.h index 614a6fb..f14e0a4 100644 --- a/src/print.h +++ b/src/print.h @@ -16,12 +16,17 @@ * along with this program. If not, see . */ +/** + * @file print.h + * Header for the external API of print.c. + * @brief header of print.c + */ + #ifndef _DROW_ENGINE_PRINT_H #define _DROW_ENGINE_PRINT_H #include "half_edge.h" -#include "parser.h" #include "vector.h" diff --git a/src/vector.c b/src/vector.c index d1b6138..ad32130 100644 --- a/src/vector.c +++ b/src/vector.c @@ -16,6 +16,13 @@ * along with this program. If not, see . */ +/** + * @file vector.c + * Various operations on the vector struct defined + * in vector.h, including simple maths. + * @brief vector operations + */ + #include "err.h" #include "vector.h" diff --git a/src/vector.h b/src/vector.h index 9e2acc4..7dd68dc 100644 --- a/src/vector.h +++ b/src/vector.h @@ -16,6 +16,13 @@ * along with this program. If not, see . */ +/** + * @file vector.h + * Header for the external API of vector.c, + * also holding the vector data structure. + * @brief header of vector.c + */ + #ifndef _DROW_ENGINE_VEC_MATH_H #define _DROW_ENGINE_VEC_MATH_H