diff options
Diffstat (limited to '')
-rw-r--r-- | src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dpm/imp_dpm.c | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dpm/imp_dpm.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dpm/imp_dpm.c new file mode 100644 index 0000000..415bc74 --- /dev/null +++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dpm/imp_dpm.c | |||
@@ -0,0 +1,156 @@ | |||
1 | /* $Id:$ */ | ||
2 | |||
3 | /* | ||
4 | libg3d - 3D object loading library | ||
5 | |||
6 | Copyright (C) 2005-2009 Markus Dahms <mad@automagically.de> | ||
7 | |||
8 | This library is free software; you can redistribute it and/or | ||
9 | modify it under the terms of the GNU Lesser General Public | ||
10 | License as published by the Free Software Foundation; either | ||
11 | version 2.1 of the License, or (at your option) any later version. | ||
12 | |||
13 | This library is distributed in the hope that it will be useful, | ||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | Lesser General Public License for more details. | ||
17 | |||
18 | You should have received a copy of the GNU Lesser General Public | ||
19 | License along with this library; if not, write to the Free Software | ||
20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | |||
22 | plugin by Martin Gerhardy <martin.gerhardy@gmail.com> | ||
23 | */ | ||
24 | |||
25 | #include <stdio.h> | ||
26 | #include <string.h> | ||
27 | |||
28 | #include <g3d/types.h> | ||
29 | #include <g3d/material.h> | ||
30 | #include <g3d/texture.h> | ||
31 | #include <g3d/stream.h> | ||
32 | #include <g3d/iff.h> | ||
33 | |||
34 | EAPI | ||
35 | gboolean plugin_load_model_from_stream(G3DContext *context, G3DStream *stream, | ||
36 | G3DModel *model, gpointer user_data) | ||
37 | { | ||
38 | G3DObject *object; | ||
39 | gchar magic[16], texture[32]; | ||
40 | guint32 type, filesize; | ||
41 | guint32 num_bones, num_meshs, num_frames; | ||
42 | guint32 ofs_bones, ofs_meshs, ofs_frames; | ||
43 | guint32 ofs_texcoords, ofs_verts, ofs_indices; | ||
44 | goffset off_start; | ||
45 | G3DImage *image = NULL; | ||
46 | G3DMaterial *material, *mat; | ||
47 | G3DFace *face; | ||
48 | GSList *mitem; | ||
49 | G3DFloat *normals; | ||
50 | int i; | ||
51 | |||
52 | off_start = g3d_stream_tell(stream); | ||
53 | |||
54 | g3d_stream_read(stream, magic, 16); | ||
55 | if(strcmp("DARKPLACESMODEL", magic)) { | ||
56 | g_warning("DPM: Unknown magic id: '%s'\n", magic); | ||
57 | return FALSE; | ||
58 | } | ||
59 | |||
60 | object = g_new0(G3DObject, 1); | ||
61 | |||
62 | /* read the header information */ | ||
63 | type = g3d_stream_read_int32_be(stream); | ||
64 | filesize = g3d_stream_read_int32_be(stream); | ||
65 | /* skip some floats */ | ||
66 | for (i = 0; i < 8; i++) | ||
67 | g3d_stream_read_float_le(stream); | ||
68 | num_bones = g3d_stream_read_int32_be(stream); | ||
69 | num_meshs = g3d_stream_read_int32_be(stream); | ||
70 | num_frames = g3d_stream_read_int32_be(stream); | ||
71 | ofs_bones = g3d_stream_read_int32_be(stream); | ||
72 | ofs_meshs = g3d_stream_read_int32_be(stream); | ||
73 | ofs_frames = g3d_stream_read_int32_be(stream); | ||
74 | |||
75 | /* default material */ | ||
76 | material = g3d_material_new(); | ||
77 | material->name = g_strdup("default material"); | ||
78 | object->materials = g_slist_append(object->materials, material); | ||
79 | |||
80 | num_meshs = 1; /* only load the first mesh */ | ||
81 | for (i = 0; i < num_meshs; i++) { | ||
82 | guint32 nvertex, ntris; | ||
83 | int j; | ||
84 | |||
85 | g3d_stream_seek(stream, off_start + ofs_meshs, G_SEEK_SET); | ||
86 | g3d_stream_read(stream, texture, 32); | ||
87 | |||
88 | /* read texture image */ | ||
89 | if(strlen(texture) > 0) { | ||
90 | image = g3d_texture_load_cached(context, model, texture); | ||
91 | if(image == NULL) { | ||
92 | /* try jpeg */ | ||
93 | char *strp = strrchr(texture, '.'); | ||
94 | if(strp) { | ||
95 | strcpy(strp, ".jpg"); | ||
96 | image = g3d_texture_load_cached(context, model, texture); | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | |||
101 | if(image == NULL) { | ||
102 | mitem = model->materials; | ||
103 | while(mitem) { | ||
104 | mat = (G3DMaterial *)mitem->data; | ||
105 | if(strcmp(mat->name, object->name) == 0) { | ||
106 | image = mat->tex_image; | ||
107 | break; | ||
108 | } | ||
109 | mitem = mitem->next; | ||
110 | } | ||
111 | } | ||
112 | |||
113 | nvertex = g3d_stream_read_int32_be(stream); | ||
114 | ntris = g3d_stream_read_int32_be(stream); | ||
115 | ofs_verts = g3d_stream_read_int32_be(stream); | ||
116 | ofs_texcoords = g3d_stream_read_int32_be(stream); | ||
117 | ofs_indices = g3d_stream_read_int32_be(stream); | ||
118 | |||
119 | /* read vertex data */ | ||
120 | g3d_stream_seek(stream, off_start + ofs_verts, G_SEEK_SET); | ||
121 | object->vertex_count = nvertex; | ||
122 | object->vertex_data = g_new0(G3DFloat, nvertex * 3); | ||
123 | normals = g_new0(G3DFloat, nvertex * 3); | ||
124 | for(j = 0; j < nvertex; j++) { | ||
125 | |||
126 | } | ||
127 | |||
128 | /* read texture coords */ | ||
129 | g3d_stream_seek(stream, off_start + ofs_texcoords, G_SEEK_SET); | ||
130 | object->tex_vertex_data = g_new0(G3DFloat, nvertex * 2); | ||
131 | for(j = 0; j < nvertex; j++) { | ||
132 | object->tex_vertex_data[j * 2 + 0] = | ||
133 | g3d_stream_read_float_be(stream); | ||
134 | object->tex_vertex_data[j * 2 + 1] = | ||
135 | g3d_stream_read_float_be(stream); | ||
136 | } | ||
137 | |||
138 | /* read triangles */ | ||
139 | /* TODO */ | ||
140 | } | ||
141 | |||
142 | return TRUE; | ||
143 | } | ||
144 | |||
145 | EAPI | ||
146 | gchar *plugin_description(G3DContext *context) | ||
147 | { | ||
148 | return g_strdup("Darkplaces engine models."); | ||
149 | } | ||
150 | |||
151 | EAPI | ||
152 | gchar **plugin_extensions(G3DContext *context) | ||
153 | { | ||
154 | return g_strsplit("dpm", ":", 0); | ||
155 | } | ||
156 | |||