Advertisement
retroman

Untitled

Sep 23rd, 2024
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 9.76 KB | None | 0 0
  1. program ModelLoader;
  2.  
  3. uses
  4.   SysUtils, Classes, crt, Math;
  5.  
  6. const
  7.   cScale = 1/20;
  8.  
  9. type
  10.   TPartStruct = record
  11.     wColour: LongInt;
  12.     bType: LongInt;
  13.   end;
  14.  
  15.   TModelList = array of TPartStruct;
  16.  
  17.   PDATFile = ^TDATFile;
  18.   TDATFile = record
  19.     tParts: array of TPartStruct;
  20.     iPartCount: LongInt;
  21.   end;
  22.  
  23. var
  24.   g_sFilenames: string;
  25.   g_sFilesToLoad: string;
  26.   g_ModelCount: LongInt;
  27.   g_tModels: array of TModelList;
  28.  
  29. function LoadModel(pFile: PChar; sFilename: string = ''; iModelIndex: LongInt = -1; iLoadDependencies: Byte = 1): PDATFile;
  30. var
  31.   iLastPart, iLimitParts, iFailed, iLineNum, iType, iColour, iResu: LongInt;
  32.   pT: PDATFile;
  33.   RecursionLevel, iTotalLines, iTotalParts: LongInt;
  34.   pNew: Pointer;
  35. begin
  36.   iLastPart := 0;
  37.   iLimitParts := -1;
  38.   iFailed := 0;
  39.   iLineNum := 1;
  40.   pT := nil;
  41.   RecursionLevel := 0;
  42.   iTotalLines := 0;
  43.   iTotalParts := 0;
  44.   Inc(RecursionLevel);
  45.  
  46.   if iModelIndex < 0 then
  47.   begin
  48.     iModelIndex := g_ModelCount;
  49.     SetLength(g_tModels, g_ModelCount + 1);
  50.     Inc(g_ModelCount);
  51.   end;
  52.  
  53.   repeat
  54.     if iLastPart > iLimitParts then
  55.     begin
  56.       Inc(iLimitParts, 4096);
  57.       ReallocMem(pT, SizeOf(TDATFile) + (iLimitParts + 1) * SizeOf(TPartStruct));
  58.       if pT = nil then
  59.       begin
  60.         WriteLn('Failed to allocate memory to load file');
  61.         iFailed := 1;
  62.         Break;
  63.       end;
  64.     end;
  65.  
  66.     // Simulate reading and processing model file
  67.     iResu := Random(10);  // Placeholder for actual reading function
  68.     if iResu = 0 then
  69.       Continue;
  70.  
  71.     // Simulate checking for model type and color
  72.     iType := Random(3);  // Random value to simulate the process
  73.     if iType <> 0 then
  74.     begin
  75.       iColour := Random(16);
  76.       pT^.tParts[iLastPart].wColour := iColour;
  77.       pT^.tParts[iLastPart].bType := iType;
  78.     end;
  79.  
  80.     Inc(iLastPart);
  81.   until False;
  82.  
  83.   Dec(RecursionLevel);
  84.  
  85.   if iFailed = 1 then
  86.   begin
  87.     WriteLn('Failure during file loading');
  88.     Exit(nil);
  89.   end;
  90.  
  91.   pT^.iPartCount := iLastPart;
  92.   Result := pT;
  93. end;
  94.  
  95. begin
  96.   Randomize;
  97.  
  98.   g_sFilenames := '';
  99.   g_sFilesToLoad := '';
  100.   g_ModelCount := 0;
  101.   SetLength(g_tModels, 1);
  102.  
  103.   WriteLn('Loading model...');
  104.   LoadModel(nil, 'testfile.dat');
  105. end.
  106. uses crt, sysutils, math;
  107.  
  108. type
  109.   SinglePtr = ^Single;
  110.   LineType2Struct = record
  111.     fX1, fY1, fZ1: Single;
  112.     fX2, fY2, fZ2: Single;
  113.   end;
  114.   LineType4Struct = record
  115.     fX1, fY1, fZ1: Single;
  116.     fX2, fY2, fZ2: Single;
  117.     fX3, fY3, fZ3: Single;
  118.     fX4, fY4, fZ4: Single;
  119.   end;
  120.  
  121. procedure CrossProduct(v1, v2, result: SinglePtr);
  122. begin
  123.   result^[0] := v1^[1] * v2^[2] - v1^[2] * v2^[1];
  124.   result^[1] := v1^[2] * v2^[0] - v1^[0] * v2^[2];
  125.   result^[2] := v1^[0] * v2^[1] - v1^[1] * v2^[0];
  126. end;
  127.  
  128. procedure Normalize(v: SinglePtr);
  129. var
  130.   length: Single;
  131. begin
  132.   length := Sqrt(v^[0] * v^[0] + v^[1] * v^[1] + v^[2] * v^[2]);
  133.   if length <> 0.0 then
  134.   begin
  135.     v^[0] := v^[0] / length;
  136.     v^[1] := v^[1] / length;
  137.     v^[2] := v^[2] / length;
  138.   end;
  139. end;
  140.  
  141. procedure SetLineNormal(var tLine: LineType2Struct);
  142. var
  143.   direction, ref, normal: array[0..2] of Single;
  144.   normalScale: Single;
  145. begin
  146.   direction[0] := tLine.fX2 - tLine.fX1;
  147.   direction[1] := tLine.fY2 - tLine.fY1;
  148.   direction[2] := tLine.fZ2 - tLine.fZ1;
  149.  
  150.   ref[0] := 0.0;
  151.   ref[1] := 1.0;
  152.   ref[2] := 0.0;
  153.  
  154.   CrossProduct(@direction[0], @ref[0], @normal[0]);
  155.   Normalize(@normal[0]);
  156.  
  157.   normalScale := 0.5;
  158.   normal[0] := normal[0] * normalScale;
  159.   normal[1] := normal[1] * normalScale;
  160.   normal[2] := normal[2] * normalScale;
  161.  
  162.   // In Pascal, you can use OpenGL to set the normal vector like this
  163.   // but this would require the OpenGL unit.
  164.   // glNormal3fv(@normal[0]);
  165. end;
  166.  
  167. procedure SetQuadNormal(var tQuad: LineType4Struct);
  168. var
  169.   edge1, edge2, normal: array[0..2] of Single;
  170. begin
  171.   edge1[0] := tQuad.fX2 - tQuad.fX1;
  172.   edge1[1] := tQuad.fY2 - tQuad.fY1;
  173.   edge1[2] := tQuad.fZ2 - tQuad.fZ1;
  174.  
  175.   edge2[0] := tQuad.fX3 - tQuad.fX1;
  176.   edge2[1] := tQuad.fY3 - tQuad.fY1;
  177.   edge2[2] := tQuad.fZ3 - tQuad.fZ1;
  178.  
  179.   // Compute normal for the first triangle
  180.   CrossProduct(@edge1[0], @edge2[0], @normal[0]);
  181.  
  182.   // Normalize the normal
  183.   Normalize(@normal[0]);
  184.  
  185.   // Set normal for the quad
  186.   // glNormal3fv(@normal[0]);
  187. end;
  188. procedure DrawLine2D(x1, y1, x2, y2: Single);
  189. begin
  190.   // OpenGL-like drawing a 2D line
  191.   // This is a placeholder for OpenGL drawing code
  192.   // glBegin(GL_LINES);
  193.   // glVertex2f(x1, y1);
  194.   // glVertex2f(x2, y2);
  195.   // glEnd;
  196. end;
  197.  
  198. procedure DrawQuad3D(var tQuad: LineType4Struct);
  199. begin
  200.   // OpenGL-like drawing a 3D quad
  201.   // glBegin(GL_QUADS);
  202.   // glVertex3f(tQuad.fX1, tQuad.fY1, tQuad.fZ1);
  203.   // glVertex3f(tQuad.fX2, tQuad.fY2, tQuad.fZ2);
  204.   // glVertex3f(tQuad.fX3, tQuad.fY3, tQuad.fZ3);
  205.   // glVertex3f(tQuad.fX4, tQuad.fY4, tQuad.fZ4);
  206.   // glEnd;
  207. end;
  208.  
  209. procedure DrawQuadTextured3D(var tQuad: LineType4Struct);
  210. begin
  211.   // OpenGL-like drawing a 3D textured quad
  212.   // glBegin(GL_QUADS);
  213.   // glTexCoord2f(0.0, 0.0);
  214.   // glVertex3f(tQuad.fX1, tQuad.fY1, tQuad.fZ1);
  215.  
  216.   // glTexCoord2f(1.0, 0.0);
  217.   // glVertex3f(tQuad.fX2, tQuad.fY2, tQuad.fZ2);
  218.  
  219.   // glTexCoord2f(1.0, 1.0);
  220.   // glVertex3f(tQuad.fX3, tQuad.fY3, tQuad.fZ3);
  221.  
  222.   // glTexCoord2f(0.0, 1.0);
  223.   // glVertex3f(tQuad.fX4, tQuad.fY4, tQuad.fZ4);
  224.  
  225.   // glEnd;
  226. end;
  227.  
  228. procedure DrawTriangle3D(x1, y1, z1, x2, y2, z2, x3, y3, z3: Single);
  229. begin
  230.   // OpenGL-like drawing a 3D triangle
  231.   // glBegin(GL_TRIANGLES);
  232.   // glVertex3f(x1, y1, z1);
  233.   // glVertex3f(x2, y2, z2);
  234.   // glVertex3f(x3, y3, z3);
  235.   // glEnd;
  236. end;
  237.  
  238. procedure DrawCube(x, y, z, size: Single);
  239. var
  240.   halfSize: Single;
  241.   quad: LineType4Struct;
  242. begin
  243.   halfSize := size / 2.0;
  244.  
  245.   // Front face
  246.   quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z + halfSize;
  247.   quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z + halfSize;
  248.   quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z + halfSize;
  249.   quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z + halfSize;
  250.   DrawQuad3D(quad);
  251.  
  252.   // Back face
  253.   quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z - halfSize;
  254.   quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z - halfSize;
  255.   quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z - halfSize;
  256.   quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z - halfSize;
  257.   DrawQuad3D(quad);
  258.  
  259.   // Other faces (left, right, top, bottom) follow the same approach
  260.   // ...
  261. end;
  262. procedure DrawCube(x, y, z, size: Single);
  263. var
  264.   halfSize: Single;
  265.   quad: LineType4Struct;
  266. begin
  267.   halfSize := size / 2.0;
  268.  
  269.   // Front face
  270.   quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z + halfSize;
  271.   quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z + halfSize;
  272.   quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z + halfSize;
  273.   quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z + halfSize;
  274.   DrawQuad3D(quad);
  275.  
  276.   // Back face
  277.   quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z - halfSize;
  278.   quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z - halfSize;
  279.   quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z - halfSize;
  280.   quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z - halfSize;
  281.   DrawQuad3D(quad);
  282.  
  283.   // Left face
  284.   quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z - halfSize;
  285.   quad.fX2 := x - halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z + halfSize;
  286.   quad.fX3 := x - halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z + halfSize;
  287.   quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z - halfSize;
  288.   DrawQuad3D(quad);
  289.  
  290.   // Right face
  291.   quad.fX1 := x + halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z - halfSize;
  292.   quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z + halfSize;
  293.   quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z + halfSize;
  294.   quad.fX4 := x + halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z - halfSize;
  295.   DrawQuad3D(quad);
  296.  
  297.   // Top face
  298.   quad.fX1 := x - halfSize; quad.fY1 := y + halfSize; quad.fZ1 := z - halfSize;
  299.   quad.fX2 := x + halfSize; quad.fY2 := y + halfSize; quad.fZ2 := z - halfSize;
  300.   quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z + halfSize;
  301.   quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z + halfSize;
  302.   DrawQuad3D(quad);
  303.  
  304.   // Bottom face
  305.   quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z - halfSize;
  306.   quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z - halfSize;
  307.   quad.fX3 := x + halfSize; quad.fY3 := y - halfSize; quad.fZ3 := z + halfSize;
  308.   quad.fX4 := x - halfSize; quad.fY4 := y - halfSize; quad.fZ4 := z + halfSize;
  309.   DrawQuad3D(quad);
  310. end;
  311. procedure DrawCubeTextured(x, y, z, size: Single);
  312. var
  313.   halfSize: Single;
  314.   quad: LineType4Struct;
  315. begin
  316.   halfSize := size / 2.0;
  317.  
  318.   // Front face
  319.   quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z + halfSize;
  320.   quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z + halfSize;
  321.   quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z + halfSize;
  322.   quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z + halfSize;
  323.   DrawQuadTextured3D(quad);
  324.  
  325.   // Back face (similarly for the rest of the cube)
  326.   quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z - halfSize;
  327.   quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z - halfSize;
  328.   quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z - halfSize;
  329.   quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z - halfSize;
  330.   DrawQuadTextured3D(quad);
  331.  
  332.   // Left, right, top, bottom faces would follow in the same fashion
  333.   // ...
  334. end;
  335.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement