#ifndef	V_OBJLIB_H
#define V_OBJLIB_H

/*
 *	Structures and functions used when loading/saving meshes
 */

/* Single vertex and triangular face, respectively */
typedef struct mesh_vertex_s
{
	u32_t	vertex;				/* Index of geometry vertex */
	u32_t	texcoord;			/* Index of texture vertex / texinfo */
	u32_t	normal;				/* Index of normal vector */
	u32_t	color;				/* Index of vertex color */
}	mesh_vertex_t, *mesh_vertex_p, mesh_face_t[3];

/* Texture map */
typedef	struct	mesh_map_s
{
	char		file_name[_MAX_PATH];
	GLuint		handle;			/* for glBindTexture() */
	u32_t		env;			/* GL_DECAL / GL_MODULATE */
	GLuint		min_filter, mag_filter; /* GL_NEAREST, ... */
	GLuint		clamp_s, clamp_t;	/* GL_CLAMP / GL_REPEAT */
	u8_t		attrib[4];		/* variable-sized per-map attrib */
}	mesh_map_t, *mesh_map_p;

/* Material */
typedef struct	mesh_mtl_s
{
	vec4_t		diffuse;		/* diffuse color */
	vec4_t		specular;		/* specular color */
	vec4_t		emissive;		/* emissive color */
	vec_t		specular_power;
	u32_t		map_primary;	/* index of diffuse map */
	u32_t		map_secondary;	/* index of FX map */
	u8_t		attrib[4];		/* variable-sized per-mtl attrib */
}	mesh_mtl_t, *mesh_mtl_p;

/* This structure represent geometry object */
typedef struct mesh_object_s
{
	char	name[64];			/* Object name */
	char	material[64];		/* Material name */
	u32_t	mtlid;				/* Valid only after mtlLoad() */
	u32_t	startface;			/* Index of start face */
	u32_t	num_faces;			/* Number of faces */
	vec3_t	mins, maxs;			/* Object bounding box */
	vec3_t	pivot;				/* Pivot point */
	matrix3x4_t xform;			/* node transform */
	u32_t	faces_flipped;		/* flip order of vertices when saving */
	u8_t	attrib[4];			/* variable-sized per-object attrib */
}	mesh_object_t, *mesh_object_p;

/* This structure represent all objects in mesh */
typedef struct	scene_s
{
	u32_t	num_vertices;	/* Number of vertices */
	u32_t	vertex_count;	/* Number of added vertices */
	u32_t	num_texcoords;	/* Number of texture coords */
	u32_t	texcoord_count;	/* Number of added texcoords */
	u32_t	num_normals;	/* Number of normals */
	u32_t	normal_count;	/* Number of added normals */
	u32_t	num_faces;		/* Number of faces */
	u32_t	face_count;		/* Number of added faces */
	u32_t	num_objects;	/* Number of objects */
	u32_t	object_count;	/* Number of added objects */
	u32_t	num_colors;		/* Number of color vertices */
	u32_t	color_count;	/* Number of added color verts */
	u32_t	num_maps;		/* Number of texture maps */
	u32_t	map_count;		/* Number of added texture maps */
	u32_t	num_mtls;		/* Number of materials */
	u32_t	mtl_count;		/* Number of added materials */
	vec3_t	mins, maxs;		/* Mesh bounding box */
	vec3_t	*vertices;		/* Vertex pool */
    vec2_t	*texcoords;		/* Texture coord. pool */
	vec3_t	*normals;		/* Vert. normal vector pool */
	mesh_face_t		*faces;			/* Face pool */
	vec3_t			*face_centers;	/* Centers used when sorting */
	plane3_t		*face_planes;	/* Face normals & dists */
	mesh_object_t	*objects;		/* Object pool */
	color4b_t		*colors;		/* Color vertices */
	mesh_map_t		*maps;			/* Texture maps */
	mesh_mtl_t		*mtls;			/* Materials */
}	scene_t, *scene_p;

#ifdef	__cplusplus
extern "C" {
#endif	/* __cplusplus */
    scene_p V_GLOBAL meshAlloc(
		u32_t num_vertices,
		u32_t num_texcoords,
		u32_t num_normals,
		u32_t num_faces,
		u32_t num_objects,
		u32_t num_colors,
		u32_t num_maps,
		u32_t num_mtls);
	/* Returns memAlloc()'ed block */
	scene_p		V_GLOBAL	objLoad(char *filename);
	/* Do NOT frees any memory, just saves mesh */
	vboolean	V_GLOBAL	objSave(char *filename,scene_p obj);
#ifdef	__cplusplus
}
#endif	/* __cplusplus */

#endif	/* V_OBJLIB_H */
/*EOF*/
