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