aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_glb/imp_glb.c
blob: d9bea0b7d8feacdafd41ace2412ba5d00c8d22a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* $Id$ */

/*
    libg3d - 3D object loading library

    Copyright (C) 2005-2009  Markus Dahms <mad@automagically.de>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <g3d/types.h>
#include <g3d/stream.h>
#include <g3d/material.h>
#include <g3d/texture.h>
#include <g3d/iff.h>
#include <g3d/vector.h>

static G3DObject *glb_load_object(G3DContext *context, G3DStream *stream,
	G3DModel *model);

EAPI
gboolean plugin_load_model_from_stream(G3DContext *context, G3DStream *stream,
	G3DModel *model, gpointer user_data)
{
	return (glb_load_object(context, stream, model) != NULL);
}

EAPI
gchar *plugin_description(G3DContext *context)
{
	return g_strdup("UltimateStunts models.");
}

EAPI
gchar **plugin_extensions(G3DContext *context)
{
	return g_strsplit("glb", ":", 0);
}

/*****************************************************************************/
/* GLB specific                                                              */

static G3DFloat glb_get_float(G3DStream *stream)
{
	return 0.001 * (
		(float)((guint32)g3d_stream_read_int32_be(stream)) - (float)(0x7FFFFFFF));
}

static G3DObject *glb_load_object(G3DContext *context, G3DStream *stream,
	G3DModel *model)
{
	G3DObject *pobject, *object;
	G3DMaterial *material;
	G3DFace *face;
	guint32 magic, otype, index;
	gint32 i, j, msize, namelen, datalen, nvertices, nindices;
	gchar *name;
	G3DFloat *normals = NULL, *texcoords = NULL;

	magic = g3d_stream_read_int32_be(stream);
	if(magic != G3D_IFF_MKID('\0', 'G', 'L', 'B')) {
		g_warning("%s is not a correct GLB file (wrong magic)\n",
			stream->uri);
		return NULL;
	}

	pobject = g_new0(G3DObject, 1);
	pobject->name = g_strdup(stream->uri);
	model->objects = g_slist_append(model->objects, pobject);

	while(!g3d_stream_eof(stream)) {
		otype = g3d_stream_read_int32_be(stream);
		namelen = g3d_stream_read_int32_be(stream);

		if(namelen == 0)
			break;

		name = g_new0(gchar, namelen + 1);
		g3d_stream_read(stream, name, namelen);
#if DEBUG > 0
		printf("GLB: object named '%s'\n", name);
#endif

		object = g_new0(G3DObject, 1);
		object->name = g_strdup(name);
		g_free(name);
		pobject->objects = g_slist_append(pobject->objects, object);

		/* hide collision planes by default */
		if(strncmp(object->name, "Collision plane", 15) == 0)
			object->hide = TRUE;

		datalen = g3d_stream_read_int32_be(stream);

		if(otype != 0) {
			/* skip */
			g3d_stream_skip(stream, datalen);
			continue;
		}

		/* object type 0 */
		msize = g3d_stream_read_int32_be(stream);
		nvertices = g3d_stream_read_int32_be(stream);
		nindices = g3d_stream_read_int32_be(stream);

#if DEBUG > 0
		printf("GLB: material size: %d bytes, %d vertices, %d indices\n",
			msize, nvertices, nindices);
#endif
		if(msize > 0) {
			/* material */
			material = g3d_material_new();
			material->name = g_strdup("default material");
			object->materials = g_slist_append(object->materials, material);

			material->r = (G3DFloat)g3d_stream_read_int8(stream) / 255.0;
			material->g = (G3DFloat)g3d_stream_read_int8(stream) / 255.0;
			material->b = (G3DFloat)g3d_stream_read_int8(stream) / 255.0;
			material->a = (G3DFloat)g3d_stream_read_int8(stream) / 255.0;

			if(material->a == 0.0)
				material->a = 1.0;

			/* replacement color */
			g3d_stream_read_int8(stream);
			g3d_stream_read_int8(stream);
			g3d_stream_read_int8(stream);
			g3d_stream_read_int8(stream); /* unused */

			g3d_stream_read_int8(stream); /* LODs */
			g3d_stream_read_int8(stream); /* reflectance */
			/* emissivity */
			material->shininess = (G3DFloat)g3d_stream_read_int8(stream) / 255.0;
			g3d_stream_read_int8(stream); /* static friction */
			g3d_stream_read_int8(stream); /* dynamic friction */
			g3d_stream_read_int8(stream); /* unused */
			g3d_stream_read_int8(stream); /* unused */
			g3d_stream_read_int8(stream); /* unused */

			/* texture name */
			namelen = msize - 16;
			if(namelen > 0) {
				name = g_new0(gchar, namelen + 1);
				g3d_stream_read(stream, name, namelen);
#if DEBUG > 1
				printf("GLB: texture name: %s\n", name);
#endif

				/* texture name is something like "0", the real name is in
				 * "../$carname.conf"; try to load default texture */
				if(name[0] == '0') {
					if(g_file_test("textures.jpg", G_FILE_TEST_EXISTS)) {
						material->tex_image = g3d_texture_load_cached(
							context, model, "textures.jpg");
						if(material->tex_image != NULL)
							material->tex_image->tex_id = 1;
					}
				}

				g_free(name);
			}
		}

		/* vertices */
		if(nvertices > 0) {
			object->vertex_count = nvertices;
			object->vertex_data = g_new0(G3DFloat, nvertices * 3);
			normals = g_new0(G3DFloat, nvertices * 3);
			texcoords = g_new0(G3DFloat, nvertices * 2);

			for(i = 0; i < nvertices; i ++) {
				object->vertex_data[i * 3 + 0] = glb_get_float(stream);
				object->vertex_data[i * 3 + 1] = glb_get_float(stream);
				object->vertex_data[i * 3 + 2] = glb_get_float(stream);

#if DEBUG > 3
				printf("D: %f, %f, %f\n",
					object->vertex_data[i * 3 + 0],
					object->vertex_data[i * 3 + 1],
					object->vertex_data[i * 3 + 2]);
#endif

				/* normal */
				normals[i * 3 + 0] = glb_get_float(stream);
				normals[i * 3 + 1] = glb_get_float(stream);
				normals[i * 3 + 2] = glb_get_float(stream);
				g3d_vector_unify(
					normals + i * 3 + 0,
					normals + i * 3 + 1,
					normals + i * 3 + 2);

				/* texture coordinates */
				texcoords[i * 2 + 0] = glb_get_float(stream) / 64;
				texcoords[i * 2 + 1] = 1.0 - glb_get_float(stream) / 64;
			}
		}

		if(nindices > 0) {
			for(i = 0; i < nindices; i += 3) {
				face = g_new0(G3DFace, 1);
				face->vertex_count = 3;
				face->vertex_indices = g_new0(guint32, 3);
				face->normals = g_new0(G3DFloat, 3 * 3);
				face->flags |= G3D_FLAG_FAC_NORMALS;
				for(j = 0; j < 3; j ++) {
					face->vertex_indices[j] = g3d_stream_read_int32_be(stream);

					/* set normals */
					index = face->vertex_indices[j];
					face->normals[j * 3 + 0] = normals[index * 3 + 0];
					face->normals[j * 3 + 1] = normals[index * 3 + 1];
					face->normals[j * 3 + 2] = normals[index * 3 + 2];
				}
				face->material = g_slist_nth_data(object->materials, 0);

				if(face->material->tex_image != NULL) {
					face->tex_vertex_count = 3;
					face->tex_vertex_data = g_new0(G3DFloat, 3 * 2);
					face->tex_image = face->material->tex_image;
					for(j = 0; j < 3; j ++) {
						index = face->vertex_indices[j];

						face->tex_vertex_data[j * 2 + 0] =
							texcoords[index * 2 + 0];
						face->tex_vertex_data[j * 2 + 1] =
							texcoords[index * 2 + 1];
						face->flags |= G3D_FLAG_FAC_TEXMAP;
					}
				}

				object->faces = g_slist_append(object->faces, face);
			}
		}

		if(normals != NULL)
			g_free(normals);
		if(texcoords != NULL)
			g_free(texcoords);
	}

	return pobject;
}