/usr/src/castle-game-engine-6.4/x3d/x3dloadinternalobj.pas is in castle-game-engine-src 6.4+dfsg1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 | {
Copyright 2002-2017 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ Load Wavefront OBJ 3D format.
See [http://www.fileformat.info/format/wavefrontobj/]
and [http://www.fileformat.info/format/material/].
Texture URL is also read from material file. }
unit X3DLoadInternalOBJ;
{$I castleconf.inc}
interface
uses X3DNodes;
function LoadWavefrontOBJ(const URL: string): TX3DRootNode;
implementation
uses SysUtils, Classes, Generics.Collections,
CastleStringUtils, CastleFilesUtils, CastleLog, CastleVectors, CastleUtils,
CastleClassUtils, X3DLoadInternalUtils, CastleURIUtils,
CastleDownload;
type
TWavefrontMaterial = class
Name: string;
AmbientColor, DiffuseColor, SpecularColor, TransmissionColor: TVector3;
IlluminationModel: Cardinal;
Opacity: Single;
SpecularExponent: Single;
Sharpness, IndexOfRefraction: Single;
DiffuseTextureURL: string;
BumpTextureURL: string;
{ Initializes material with default values.
Since Wavefront specification doesn't say what the default values are,
we just assign something along the lines of default VRML material. }
constructor Create(const AName: string);
end;
TWavefrontMaterialList = class({$ifdef CASTLE_OBJFPC}specialize{$endif} TObjectList<TWavefrontMaterial>)
{ Find material with given name, @nil if not found. }
function TryFindName(const Name: string): TWavefrontMaterial;
end;
TWavefrontFace = class
VertIndices, TexCoordIndices, NormalIndices: TLongIntList;
HasTexCoords: boolean;
HasNormals: boolean;
{ Material assigned to this face. @nil means that no material was assigned. }
Material: TWavefrontMaterial;
constructor Create;
destructor Destroy; override;
end;
TWavefrontFaceList = {$ifdef CASTLE_OBJFPC}specialize{$endif} TObjectList<TWavefrontFace>;
{ 3D model in OBJ file format. }
TObject3DOBJ = class
strict private
{ Lists to fill with vertex, tex coord and normal data }
Verts: TVector3List;
TexCoords: TVector2List;
Normals: TVector3List;
FFaces: TWavefrontFaceList;
FMaterials: TWavefrontMaterialList;
public
Coord: TCoordinateNode;
TexCoord: TTextureCoordinateNode;
Normal: TNormalNode;
constructor Create(const URL: string);
destructor Destroy; override;
property Faces: TWavefrontFaceList read FFaces;
property Materials: TWavefrontMaterialList read FMaterials;
end;
EInvalidOBJFile = class(Exception);
{ Read string to TVector3, ignoring invalid ending,
so it handles even incorrect things like '0 0 0c',
see https://github.com/castle-engine/castle-engine/pull/76/ }
function Vector3FromStrPermissive(const S: string): TVector3;
const
OnlyNums = AllChars - ['0'..'9', '.', '-','+','e','E'];
var
SPosition: Integer;
begin
try
Result := Vector3FromStr(S);
except
on E: EConvertError do
begin
SPosition := 1;
Result.Data[0] := StrToFloat(NextToken(S, SPosition));
Result.Data[1] := StrToFloat(NextToken(S, SPosition));
Result.Data[2] := StrToFloat(NextToken(S, SPosition, OnlyNums));
if NextToken(S, SPosition) <> '' then
raise EConvertError.Create('Expected end of data when reading vector from string');
WritelnWarning('Invalid TVector3 format: "%s", ignored the incorrect characters at the end', [S]);
end;
end;
end;
{ TWavefrontMaterial --------------------------------------------------------- }
constructor TWavefrontMaterial.Create(const AName: string);
begin
inherited Create;
Name := AName;
AmbientColor := Vector3(0.2, 0.2, 0.2);
DiffuseColor := Vector3(0.8, 0.8, 0.8);
SpecularColor := Vector3(0, 0, 0);
{ This is not necessarily good, I don't use it anywhere for now }
TransmissionColor := Vector3(0, 0, 0);
{ Blender exported writes such illumination, I guess it's good default }
IlluminationModel := 2;
Opacity := 1.0;
SpecularExponent := 1;
Sharpness := 60;
IndexOfRefraction := 1;
DiffuseTextureURL := '';
BumpTextureURL := '';
end;
{ TWavefrontMaterialList ---------------------------------------------------- }
function TWavefrontMaterialList.TryFindName(const Name: string):
TWavefrontMaterial;
var
I: Integer;
begin
for I := 0 to Count - 1 do
begin
Result := Items[I];
if Result.Name = Name then Exit;
end;
Result := nil;
end;
{ TWavefrontFace ------------------------------------------------------------- }
constructor TWavefrontFace.Create;
begin
inherited;
VertIndices := TLongIntList.Create;
TexCoordIndices := TLongIntList.Create;
NormalIndices := TLongIntList.Create;
end;
destructor TWavefrontFace.Destroy;
begin
FreeAndNil(VertIndices);
FreeAndNil(TexCoordIndices);
FreeAndNil(NormalIndices);
inherited;
end;
{ TObject3DOBJ --------------------------------------------------------------- }
constructor TObject3DOBJ.Create(const URL: string);
var
BasePath: string;
procedure ReadFacesFromOBJLine(const line: string; Material: TWavefrontMaterial);
var
Face: TWavefrontFace;
{ Add indexes of next face vertex based on VertexStr. }
procedure ReadIndices(const VertexStr: string);
var
VertexSeekPos: Integer;
{ Reads and updates VertexStr and VertexSeekPos, and sets
IndiceExists and adds to IndexList. IndiceExists is set to @false
if next indice is indeed empty,
or if we're standing at the end of VertexStr string.
Just call it in sequence to read indices from VertexStr.
Remember that empty indice may be followed by non-empty,
e.g. VectorStr = '2//3' is allowed, and means that vertex
index is 2, there's no texCoord index, and normal index is 3. }
procedure ReadIndex(out IndiceExists: boolean;
const IndexList: TLongIntList; const Count: Cardinal);
var
NewVertexSeekPos: Integer;
Index: Integer;
begin
NewVertexSeekPos := VertexSeekPos;
while (NewVertexSeekPos <= Length(VertexStr)) and
(VertexStr[NewVertexSeekPos] <> '/') do
Inc(NewVertexSeekPos);
IndiceExists := NewVertexSeekPos > VertexSeekPos;
if IndiceExists then
begin
{ get signed Index }
Index := StrToInt(CopyPos(VertexStr, VertexSeekPos, NewVertexSeekPos - 1));
if Index > 0 then
{ we subtract 1, because indexed in OBJ are 1-based and we prefer 0-based }
IndexList.Add(Index - 1) else
if Index < 0 then
begin
Index := Index + Integer(Count);
if Index < 0 then
raise EInvalidOBJFile.Create('Invalid OBJ: Index is < 0 after summing with current count');
IndexList.Add(Index);
end else
raise EInvalidOBJFile.Create('Invalid OBJ: Index is 0 (should be < 0 for relative and > 0 for absolute index)');
end;
{ We add +1 to skip our ending '/' char.
Note that we add this even if VertexSeekPos was already
> Length(VertexStr), but this is harmless. }
VertexSeekPos := NewVertexSeekPos + 1;
end;
var
IndiceHasVertex, IndiceHasTexCoord, IndiceHasNormal: boolean;
begin
VertexSeekPos := 1;
{ read vertex index }
ReadIndex(IndiceHasVertex, Face.VertIndices, Verts.Count);
if not IndiceHasVertex then
raise EInvalidOBJFile.CreateFmt(
'Invalid OBJ vertex indexes "%s"', [VertexStr]);
{ read texCoord index }
ReadIndex(IndiceHasTexCoord, Face.TexCoordIndices, TexCoords.Count);
{ read normal index }
ReadIndex(IndiceHasNormal, Face.NormalIndices, Normals.Count);
{ update Face.HasXxx using IndiceHasXxx }
Face.HasTexCoords := Face.HasTexCoords and IndiceHasTexCoord;
Face.HasNormals := Face.HasNormals and IndiceHasNormal;
end;
var
SeekPos: integer;
function NextVertex: string;
begin
Result := NextToken(Line, SeekPos);
if Result = '' then
raise EInvalidOBJFile.CreateFmt(
'Incomplete OBJ face specification "%s"', [Line]);
while SCharIs(Line, SeekPos, WhiteSpaces) do Inc(SeekPos);
end;
begin
{ ReadIndices will eventually change this to @false, if for any
vertex normal or texCoord will not be present. }
Face := TWavefrontFace.Create;
Face.HasTexCoords := true;
Face.HasNormals := true;
Face.Material := Material;
Faces.Add(Face);
SeekPos := 1;
while SeekPos <= Length(Line) do
ReadIndices(NextVertex);
end;
function ReadTexCoordFromOBJLine(const line: string): TVector2;
var
SeekPos: integer;
begin
SeekPos := 1;
result[0] := StrToFloat(NextToken(line, SeekPos));
result[1] := StrToFloat(NextToken(line, SeekPos));
{ nie uzywamy DeFormat - bo tex coord w OBJ moze byc 3d (z trzema
parametrami) a my uzywamy i tak tylko dwoch pierwszych }
end;
{ Reads single line from Wavefront OBJ or materials file.
This takes care of stripping comments.
If current line is empty or only comment, LineTok = ''.
Otherwise, LineTok <> '' and LineAfterMarker contains the rest of the line. }
procedure ReadLine(Line: string; out LineTok, LineAfterMarker: string);
var
SeekPosAfterMarker: Integer;
begin
Line := STruncateHash(Line);
{ calculate first line token }
SeekPosAfterMarker := 1;
LineTok := NextToken(Line, SeekPosAfterMarker);
if LineTok <> '' then
LineAfterMarker := Trim(SEnding(Line, SeekPosAfterMarker));
end;
procedure ReadMaterials(const URL: string);
var
IsMaterial: boolean;
function ReadRGBColor(const Line: string): TVector3;
var
FirstToken: string;
begin
FirstToken := NextTokenOnce(Line);
if SameText(FirstToken, 'xyz') or SameText(FirstToken, 'spectral') then
{ we can't interpret other colors than RGB, so we silently ignore them }
Result := Vector3(1, 1, 1) else
Result := Vector3FromStrPermissive(Line);
end;
procedure CheckIsMaterial(const AttributeName: string);
begin
if not IsMaterial then
raise EInvalidOBJFile.CreateFmt(
'Material not named yet, but it''s %s specified', [AttributeName]);
end;
function FixBumpTextureUrl(const S: string): string;
var
P: Integer;
begin
Result := S;
P := Pos(' -bm', S);
if P <> 0 then
Result := Copy(S, 1, P - 1);
end;
var
F: TTextReader;
LineTok, LineAfterMarker: string;
begin
{ Specification doesn't say what to do when multiple matlib directives
encountered, i.e. when ReadMaterials is called multiple times.
I simply add to Materials list. }
{ is some material in current file ? If false, then Materials is empty
or last material is from some other file. }
IsMaterial := false;
try
F := TTextReader.Create(CombineURI(BasePath, URL));
except
on E: Exception do
begin
WritelnWarning('Wavefront OBJ Material', E.Message);
Exit;
end;
end;
try
while not F.Eof do
begin
ReadLine(F.Readln, LineTok, LineAfterMarker);
if LineTok = '' then Continue;
case ArrayPosText(LineTok, ['newmtl', 'Ka', 'Kd', 'Ks', 'Tf', 'illum',
'd', 'Ns', 'sharpness', 'Ni', 'map_Kd', 'map_bump', 'bump']) of
0: begin
Materials.Add(TWavefrontMaterial.Create(LineAfterMarker));
IsMaterial := true;
end;
1: begin
CheckIsMaterial('ambient color (Ka)');
Materials.Last.AmbientColor := ReadRGBColor(LineAfterMarker);
end;
2: begin
CheckIsMaterial('diffuse color (Kd)');
Materials.Last.DiffuseColor := ReadRGBColor(LineAfterMarker);
end;
3: begin
CheckIsMaterial('specular color (Ks)');
Materials.Last.SpecularColor := ReadRGBColor(LineAfterMarker);
end;
4: begin
CheckIsMaterial('transmission filter color (Tf)');
Materials.Last.TransmissionColor := ReadRGBColor(LineAfterMarker);
end;
5: begin
CheckIsMaterial('illumination model (illum)');
Materials.Last.IlluminationModel := StrToInt(LineAfterMarker);
end;
6: begin
CheckIsMaterial('dissolve (d)');
Materials.Last.Opacity := StrToFloat(LineAfterMarker);
end;
7: begin
CheckIsMaterial('specular exponent (Ns)');
Materials.Last.SpecularExponent := StrToFloat(LineAfterMarker);
end;
8: begin
CheckIsMaterial('sharpness');
Materials.Last.Sharpness := StrToFloat(LineAfterMarker);
end;
9: begin
CheckIsMaterial('index of refraction (Ni)');
Materials.Last.IndexOfRefraction := StrToFloat(LineAfterMarker);
end;
10:begin
CheckIsMaterial('diffuse map (map_Kd)');
Materials.Last.DiffuseTextureURL := LineAfterMarker;
end;
11, 12:
begin
CheckIsMaterial('bump map (map_bump,bump)');
Materials.Last.BumpTextureURL := FixBumpTextureUrl(LineAfterMarker);
end;
else { we ignore other linetoks };
end;
end;
finally FreeAndNil(F) end;
end;
var
F: TTextReader;
LineTok, LineAfterMarker: string;
//GroupName: string;
UsedMaterial: TWavefrontMaterial;
begin
inherited Create;
BasePath := AbsoluteURI(URL);
FFaces := TWavefrontFaceList.Create(true);
FMaterials := TWavefrontMaterialList.Create(true);
UsedMaterial := nil;
F := TTextReader.Create(URL);
try
Coord := TCoordinateNode.Create('ObjCoordinates');
TexCoord := TTextureCoordinateNode.Create('ObjTextureCoordinates');
Normal := TNormalNode.Create('ObjNormals');
Verts := Coord.FdPoint.Items;
TexCoords := TexCoord.FdPoint.Items;
Normals := Normal.FdVector.Items;
while not F.Eof do
begin
ReadLine(F.Readln, LineTok, LineAfterMarker);
{ LineTok = '' means "this line is a comment" }
if LineTok = '' then Continue;
{ specialized token line parsing }
case ArrayPosText(lineTok, ['v', 'vt', 'f', 'vn', 'g', 'mtllib', 'usemtl']) of
0: Verts.Add(Vector3FromStr(lineAfterMarker));
1: TexCoords.Add(ReadTexCoordFromOBJLine(lineAfterMarker));
2: ReadFacesFromOBJLine(lineAfterMarker, UsedMaterial);
3: Normals.Add(Vector3FromStr(lineAfterMarker));
4: {GroupName := LineAfterMarker};
5: ReadMaterials(LineAfterMarker);
6: begin
if LineAfterMarker = '(null)' then
UsedMaterial := nil else
begin
UsedMaterial := Materials.TryFindName(LineAfterMarker);
if UsedMaterial = nil then
WritelnWarning('Wavefront OBJ', Format('Unknown material name "%s"',
[LineAfterMarker]));
end;
end;
else { we ignore other linetoks };
end;
end;
finally FreeAndNil(F) end;
end;
destructor TObject3DOBJ.Destroy;
begin
FreeIfUnusedAndNil(Coord);
FreeIfUnusedAndNil(TexCoord);
FreeIfUnusedAndNil(Normal);
FreeAndNil(FFaces);
FreeAndNil(FMaterials);
inherited;
end;
{ LoadWavefrontOBJ ----------------------------------------------------------- }
function LoadWavefrontOBJ(const URL: string): TX3DRootNode;
const
{ When constructing large index arrays, we use larger Capacity
to make them faster.
TODO: would be better to allocate necessary space once, by assigning Count. }
IndicesCapacity = 100;
var
BaseUrl: string;
function MatOBJNameToX3DName(const MatOBJName: string): string;
begin
Result := 'Material_' + ToX3DName(MatOBJName);
end;
function MaterialToX3D(const Material: TWavefrontMaterial): TAppearanceNode;
var
Mat: TMaterialNode;
Texture: TImageTextureNode;
begin
Result := TAppearanceNode.Create(
MatOBJNameToX3DName(Material.Name), BaseUrl);
Mat := TMaterialNode.Create('', BaseUrl);
Result.Material := Mat;
Mat.AmbientIntensity := AmbientIntensity(
Material.AmbientColor, Material.DiffuseColor);
Mat.DiffuseColor := Material.DiffuseColor;
Mat.SpecularColor := Material.SpecularColor;
Mat.Transparency := 1 - Material.Opacity;
Mat.Shininess := Material.SpecularExponent / 128.0;
if Material.DiffuseTextureURL <> '' then
begin
Texture := TImageTextureNode.Create('', BaseUrl);
Result.Texture := Texture;
Texture.SetUrl([SearchTextureFile(BaseUrl, Material.DiffuseTextureURL)]);
if Material.BumpTextureURL <> '' then
begin
Texture := TImageTextureNode.Create('', BaseUrl);
Result.NormalMap := Texture;
Texture.SetUrl([SearchTextureFile(BaseUrl, Material.BumpTextureURL)]);
end;
end;
end;
var
Obj: TObject3DOBJ;
Faces: TIndexedFaceSetNode;
I: integer;
FacesWithTexCoord, FacesWithNormal: boolean;
FacesWithMaterial: TWavefrontMaterial;
Appearances: TX3DNodeList;
Shape: TShapeNode;
begin
BaseUrl := AbsoluteURI(URL);
Appearances := nil;
Result := TX3DRootNode.Create('', BaseUrl);
try
Result.HasForceVersion := true;
Result.ForceVersion := X3DVersion;
Obj := TObject3DOBJ.Create(URL);
try
Appearances := TX3DNodeList.Create(false);
Appearances.Count := Obj.Materials.Count;
for I := 0 to Obj.Materials.Count - 1 do
Appearances[I] := MaterialToX3D(Obj.Materials[I]);
I := 0;
while I < Obj.Faces.Count do
begin
FacesWithTexCoord := Obj.Faces[I].HasTexCoords;
FacesWithNormal := Obj.Faces[I].HasNormals;
FacesWithMaterial := Obj.Faces[I].Material;
Shape := TShapeNode.Create('', BaseUrl);
Result.AddChildren(Shape);
if FacesWithMaterial <> nil then
begin
{ We find appearance by name, using FindName. We're sure
that we will find it --- because we added them all to Appearances. }
Shape.Appearance := Appearances.FindName(
MatOBJNameToX3DName(FacesWithMaterial.Name)) as TAppearanceNode;
end else
Shape.Material := TMaterialNode.Create('', BaseUrl);
{ We don't do anything special for the case when FacesWithMaterial = nil
and FacesWithTexCoord = true. This may be generated e.g. by Blender
exporter, if Blender object has UV texture coords but no material.
We will then just output VRML/X3D texCoord
field, but without texture it will not have any effect.
This is natural, and there's no reason for now to do anything else. }
Faces := TIndexedFaceSetNode.Create('', BaseUrl);
Shape.Geometry := Faces;
Faces.CreaseAngle := NiceCreaseAngle;
{ faces may be concave, see https://sourceforge.net/p/castle-engine/tickets/20
and https://sourceforge.net/p/castle-engine/tickets/19/ }
Faces.Convex := false;
Faces.Solid := false;
Faces.Coord := Obj.Coord;
Faces.FdCoordIndex.Items.Clear;
Faces.FdCoordIndex.Items.Capacity := IndicesCapacity;
if FacesWithTexCoord then
begin
Faces.TexCoord := Obj.TexCoord;
Faces.FdTexCoordIndex.Items.Clear;
Faces.FdTexCoordIndex.Items.Capacity := IndicesCapacity;
end;
if FacesWithNormal then
begin
Faces.Normal := Obj.Normal;
Faces.FdNormalIndex.Items.Clear;
Faces.FdNormalIndex.Items.Capacity := IndicesCapacity;
end;
{ We add Faces as long as FacesWithXxx parameters stay the same.
We know that at least the next face is Ok. }
repeat
Faces.FdCoordIndex.Items.AddRange(Obj.Faces[I].VertIndices);
Faces.FdCoordIndex.Items.Add(-1);
if FacesWithTexCoord then
begin
Faces.FdTexCoordIndex.Items.AddRange(Obj.Faces[I].TexCoordIndices);
Faces.FdTexCoordIndex.Items.Add(-1);
end;
if FacesWithNormal then
begin
Faces.FdNormalIndex.Items.AddRange(Obj.Faces[I].NormalIndices);
Faces.FdNormalIndex.Items.Add(-1);
end;
Inc(I);
until (I >= Obj.Faces.Count) or
(FacesWithTexCoord <> Obj.Faces[I].HasTexCoords) or
(FacesWithNormal <> Obj.Faces[I].HasNormals) or
(FacesWithMaterial <> Obj.Faces[I].Material);
end;
for I := 0 to Appearances.Count - 1 do
Appearances[I].FreeIfUnused;
finally
FreeAndNil(Obj);
FreeAndNil(Appearances);
end;
except FreeAndNil(result); raise end;
end;
end.
|