initial commit
This commit is contained in:
43
trimeshloader/Changelog.txt
Normal file
43
trimeshloader/Changelog.txt
Normal file
@ -0,0 +1,43 @@
|
||||
2007/05/28 - Paolo Manna
|
||||
|
||||
* added [3ds|obj]HasNormal functions.
|
||||
* fixed severe bugs in 3ds loader
|
||||
* fixed texcoord and minor parsing bugs in obj loader
|
||||
* merged material loading code
|
||||
|
||||
*** RELEASE 0.0.12
|
||||
|
||||
2007/05/28 - Cirdan
|
||||
|
||||
* again bug fixes in obj code
|
||||
|
||||
*** RELEASE 0.0.11
|
||||
|
||||
2007/05/28 - Cirdan
|
||||
|
||||
* relative indices
|
||||
* bug fix in new obj code
|
||||
|
||||
*** RELEASE 0.0.10
|
||||
|
||||
2007/05/28 - Cirdan
|
||||
|
||||
* use a faster lookup method in obj code
|
||||
|
||||
2007/04/24 - Cirdan
|
||||
|
||||
* fixed a memory leak in tlLoad3DS/tlLoadObj functions
|
||||
* added flexible vertex formats for high level loading
|
||||
|
||||
2007/04/17 - Cirdan
|
||||
|
||||
* renamed tlTrimeshFrom[3ds|Obj]State to tlCreateTrimeshFrom[3ds|Obj]State
|
||||
* changed license from MIT to zlib
|
||||
|
||||
2007/04/17 - Cirdan
|
||||
|
||||
* replaced "0" with NULL
|
||||
|
||||
2007/04/16 - Cirdan
|
||||
|
||||
* added tl3dsCheckFileExtension and tlObjCheckFileExtension functions.
|
17
trimeshloader/Makefile
Normal file
17
trimeshloader/Makefile
Normal file
@ -0,0 +1,17 @@
|
||||
include ../Makefile.common
|
||||
|
||||
OBJ = $(patsubst %.c,%.o,$(wildcard src/*.c))
|
||||
LIBNAME = libtrimeshloader.a
|
||||
CFLAGS += -Iinclude
|
||||
|
||||
all: $(LIBNAME)
|
||||
|
||||
$(LIBNAME): $(OBJ)
|
||||
@echo Creating archive $@
|
||||
@ar -csru $@ $(OBJ)
|
||||
@echo
|
||||
|
||||
clean:
|
||||
-@$(RM) src$(SLASH)*.o
|
||||
-@$(RM) src$(SLASH)*.d
|
||||
-@$(RM) $(LIBNAME)
|
21
trimeshloader/Readme.txt
Normal file
21
trimeshloader/Readme.txt
Normal file
@ -0,0 +1,21 @@
|
||||
Authors
|
||||
-------
|
||||
|
||||
Gero Müller <gero.mueller@cloo.de>
|
||||
|
||||
Contributions
|
||||
-------------
|
||||
|
||||
Paolo Manna (many good ideas and feedback)
|
||||
|
||||
Website
|
||||
-------
|
||||
|
||||
http://trimeshloader.sourceforge.net
|
||||
|
||||
Versioning
|
||||
----------
|
||||
|
||||
[Major].[Minor].[Patchlevel] e.g. 1.0.1
|
||||
|
||||
Where all releases with the same Major revision need to be API compatible.
|
10
trimeshloader/ToDo.txt
Normal file
10
trimeshloader/ToDo.txt
Normal file
@ -0,0 +1,10 @@
|
||||
* optimize obj_parameter_buffer_add
|
||||
* error reporting
|
||||
* loading options: optimize, isolate objects, generate normals
|
||||
* finish self tests
|
||||
* provide feature info: are uv maps
|
||||
* use size_t
|
||||
* add LWO (Lightwave Objects) loader
|
||||
* complete doxygen comments
|
||||
* add proper EXPORT/API defines
|
||||
* add debug output
|
167
trimeshloader/include/tl3ds.h
Normal file
167
trimeshloader/include/tl3ds.h
Normal file
@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Copyright (c) 2007 Gero Mueller <gero.mueller@cloo.de>
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
|
||||
#ifndef TRIMESH_LOADER_3DS_H
|
||||
#define TRIMESH_LOADER_3DS_H
|
||||
|
||||
/**
|
||||
@file tl3ds.h
|
||||
@brief Trimeshloader 3DS parser public header file
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef TRIMESH_LOADER_EXPORT
|
||||
#define TRIMESH_LOADER_API
|
||||
#else
|
||||
#define TRIMESH_LOADER_API extern
|
||||
#endif
|
||||
|
||||
/** @defgroup low_level_3ds_api Trimeshloader low level 3DS API
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** Structure describing the parsing state. the user has no direkt access to it. */
|
||||
typedef struct tl3dsState tl3dsState;
|
||||
|
||||
/** Create a new parsing state.
|
||||
* \return A new parsing state, which needs to be deleted after parsing. NULL on error.
|
||||
*/
|
||||
TRIMESH_LOADER_API tl3dsState *tl3dsCreateState();
|
||||
|
||||
/** Reset the parsing state
|
||||
* \param state pointer to an previously created state.
|
||||
*/
|
||||
TRIMESH_LOADER_API int tl3dsResetState( tl3dsState *state );
|
||||
|
||||
/** Destroy a previously created state.
|
||||
* \param state pointer to an previously created state.
|
||||
*/
|
||||
TRIMESH_LOADER_API void tl3dsDestroyState( tl3dsState *state );
|
||||
|
||||
/** Parse a chunk of data.
|
||||
* \param state a previously created state.
|
||||
* \param buffer pointer to the chunk of data to be parsed
|
||||
* \param length number of bytes to be parsed
|
||||
* \param last indicator if this is the last chunk. 1 = yes, 0 = no.
|
||||
* \return Returns 0 on success, 1 on error.
|
||||
*/
|
||||
TRIMESH_LOADER_API int tl3dsParse(
|
||||
tl3dsState *state,
|
||||
const char *buffer,
|
||||
unsigned int length,
|
||||
int last );
|
||||
|
||||
/* data access */
|
||||
TRIMESH_LOADER_API unsigned int tl3dsObjectCount( tl3dsState *state );
|
||||
|
||||
TRIMESH_LOADER_API const char *tl3dsObjectName(
|
||||
tl3dsState *state,
|
||||
unsigned int object );
|
||||
|
||||
TRIMESH_LOADER_API unsigned int tl3dsObjectFaceCount(
|
||||
tl3dsState *state,
|
||||
unsigned int object );
|
||||
|
||||
TRIMESH_LOADER_API unsigned int tl3dsObjectFaceIndex(
|
||||
tl3dsState *state,
|
||||
unsigned int object );
|
||||
|
||||
TRIMESH_LOADER_API unsigned int tl3dsMaterialCount( tl3dsState *state );
|
||||
|
||||
TRIMESH_LOADER_API const char *tl3dsMaterialName(
|
||||
tl3dsState *state,
|
||||
unsigned int object );
|
||||
|
||||
TRIMESH_LOADER_API int tl3dsGetMaterial(
|
||||
tl3dsState *state,
|
||||
unsigned int index,
|
||||
float *ambient,
|
||||
float *diffuse,
|
||||
float *specular,
|
||||
float *reflect );
|
||||
|
||||
TRIMESH_LOADER_API unsigned int tl3dsMaterialReferenceCount( tl3dsState *state );
|
||||
|
||||
TRIMESH_LOADER_API const char *tl3dsMaterialReferenceName(
|
||||
tl3dsState *state,
|
||||
unsigned int object );
|
||||
|
||||
TRIMESH_LOADER_API int tl3dsGetMaterialReference(
|
||||
tl3dsState *state,
|
||||
unsigned int index,
|
||||
unsigned int *face_index,
|
||||
unsigned int *face_count );
|
||||
|
||||
TRIMESH_LOADER_API unsigned int tl3dsVertexCount( tl3dsState *state );
|
||||
|
||||
TRIMESH_LOADER_API int tl3dsGetVertexDouble(
|
||||
tl3dsState *state,
|
||||
unsigned int index,
|
||||
double *x, double *y, double *z,
|
||||
double *tu, double *tv,
|
||||
double *nx, double *ny, double *nz );
|
||||
|
||||
TRIMESH_LOADER_API int tl3dsGetVertex(
|
||||
tl3dsState *state,
|
||||
unsigned int index,
|
||||
float *x, float *y, float *z,
|
||||
float *tu, float *tv,
|
||||
float *nx, float *ny, float *nz );
|
||||
|
||||
TRIMESH_LOADER_API unsigned int tl3dsFaceCount(
|
||||
tl3dsState *state );
|
||||
|
||||
TRIMESH_LOADER_API int tl3dsGetFaceInt(
|
||||
tl3dsState *state,
|
||||
unsigned int index,
|
||||
unsigned int *a,
|
||||
unsigned int *b,
|
||||
unsigned int *c );
|
||||
|
||||
TRIMESH_LOADER_API int tl3dsGetFace(
|
||||
tl3dsState *state,
|
||||
unsigned int index,
|
||||
unsigned short *a,
|
||||
unsigned short *b,
|
||||
unsigned short *c );
|
||||
|
||||
TRIMESH_LOADER_API int tl3dsCheckFileExtension( const char *filename );
|
||||
|
||||
/** Check if the loaded mesh has normals. 3DS does not support normals. It is for convenience only, and always returns 0.
|
||||
* \param state a previously created state.
|
||||
* \return Returns 0 if no normals are present, >0 if they are.
|
||||
*/
|
||||
TRIMESH_LOADER_API unsigned int tl3dsHasNormals( tl3dsState *state );
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
158
trimeshloader/include/tlobj.h
Normal file
158
trimeshloader/include/tlobj.h
Normal file
@ -0,0 +1,158 @@
|
||||
/*
|
||||
* Copyright (c) 2007 Gero Mueller <gero.mueller@cloo.de>
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
|
||||
#ifndef TRIMESH_LOADER_OBJ_H
|
||||
#define TRIMESH_LOADER_OBJ_H
|
||||
|
||||
/**
|
||||
@file tlobj.h
|
||||
@brief Trimeshloader OBJ parser public header file
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef TRIMESH_LOADER_EXPORT
|
||||
#define TRIMESH_LOADER_API
|
||||
#else
|
||||
#define TRIMESH_LOADER_API extern
|
||||
#endif
|
||||
|
||||
/** @defgroup low_level_obj_api Trimeshloader low level OBJ API
|
||||
* @{
|
||||
*/
|
||||
|
||||
typedef struct tlObjState tlObjState;
|
||||
|
||||
/* state handling */
|
||||
TRIMESH_LOADER_API tlObjState *tlObjCreateState();
|
||||
|
||||
TRIMESH_LOADER_API int tlObjResetState( tlObjState *state );
|
||||
|
||||
TRIMESH_LOADER_API void tlObjDestroyState( tlObjState *state );
|
||||
|
||||
/* parsing */
|
||||
TRIMESH_LOADER_API int tlObjParse(
|
||||
tlObjState *state,
|
||||
const char *buffer,
|
||||
unsigned int length,
|
||||
int last );
|
||||
|
||||
/* data access */
|
||||
TRIMESH_LOADER_API unsigned int tlObjObjectCount( tlObjState *state );
|
||||
|
||||
TRIMESH_LOADER_API const char *tlObjObjectName(
|
||||
tlObjState *state,
|
||||
unsigned int object );
|
||||
|
||||
TRIMESH_LOADER_API unsigned int tlObjObjectFaceCount(
|
||||
tlObjState *state,
|
||||
unsigned int object );
|
||||
|
||||
TRIMESH_LOADER_API unsigned int tlObjObjectFaceIndex(
|
||||
tlObjState *state,
|
||||
unsigned int object );
|
||||
|
||||
TRIMESH_LOADER_API unsigned int tlObjMaterialCount( tlObjState *state );
|
||||
|
||||
TRIMESH_LOADER_API const char *tlObjMaterialName(
|
||||
tlObjState *state,
|
||||
unsigned int object );
|
||||
|
||||
TRIMESH_LOADER_API unsigned int tlObjMaterialLibCount( tlObjState *state );
|
||||
|
||||
TRIMESH_LOADER_API const char *tlObjMaterialLibName(
|
||||
tlObjState *state,
|
||||
unsigned int object );
|
||||
|
||||
TRIMESH_LOADER_API int tlObjGetMaterial(
|
||||
tlObjState *state,
|
||||
unsigned int index,
|
||||
float *ambient,
|
||||
float *diffuse,
|
||||
float *specular,
|
||||
float *reflect );
|
||||
|
||||
TRIMESH_LOADER_API unsigned int tlObjMaterialReferenceCount( tlObjState *state );
|
||||
|
||||
TRIMESH_LOADER_API const char *tlObjMaterialReferenceName(
|
||||
tlObjState *state,
|
||||
unsigned int object );
|
||||
|
||||
TRIMESH_LOADER_API int tlObjGetMaterialReference(
|
||||
tlObjState *state,
|
||||
unsigned int index,
|
||||
unsigned int *face_index,
|
||||
unsigned int *face_count );
|
||||
|
||||
TRIMESH_LOADER_API unsigned int tlObjVertexCount( tlObjState *state );
|
||||
|
||||
TRIMESH_LOADER_API int tlObjGetVertexDouble(
|
||||
tlObjState *state,
|
||||
unsigned int index,
|
||||
double *x, double *y, double *z,
|
||||
double *tu, double *tv,
|
||||
double *nx, double *ny, double *nz );
|
||||
|
||||
TRIMESH_LOADER_API int tlObjGetVertex(
|
||||
tlObjState *state,
|
||||
unsigned int index,
|
||||
float *x, float *y, float *z,
|
||||
float *tu, float *tv,
|
||||
float *nx, float *ny, float *nz );
|
||||
|
||||
TRIMESH_LOADER_API unsigned int tlObjFaceCount(
|
||||
tlObjState *state );
|
||||
|
||||
TRIMESH_LOADER_API int tlObjGetFaceInt(
|
||||
tlObjState *state,
|
||||
unsigned int index,
|
||||
unsigned int *a,
|
||||
unsigned int *b,
|
||||
unsigned int *c );
|
||||
|
||||
TRIMESH_LOADER_API int tlObjGetFace(
|
||||
tlObjState *state,
|
||||
unsigned int index,
|
||||
unsigned short *a,
|
||||
unsigned short *b,
|
||||
unsigned short *c );
|
||||
|
||||
TRIMESH_LOADER_API int tlObjCheckFileExtension( const char *filename );
|
||||
|
||||
/** Check if the loaded mesh has normals.
|
||||
* \param state a previously created state.
|
||||
* \return Returns 0 if no normals are present, >0 if they are.
|
||||
*/
|
||||
TRIMESH_LOADER_API unsigned int tlObjHasNormals( tlObjState *state );
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
199
trimeshloader/include/trimeshloader.h
Normal file
199
trimeshloader/include/trimeshloader.h
Normal file
@ -0,0 +1,199 @@
|
||||
/*
|
||||
* Copyright (c) 2007 Gero Mueller <gero.mueller@cloo.de>
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
|
||||
/** \mainpage trimeshloader-0.1.0
|
||||
* \section project_page Project Page
|
||||
* \url http://sourceforge.net/projects/trimeshloader
|
||||
* \section website Website with tutorials
|
||||
* \url http://trimeshloader.sourceforge.net
|
||||
*/
|
||||
|
||||
/**
|
||||
@file trimeshloader.h
|
||||
@brief Trimeshloader public header file
|
||||
*/
|
||||
|
||||
#ifndef TRIMESH_LOADER_H
|
||||
#define TRIMESH_LOADER_H
|
||||
|
||||
#include "tlobj.h"
|
||||
#include "tl3ds.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef TRIMESH_LOADER_EXPORT
|
||||
#define TRIMESH_LOADER_API
|
||||
#else
|
||||
#define TRIMESH_LOADER_API extern
|
||||
#endif
|
||||
|
||||
|
||||
/** @defgroup high_level_api Trimeshloader high level API
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** Structure describing an Object (or SubMesh, Batch) */
|
||||
typedef struct tlObject
|
||||
{
|
||||
/** Name of the Object */
|
||||
char *name;
|
||||
|
||||
/** First face in the index list */
|
||||
unsigned int face_index;
|
||||
|
||||
/** Face count */
|
||||
unsigned int face_count;
|
||||
|
||||
} tlObject;
|
||||
|
||||
/** Used as format flag in loading functions: load the position of the vertex */
|
||||
#define TL_FVF_XYZ 1
|
||||
|
||||
/** Used as format flag in loading functions: load the texturecoordinate of the vertex */
|
||||
#define TL_FVF_UV 2
|
||||
|
||||
/** Used as format flag in loading functions: load the normal of the vertex */
|
||||
#define TL_FVF_NORMAL 4
|
||||
|
||||
/** Structure describing a Material (Colors and/or Texture) */
|
||||
typedef struct tlMaterial
|
||||
{
|
||||
/** Name of the Material */
|
||||
char *name;
|
||||
|
||||
/** RGBA Colors */
|
||||
float ambient[4], diffuse[4], specular[4];
|
||||
|
||||
/** Shininess */
|
||||
float shininess;
|
||||
|
||||
} tlMaterial;
|
||||
|
||||
/** Structure describing the reference to a Material */
|
||||
typedef struct tlMaterialReference
|
||||
{
|
||||
/** Name of the Material */
|
||||
char *name;
|
||||
|
||||
/** First face in the index list */
|
||||
unsigned int face_index;
|
||||
|
||||
/** Face count */
|
||||
unsigned int face_count;
|
||||
|
||||
} tlMaterialReference;
|
||||
|
||||
/** Structure describing an Trimesh (index triangle list) containing objects, vertices (point, texture coordinate and normal) and triangle indices */
|
||||
typedef struct tlTrimesh
|
||||
{
|
||||
/** pointer to the vertex data */
|
||||
float *vertices;
|
||||
|
||||
/** number of vertices */
|
||||
unsigned int vertex_count;
|
||||
|
||||
/** format of the vertices */
|
||||
unsigned int vertex_format;
|
||||
|
||||
/** size/stride of each vertex, in bytes */
|
||||
unsigned int vertex_size;
|
||||
|
||||
/** pointer to the face (triangle) indices (3 unsigned shorts) */
|
||||
unsigned short *faces;
|
||||
|
||||
/** number of faces */
|
||||
unsigned int face_count;
|
||||
|
||||
/** list of objects in this trimesh */
|
||||
tlObject *objects;
|
||||
|
||||
/** number of objects */
|
||||
unsigned int object_count;
|
||||
|
||||
/** list of materials in this trimesh */
|
||||
tlMaterial *materials;
|
||||
|
||||
/** number of materials */
|
||||
unsigned int material_count;
|
||||
|
||||
/** list of references to materials in this trimesh */
|
||||
tlMaterialReference *material_references;
|
||||
|
||||
/** number of references to materials */
|
||||
unsigned int material_reference_count;
|
||||
|
||||
} tlTrimesh;
|
||||
|
||||
|
||||
/** Load a 3DS file in an tlTrimesh structure
|
||||
* \param filename Pointer to NULL-terminated string containing the filename
|
||||
* \param vertex_format Defines the vertex format. any format combination of TL_FVF_XYZ, TL_FVF_UV, TL_FVF_NORMAL
|
||||
* \return Returns a new tlTrimesh object, which needs to be deleted with tlDeleteTrimesh. NULL on error.
|
||||
*/
|
||||
TRIMESH_LOADER_API tlTrimesh *tlLoad3DS( const char*filename, unsigned int vertex_format );
|
||||
|
||||
|
||||
/** Load a OBJ file in an tlTrimesh structure
|
||||
* \param filename Pointer to NULL-terminated string containing the filename
|
||||
* \param vertex_format Defines the vertex format. any format combination of TL_FVF_XYZ, TL_FVF_UV, TL_FVF_NORMAL
|
||||
* \return Returns a new tlTrimesh object, which needs to be deleted with tlDeleteTrimesh. NULL on error.
|
||||
*/
|
||||
TRIMESH_LOADER_API tlTrimesh *tlLoadOBJ( const char*filename, unsigned int vertex_format );
|
||||
|
||||
/** Create an a tlTrimesh structure from a tlObjState
|
||||
* \param state Pointer to state after parsing.
|
||||
* \param vertex_format Defines the vertex format. any format combination of TL_FVF_XYZ, TL_FVF_UV, TL_FVF_NORMAL
|
||||
* \return Returns a new tlTrimesh object, which needs to be deleted with tlDeleteTrimesh. NULL on error.
|
||||
*/
|
||||
TRIMESH_LOADER_API tlTrimesh *tlCreateTrimeshFromObjState( tlObjState *state, unsigned int vertex_format );
|
||||
|
||||
/** Create an a tlTrimesh structure from a tl3dsState
|
||||
* \param state Pointer to state after parsing.
|
||||
* \param vertex_format Defines the vertex format. any format combination of TL_FVF_XYZ, TL_FVF_UV, TL_FVF_NORMAL
|
||||
* \return Returns a new tlTrimesh object, which needs to be deleted with tlDeleteTrimesh. NULL on error.
|
||||
*/
|
||||
TRIMESH_LOADER_API tlTrimesh *tlCreateTrimeshFrom3dsState( tl3dsState *state, unsigned int vertex_format );
|
||||
|
||||
/** Load an 3DS or OBJ file in an tlTrimesh structure. Automatic extension parsing is done.
|
||||
* \param filename Pointer to NULL-terminated string containing the filename
|
||||
* \param vertex_format Defines the vertex format. any format combination of TL_FVF_XYZ, TL_FVF_UV, TL_FVF_NORMAL
|
||||
* \return Returns a new tlTrimesh object, which needs to be deleted with tlDeleteTrimesh. NULL on error.
|
||||
*/
|
||||
TRIMESH_LOADER_API tlTrimesh *tlLoadTrimesh( const char*filename, unsigned int vertex_format );
|
||||
|
||||
/** Delete an previously loaded tlTrimesh object
|
||||
* \param trimesh Previously loaded tlTrimesh object
|
||||
*/
|
||||
TRIMESH_LOADER_API void tlDeleteTrimesh( tlTrimesh *trimesh );
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /*TRIMESHLOADER_H_*/
|
1302
trimeshloader/src/tl3ds.c
Normal file
1302
trimeshloader/src/tl3ds.c
Normal file
File diff suppressed because it is too large
Load Diff
1553
trimeshloader/src/tlobj.c
Normal file
1553
trimeshloader/src/tlobj.c
Normal file
File diff suppressed because it is too large
Load Diff
396
trimeshloader/src/trimeshloader.c
Normal file
396
trimeshloader/src/trimeshloader.c
Normal file
@ -0,0 +1,396 @@
|
||||
/*
|
||||
* Copyright (c) 2007 Gero Mueller <gero.mueller@cloo.de>
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
*
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
*
|
||||
* 3. This notice may not be removed or altered from any source
|
||||
* distribution.
|
||||
*/
|
||||
|
||||
#include "trimeshloader.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef WIN32
|
||||
#define PATH_SEPARATOR '\\'
|
||||
#else
|
||||
#define PATH_SEPARATOR '/'
|
||||
#endif
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
tlTrimesh *tlCreateTrimeshFrom3dsState( tl3dsState *state, unsigned int vertex_format )
|
||||
{
|
||||
tlTrimesh *trimesh = NULL;
|
||||
unsigned int i = 0, index = 0;
|
||||
|
||||
if( state == NULL )
|
||||
return NULL;
|
||||
|
||||
trimesh = malloc( sizeof(tlTrimesh) );
|
||||
memset(trimesh, 0, sizeof(tlTrimesh));
|
||||
|
||||
/* objects */
|
||||
trimesh->object_count = tl3dsObjectCount( state );
|
||||
trimesh->objects = malloc( sizeof(tlObject) * trimesh->object_count );
|
||||
for( i = 0; i < trimesh->object_count; i++ )
|
||||
{
|
||||
size_t length = strlen( tl3dsObjectName( state, i ) ) + 1;
|
||||
trimesh->objects[i].name = malloc( length );
|
||||
memcpy( trimesh->objects[i].name, tl3dsObjectName( state, i ), length );
|
||||
trimesh->objects[i].face_index = tl3dsObjectFaceIndex( state, i );
|
||||
trimesh->objects[i].face_count = tl3dsObjectFaceCount( state, i );
|
||||
}
|
||||
|
||||
/* materials */
|
||||
trimesh->material_count = tl3dsMaterialCount( state );
|
||||
trimesh->materials = malloc( sizeof(tlMaterial) * trimesh->material_count );
|
||||
for( i = 0; i < trimesh->material_count; i++ )
|
||||
{
|
||||
size_t length = strlen( tl3dsMaterialName( state, i ) ) + 1;
|
||||
trimesh->materials[i].name = malloc( length );
|
||||
memcpy( trimesh->materials[i].name, tl3dsMaterialName( state, i ), length );
|
||||
tl3dsGetMaterial( state, i,
|
||||
trimesh->materials[i].ambient,
|
||||
trimesh->materials[i].diffuse,
|
||||
trimesh->materials[i].specular,
|
||||
&(trimesh->materials[i].shininess));
|
||||
}
|
||||
|
||||
trimesh->material_reference_count = tl3dsMaterialReferenceCount( state );
|
||||
trimesh->material_references = malloc( sizeof(tlMaterialReference) * trimesh->material_reference_count );
|
||||
for( i = 0; i < trimesh->material_reference_count; i++ )
|
||||
{
|
||||
size_t length = strlen( tl3dsMaterialReferenceName( state, i ) ) + 1;
|
||||
trimesh->material_references[i].name = malloc( length );
|
||||
strcpy( trimesh->material_references[i].name, tl3dsMaterialReferenceName( state, i ) );
|
||||
tl3dsGetMaterialReference( state, i, &(trimesh->material_references[i].face_index), &(trimesh->material_references[i].face_count));
|
||||
}
|
||||
|
||||
trimesh->face_count = tl3dsFaceCount( state );
|
||||
trimesh->faces = malloc( sizeof(unsigned short) * trimesh->face_count * 3 );
|
||||
for( i = 0; i < trimesh->face_count; i++ )
|
||||
{
|
||||
unsigned int offset = i * 3;
|
||||
tl3dsGetFace( state, i,
|
||||
&trimesh->faces[offset],
|
||||
&trimesh->faces[offset + 1],
|
||||
&trimesh->faces[offset + 2] );
|
||||
}
|
||||
|
||||
trimesh->vertex_count = tl3dsVertexCount( state );
|
||||
trimesh->vertex_format = vertex_format;
|
||||
trimesh->vertex_size = vertex_format & TL_FVF_XYZ ? 3 * sizeof(float) : 0;
|
||||
trimesh->vertex_size += vertex_format & TL_FVF_UV ? 2 * sizeof(float) : 0;
|
||||
trimesh->vertex_size += vertex_format & TL_FVF_NORMAL ? 3 * sizeof(float) : 0;
|
||||
trimesh->vertices = malloc( trimesh->vertex_count * trimesh->vertex_size );
|
||||
index = 0;
|
||||
for( i = 0; i < trimesh->vertex_count; i++ )
|
||||
{
|
||||
float *x = NULL, *y = NULL, *z = NULL;
|
||||
float *u = NULL, *v = NULL;
|
||||
float *nx = NULL, *ny = NULL, *nz = NULL;
|
||||
|
||||
if( vertex_format & TL_FVF_XYZ )
|
||||
{
|
||||
x = &trimesh->vertices[index++];
|
||||
y = &trimesh->vertices[index++];
|
||||
z = &trimesh->vertices[index++];
|
||||
}
|
||||
|
||||
if( vertex_format & TL_FVF_UV )
|
||||
{
|
||||
u = &trimesh->vertices[index++];
|
||||
v = &trimesh->vertices[index++];
|
||||
}
|
||||
|
||||
if( vertex_format & TL_FVF_NORMAL )
|
||||
{
|
||||
nx = &trimesh->vertices[index++];
|
||||
ny = &trimesh->vertices[index++];
|
||||
nz = &trimesh->vertices[index++];
|
||||
}
|
||||
|
||||
tl3dsGetVertex( state, i, x, y, z, u, v, nx, ny, nz );
|
||||
}
|
||||
|
||||
return trimesh;
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
tlTrimesh *tlLoad3DS( const char *filename, unsigned int vertex_format )
|
||||
{
|
||||
FILE *f = 0;
|
||||
tlTrimesh *trimesh = NULL;
|
||||
|
||||
if( filename == NULL )
|
||||
return NULL;
|
||||
|
||||
f = fopen( filename, "r" );
|
||||
if( f )
|
||||
{
|
||||
tl3dsState *state = NULL;
|
||||
|
||||
state = tl3dsCreateState();
|
||||
if( state )
|
||||
{
|
||||
char buffer[1024];
|
||||
unsigned int size = 0;
|
||||
|
||||
while( !feof( f ) )
|
||||
{
|
||||
size = (unsigned int) fread( buffer, 1, sizeof(buffer), f );
|
||||
tl3dsParse( state, buffer, size, size < sizeof(buffer) ? 1 : 0 );
|
||||
}
|
||||
|
||||
trimesh = tlCreateTrimeshFrom3dsState( state, vertex_format );
|
||||
|
||||
tl3dsDestroyState( state );
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
}
|
||||
|
||||
return trimesh;
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
tlTrimesh *tlCreateTrimeshFromObjState( tlObjState *state, unsigned int vertex_format )
|
||||
{
|
||||
tlTrimesh *trimesh = NULL;
|
||||
unsigned int i = 0, index = 0;
|
||||
|
||||
if( state == NULL )
|
||||
return NULL;
|
||||
|
||||
trimesh = malloc( sizeof(tlTrimesh) );
|
||||
memset(trimesh, 0, sizeof(tlTrimesh));
|
||||
|
||||
trimesh->object_count = tlObjObjectCount( state );
|
||||
trimesh->objects = malloc( sizeof(tlObject) * trimesh->object_count );
|
||||
for( i = 0; i < trimesh->object_count; i++ )
|
||||
{
|
||||
size_t length = strlen( tlObjObjectName( state, i ) ) + 1;
|
||||
trimesh->objects[i].name = malloc( length );
|
||||
memcpy( trimesh->objects[i].name, tlObjObjectName( state, i ), length );
|
||||
trimesh->objects[i].face_index = tlObjObjectFaceIndex( state, i );
|
||||
trimesh->objects[i].face_count = tlObjObjectFaceCount( state, i );
|
||||
}
|
||||
|
||||
/* materials */
|
||||
trimesh->material_count = tlObjMaterialCount( state );
|
||||
trimesh->materials = malloc( sizeof(tlMaterial) * trimesh->material_count );
|
||||
for( i = 0; i < trimesh->material_count; i++ )
|
||||
{
|
||||
size_t length = strlen( tlObjMaterialName( state, i ) ) + 1;
|
||||
trimesh->materials[i].name = malloc( length );
|
||||
memcpy( trimesh->materials[i].name, tlObjMaterialName( state, i ), length );
|
||||
tlObjGetMaterial( state, i,
|
||||
trimesh->materials[i].ambient,
|
||||
trimesh->materials[i].diffuse,
|
||||
trimesh->materials[i].specular,
|
||||
&(trimesh->materials[i].shininess));
|
||||
}
|
||||
|
||||
trimesh->material_reference_count = tlObjMaterialReferenceCount( state );
|
||||
trimesh->material_references = malloc( sizeof(tlMaterialReference) * trimesh->material_reference_count );
|
||||
for( i = 0; i < trimesh->material_reference_count; i++ )
|
||||
{
|
||||
size_t length = strlen( tlObjMaterialReferenceName( state, i ) ) + 1;
|
||||
trimesh->material_references[i].name = malloc( length );
|
||||
strcpy( trimesh->material_references[i].name, tlObjMaterialReferenceName( state, i ) );
|
||||
tlObjGetMaterialReference( state, i, &(trimesh->material_references[i].face_index), &(trimesh->material_references[i].face_count));
|
||||
}
|
||||
|
||||
|
||||
trimesh->face_count = tlObjFaceCount( state );
|
||||
trimesh->faces = malloc( sizeof(unsigned short) * trimesh->face_count * 3 );
|
||||
for( i = 0; i < trimesh->face_count; i++ )
|
||||
{
|
||||
unsigned int offset = i * 3;
|
||||
tlObjGetFace( state, i,
|
||||
&trimesh->faces[offset],
|
||||
&trimesh->faces[offset + 1],
|
||||
&trimesh->faces[offset + 2] );
|
||||
}
|
||||
|
||||
trimesh->vertex_count = tlObjVertexCount( state );
|
||||
trimesh->vertex_format = vertex_format;
|
||||
trimesh->vertex_size = vertex_format & TL_FVF_XYZ ? 3 * sizeof(float) : 0;
|
||||
trimesh->vertex_size += vertex_format & TL_FVF_UV ? 2 * sizeof(float) : 0;
|
||||
trimesh->vertex_size += vertex_format & TL_FVF_NORMAL ? 3 * sizeof(float) : 0;
|
||||
trimesh->vertices = malloc( trimesh->vertex_count * trimesh->vertex_size );
|
||||
index = 0;
|
||||
for( i = 0; i < trimesh->vertex_count; i++ )
|
||||
{
|
||||
float *x = NULL, *y = NULL, *z = NULL;
|
||||
float *u = NULL, *v = NULL;
|
||||
float *nx = NULL, *ny = NULL, *nz = NULL;
|
||||
|
||||
if( vertex_format & TL_FVF_XYZ )
|
||||
{
|
||||
x = &trimesh->vertices[index++];
|
||||
y = &trimesh->vertices[index++];
|
||||
z = &trimesh->vertices[index++];
|
||||
}
|
||||
|
||||
if( vertex_format & TL_FVF_UV )
|
||||
{
|
||||
u = &trimesh->vertices[index++];
|
||||
v = &trimesh->vertices[index++];
|
||||
}
|
||||
|
||||
if( vertex_format & TL_FVF_NORMAL )
|
||||
{
|
||||
nx = &trimesh->vertices[index++];
|
||||
ny = &trimesh->vertices[index++];
|
||||
nz = &trimesh->vertices[index++];
|
||||
}
|
||||
|
||||
tlObjGetVertex( state, i, x, y, z, u, v, nx, ny, nz );
|
||||
}
|
||||
|
||||
return trimesh;
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static char *get_dirname( const char *filename )
|
||||
{
|
||||
char *dirname, *separator;
|
||||
|
||||
/* find last sperator */
|
||||
separator = strrchr( filename, PATH_SEPARATOR );
|
||||
|
||||
/* copy dirname */
|
||||
if( separator && (*separator == PATH_SEPARATOR) )
|
||||
{
|
||||
size_t length = (separator - filename);
|
||||
dirname = malloc( length + 1 );
|
||||
strncpy( dirname, filename, length );
|
||||
dirname[length] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
dirname = malloc( 1 );
|
||||
dirname[0] = '\0';
|
||||
}
|
||||
|
||||
return dirname;
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
static void parse_obj_file( tlObjState *state, const char *filename )
|
||||
{
|
||||
FILE *file = fopen( filename, "r" );
|
||||
if( file )
|
||||
{
|
||||
char buffer[1024];
|
||||
unsigned int size = 0;
|
||||
|
||||
while( !feof( file ) )
|
||||
{
|
||||
size = (unsigned int)fread( buffer, 1, sizeof(buffer), file );
|
||||
tlObjParse( state, buffer, size, size < sizeof(buffer) ? 1 : 0 );
|
||||
}
|
||||
|
||||
fclose( file );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
tlTrimesh *tlLoadOBJ( const char *filename, unsigned int vertex_format )
|
||||
{
|
||||
tlTrimesh *trimesh = NULL;
|
||||
tlObjState *state = NULL;
|
||||
char *dirname = NULL;
|
||||
size_t dirname_length = 0;
|
||||
|
||||
if( filename == NULL )
|
||||
return NULL;
|
||||
|
||||
dirname = get_dirname( filename );
|
||||
dirname_length = strlen( dirname );
|
||||
|
||||
state = tlObjCreateState();
|
||||
if( state )
|
||||
{
|
||||
unsigned int mtlcount = 0, i;
|
||||
|
||||
parse_obj_file( state, filename );
|
||||
|
||||
mtlcount = tlObjMaterialLibCount( state );
|
||||
for( i = 0; i < mtlcount; i++ )
|
||||
{
|
||||
const char *libname = tlObjMaterialLibName( state, i );
|
||||
size_t path_length = dirname_length + strlen( libname );
|
||||
char *path = malloc( path_length + 1 );
|
||||
strcpy( path, dirname );
|
||||
strcpy( path + dirname_length, libname );
|
||||
|
||||
parse_obj_file( state, path );
|
||||
|
||||
free( path );
|
||||
}
|
||||
|
||||
trimesh = tlCreateTrimeshFromObjState( state, vertex_format );
|
||||
tlObjDestroyState( state );
|
||||
|
||||
}
|
||||
|
||||
free( dirname );
|
||||
|
||||
return trimesh;
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
tlTrimesh *tlLoadTrimesh( const char* filename, unsigned int vertex_format )
|
||||
{
|
||||
tlTrimesh *trimesh = NULL;
|
||||
|
||||
if( tl3dsCheckFileExtension( filename ) == 0 )
|
||||
trimesh = tlLoad3DS( filename, vertex_format );
|
||||
else if( tlObjCheckFileExtension( filename ) == 0 )
|
||||
trimesh = tlLoadOBJ( filename, vertex_format );
|
||||
|
||||
return trimesh;
|
||||
}
|
||||
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
void tlDeleteTrimesh( tlTrimesh *trimesh )
|
||||
{
|
||||
unsigned int i = 0;
|
||||
|
||||
if( trimesh == NULL )
|
||||
return;
|
||||
|
||||
for( i = 0; i < trimesh->object_count; i++ )
|
||||
free( trimesh->objects[i].name );
|
||||
|
||||
free( trimesh->objects );
|
||||
free( trimesh->faces );
|
||||
free( trimesh->vertices );
|
||||
free( trimesh );
|
||||
}
|
Reference in New Issue
Block a user