2 * Create normal map textures from regular textures.
3 * Created by: Erik Hofman
5 * This file is in public domain.
10 #include <simgear/screen/texture.hxx>
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;
18 int parse_option(char **args, int n) {
26 if ((arg = strchr(opt, '=')) != NULL)
35 if (!strncmp(opt, "-help", sz)) {
36 printf("usage:\n normalmap [-c=contrast] [-b=brightness]");
37 printf(" --t=file [--o=file]\n");
40 if (!strncmp(opt, "-contrast", sz)) {
44 if (!strncmp(opt, "-brightness", sz)) {
45 brightness = atof(arg);
48 if (!strncmp(opt, "-texture", sz) ||
49 !strncmp(opt, "-input", sz)) {
50 texture_file = strdup(arg);
53 if (!strncmp(opt, "-normalmap", sz) ||
54 !strncmp(opt, "-output", sz)) {
55 normalmap_file = strdup(arg);
62 GLubyte *make_map( GLubyte *data, int width, int height, int colors )
64 GLubyte *map = (GLubyte *)malloc (width * height * 3);
66 for (int y=0; y<height; y++)
67 for (int x=0; x<width; x++)
69 int mpos = (x + y*width)*3;
70 int dpos = (x + y*width)*colors;
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;
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);
85 int main (int argc, char **argv)
90 i += parse_option(argv, i);
94 printf("Error: texture file not specified\n");
98 texture.read_rgb_texture(texture_file);
99 if ( !texture.texture() )
101 printf("Error: unable to process input file: %s\n", texture_file);
102 printf(" (%s)\n", texture.err_str());
106 if ( !normalmap_file )
109 for (i=strlen(texture_file); i>=0; --i)
110 if (texture_file[i] == '.')
113 normalmap_file = (char *)malloc( i+8 );
114 memcpy(normalmap_file, texture_file, i);
115 memcpy(normalmap_file+i, "_n.rgb\0", 7);
118 // texture.make_grayscale();
119 texture.make_normalmap();
120 texture.write_texture(normalmap_file);
122 free( normalmap_file );
123 free( texture_file );