Guest User

Untitled

a guest
May 24th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 23.75 KB | None | 0 0
  1. procedure InitializeRC;
  2. const
  3.   lightAmbient:array[0..3] of GLfloat=(1,1,1,1);
  4.   lightDiffuse:array[0..3] of GLfloat=(1,1,1,1);
  5.   lightSpecular:array[0..3] of GLfloat=(0,0,0,1);
  6.   lightPosition:array[0..3] of GLfloat=(1000,10000,1000,0);
  7. const
  8.   FirstRC:boolean=True;
  9. begin
  10.   if not FirstRC then Exit;
  11.   FirstRC:=False;
  12.   glFrontFace(GL_CCW);
  13.   glCullFace(GL_FRONT);
  14.   glEnable(GL_CULL_FACE);
  15.   glDepthFunc(GL_LEQUAL);
  16.   glEnable(GL_DEPTH_TEST);
  17.   glShadeModel(GL_FLAT);
  18.   glClearColor(0,0,0,0);
  19.   glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_FASTEST);
  20.   glHint(GL_POLYGON_SMOOTH_HINT,GL_FASTEST);
  21.   glDisable(GL_DITHER);
  22.   glEnable(GL_LIGHTING);
  23.   glLightfv(GL_LIGHT0,GL_AMBIENT,@lightAmbient);
  24.   glLightfv(GL_LIGHT0,GL_DIFFUSE,@lightDiffuse);
  25.   glLightfv(GL_LIGHT0,GL_SPECULAR,@lightSpecular);
  26.   glLightfv(GL_LIGHT0,GL_POSITION,@lightPosition);
  27.   glEnable(GL_LIGHT0);
  28.   if AlphaBlending then
  29.   begin
  30.     glEnable(GL_BLEND);
  31.     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  32.   end;
  33. {  glEnable(GL_FOG);
  34.   glFog(GL_FOG_MODE,GL_LINEAR);
  35.   glFog(GL_FOG_DENSITY,20);
  36.   glFog(GL_FOG_START,10);
  37.   glFog(GL_FOG_END,DrawingDistantion*2);
  38.   glFogiv(GL_FOG_COLOR,@(white[0]));}
  39. end;
  40.  
  41. procedure DrawObject(var AModel:GLint);
  42. var
  43.   i,j,k:GLint;
  44.   LTex:integer;
  45. begin
  46.   LTex:=0;
  47.   DistanceRequestFromObject:=True;
  48.   if (ClippingAlgorithm=1) and (CalcDistance(0,0,0)>DrawingDistantion) then Exit;
  49.   if Models[AModel].ListNumber<>ListNotBinded then
  50.   begin
  51.     glCallList(Models[AModel].ListNumber);
  52.     Exit;
  53.   end;
  54.   Models[AModel].ListNumber:=glGenLists(1);
  55.   glNewList(Models[AModel].ListNumber,GL_COMPILE);
  56.   glEnable(GL_TEXTURE_2D);
  57.   glBindTexture(GL_TEXTURE_2D,Models[AModel].Textures[LTex].TexNum);
  58.   glBegin(GL_TRIANGLES);
  59.   glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@white);
  60.  
  61.   for i:=0 to Models[AModel].CSplines-1 do
  62.   begin
  63.     if Models[AModel].Splines[i].TextureNumber<>LTex then
  64.     begin
  65.       LTex:=Models[AModel].Splines[i].TextureNumber;
  66.       glEnd;
  67.       glBindTexture(GL_TEXTURE_2D,0);
  68.       glBindTexture(GL_TEXTURE_2D,Models[AModel].Textures[LTex].TexNum);
  69.       glBegin(GL_TRIANGLES);
  70.       glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@white);
  71.     end;
  72.     k:=Models[AModel].Splines[i].Aliases[0];
  73.     glNormal3f(Models[AModel].Points[k].Nx,Models[AModel].Points[k].Ny,Models[AModel].Points[k].Nz);
  74.     for j:=Models[AModel].Splines[i].PointsCount-1 downto 0 do
  75.     begin
  76.       k:=Models[AModel].Splines[i].Aliases[j];
  77.       glTexCoord2f(Models[AModel].Points[k].TexX,Models[AModel].Points[k].TexY);
  78.       glVertex3f(Models[AModel].Points[k].x,Models[AModel].Points[k].y,Models[AModel].Points[k].z);
  79.     end;
  80.   end;
  81.   glEnd;
  82.   glBindTexture(GL_TEXTURE_2D,0);
  83.   glDisable(GL_TEXTURE_2D);
  84.   glEndList;
  85. end;
  86.  
  87. procedure DrawModelAtPoints(a,b:TRailPoint;AModel:GLint;aOrientation:TTrainOrientation);
  88. var
  89.   xe,ye,ze,xf,yf,zf:GLfloat;
  90. begin
  91.   CalcCoords(a);
  92.   xe:=xmp;
  93.   ye:=ymp;
  94.   ze:=zmp;
  95.   DistanceRequestFromObject:=False;
  96.   if (ClippingAlgorithm=1) and (CalcDistance(xe,ye,ze)>(2*DrawingDistantion)) then Exit;
  97.   CalcCoords(b);
  98.   xf:=xmp;
  99.   yf:=ymp;
  100.   zf:=zmp;
  101.   xmp:=(xe+xf)/2;
  102.   ymp:=(ye+yf)/2;
  103.   zmp:=(ze+zf)/2;
  104.   glPushMatrix;
  105.   glTranslatef(xmp,ymp,zmp);
  106.   if ((xe-xmp)>=0) and ((ze-zmp)>=0) then
  107.     glRotatef(arcsin((xe-xmp)/Sqrt(Sqr(xmp-xe)+Sqr(zmp-ze)))*180/PiConst,0,1,0)
  108.   else if ((xe-xmp)>=0) and ((ze-zmp)<0) then
  109.     glRotatef(-arcsin((xe-xmp)/Sqrt(Sqr(xmp-xe)+Sqr(zmp-ze)))*180/PiConst,0,1,0)
  110.   else if ((xe-xmp)<0) and ((ze-zmp)>=0) then
  111.     glRotatef(arcsin((xe-xmp)/Sqrt(Sqr(xmp-xe)+Sqr(zmp-ze)))*180/PiConst,0,1,0)
  112.   else
  113.     glRotatef(-arcsin((xe-xmp)/Sqrt(Sqr(xmp-xe)+Sqr(zmp-ze)))*180/PiConst,0,1,0);
  114.   if aOrientation=toReverse then
  115.     glRotatef(180,0,1,0);
  116.   CalcRequestX:=xmp;
  117.   CalcRequestY:=ymp;
  118.   CalcRequestZ:=zmp;
  119.   DrawObject(AModel);
  120.   glPopMatrix;
  121. end;
  122.  
  123. procedure DrawHouse(AHouse:THouse);
  124. begin
  125.   DistanceRequestFromObject:=False;
  126.   if (ClippingAlgorithm=1) and (CalcDistance(AHouse.x,AHouse.y,AHouse.z)>(2*DrawingDistantion)) then Exit;
  127.   glPushMatrix;
  128.   glTranslatef(AHouse.x,AHouse.y,AHouse.z);
  129.   glRotatef(AHouse.Angle,0,1,0);
  130.   CalcRequestX:=AHouse.x;
  131.   CalcRequestY:=AHouse.y;
  132.   CalcRequestZ:=AHouse.z;
  133.   DrawObject(AHouse.Model);
  134.   glPopMatrix;
  135. end;
  136.  
  137. procedure DrawTree(ATree:TTree);
  138. begin
  139.   DistanceRequestFromObject:=False;
  140. //  if (ClippingAlgorithm=1) and (CalcDistance(ATree.x,ATree.y,ATree.z)>(2*DrawingDistantion)) then Exit;
  141.   glPushMatrix;
  142.   glTranslatef(ATree.x,ATree.y,ATree.z);
  143.   CalcRequestX:=ATree.x;
  144.   CalcRequestY:=ATree.y;
  145.   CalcRequestZ:=ATree.z;
  146.   DrawObject(ATree.Model);
  147.   glPopMatrix;
  148. end;
  149.  
  150. procedure DrawTrees;
  151. var
  152.   VisibleTrees:array of record
  153.     TreeIndex:GLint;
  154.     Distance:GLint;
  155.   end;
  156.   i:integer;
  157.   a,b:GLint;
  158.  
  159. procedure QuickSort(iLo, iHi: Integer);
  160. var
  161.   Lo, Hi, Mid: Integer;
  162. begin
  163.   Lo := iLo;
  164.   Hi := iHi;
  165.   Mid := VisibleTrees[(Lo + Hi) div 2].Distance;
  166.   repeat
  167.     while VisibleTrees[Lo].Distance < Mid do Inc(Lo);
  168.     while VisibleTrees[Hi].Distance > Mid do Dec(Hi);
  169.     if Lo <= Hi then
  170.     begin
  171.       b:=VisibleTrees[Lo].TreeIndex;
  172.       a:=VisibleTrees[Lo].Distance;
  173.       VisibleTrees[Lo].TreeIndex:=VisibleTrees[Hi].TreeIndex;
  174.       VisibleTrees[Lo].Distance:=VisibleTrees[Hi].Distance;
  175.       VisibleTrees[Hi].TreeIndex:=b;
  176.       VisibleTrees[Hi].Distance:=a;
  177.       Inc(Lo);
  178.       Dec(Hi);
  179.     end;
  180.   until Lo > Hi;
  181.   if Hi > iLo then QuickSort(iLo, Hi);
  182.   if Lo < iHi then QuickSort(Lo, iHi);
  183. end;
  184.  
  185. begin
  186.   DistanceRequestFromObject:=False;
  187.   SetLength(VisibleTrees,0);
  188.   for i:=0 to MaxTrees do
  189.   begin
  190.     a:=Round(CalcDistance(Trees[i].x,Trees[i].y,Trees[i].z));
  191.     if (ClippingAlgorithm=1) and (a<=(2*DrawingDistantion)) then
  192.     begin
  193.       SetLength(VisibleTrees,Length(VisibleTrees)+1);
  194.       VisibleTrees[High(VisibleTrees)].Distance:=a;
  195.       VisibleTrees[High(VisibleTrees)].TreeIndex:=i;
  196.     end;
  197.   end;
  198.  
  199.   if Length(VisibleTrees)=0 then
  200.   begin
  201.     SetLength(VisibleTrees,0);
  202.     Exit;
  203.   end;
  204.  
  205.   QuickSort(Low(VisibleTrees),High(VisibleTrees));
  206.  
  207.   for i:=High(VisibleTrees) downto Low(VisibleTrees) do
  208.     DrawTree(Trees[VisibleTrees[i].TreeIndex]);
  209.    
  210.   SetLength(VisibleTrees,0);
  211. end;
  212.  
  213. function LoadTexture(var ATexture:PGLTexture;FileName:string;aType:TTextureType):boolean;
  214. var
  215.   f:file;
  216.   i,j,k,l:GLint;
  217.   BufferRGB:PRGBTexture;
  218.   BufferRGBA:PRGBATexture;
  219.   tsz,tml,tov,txs,tys:GLint;
  220.   ra,ga,ba,aa:GLfloat;
  221.   TGAHeader:record
  222.     Data:array[0..11] of byte;
  223.     Width,Height:word;
  224.     Size:byte;
  225.   end;
  226.   ColorDepth:integer;
  227.   s:string;
  228.   LocalTexSize:GLint;
  229.   a:GLuint;
  230.   b:cardinal;
  231. begin
  232.   OutDbgMsg('----> LoadTexture: Loading '+FileName);
  233.   if aType=tiTrain then
  234.     LocalTexSize:=TrainTextureSize
  235.   else if aType=tiScreen then
  236.     LocalTexSize:=ScreensRes
  237.   else
  238.     LocalTexSize:=TextureSize;
  239.   BufferRGB:=nil;
  240.   BufferRGBA:=nil;
  241.   LoadTexture:=False;
  242.   GetMem(ATexture,SizeOf(TRGBAQuad)*LocalTexSize*LocalTexSize+SizeOf(GLuint)+SizeOf(cardinal));
  243.   ATexture^.AllocatedSize:=SizeOf(TRGBAQuad)*LocalTexSize*LocalTexSize+SizeOf(GLuint)+SizeOf(cardinal);
  244.   MemoryAllocated:=MemoryAllocated+SizeOf(TRGBAQuad)*LocalTexSize*LocalTexSize+SizeOf(GLuint)+SizeOf(cardinal);
  245.   if ATexture=nil then
  246.   begin
  247.     s:='Нехватка памяти при размещении блока размером '+IntToStr(SizeOf(TGLTexture) div 1024)+' Кб.';
  248.     MessageBox(0,@s[1],'Core3Da: Ошибка',mb_Ok or mb_IconStop);
  249.     Halt(0);
  250.   end;
  251.   try
  252.     AssignFile(f,FileName);
  253.     Reset(f,1);
  254.     FillChar(TGAHeader,SizeOf(TGAHeader),0);
  255.     BlockRead(f,TGAHeader,SizeOf(TGAHeader));
  256.     if TGAHeader.Size=$20 then
  257.       ColorDepth:=32
  258.     else
  259.       ColorDepth:=24;
  260.     Seek(f,$12);
  261.     if ColorDepth=24 then
  262.     begin
  263.       New(BufferRGB);
  264.       MemoryAllocated:=MemoryAllocated+SizeOf(TRGBTexture);
  265.       if BufferRGB=nil then
  266.       begin
  267.         s:='Нехватка памяти при размещении блока размером '+IntToStr(SizeOf(TRGBTexture) div 1024)+' Кб.';
  268.         MessageBox(0,@s[1],'Core3Da: Ошибка',mb_Ok or mb_IconStop);
  269.         Halt(0);
  270.       end;
  271.       BlockRead(f,BufferRGB^,512*512*(ColorDepth div 8));
  272.     end else
  273.     begin
  274.       New(BufferRGBA);
  275.       MemoryAllocated:=MemoryAllocated+SizeOf(TRGBATexture);
  276.       if BufferRGBA=nil then
  277.       begin
  278.         s:='Нехватка памяти при размещении блока размером '+IntToStr(SizeOf(TRGBATexture) div 1024)+' Кб.';
  279.         MessageBox(0,@s[1],'Core3Da: Ошибка',mb_Ok or mb_IconStop);
  280.         Halt(0);
  281.       end;
  282.       BlockRead(f,BufferRGBA^,512*512*(ColorDepth div 8));
  283.     end;
  284.     CloseFile(f);
  285.   except
  286.     on E:EInOutError do
  287.     begin
  288.       s:='Файл '+FileName+' не найден или имеет неверный формат.';
  289.       MessageBox(0,@s[1],'Core3Da: Ошибка',mb_Ok or mb_IconStop);
  290.       Exit;
  291.     end;
  292.   end;
  293.   tsz:=LocalTexSize-1;
  294.   tml:=512 div LocalTexSize;
  295.   for i:=0 to tsz do
  296.     for j:=0 to tsz do
  297.     begin
  298.       tov:=i*(tsz+1)+(tsz-j);
  299.       txs:=tml*i;
  300.       tys:=tml*j;
  301.       ra:=0;
  302.       ga:=0;
  303.       ba:=0;
  304.       aa:=0;
  305.       for k:=0 to tml-1 do
  306.         for l:=0 to tml-1 do
  307.         begin
  308.           if ColorDepth=24 then
  309.           begin
  310.             ra:=ra+BufferRGB^[(txs+k),511-(tys+l)].rgbtRed;
  311.             ga:=ga+BufferRGB^[(txs+k),511-(tys+l)].rgbtGreen;
  312.             ba:=ba+BufferRGB^[(txs+k),511-(tys+l)].rgbtBlue;
  313.             aa:=aa+256;
  314.           end else
  315.           begin
  316.             ra:=ra+BufferRGBA^[(txs+k),511-(tys+l)].rgbRed;
  317.             ga:=ga+BufferRGBA^[(txs+k),511-(tys+l)].rgbGreen;
  318.             ba:=ba+BufferRGBA^[(txs+k),511-(tys+l)].rgbBlue;
  319.             aa:=aa+BufferRGBA^[(txs+k),511-(tys+l)].rgbReserved;
  320.           end;
  321.         end;
  322.       ATexture^.Data[tov].r:=ra/(256*tml*tml);
  323.       ATexture^.Data[tov].g:=ga/(256*tml*tml);
  324.       ATexture^.Data[tov].b:=ba/(256*tml*tml);
  325.       ATexture^.Data[tov].a:=aa/(256*tml*tml);
  326.     end;
  327.   if ColorDepth=24 then
  328.   begin
  329.     Dispose(BufferRGB);
  330.     MemoryAllocated:=MemoryAllocated-SizeOf(TRGBTexture);
  331.   end else
  332.   begin
  333.     Dispose(BufferRGBA);
  334.     MemoryAllocated:=MemoryAllocated-SizeOf(TRGBATexture);
  335.   end;
  336.   if aType<>tiScreen then
  337.   begin
  338.     glEnable(GL_TEXTURE_2D);
  339.     glGenTextures(1,@a);
  340.     ATexture^.TexNum:=a;
  341.     glBindTexture(GL_TEXTURE_2D,ATexture^.TexNum);
  342.     gluBuild2DMipmaps(GL_TEXTURE_2D,4,LocalTexSize,LocalTexSize,GL_RGBA,GL_FLOAT,@(ATexture^.Data[0]));
  343.     case aType of
  344.       tiGround: begin
  345.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
  346.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
  347.         if TextureFilter=tfFast then
  348.           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
  349.         else
  350.           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  351.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  352.       end;
  353.       tiRail: begin
  354.         if TextureFilter=tfFast then
  355.           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
  356.         else
  357.           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  358.         if TextureFilter=tfFast then
  359.           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
  360.         else
  361.           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  362.       end;
  363.       tiLandscape: begin
  364.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
  365.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  366.       end;
  367.       tiHouse: begin
  368.         if TextureFilter=tfFast then
  369.           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
  370.         else
  371.           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  372.         if TextureFilter=tfFast then
  373.           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST)
  374.         else
  375.           glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
  376.       end;
  377.     else
  378.       if TextureFilter=tfFast then
  379.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
  380.       else
  381.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  382.       glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  383.     end;
  384.     glBindTexture(GL_TEXTURE_2D,0);
  385.     glDisable(GL_TEXTURE_2D);
  386.  
  387.     b:=ATexture^.AllocatedSize;
  388.     FreeMem(ATexture,b);
  389.     MemoryAllocated:=MemoryAllocated-SizeOf(TRGBAQuad)*LocalTexSize*LocalTexSize;
  390.     GetMem(ATexture,SizeOf(GLuint)+SizeOf(cardinal));
  391.     ATexture^.AllocatedSize:=SizeOf(GLuint)+SizeOf(cardinal);
  392.     ATexture^.TexNum:=a;
  393.   end else
  394.     ATexture^.TexNum:=TexNotBinded;
  395.  
  396.   LoadTexture:=True;
  397.   OutDbgMsg('----> LoadTexture: Texture successfully loaded.');
  398. end;
  399.  
  400. function LoadModel(var ToModel:GLint;FileName:string;aType:TTextureType):boolean;
  401. var
  402.   f:textfile;
  403.   s:string;
  404.   z:GLint;
  405.   a,b,c:GLfloat;
  406.   i,d,e,g,m,h:GLint;
  407. begin
  408.   OutDbgMsg('--> LoadModel: '+FileName);
  409.   LoadModel:=False;
  410.   for i:=0 to MaxModels do
  411.     if Models[i].FileName=FileName then
  412.     begin
  413.       ToModel:=i;
  414.       LoadModel:=True;
  415.       Exit;
  416.     end;
  417.   h:=MaxModels+1;
  418.   SetLength(Models,h+1);
  419.   MaxModels:=h;
  420.   Models[h].FileName:=FileName;
  421.   Models[h].ListNumber:=ListNotBinded;
  422.   ToModel:=h;
  423.   AssignFile(f,FileName);
  424.   try
  425.     Reset(f);
  426.     try
  427.       s:='';
  428.       while Pos('Mesh',s)=0 do Readln(f,s);
  429.       if Eof(f) then
  430.       begin
  431.         s:='Секция {Mesh} не найдена в файле '+FileName;
  432.         MessageBox(0,@s[1],'Core3Da: Ошибка',mb_Ok or mb_IconStop);
  433.         Exit;
  434.       end;
  435.       if aType=tiTrain then
  436.         Models[h].TextureSize:=TrainTextureSize
  437.       else
  438.         Models[h].TextureSize:=TextureSize;
  439.       Read(f,Models[h].CPoints);
  440.       SetLength(Models[h].Points,Models[h].CPoints);
  441.       for i:=0 to Models[h].CPoints-1 do
  442.       begin
  443.         Read(f,a,b,c);
  444.         Models[h].Points[i].x:=a;
  445.         Models[h].Points[i].y:=b;
  446.         Models[h].Points[i].z:=c;
  447.       end;
  448.       UpdateProgress;
  449.       Read(f,Models[h].CSplines);
  450.       SetLength(Models[h].Splines,Models[h].CSplines);
  451.       for i:=0 to Models[h].CSplines-1 do
  452.       begin
  453.         Read(f,z,d,e,g);
  454.         if z=0 then;
  455.         Models[h].Splines[i].PointsCount:=3;
  456.         Models[h].Splines[i].Aliases[0]:=d;
  457.         Models[h].Splines[i].Aliases[1]:=e;
  458.         Models[h].Splines[i].Aliases[2]:=g;
  459.       end;
  460.  
  461.       while Pos('MaterialList',s)=0 do Readln(f,s);
  462.       if Eof(f) then
  463.       begin
  464.         s:='Секция {Material} не найдена в файле '+FileName;
  465.         MessageBox(0,@s[1],'Core3Da: Ошибка',mb_Ok or mb_IconStop);
  466.         Exit;
  467.       end;
  468.       Readln(f,m);
  469.       SetLength(Models[h].Textures,m);
  470.       Readln(f,s);
  471.       for i:=0 to Models[h].CSplines-1 do
  472.       begin
  473.         Readln(f,d);
  474.         Models[h].Splines[i].TextureNumber:=d;
  475.       end;
  476.       UpdateProgress;
  477.  
  478.       for i:=0 to m-1 do
  479.       begin
  480.         while Pos('Texture',s)=0 do Readln(f,s);
  481.         if Eof(f) then
  482.         begin
  483.           s:='Секция {Texture} не найдена в файле '+FileName;
  484.           MessageBox(0,@s[1],'Core3Da: Ошибка',mb_Ok or mb_IconStop);
  485.           Exit;
  486.         end;
  487.         Readln(f,s);
  488.         s:=Copy(s,2,Length(s)-2);
  489.         s:=ExtractFilePath(FileName)+s;
  490.         if not LoadTexture(Models[h].Textures[i],s,aType) then
  491.           raise EInOutError.Create('Не могу загрузить файл '+FileName+' с текстурой.');
  492.       end;
  493.  
  494.       while Pos('MeshNormals',s)=0 do Readln(f,s);
  495.       if Eof(f) then
  496.       begin
  497.         s:='Секция {MeshNormals} не найдена в файле '+FileName;
  498.         MessageBox(0,@s[1],'Core3Da: Ошибка',mb_Ok or mb_IconStop);
  499.         Exit;
  500.       end;
  501.       Read(f,z);
  502.       if z=0 then;
  503.       for i:=0 to Models[h].CPoints-1 do
  504.       begin
  505.         Read(f,a,b,c);
  506.         Models[h].Points[i].Nx:=a;
  507.         Models[h].Points[i].Ny:=b;
  508.         Models[h].Points[i].Nz:=c;
  509.       end;
  510.  
  511.       while Pos('TextureCoords',s)=0 do Readln(f,s);
  512.       if Eof(f) then
  513.       begin
  514.         s:='Секция {MeshTextureCoords} не найдена в файле '+FileName;
  515.         MessageBox(0,@s[1],'Core3Da: Ошибка',mb_Ok or mb_IconStop);
  516.         Exit;
  517.       end;
  518.       Read(f,z);
  519.       if z=0 then;
  520.       for i:=0 to Models[h].CPoints-1 do
  521.       begin
  522.         Read(f,a,b);
  523.         Models[h].Points[i].TexX:=-1+a;
  524.         Models[h].Points[i].TexY:=-b;
  525.       end;
  526.       UpdateProgress;
  527.       LoadModel:=True;
  528.       OutDbgMsg('--> LoadModel: Model successfully loaded.');
  529.     except
  530.       on E:EInOutError do
  531.       begin
  532.         s:='Неверный формат файла '+FileName;
  533.         MessageBox(0,@s[1],'Core3Da: Ошибка',mb_Ok or mb_IconStop);
  534.       end;
  535.     end;
  536.     CloseFile(f);
  537.   except
  538.     on E:EInOutError do
  539.     begin
  540.       s:='Файл '+FileName+' не найден.';
  541.       MessageBox(0,@s[1],'Core3Da: Ошибка',mb_Ok or mb_IconStop);
  542.     end;
  543.   end;
  544. end;
  545.  
  546. procedure LoadMap(FileName:string);
  547. var
  548.   b:TRail;
  549.   w,h,a,i,l,j:integer;
  550.   f:file;
  551.   c:cardinal;
  552.   Train:TTrainEx;
  553.   Link:TLinkEx;
  554.   House:THouseEx;
  555.   Tree:TTreeEx;
  556.   Light:TLightEx;
  557.   s:string;
  558. begin
  559.   OutDbgMsg('Loading map '+FileName);
  560.   AssignFile(f,FileName);
  561.   try
  562.     Reset(f,1);
  563.     try
  564.       SetLength(Rails,0);
  565.       OutDbgMsg('LoadMap: Reading map version signature.');
  566.       BlockRead(f,c,SizeOf(c));
  567.       if c<>MapSignature then
  568.       begin
  569.         OutDbgMsg('LoadMap: ERROR LOADING MAP!!! UNSUPPORTED VERSION!!!');
  570.         wglMakeCurrent(MainDC,0);
  571.         ReleaseDC(Window,MainDC);
  572.         CloseFile(f);
  573.         MessageBox(0,'Формат не поддерживается. Используйте соответствующую версию '+
  574.           'редактора карт.','Формат MAP файла',mb_Ok or mb_IconStop or mb_TaskModal);
  575.         Halt(0);
  576.       end;
  577.       OutDbgMsg('LoadMap: Reading map size.');
  578.       BlockRead(f,w,SizeOf(w));
  579.       BlockRead(f,h,SizeOf(h));
  580.       MapWidth:=w;
  581.       MapHeight:=h;
  582.       BlockRead(f,a,SizeOf(a));
  583.       SetLength(Rails,a);
  584.       OutDbgMsg('LoadMap: Creating rails.');
  585.       MaxRails:=a-1;
  586.       for i:=0 to a-1 do
  587.       begin
  588.         BlockRead(f,b,SizeOf(b));
  589.         Rails[i]:=b;
  590.         if (Rails[i].Kind=rkStraight) or (Rails[i].Kind=rkLeftPoint) or
  591.           (Rails[i].Kind=rkRightPoint) or (Rails[i].Kind=rkThreePoint) then
  592.         begin
  593.           l:=Round(Rails[i].StraightLength) div sl;
  594.           if (Round(Rails[i].StraightLength) mod sl)>0 then l:=l+1;
  595.           Rails[i].Length:=l*sl;
  596.         end;
  597.  
  598.         if Rails[i].Kind=rkRadius then
  599.           Rails[i].Length:=Round((Rails[i].Angle*PiConst/180)*Rails[i].Radius);
  600.  
  601.         if (i mod 50)=0 then
  602.           UpdateProgress;
  603.       end;
  604.       OutDbgMsg('LoadMap: Reading map parameters.');
  605.       BlockRead(f,MapFlagX,SizeOf(MapFlagX));
  606.       BlockRead(f,MapFlagZ,SizeOf(MapFlagZ));
  607.       CameraX:=MapFlagX-400;
  608.       CameraZ:=MapFlagZ-1000;
  609.  
  610.       OutDbgMsg('LoadMap: Creating trains.');
  611.       BlockRead(f,a,SizeOf(a));
  612.       SetLength(Trains,a);
  613.       MaxTrains:=a-1;
  614.       for i:=0 to a-1 do
  615.       begin
  616.         BlockRead(f,Train,SizeOf(Train));
  617.         FillChar(Trains[i],SizeOf(TTrain),0);
  618.         Trains[i].Rail:=Train.Rail;
  619.         Trains[i].Position:=Train.Position;
  620.         Trains[i].Orientation:=Train.Orientation;
  621.         Trains[i].Direction:=Train.Direction;
  622.         Trains[i].TrainType:=Train.TrainType;
  623.         Trains[i].Length:=Train.Length;
  624.         Trains[i].Name:=Train.Name;
  625.         Trains[i].HiMesh:=Train.HiMesh;
  626.         Trains[i].MidMesh:=Train.MidMesh;
  627.         Trains[i].LoMesh:=Train.LoMesh;
  628.         case TrainsDetalizationLevel of
  629.           0: LoadModel(Trains[i].Train,DataPath+'\Machines\'+Train.LoMesh,tiTrain);
  630.           1: LoadModel(Trains[i].Train,DataPath+'\Machines\'+Train.MidMesh,tiTrain);
  631.           2: LoadModel(Trains[i].Train,DataPath+'\Machines\'+Train.HiMesh,tiTrain);
  632.         else
  633.           s:='Неверный формат файла карты '+FileName;
  634.           MessageBox(0,@s[1],'Core3Da: Ошибка',mb_IconStop or mb_Ok);
  635.           Halt(0);
  636.         end;
  637.       end;
  638.       OutDbgMsg('LoadMap: Creating links.');
  639.       BlockRead(f,a,SizeOf(a));
  640.       SetLength(Links,a);
  641.       for i:=0 to a-1 do
  642.       begin
  643.         BlockRead(f,Link,SizeOf(Link));
  644.         FillChar(Links[i],SizeOf(TLink),0);
  645.         Links[i].Head:=Link.Head;
  646.         Links[i].Speed:=Link.Speed;
  647.         SetLength(Links[i].Trains,Link.TrainsCount);
  648.         for j:=0 to Link.TrainsCount-1 do
  649.         begin
  650.           BlockRead(f,w,SizeOf(w));
  651.           Links[i].Trains[j]:=w;
  652.         end;
  653.         UpdateProgress;
  654.       end;
  655.  
  656.       OutDbgMsg('LoadMap: Creating houses.');
  657.       BlockRead(f,a,SizeOf(a));
  658.       SetLength(Houses,a);
  659.       MaxHouses:=a-1;
  660.       for i:=0 to a-1 do
  661.       begin
  662.         BlockRead(f,House,SizeOf(House));
  663.         FillChar(Houses[i],SizeOf(THouse),0);
  664.         Houses[i].x:=House.x;
  665.         Houses[i].y:=House.y;
  666.         Houses[i].z:=House.z;
  667.         Houses[i].Angle:=House.Angle;
  668.         Houses[i].HouseType:=House.HouseType;
  669.         Houses[i].HouseName:=House.HouseName;
  670.         case DetalizationLevel of
  671.           0,1: LoadModel(Houses[i].Model,DataPath+'\Houses\'+House.HouseName+'_min.x',tiHouse);
  672.           2: LoadModel(Houses[i].Model,DataPath+'\Houses\'+House.HouseName+'.x',tiHouse);
  673.         else
  674.           s:='Неверный формат файла карты '+FileName;
  675.           MessageBox(0,@s[1],'Core3Da: Ошибка',mb_IconStop or mb_Ok);
  676.           Halt(0);
  677.         end;
  678.         UpdateProgress;
  679.       end;
  680.  
  681.       OutDbgMsg('LoadMap: Creating trees.');
  682.       BlockRead(f,a,SizeOf(a));
  683.       SetLength(Trees,a);
  684.       MaxTrees:=a-1;
  685.       for i:=0 to a-1 do
  686.       begin
  687.         BlockRead(f,Tree,SizeOf(Tree));
  688.         FillChar(Trees[i],SizeOf(TTree),0);
  689.         Trees[i].x:=Tree.x;
  690.         Trees[i].y:=Tree.y;
  691.         Trees[i].z:=Tree.z;
  692.         Trees[i].TreeType:=Tree.TreeType;
  693.         LoadModel(Trees[i].Model,DataPath+'\Trees\Strom'+IntToStr(Tree.TreeType)+'.x',tiTree);
  694.         UpdateProgress;
  695.       end;
  696.       OutDbgMsg('LoadMap: Creating landscape.');
  697.       BlockRead(f,Landscape,SizeOf(Landscape));
  698.       s:=DataPath+'\Skyboxes\Sky'+IntToStr(Landscape)+'\';
  699.       LoadTexture(LandTex.Top,s+'top.tga',tiLandscape);
  700.       LoadTexture(LandTex.Back,s+'back.tga',tiLandscape);
  701.       LoadTexture(LandTex.Front,s+'front.tga',tiLandscape);
  702.       LoadTexture(LandTex.Bottom,s+'bottom.tga',tiLandscape);
  703.       LoadTexture(LandTex.Left,s+'left.tga',tiLandscape);
  704.       LoadTexture(LandTex.Right,s+'right.tga',tiLandscape);
  705.  
  706.       OutDbgMsg('LoadMap: Creating lights.');
  707.       BlockRead(f,a,SizeOf(a));
  708.       SetLength(Lights,a);
  709.       MaxLights:=a-1;
  710.       for i:=0 to a-1 do
  711.       begin
  712.         BlockRead(f,Light,SizeOf(Light));
  713.         SetLength(Lights[i].Script,Light.ScriptLength);
  714.         BlockRead(f,Lights[i].Script[1],Light.ScriptLength);
  715.         Lights[i].x:=Light.x;
  716.         Lights[i].y:=Light.y;
  717.         Lights[i].z:=Light.z;
  718.         Lights[i].Angle:=Light.Angle;
  719.         Lights[i].LightState:=Light.LightState;
  720.         Lights[i].ScriptLength:=Light.ScriptLength;
  721.         Lights[i].ScriptIsValid:=False;
  722.         UpdateProgress;
  723.       end;
  724.     except
  725.       on E:EInOutError do
  726.       begin
  727.         s:='Неверный формат файла карты '+FileName;
  728.         MessageBox(0,@s[1],'Core3Da: Ошибка',mb_IconStop or mb_Ok);
  729.       end;
  730.     end;
  731.     CloseFile(f);
  732.     OutDbgMsg('*** Map structures successfully loaded.');
  733.   except
  734.     on E:EInOutError do
  735.     begin
  736.       s:='Файл '+FileName+' не найден.';
  737.       MessageBox(0,@s[1],'Core3Da: Ошибка',mb_IconStop or mb_Ok);
  738.     end;
  739.   end;
  740. end;
Add Comment
Please, Sign In to add comment