summaryrefslogtreecommitdiff
path: root/utils/height2ssbump/height2ssbump.cpp
blob: 4e580a3dd2927bdc3d1a3da6e77659bb9c915d16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//===========================================================================//

#include "tier0/platform.h"
#include "tier0/progressbar.h"
#include "bitmap/float_bm.h"
#include "mathlib/mathlib.h"
#include "tier2/tier2.h"
#include "tier0/memdbgon.h"

static void PrintArgSummaryAndExit( void )
{
	printf( "format is 'height2ssbump filename.tga bumpscale'\n" );
	printf( "options:\n-r NUM\tSet the number of rays (default = 250. more rays take more time).\n");
	printf( "-n\tGenerate a conventional normal map\n" );
	printf( "-A\tGenerate ambient occlusion in the alpha channel\n" );
	printf( "-f NUM\tSet smoothing filter radius (0=no filter, default = 10)\n");
	printf( "-D\tWrite out the filtered result as filterd.tga\n");
	exit(1);
}

void main(int argc,char **argv)
{
	InitCommandLineProgram( argc, argv );
	int nCurArg = 1;

	bool bNormalOnly = false;
	float flFilterRadius = 10.0;
	bool bWriteFiltered = false;

	uint32 nSSBumpFlags = 0;

	int nNumRays = 250;

	while ( ( nCurArg < argc ) && ( argv[nCurArg][0] == '-' ) )
	{
		switch( argv[nCurArg][1] )
		{
			case 'n':										// lighting from normal only
			{
				bNormalOnly = true;
			}
			break;

			case 'r':										// set # of rays
			{
				nNumRays = atoi( argv[++nCurArg] );
			}
			break;

			case 'f':										// filter radius
			{
				flFilterRadius = atof( argv[++nCurArg] );
			}
			break;

			case 'd':										// detail texture
			{
				nSSBumpFlags |= SSBUMP_MOD2X_DETAIL_TEXTURE;
			}
			break;

			case 'A':										// ambeint occlusion
			{
				nSSBumpFlags |= SSBUMP_OPTION_NONDIRECTIONAL;
			}
			break;

			case 'D':										// debug - write filtered output
			{
				bWriteFiltered = true;
			}
			break;

			case '?':										// args
			{
				PrintArgSummaryAndExit();
			}
			break;

			default:
				printf("unrecogized option %s\n", argv[nCurArg] );
				PrintArgSummaryAndExit();
		}
		nCurArg++;
		
	}
	argc -= ( nCurArg - 1 );
	argv += ( nCurArg - 1 );

	if ( argc != 3 )
	{
		PrintArgSummaryAndExit();
	}
	ReportProgress( "reading src texture", 0, 0);
	FloatBitMap_t src_texture( argv[1] );
	src_texture.TileableBilateralFilter( flFilterRadius, 2.0 / 255.0 );
	if ( bWriteFiltered )
		src_texture.WriteTGAFile( "filtered.tga" );

	FloatBitMap_t * out;

	if ( bNormalOnly )
		out = src_texture.ComputeBumpmapFromHeightInAlphaChannel( atof( argv[2] ) );
	else
		out = src_texture.ComputeSelfShadowedBumpmapFromHeightInAlphaChannel( 
			atof( argv[2] ), nNumRays, nSSBumpFlags );

	char oname[1024];
	strcpy( oname, argv[1] );
	char * dot = strchr( oname, '.' );
	if ( ! dot )
		dot = oname + strlen( oname );
	if ( bNormalOnly )
		strcpy( dot, "-bump.tga" );
	else
		strcpy( dot, "-ssbump.tga" );
	out->WriteTGAFile( oname );
	delete out;
}