]> git.mxchange.org Git - flightgear.git/blob - utils/Modeller/normalmap.cxx
Bugfixes and some finetuning:
[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 "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, "-contrast", sz)) {
39       contrast = atof(arg);
40       return ret;
41    }
42    if (!strncmp(opt, "-brightness", sz)) {
43       brightness = atof(arg);
44       return ret;
45    }
46    if (!strncmp(opt, "-texture", sz) ||
47        !strncmp(opt, "-input", sz)) {
48       texture_file = strdup(arg);
49       return ret;
50    }
51    if (!strncmp(opt, "-normalmap", sz) ||
52        !strncmp(opt, "-output", sz)) {
53       normalmap_file = strdup(arg);
54       return ret;
55    }
56    if (!strncmp(opt, "-help", sz)) {
57       printf("usage:\n  normalmap [-c=contrast] [-b=brightness]");
58       printf(" --i=file [--o=file]\n\n");
59       exit(0);
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       printf("usage:\n  normalmap [-c=contrast] [-b=brightness]");
76       printf(" --i=file [--o=file]\n\n");
77       return -1;
78    }
79
80    texture.read_rgb_texture(texture_file);
81    if ( !texture.texture() )
82    {
83       printf("Error: unable to process input file: %s\n", texture_file);
84       printf("       (%s)\n\n", texture.err_str());
85       return -2;
86    }
87
88    if ( !normalmap_file )
89    {
90       int i;
91       for (i=strlen(texture_file); i>=0; --i)
92          if (texture_file[i] == '.')
93             break;
94
95       normalmap_file = (char *)malloc( i+8 );
96       memcpy(normalmap_file, texture_file, i);
97       memcpy(normalmap_file+i, "_n.rgb\0", 7);
98    }
99
100    texture.make_normalmap(brightness, contrast);
101    texture.write_texture(normalmap_file);
102
103    free( normalmap_file );
104    free( texture_file );
105
106    return 0;
107 }