Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program ModelLoader;
- uses
- SysUtils, Classes, crt, Math;
- const
- cScale = 1/20;
- type
- TPartStruct = record
- wColour: LongInt;
- bType: LongInt;
- end;
- TModelList = array of TPartStruct;
- PDATFile = ^TDATFile;
- TDATFile = record
- tParts: array of TPartStruct;
- iPartCount: LongInt;
- end;
- var
- g_sFilenames: string;
- g_sFilesToLoad: string;
- g_ModelCount: LongInt;
- g_tModels: array of TModelList;
- function LoadModel(pFile: PChar; sFilename: string = ''; iModelIndex: LongInt = -1; iLoadDependencies: Byte = 1): PDATFile;
- var
- iLastPart, iLimitParts, iFailed, iLineNum, iType, iColour, iResu: LongInt;
- pT: PDATFile;
- RecursionLevel, iTotalLines, iTotalParts: LongInt;
- pNew: Pointer;
- begin
- iLastPart := 0;
- iLimitParts := -1;
- iFailed := 0;
- iLineNum := 1;
- pT := nil;
- RecursionLevel := 0;
- iTotalLines := 0;
- iTotalParts := 0;
- Inc(RecursionLevel);
- if iModelIndex < 0 then
- begin
- iModelIndex := g_ModelCount;
- SetLength(g_tModels, g_ModelCount + 1);
- Inc(g_ModelCount);
- end;
- repeat
- if iLastPart > iLimitParts then
- begin
- Inc(iLimitParts, 4096);
- ReallocMem(pT, SizeOf(TDATFile) + (iLimitParts + 1) * SizeOf(TPartStruct));
- if pT = nil then
- begin
- WriteLn('Failed to allocate memory to load file');
- iFailed := 1;
- Break;
- end;
- end;
- // Simulate reading and processing model file
- iResu := Random(10); // Placeholder for actual reading function
- if iResu = 0 then
- Continue;
- // Simulate checking for model type and color
- iType := Random(3); // Random value to simulate the process
- if iType <> 0 then
- begin
- iColour := Random(16);
- pT^.tParts[iLastPart].wColour := iColour;
- pT^.tParts[iLastPart].bType := iType;
- end;
- Inc(iLastPart);
- until False;
- Dec(RecursionLevel);
- if iFailed = 1 then
- begin
- WriteLn('Failure during file loading');
- Exit(nil);
- end;
- pT^.iPartCount := iLastPart;
- Result := pT;
- end;
- begin
- Randomize;
- g_sFilenames := '';
- g_sFilesToLoad := '';
- g_ModelCount := 0;
- SetLength(g_tModels, 1);
- WriteLn('Loading model...');
- LoadModel(nil, 'testfile.dat');
- end.
- uses crt, sysutils, math;
- type
- SinglePtr = ^Single;
- LineType2Struct = record
- fX1, fY1, fZ1: Single;
- fX2, fY2, fZ2: Single;
- end;
- LineType4Struct = record
- fX1, fY1, fZ1: Single;
- fX2, fY2, fZ2: Single;
- fX3, fY3, fZ3: Single;
- fX4, fY4, fZ4: Single;
- end;
- procedure CrossProduct(v1, v2, result: SinglePtr);
- begin
- result^[0] := v1^[1] * v2^[2] - v1^[2] * v2^[1];
- result^[1] := v1^[2] * v2^[0] - v1^[0] * v2^[2];
- result^[2] := v1^[0] * v2^[1] - v1^[1] * v2^[0];
- end;
- procedure Normalize(v: SinglePtr);
- var
- length: Single;
- begin
- length := Sqrt(v^[0] * v^[0] + v^[1] * v^[1] + v^[2] * v^[2]);
- if length <> 0.0 then
- begin
- v^[0] := v^[0] / length;
- v^[1] := v^[1] / length;
- v^[2] := v^[2] / length;
- end;
- end;
- procedure SetLineNormal(var tLine: LineType2Struct);
- var
- direction, ref, normal: array[0..2] of Single;
- normalScale: Single;
- begin
- direction[0] := tLine.fX2 - tLine.fX1;
- direction[1] := tLine.fY2 - tLine.fY1;
- direction[2] := tLine.fZ2 - tLine.fZ1;
- ref[0] := 0.0;
- ref[1] := 1.0;
- ref[2] := 0.0;
- CrossProduct(@direction[0], @ref[0], @normal[0]);
- Normalize(@normal[0]);
- normalScale := 0.5;
- normal[0] := normal[0] * normalScale;
- normal[1] := normal[1] * normalScale;
- normal[2] := normal[2] * normalScale;
- // In Pascal, you can use OpenGL to set the normal vector like this
- // but this would require the OpenGL unit.
- // glNormal3fv(@normal[0]);
- end;
- procedure SetQuadNormal(var tQuad: LineType4Struct);
- var
- edge1, edge2, normal: array[0..2] of Single;
- begin
- edge1[0] := tQuad.fX2 - tQuad.fX1;
- edge1[1] := tQuad.fY2 - tQuad.fY1;
- edge1[2] := tQuad.fZ2 - tQuad.fZ1;
- edge2[0] := tQuad.fX3 - tQuad.fX1;
- edge2[1] := tQuad.fY3 - tQuad.fY1;
- edge2[2] := tQuad.fZ3 - tQuad.fZ1;
- // Compute normal for the first triangle
- CrossProduct(@edge1[0], @edge2[0], @normal[0]);
- // Normalize the normal
- Normalize(@normal[0]);
- // Set normal for the quad
- // glNormal3fv(@normal[0]);
- end;
- procedure DrawLine2D(x1, y1, x2, y2: Single);
- begin
- // OpenGL-like drawing a 2D line
- // This is a placeholder for OpenGL drawing code
- // glBegin(GL_LINES);
- // glVertex2f(x1, y1);
- // glVertex2f(x2, y2);
- // glEnd;
- end;
- procedure DrawQuad3D(var tQuad: LineType4Struct);
- begin
- // OpenGL-like drawing a 3D quad
- // glBegin(GL_QUADS);
- // glVertex3f(tQuad.fX1, tQuad.fY1, tQuad.fZ1);
- // glVertex3f(tQuad.fX2, tQuad.fY2, tQuad.fZ2);
- // glVertex3f(tQuad.fX3, tQuad.fY3, tQuad.fZ3);
- // glVertex3f(tQuad.fX4, tQuad.fY4, tQuad.fZ4);
- // glEnd;
- end;
- procedure DrawQuadTextured3D(var tQuad: LineType4Struct);
- begin
- // OpenGL-like drawing a 3D textured quad
- // glBegin(GL_QUADS);
- // glTexCoord2f(0.0, 0.0);
- // glVertex3f(tQuad.fX1, tQuad.fY1, tQuad.fZ1);
- // glTexCoord2f(1.0, 0.0);
- // glVertex3f(tQuad.fX2, tQuad.fY2, tQuad.fZ2);
- // glTexCoord2f(1.0, 1.0);
- // glVertex3f(tQuad.fX3, tQuad.fY3, tQuad.fZ3);
- // glTexCoord2f(0.0, 1.0);
- // glVertex3f(tQuad.fX4, tQuad.fY4, tQuad.fZ4);
- // glEnd;
- end;
- procedure DrawTriangle3D(x1, y1, z1, x2, y2, z2, x3, y3, z3: Single);
- begin
- // OpenGL-like drawing a 3D triangle
- // glBegin(GL_TRIANGLES);
- // glVertex3f(x1, y1, z1);
- // glVertex3f(x2, y2, z2);
- // glVertex3f(x3, y3, z3);
- // glEnd;
- end;
- procedure DrawCube(x, y, z, size: Single);
- var
- halfSize: Single;
- quad: LineType4Struct;
- begin
- halfSize := size / 2.0;
- // Front face
- quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z + halfSize;
- quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z + halfSize;
- quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z + halfSize;
- quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z + halfSize;
- DrawQuad3D(quad);
- // Back face
- quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z - halfSize;
- quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z - halfSize;
- quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z - halfSize;
- quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z - halfSize;
- DrawQuad3D(quad);
- // Other faces (left, right, top, bottom) follow the same approach
- // ...
- end;
- procedure DrawCube(x, y, z, size: Single);
- var
- halfSize: Single;
- quad: LineType4Struct;
- begin
- halfSize := size / 2.0;
- // Front face
- quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z + halfSize;
- quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z + halfSize;
- quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z + halfSize;
- quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z + halfSize;
- DrawQuad3D(quad);
- // Back face
- quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z - halfSize;
- quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z - halfSize;
- quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z - halfSize;
- quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z - halfSize;
- DrawQuad3D(quad);
- // Left face
- quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z - halfSize;
- quad.fX2 := x - halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z + halfSize;
- quad.fX3 := x - halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z + halfSize;
- quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z - halfSize;
- DrawQuad3D(quad);
- // Right face
- quad.fX1 := x + halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z - halfSize;
- quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z + halfSize;
- quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z + halfSize;
- quad.fX4 := x + halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z - halfSize;
- DrawQuad3D(quad);
- // Top face
- quad.fX1 := x - halfSize; quad.fY1 := y + halfSize; quad.fZ1 := z - halfSize;
- quad.fX2 := x + halfSize; quad.fY2 := y + halfSize; quad.fZ2 := z - halfSize;
- quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z + halfSize;
- quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z + halfSize;
- DrawQuad3D(quad);
- // Bottom face
- quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z - halfSize;
- quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z - halfSize;
- quad.fX3 := x + halfSize; quad.fY3 := y - halfSize; quad.fZ3 := z + halfSize;
- quad.fX4 := x - halfSize; quad.fY4 := y - halfSize; quad.fZ4 := z + halfSize;
- DrawQuad3D(quad);
- end;
- procedure DrawCubeTextured(x, y, z, size: Single);
- var
- halfSize: Single;
- quad: LineType4Struct;
- begin
- halfSize := size / 2.0;
- // Front face
- quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z + halfSize;
- quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z + halfSize;
- quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z + halfSize;
- quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z + halfSize;
- DrawQuadTextured3D(quad);
- // Back face (similarly for the rest of the cube)
- quad.fX1 := x - halfSize; quad.fY1 := y - halfSize; quad.fZ1 := z - halfSize;
- quad.fX2 := x + halfSize; quad.fY2 := y - halfSize; quad.fZ2 := z - halfSize;
- quad.fX3 := x + halfSize; quad.fY3 := y + halfSize; quad.fZ3 := z - halfSize;
- quad.fX4 := x - halfSize; quad.fY4 := y + halfSize; quad.fZ4 := z - halfSize;
- DrawQuadTextured3D(quad);
- // Left, right, top, bottom faces would follow in the same fashion
- // ...
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement