/* * imp_heightfield.c: plugin to generated height fields from images * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * * (C) 2002 Markus Dahms */ #include #include #include #include int plugin_load(const char *filename, G3DModel *model) { G3DImage *image = g_new0(G3DImage, 1); G3DObject *object; G3DMaterial *material; unsigned int x, y, index; if(g3d_plugins_load_image(filename, image) != EXIT_SUCCESS) { g_free(image); return EXIT_FAILURE; } object = g_new0(G3DObject, 1); object->name = g_strdup("height field"); model->objects = g_slist_append(model->objects, object); material = g3d_new_G3DMaterial(); material->name = g_strdup("default material"); material->r = 0.2; material->g = 1.0; material->b = 0.1; material->a = 1.0; object->materials = g_slist_append(object->materials, material); object->vertex_count = image->width * image->height; object->vertices = g_malloc0(object->vertex_count * 3 * sizeof(GLfloat)); #if DEBUG > 0 g_printerr("height field loader: image: %dx%dx%d\n", image->width, image->height, image->depth); #endif for(y = 0; y < image->height; y ++) { #if DEBUG > 5 g_print("Heightfield: line: "); #endif for(x=0; xwidth; x++) { index = y*image->width + x; object->vertices[index*3+0] = x; object->vertices[index*3+1] = y; switch(image->depth) { case 8: object->vertices[index*3+2] = 0.0 + (float)image->pixeldata[index] / 32.0; break; case 15: case 16: object->vertices[index*3+2] = 0.0 + *((short int*)&image->pixeldata[index]); break; case 24: case 32: object->vertices[index * 3 + 2] = 0.0 + image->pixeldata[index * 4] / 32.0; break; default: break; } #if DEBUG > 5 i g_print(" (%.1f,%.1f,%.1f)", object->vertices[index*3+0], object->vertices[index*3+1], object->vertices[index*3+2]); #endif if((x < (image->width-1)) && (y < (image->height-1))) { G3DFace *face = g_new0(G3DFace, 1); face->material = material; face->num_vertices = 3; face->index_vertex = g_malloc(3 * sizeof(int)); face->index_vertex[0] = index; face->index_vertex[1] = index + 1; face->index_vertex[2] = index + image->width; object->faces = g_slist_prepend(object->faces, face); face = g_new0(G3DFace, 1); face->material = material; face->num_vertices = 3; face->index_vertex = g_malloc(3 * sizeof(int)); face->index_vertex[0] = index + 1; face->index_vertex[1] = index + image->width + 1; face->index_vertex[2] = index + image->width; object->faces = g_slist_prepend(object->faces, face); } } /* for(x) */ #if DEBUG > 4 g_printerr("loader: line %d ready\n", y+1); #endif #if DEBUG > 5 g_print("\n"); #endif } /* for(y) */ return EXIT_SUCCESS; } char *plugin_description(void) { return g_strdup( "plugin to generate height fields from images\n" "Author: Markus Dahms\n"); } char **plugin_extensions(void) { return g3d_plugins_get_image_extensions(); }