Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include "tier0/platform.h"
  7. #include "tier0/progressbar.h"
  8. #include "bitmap/float_bm.h"
  9. #include "mathlib/mathlib.h"
  10. #include "tier1/strtools.h"
  11. #include "tier2/tier2.h"
  12. #include "tier0/memdbgon.h"
  13. static float RangeAdjust( float x )
  14. {
  15. return (2*(x-.5));
  16. }
  17. static float saturate_and_square( float x )
  18. {
  19. x=max(0.f,min(1.f, x) );
  20. return x * x;
  21. }
  22. #define OO_SQRT_3 0.57735025882720947f
  23. static Vector bumpBasis[3] = {
  24. Vector( 0.81649661064147949f, 0.0f, OO_SQRT_3 ),
  25. Vector( -0.40824833512306213f, 0.70710676908493042f, OO_SQRT_3 ),
  26. Vector( -0.40824821591377258f, -0.7071068286895752f, OO_SQRT_3 )
  27. };
  28. void main(int argc,char **argv)
  29. {
  30. InitCommandLineProgram( argc, argv );
  31. if (argc != 2)
  32. {
  33. printf("format is 'normal2ssbump filename.tga\n");
  34. }
  35. else
  36. {
  37. ReportProgress( "reading src texture",0,0 );
  38. FloatBitMap_t src_texture(argv[1]);
  39. for(int y=0;y<src_texture.Height;y++)
  40. {
  41. ReportProgress( "Converting to ssbump format",src_texture.Height,y );
  42. for(int x=0;x<src_texture.Width;x++)
  43. {
  44. Vector n( RangeAdjust( src_texture.Pixel( x,y,0 ) ),
  45. RangeAdjust( src_texture.Pixel( x,y,1 ) ),
  46. RangeAdjust( src_texture.Pixel( x,y,2 ) ) );
  47. Vector dp( saturate_and_square( DotProduct( n, bumpBasis[0] ) ),
  48. saturate_and_square( DotProduct( n, bumpBasis[1] ) ),
  49. saturate_and_square( DotProduct( n, bumpBasis[2] ) ) );
  50. float sum = DotProduct(dp, Vector(1,1,1));
  51. dp *= 1.0/sum;
  52. src_texture.Pixel(x,y,0) = dp.x;
  53. src_texture.Pixel(x,y,1) = dp.y;
  54. src_texture.Pixel(x,y,2) = dp.z;
  55. }
  56. }
  57. char oname[1024];
  58. strcpy(oname,argv[1]);
  59. char *dot=Q_stristr(oname,"_normal");
  60. if (! dot)
  61. dot=strchr(oname,'.');
  62. if (! dot)
  63. dot=oname+strlen(oname);
  64. strcpy(dot,"_ssbump.tga");
  65. printf( "\nWriting %s\n",oname );
  66. src_texture.WriteTGAFile( oname );
  67. }
  68. }