home *** CD-ROM | disk | FTP | other *** search
/ WarCraft 2000 - Nuclear Epidemic / W2000.nrg / SOURCE.War2000 / 3DSurf.cpp next >
C/C++ Source or Header  |  1998-09-25  |  6KB  |  247 lines

  1. //3D surface rendering
  2. #include "ddini.h"
  3. #include "ResFile.h"
  4. #include "FastDraw.h"
  5. #include "mgraph.h"
  6. #include "mouse.h"
  7. #include "menu.h"
  8. #include "MapDiscr.h"
  9. #include "multipl.h"
  10. #include "fog.h"
  11. #include "walls.h"
  12. #include "Nature.h"
  13. #include <time.h>
  14. #include "Nucl.h"
  15. #include "TopZones.h"
  16. #include "Megapolis.h"
  17. #include "dialogs.h"
  18. #include <assert.h>
  19. #include <math.h>
  20. #define MediaMax 8
  21. #define CompoMax 8
  22. #define RepTileMax 4
  23. #define MaxStackSize 32768
  24. extern byte tiles[32][8192];
  25. static int StackSize=8192;
  26. short HiMap[256][256];//Height of surface
  27. word RefMap[256][256];//Reference to cell being rendered
  28. byte RXCoor[MaxStackSize];
  29. byte RYCoor[MaxStackSize];
  30. static int Theta=20;
  31. static int Phi=100;
  32. int STHead;
  33. int STTile;
  34. static char* RendSurf=NULL;
  35. void SetupRenderStack(){
  36.     RendSurf=new char [StackSize<<10];
  37. };
  38. void InitRenderStack(){
  39.     memset(RXCoor,0,sizeof RXCoor);
  40.     memset(RYCoor,0,sizeof RYCoor);
  41.     memset(&RefMap[0][0],255,sizeof RefMap);
  42.     STHead=0;
  43.     STTile=0;
  44. };
  45. void ReRenderMap(){
  46.     memset(&RefMap[0][0],255,sizeof RefMap);
  47.     STHead=0;
  48.     STTile=0;
  49. };
  50. void SetStackSize(int STSize){
  51.     StackSize=STSize;
  52. };
  53. short GetHeight(int x,int y){
  54.     int xx=x;
  55.     int yy=y;
  56.     if(xx<0)xx=0;
  57.     if(yy<0)yy=0;
  58.     if(xx>msx)xx=msx;
  59.     if(yy>msy)yy=msy;
  60.     return HiMap[y][x];
  61. };
  62. static int LightDX;
  63. static int LightDY;
  64. static int LightDZ;
  65. void SetLight(int Ldx,int Ldy,int Ldz){
  66.     int Ab=sqrt(Ldx*Ldx+Ldy*Ldy+Ldz*Ldz);
  67.     LightDX=div(Ldx<<8,Ab).quot;
  68.     LightDY=div(Ldy<<8,Ab).quot;
  69.     LightDZ=div(Ldz<<8,Ab).quot;
  70.     memset(RXCoor,0,sizeof RXCoor);
  71.     memset(&RefMap[0][0],255,sizeof RefMap);
  72. };
  73. void SetAngLight(int Theta,int Phi){
  74.     double teta=Theta*3.1415/180;
  75.     double phi=Phi*3.1415/180;
  76.     SetLight(100*sin(teta)*cos(phi),100*sin(teta)*cos(phi),100*cos(teta));
  77. };
  78. void RotateTeta(){
  79.     Theta+=2;
  80.     SetAngLight(Theta,Phi);
  81. };
  82. void RotateTetaI(){
  83.     Theta-=2;
  84.     SetAngLight(Theta,Phi);
  85. };
  86. void RotatePhi(){
  87.     Phi+=2;
  88.     SetAngLight(Theta,Phi);
  89. };
  90. void RotatePhiI(){
  91.     Phi-=2;
  92.     SetAngLight(Theta,Phi);
  93. };
  94. int GetLight(int x,int y){
  95.     int dy=GetHeight(x+1,y)-GetHeight(x-1,y);
  96.     int dx=GetHeight(x,y+1)-GetHeight(x,y-1);
  97.     int lig=16+(div(dx*LightDX+dy*LightDY+64*LightDZ,sqrt(dx*dx+dy*dy+64*64)).quot>>4); 
  98.     if(lig<2)lig=2;
  99.     if(lig>31)lig=31;
  100.     return 32-lig;
  101. };
  102. void ShowSuperFluentFog32(int x,int y,int z1x,int z2x,int z3x,int z4x);
  103. void ShowSuperFluentFog(int x,int y,int z1x,int z2x,int z3x,int z4x);
  104. void ShowSuperFluentFog2_2(int x,int y,int z1x,int z2x,int z3x,int z4x);
  105. void ShowSuperFluentFog2_2_16(int x,int y,int z1x,int z2x,int z3x,int z4x);
  106. char* RenderSquare(int x,int y){
  107.     word rn=RefMap[y][x];
  108.     if(rn==0xFFFF){
  109.         COUNTER++;
  110.         //Need Rendering;
  111.         if(RXCoor[STHead]){
  112.             int xx=RXCoor[STHead];
  113.             int yy=RYCoor[STHead];
  114.             RefMap[yy][xx]=0xFFFF;
  115.         };
  116.         int rPos=int(RendSurf+(STHead<<10));
  117.         int xScrPtr=int(ScreenPtr);
  118.         int SSX=SCRSizeX;
  119.         ScreenPtr=(void*)rPos;
  120.         SCRSizeX=32;
  121.         int tiptr=int(tiles)+(int(tmap[x-1][y-1])<<10);
  122.         __asm{
  123.             push    esi
  124.             push    edi
  125.             cld
  126.             mov        ecx,256
  127.             mov        esi,tiptr
  128.             mov        edi,rPos
  129.             rep        movsd
  130.             pop        edi
  131.             pop        esi
  132.         };
  133.         //ShowSuperFluentFog32(0,0,GetLight(x-1,y-1),
  134.         //                         GetLight(x,y-1),
  135.         //                         GetLight(x-1,y),
  136.         //                         GetLight(x,y));
  137.         ShowSuperFluentFog2_2(0,0,GetLight(x-1,y-1),
  138.                                  GetLight(x,y-1),
  139.                                  GetLight(x-1,y),
  140.                                  GetLight(x,y));
  141.         ScreenPtr=(void*)(xScrPtr);
  142.         SCRSizeX=SSX;
  143.         RXCoor[STHead]=x;
  144.         RYCoor[STHead]=y;
  145.         RefMap[y][x]=STHead;
  146.         STHead++;
  147.         if(STHead>=StackSize)
  148.             STHead=0;
  149.         return (char*)rPos;
  150.     }else return RendSurf+(int(rn)<<10);
  151. };
  152. extern byte mtiles[16][4096];
  153. char* RenderMiniSquare(int x,int y){
  154.     word rn=RefMap[y][x];
  155.     if(rn==0xFFFF){
  156.         //COUNTER++;
  157.         //Need Rendering;
  158.         if(RXCoor[STHead]){
  159.             int xx=RXCoor[STHead];
  160.             int yy=RYCoor[STHead];
  161.             RefMap[yy][xx]=0xFFFF;
  162.         };
  163.         int rPos=int(RendSurf+(STHead<<8));
  164.         int xScrPtr=int(ScreenPtr);
  165.         int SSX=SCRSizeX;
  166.         ScreenPtr=(void*)rPos;
  167.         SCRSizeX=16;
  168.         int tiptr=int(mtiles)+(int(tmap[x-1][y-1])<<8);
  169.         __asm{
  170.             push    esi
  171.             push    edi
  172.             cld
  173.             mov        ecx,64
  174.             mov        esi,tiptr
  175.             mov        edi,rPos
  176.             rep        movsd
  177.             pop        edi
  178.             pop        esi
  179.         };
  180.         //ShowSuperFluentFog32(0,0,GetLight(x-1,y-1),
  181.         //                         GetLight(x,y-1),
  182.         //                         GetLight(x-1,y),
  183.         //                         GetLight(x,y));
  184.         ShowSuperFluentFog2_2_16(0,0,GetLight(x-1,y-1),
  185.                                  GetLight(x,y-1),
  186.                                  GetLight(x-1,y),
  187.                                  GetLight(x,y));
  188.         ScreenPtr=(void*)(xScrPtr);
  189.         SCRSizeX=SSX;
  190.         RXCoor[STHead]=x;
  191.         RYCoor[STHead]=y;
  192.         RefMap[y][x]=STHead;
  193.         STHead++;
  194.         if(STHead>=(StackSize<<1))
  195.             STHead=0;
  196.         return (char*)rPos;
  197.     }else return RendSurf+(int(rn)<<8);
  198. };
  199. void ClearRender(int x,int y){
  200.     if(x>0&&y>0&&x<=msx&&y<=msy){
  201.         word rm=RefMap[y][x];
  202.         if(rm!=0xFFFF)RXCoor[rm]=0;
  203.         RefMap[y][x]=0xFFFF;
  204.     };
  205. };
  206. void AddHeight(int x,int y,int dh){
  207.     if(x>=0&&y>=0&&x<=msx&&y<=msy){
  208.         HiMap[y][x]+=dh;
  209.         ClearRender(x,y);
  210.         ClearRender(x+1,y);
  211.         ClearRender(x,y+1);
  212.         ClearRender(x+1,y+1);
  213.     };
  214. };
  215. void CreateBlob(int x,int y,int h,int r){
  216.     int x0=x-(r<<1);
  217.     int y0=y-(r<<1);
  218.     int x1=x+(r<<1);
  219.     int y1=y+(r<<1);
  220.     int r2=r*r;
  221.     for(int i=x0;i<x1;i++)
  222.         for(int j=y0;j<y1;j++){
  223.             AddHeight(i,j,h*exp(-double((x-i)*(x-i)+(y-j)*(y-j))/r2));
  224.         };
  225. };
  226. void InitRenderMap(){
  227.     InitRenderStack();
  228.     SetLight(-10,-30,30);
  229.     memset(&HiMap[0][0],0,sizeof HiMap);
  230. };
  231. int GetHig(int x,int y){
  232.     return 0;
  233.     int mx=x>>5;
  234.     int my=y>>5;
  235.     int z1=HiMap[my][mx];
  236.     int z2=HiMap[my][mx+1];
  237.     int z3=HiMap[my+1][mx];
  238.     int z4=HiMap[my+1][mx+1];
  239.     int z1a=(z1+z3)>>1;
  240.     int z2a=(z1+z2)>>1;
  241.     int z3a=(z4+z2)>>1;
  242.     int z4a=(z3+z4)>>1;
  243.     int za=(z1a+z2a+z3a+z4a)>>2;
  244.     int dx=(x&31)-16;
  245.     int dy=(y&31)-16;
  246.     return (za+((dx*(z3a-z1a))>>5)+((dy*(z4a-z2a))>>5))>>1;
  247. };