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.

87 lines
3.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. #include "tier0/platform.h"
  3. #include "tier0/progressbar.h"
  4. #include "bitmap/float_bm.h"
  5. #include "mathlib/mathlib.h"
  6. #include "tier2/tier2.h"
  7. #include "tier0/memdbgon.h"
  8. #include "raytrace.h"
  9. #include "bitmap/tgawriter.h"
  10. void main(int argc,char **argv)
  11. {
  12. InitCommandLineProgram( argc, argv );
  13. if (argc != 5)
  14. {
  15. printf("format is 'rt_test src_image dest_image xsize ysize'\n");
  16. }
  17. else
  18. {
  19. ReportProgress("reading src texture",0,0);
  20. FloatBitMap_t src_texture(argv[1]);
  21. int xsize = atoi( argv[3] );
  22. int ysize = atoi( argv[4] );
  23. // render a simple scene of a terrain, using a bitmap for color data and its alpha channel for the height
  24. RayTracingEnvironment rt_Env;
  25. int id = 1;
  26. float flXScale = (1.0/(src_texture.Width-1) );
  27. float flZScale = (1.0/(src_texture.Height-1) );
  28. for( int y=0; y < src_texture.Height-1; y++ )
  29. for(int x=0 ; x < src_texture.Width-1; x++ )
  30. {
  31. Vector vecVerts[2][2];
  32. for(int iy=0 ; iy < 2; iy++)
  33. for(int ix=0 ; ix < 2; ix++)
  34. {
  35. vecVerts[ix][iy].x = 2.0* ( ( x+ix )*flXScale-0.5 );
  36. if ( ( x+ix == src_texture.Width-1 ) || ( y+iy==src_texture.Height-1 ) )
  37. vecVerts[ix][iy].y = 0;
  38. else
  39. vecVerts[ix][iy].y = 0.3*src_texture.Pixel( x+ix, y+iy, 1 );
  40. vecVerts[ix][iy].z = -2.0* ( ( y+iy )*flZScale-0.5 );
  41. }
  42. Vector vecColor( GammaToLinear(src_texture.Pixel(x,y,0)),
  43. GammaToLinear( src_texture.Pixel( x, y, 1 )),
  44. GammaToLinear( src_texture.Pixel( x, y, 2 )) );
  45. rt_Env.AddTriangle( id++, vecVerts[0][0], vecVerts[1][0], vecVerts[1][1], vecColor );
  46. rt_Env.AddTriangle( id++, vecVerts[0][0], vecVerts[0][1], vecVerts[1][1], vecColor );
  47. }
  48. rt_Env.AddTriangle( id++, Vector(0,0,-.2), Vector(.2,0,.2), Vector( -.2,0,.2), Vector( 0,0,1 ) );
  49. printf("n triangles %d\n",id);
  50. ReportProgress("Creating kd-tree",0,0);
  51. float stime = Plat_FloatTime();
  52. rt_Env.SetupAccelerationStructure();
  53. printf("kd built time := %d\n", (int) ( Plat_FloatTime() - stime ) );
  54. rt_Env.AddInfinitePointLight( Vector( 0,5, 0), Vector( .1,.1,.1 ));
  55. // lets render a frame
  56. uint32 *buf=reinterpret_cast<uint32 *> ( MemAlloc_AllocAligned( xsize * ysize * 4 , 16 ) );
  57. Vector EyePos(0,2,0);
  58. ReportProgress("Rendering",0,0);
  59. // rt_Env.RenderScene( xsize, ysize, xsize, buf, Vector( 0, 0.5, -1.0 ),
  60. // Vector( -1, 1, 0),
  61. // Vector( 1, 1, 0 ),
  62. // Vector( -1, -1, 0 ),
  63. // Vector( 1, -1, 0 ) );
  64. float curtime = Plat_FloatTime();
  65. for(int i=0;i<10;i++)
  66. {
  67. rt_Env.RenderScene( xsize, ysize, xsize, buf,
  68. EyePos,
  69. Vector( -1, 0,1)-EyePos,
  70. Vector( 1, 0, 1 )-EyePos,
  71. Vector( -1, 0, -1 )-EyePos,
  72. Vector( 1, 0, -1 )-EyePos );
  73. }
  74. float etime=Plat_FloatTime()-curtime;
  75. printf("pixels traced and lit per second := %f\n",(10*xsize*ysize)*(1.0/etime));
  76. TGAWriter::WriteTGAFile( "test.tga", xsize, ysize, IMAGE_FORMAT_RGBA8888,
  77. reinterpret_cast<uint8 *> (buf), 4*xsize );
  78. MemAlloc_FreeAligned( buf );
  79. }
  80. }