00001
00002
00003
00004
00005
00006
00007 #include "geom/image.H"
00008 #include "std/config.H"
00009 #include "mesh/mi.H"
00010
00011 static bool debug = Config::get_var_bool("DEBUG_PNG2SM",false,true);
00012 static bool make_box = Config::get_var_bool("PNG2SM_MAKE_BOX",false,true);
00013
00014 static double yscale = Config::get_var_dbl("PNG_YSCALE",0.16,true);
00015
00016 int
00017 main(int argc, char *argv[])
00018 {
00019 if (argc != 2) {
00020 err_msg("Usage: %s heightmap.png > mesh.sm", argv[0]);
00021 return 1;
00022 }
00023
00024 Image img;
00025 if (!img.read_png(argv[1])) {
00026 err_msg("png2sm: can't read PNG file from stdin");
00027 return 1;
00028 }
00029
00030 err_adv(debug, "read %dx%d png", img.width(), img.height());
00031
00032 BMESHptr mesh = new LMESH;
00033
00034 uint x, z;
00035
00036 ARRAY<Bvert_list> top_verts(img.height());
00037
00038
00039 for (z=0; z<img.height(); z++) {
00040 top_verts += Bvert_list();
00041 for (x=0; x<img.width(); x++) {
00042 top_verts.last() += mesh->add_vertex(Wpt(x,img.pixel(x,z),z));
00043 }
00044 }
00045
00046
00047 for (z=1; z<img.height(); z++) {
00048 for (x=1; x<img.width(); x++) {
00049 mesh->add_quad(top_verts[z][x-1], top_verts[z][x],
00050 top_verts[z-1][x], top_verts[z-1][x-1]);
00051 }
00052 }
00053
00054 if (make_box) {
00055 ARRAY<Bvert_list> bot_verts(img.height());
00056
00057
00058 for (z=0; z<img.height(); z++) {
00059 bot_verts += Bvert_list();
00060 for (x=0; x<img.width(); x++) {
00061 bot_verts.last() += mesh->add_vertex(Wpt(x,-1,z));
00062 }
00063 }
00064
00065 for (z=1; z<img.height(); z++) {
00066 for (x=1; x<img.width(); x++) {
00067 mesh->add_quad(bot_verts[z][x-1], bot_verts[z-1][x-1],
00068 bot_verts[z-1][x], bot_verts[z][x]);
00069 }
00070 }
00071
00072
00073 for (x=1; x<img.width(); x++) {
00074 mesh->add_quad(top_verts[0][x-1], top_verts[0][x],
00075 bot_verts[0][x], bot_verts[0][x-1]);
00076 }
00077 uint n = img.height()-1;
00078 for (x=1; x<img.width(); x++) {
00079 mesh->add_quad(bot_verts[n][x-1], bot_verts[n][x],
00080 top_verts[n][x], top_verts[n][x-1]);
00081 }
00082
00083 n = img.width()-1;
00084 for (z=1; z<img.height(); z++) {
00085 mesh->add_quad(top_verts[z-1][n], top_verts[z][n],
00086 bot_verts[z][n], bot_verts[z-1][n]);
00087 }
00088 for (z=1; z<img.height(); z++) {
00089 mesh->add_quad(bot_verts[z-1][0], bot_verts[z][0],
00090 top_verts[z][0], top_verts[z-1][0]);
00091 }
00092 }
00093
00094
00095 mesh->transform(
00096 Wtransf::translation(Wvec(-0.5*img.width(), 0.0, -0.5*img.height()))
00097 );
00098
00099
00100 mesh->transform(Wtransf::scaling(1, yscale, 1));
00101
00102
00103
00104 mesh->transform(Wtransf::scaling(0.5));
00105
00106 if (debug || Config::get_var_bool("JOT_PRINT_MESH"))
00107 mesh->print();
00108
00109 mesh->write_stream(cout);
00110
00111 return 0;
00112 }