]> git.mxchange.org Git - flightgear.git/blob - utils/Modeller/normalmap.cxx
80d1fe4d6fdb44c460c21ee93669b62f97239fa9
[flightgear.git] / utils / Modeller / normalmap.cxx
1 /*
2  * Create normal map textures from regular textures.
3  * Created by: Erik Hofman
4  *
5  * This file is in public domain.
6  */
7
8 #include <string.h>
9 #include <GL/gl.h>
10 #include <simgear/screen/texture.hxx>
11
12
13 static float contrast = 1.0;
14 static float brightness = 1.0;
15 static char *texture_file = NULL, *normalmap_file = NULL;
16 static SGTexture texture;
17
18 int parse_option(char **args, int n) {
19    char *opt, *arg;
20    int sz, ret=1;
21
22    opt = args[n];
23    if (*(opt+1) == '-')
24       opt++;
25
26    if ((arg = strchr(opt, '=')) != NULL)
27       *arg++ = 0;
28
29    else {
30       ret++;
31       arg = args[n+1];
32    }
33
34    sz = strlen(opt);
35    if (!strncmp(opt, "-help", sz)) {
36       printf("usage:\n  normalmap [-c=contrast] [-b=brightness]");
37       printf(" --t=file [--o=file]\n");
38       exit(0);
39    }
40    if (!strncmp(opt, "-contrast", sz)) {
41       contrast = atof(arg);
42       return ret;
43    }
44    if (!strncmp(opt, "-brightness", sz)) {
45       brightness = atof(arg);
46       return ret;
47    }
48    if (!strncmp(opt, "-texture", sz) ||
49        !strncmp(opt, "-input", sz)) {
50       texture_file = strdup(arg);
51       return ret;
52    }
53    if (!strncmp(opt, "-normalmap", sz) ||
54        !strncmp(opt, "-output", sz)) {
55       normalmap_file = strdup(arg);
56       return ret;
57    }
58
59    return 1;
60 }
61
62 int main (int argc, char **argv)
63 {
64    int i;
65
66    for (i=1; i<argc;)
67       i += parse_option(argv, i);
68
69    if ( !texture_file )
70    {
71       printf("Error: texture file not specified\n");
72       return -1;
73    }
74
75    texture.read_rgb_texture(texture_file);
76    if ( !texture.texture() )
77    {
78       printf("Error: unable to process input file: %s\n", texture_file);
79       printf("       (%s)\n", texture.err_str());
80       return -2;
81    }
82
83    if ( !normalmap_file )
84    {
85       int i;
86       for (i=strlen(texture_file); i>=0; --i)
87          if (texture_file[i] == '.')
88             break;
89
90       normalmap_file = (char *)malloc( i+8 );
91       memcpy(normalmap_file, texture_file, i);
92       memcpy(normalmap_file+i, "_n.rgb\0", 7);
93    }
94
95    texture.make_normalmap();
96    texture.write_texture(normalmap_file);
97
98    free( normalmap_file );
99    free( texture_file );
100
101    return 0;
102 }