]> git.mxchange.org Git - flightgear.git/blob - utils/Modeller/normalmap.cxx
deb341eb628fc23e77d9a547c44dfa84e0c69b15
[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 GLubyte *make_map( GLubyte *data, int width, int height, int colors )
63 {
64    GLubyte *map = (GLubyte *)malloc (width * height * 3);
65    
66    for (int y=0; y<height; y++)
67       for (int x=0; x<width; x++)
68       {
69          int mpos = (x + y*width)*3;
70          int dpos = (x + y*width)*colors;
71
72          int xp1 = (x < (width-1)) ? x+1 : 0;
73          int yp1 = (y < (height-1)) ? y+1 : 0;
74          int posxp1 = (xp1 + y*width)*colors;
75          int posyp1 = (x + yp1*width)*colors;
76          
77          map[mpos+0] = (128+(data[posxp1]-data[dpos])/2);
78          map[mpos+1] = (128+(data[posyp1]-data[dpos])/2);
79          map[mpos+2] = 128 + GLubyte(128*brightness);
80       }
81
82    return map;
83 }
84
85 int main (int argc, char **argv)
86 {
87    int i;
88
89    for (i=1; i<argc;)
90       i += parse_option(argv, i);
91
92    if ( !texture_file )
93    {
94       printf("Error: texture file not specified\n");
95       return -1;
96    }
97
98    texture.read_rgb_texture(texture_file);
99    if ( !texture.texture() )
100    {
101       printf("Error: unable to process input file: %s\n", texture_file);
102       printf("       (%s)\n", texture.err_str());
103       return -2;
104    }
105
106    if ( !normalmap_file )
107    {
108       int i;
109       for (i=strlen(texture_file); i>=0; --i)
110          if (texture_file[i] == '.')
111             break;
112
113       normalmap_file = (char *)malloc( i+8 );
114       memcpy(normalmap_file, texture_file, i);
115       memcpy(normalmap_file+i, "_n.rgb\0", 7);
116    }
117
118    // texture.make_grayscale();
119    texture.make_normalmap();
120    texture.write_texture(normalmap_file);
121
122    free( normalmap_file );
123    free( texture_file );
124
125    return 0;
126 }