Permalink
Browse files

2006-08-24 Al Riddoch <alriddoch@zepler.org>

	* libmd3/endian.c, libmd3/endian.h: Add more endian functions to
	  transform bulk data.

	* libmd3/loader.c: Call the new endian functions on the mesh data.
  • Loading branch information...
1 parent a4bd7bf commit c604a945190f719d1a66ac96db06f33152e5539d @alriddoch committed Aug 24, 2006
Showing with 124 additions and 6 deletions.
  1. +7 −0 ChangeLog
  2. +91 −1 libmd3/endian.c
  3. +11 −0 libmd3/endian.h
  4. +15 −5 libmd3/loader.c
View
@@ -1,5 +1,12 @@
2006-08-24 Al Riddoch <alriddoch@zepler.org>
+ * libmd3/endian.c, libmd3/endian.h: Add more endian functions to
+ transform bulk data.
+
+ * libmd3/loader.c: Call the new endian functions on the mesh data.
+
+2006-08-24 Al Riddoch <alriddoch@zepler.org>
+
* tests/rendermd3.c: Use SDL_Image to load textures, fix includes
to deal with Apple's broken header names. Add zoom controls.
View
@@ -17,7 +17,6 @@
*/
#include <libmd3/endian.h>
-#include <libmd3/inttypes.h>
#include <libmd3/structure.h>
void swap_32(uint8_t * ptr)
@@ -30,6 +29,14 @@ void swap_32(uint8_t * ptr)
ptr[2] = tmp;
}
+void swap_16(uint8_t * ptr)
+{
+ uint8_t tmp = ptr[0];
+ ptr[0] = ptr[1];
+ ptr[1] = tmp;
+}
+
+#if (LIBMD3_BYTE_ORDER != LIBMD3_LITTLE_ENDIAN)
static void swap_int32(int32_t * num)
{
swap_32((uint8_t*)num);
@@ -45,6 +52,12 @@ static void swap_float(float * num)
swap_32((uint8_t*)num);
}
+static void swap_int16(int16_t * num)
+{
+ swap_16((uint8_t*)num);
+}
+#endif
+
void libmd3_endian_header(md3_header * header)
{
#if (LIBMD3_BYTE_ORDER != LIBMD3_LITTLE_ENDIAN)
@@ -87,3 +100,80 @@ void libmd3_endian_frames(md3_frame * frames, int count)
}
#endif
}
+
+void libmd3_endian_mesh(md3_mesh * mesh)
+{
+#if (LIBMD3_BYTE_ORDER != LIBMD3_LITTLE_ENDIAN)
+ swap_int32(&mesh->frame_count);
+ swap_int32(&mesh->skin_count);
+ swap_int32(&mesh->vertex_count);
+ swap_int32(&mesh->triangle_count);
+ swap_int32(&mesh->triangle_start);
+ swap_int32(&mesh->skin_start);
+ swap_int32(&mesh->texcoord_start);
+ swap_int32(&mesh->vertex_start);
+ swap_int32(&mesh->mesh_len);
+#endif
+}
+
+void libmd3_endian_tag(md3_tag * tag)
+{
+#if (LIBMD3_BYTE_ORDER != LIBMD3_LITTLE_ENDIAN)
+ float * r = tag->rotation;
+ unsigned int i;
+
+ swap_float(&tag->position[0]);
+ swap_float(&tag->position[1]);
+ swap_float(&tag->position[2]);
+
+ for (i=0; i<9; ++i, ++r)
+ swap_float(r);
+#endif
+}
+
+void libmd3_endian_tags(md3_tag * tags, int count)
+{
+#if (LIBMD3_BYTE_ORDER != LIBMD3_LITTLE_ENDIAN)
+ int i;
+ md3_tag * tag;
+
+ for (tag = tags, i = 0; i < count; ++tag, ++i)
+ libmd3_endian_tag(tag);
+
+#endif
+}
+
+void libmd3_endian_int32s(int32_t *d, int count)
+{
+#if (LIBMD3_BYTE_ORDER != LIBMD3_LITTLE_ENDIAN)
+ int i;
+
+ for (i = 0; i < count; ++d, ++i)
+ swap_int32(d);
+#endif
+}
+
+void libmd3_endian_floats(float * d, int count)
+{
+#if (LIBMD3_BYTE_ORDER != LIBMD3_LITTLE_ENDIAN)
+ int i;
+
+ for (i = 0; i < count; ++d, ++i)
+ swap_float(d);
+#endif
+}
+
+void libmd3_endian_vertices(int16_t* d, int num_vertices)
+{
+#if (LIBMD3_BYTE_ORDER != LIBMD3_LITTLE_ENDIAN)
+ int i;
+
+ for (i = 0; i < num_vertices; ++i, ++d)
+ {
+ swap_int16(d++);
+ swap_int16(d++);
+ swap_int16(d++);
+ /* extra int16 encodes normal, skipped by ++d in loop update */
+ }
+#endif
+}
View
@@ -52,10 +52,21 @@
#endif /* __linux __ */
+#include <libmd3/inttypes.h>
+
struct _md3_header;
struct _md3_frame;
+struct _md3_mesh;
+struct _md3_tag;
void libmd3_endian_header(struct _md3_header *);
void libmd3_endian_frames(struct _md3_frame * frames, int count);
+void libmd3_endian_mesh(struct _md3_mesh *);
+void libmd3_endian_tags(struct _md3_tag * tags, int count);
+
+void libmd3_endian_int32s(int32_t*, int count);
+void libmd3_endian_floats(float*, int count);
+
+void libmd3_endian_vertices(int16_t*, int num_vertices);
#endif /* LIBMD3_ENDIAN_H */
View
@@ -51,7 +51,7 @@ static int libmd3_frame_load(FILE * fptr, libmd3_file * file)
}
cnt = fread(frames, sizeof(md3_frame), file->header->frame_count, fptr);
if (cnt != file->header->frame_count) {
- fprintf(stderr, "Unexpected end of file.\n");
+ fprintf(stderr, "Unexpected end of file reading frames.\n");
free(frames);
return 1;
}
@@ -94,10 +94,12 @@ static int libmd3_tag_load(FILE * fptr, libmd3_file * file)
}
cnt = fread(tags, sizeof(md3_tag), num_tags, fptr);
if (cnt != num_tags) {
- fprintf(stderr, "Unexpected end of file.\n");
+ fprintf(stderr, "Unexpected end of file reading tags.\n");
free(tags);
return 1;
}
+
+ libmd3_endian_tags(tags, cnt);
file->tags = tags;
return 0;
}
@@ -124,11 +126,13 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
}
if (fread(mesh->mesh_header, sizeof(md3_mesh), 1, fptr) != 1) {
- fprintf(stderr, "Unexpected end of file.\n");
+ fprintf(stderr, "Unexpected end of file reading mesh.\n");
free(mesh->mesh_header);
return 1;
}
+ libmd3_endian_mesh(mesh->mesh_header);
+
if ((mesh->mesh_header->ident[0] != 'I') ||
(mesh->mesh_header->ident[1] != 'D') ||
(mesh->mesh_header->ident[2] != 'P') ||
@@ -158,7 +162,7 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
cnt = fread(mesh->skins, sizeof(md3_skin),
mesh->mesh_header->skin_count, fptr);
if (cnt != mesh->mesh_header->skin_count) {
- fprintf(stderr, "Unexpected end of file.\n");
+ fprintf(stderr, "Unexpected end of file reading skins.\n");
free(mesh->mesh_header);
free(mesh->skins);
return 1;
@@ -182,13 +186,15 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
cnt = fread(mesh->triangles, sizeof(int32_t), num_indices, fptr);
if (cnt != num_indices) {
- fprintf(stderr, "Unexpected end of file.\n");
+ fprintf(stderr, "Unexpected end of file reading triangles.\n");
free(mesh->mesh_header);
free(mesh->skins);
free(mesh->triangles);
return 1;
}
+ libmd3_endian_int32s(mesh->triangles, num_indices);
+
if (fseek(fptr, file_pos + mesh->mesh_header->texcoord_start, SEEK_SET) != 0) {
fprintf(stderr, "Unexpected error finding start of texcoords.\n");
free(mesh->mesh_header);
@@ -217,6 +223,8 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
return 1;
}
+ libmd3_endian_floats(mesh->texcoords, num_texcoords);
+
if (fseek(fptr, file_pos + mesh->mesh_header->vertex_start, SEEK_SET) != 0) {
fprintf(stderr, "Unexpected error finding start of texcoords.\n");
free(mesh->mesh_header);
@@ -250,6 +258,8 @@ static int libmd3_mesh_load(FILE * fptr, libmd3_mesh * mesh)
return 1;
}
+ libmd3_endian_vertices(mesh->vertices, num_vertices);
+
return 0;
}

0 comments on commit c604a94

Please sign in to comment.