diff options
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_heightfield/imp_heightfield.c')
-rw-r--r-- | src/others/mimesh/libg3d-0.0.8/plugins/import/imp_heightfield/imp_heightfield.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_heightfield/imp_heightfield.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_heightfield/imp_heightfield.c new file mode 100644 index 0000000..a8251a1 --- /dev/null +++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_heightfield/imp_heightfield.c | |||
@@ -0,0 +1,115 @@ | |||
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 | |||
23 | #include <stdlib.h> | ||
24 | #include <string.h> | ||
25 | |||
26 | #include <g3d/types.h> | ||
27 | #include <g3d/context.h> | ||
28 | #include <g3d/plugins.h> | ||
29 | #include <g3d/material.h> | ||
30 | #include <g3d/primitive.h> | ||
31 | |||
32 | EAPI | ||
33 | gboolean plugin_load_model_from_stream(G3DContext *context, G3DStream *stream, | ||
34 | G3DModel *model, gpointer user_data) | ||
35 | { | ||
36 | G3DImage *image = g_new0(G3DImage, 1); | ||
37 | G3DObject *object; | ||
38 | G3DMaterial *material; | ||
39 | guint32 x, y, index; | ||
40 | G3DFloat pcnt, prev_pcnt = 0.0; | ||
41 | |||
42 | if(!g3d_plugins_load_image_from_stream(context, stream, image)) { | ||
43 | g_free(image); | ||
44 | return FALSE; | ||
45 | } | ||
46 | |||
47 | material = g3d_material_new(); | ||
48 | material->name = g_strdup("default material"); | ||
49 | material->r = 0.4; | ||
50 | material->g = 0.4; | ||
51 | material->b = 0.4; | ||
52 | material->a = 1.0; | ||
53 | model->materials = g_slist_append(model->materials, material); | ||
54 | |||
55 | object = g3d_primitive_mesh(image->width, image->height, FALSE, FALSE, | ||
56 | material); | ||
57 | object->name = g_strdup("height field"); | ||
58 | model->objects = g_slist_append(model->objects, object); | ||
59 | |||
60 | #if DEBUG > 0 | ||
61 | g_debug("height field loader: image: %dx%dx%d", | ||
62 | image->width, image->height, image->depth); | ||
63 | #endif | ||
64 | |||
65 | for(y = 0; y < image->height; y ++) { | ||
66 | for(x = 0; x < image->width; x ++) { | ||
67 | index = y * image->width + x; | ||
68 | |||
69 | object->vertex_data[index * 3 + 0] = x; | ||
70 | object->vertex_data[index * 3 + 1] = y; | ||
71 | switch(image->depth) { | ||
72 | case 8: | ||
73 | object->vertex_data[index * 3 + 2] = 0.0 + | ||
74 | (G3DFloat)image->pixeldata[index] / 32.0; | ||
75 | break; | ||
76 | case 15: | ||
77 | case 16: | ||
78 | object->vertex_data[index*3+2] = 0.0 + | ||
79 | *((guint16*)&image->pixeldata[index]); | ||
80 | break; | ||
81 | case 24: | ||
82 | case 32: | ||
83 | object->vertex_data[index * 3 + 2] = 0.0 + | ||
84 | image->pixeldata[index * 4] / 32.0; | ||
85 | break; | ||
86 | default: | ||
87 | break; | ||
88 | } | ||
89 | |||
90 | pcnt = (G3DFloat)(y * image->width + x) / | ||
91 | (G3DFloat)(image->width * image->height); | ||
92 | if((pcnt - prev_pcnt) > 0.01) { | ||
93 | prev_pcnt = pcnt; | ||
94 | g3d_context_update_progress_bar(context, pcnt, TRUE); | ||
95 | } | ||
96 | g3d_context_update_interface(context); | ||
97 | } /* for(x) */ | ||
98 | } /* for(y) */ | ||
99 | |||
100 | return TRUE; | ||
101 | } | ||
102 | |||
103 | EAPI | ||
104 | gchar *plugin_description(G3DContext *context) | ||
105 | { | ||
106 | return g_strdup("Generate height fields from images.\n" | ||
107 | "Author: Markus Dahms"); | ||
108 | } | ||
109 | |||
110 | EAPI | ||
111 | gchar **plugin_extensions(G3DContext *context) | ||
112 | { | ||
113 | return g3d_plugins_get_image_extensions(context); | ||
114 | } | ||
115 | |||