File: v3dsceneboxes.pas

package info (click to toggle)
castle-model-viewer 5.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 58,588 kB
  • sloc: pascal: 654,327; sh: 314; xml: 282; makefile: 133
file content (100 lines) | stat: -rw-r--r-- 3,263 bytes parent folder | download
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
{
  Copyright 2006-2023 Michalis Kamburelis.

  This file is part of "castle-model-viewer".

  "castle-model-viewer" is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  "castle-model-viewer" 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.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with "castle-model-viewer"; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

  ----------------------------------------------------------------------------
}

{ glDrawCornerMarkers utility. }
unit V3DSceneBoxes;

{$I v3dsceneconf.inc}

interface

uses CastleBoxes, CastleVectors, CastleColors;

{ Draw corner markers (3 lines) at the 8 corners of the box.
  Proportion is the fraction of the box length, the marker extends too. }
procedure glDrawCornerMarkers(const Box: TBox3D;
  const Color: TCastleColor; const ModelViewProjection: TMatrix4);

implementation

uses SysUtils,
  CastleRenderPrimitives, CastleGLUtils;

procedure glDrawCornerMarkers(const Box: TBox3D;
  const Color: TCastleColor; const ModelViewProjection: TMatrix4);
const
  Proportion = 0.1;
var
  Vertexes: TVector4List;

  procedure glDrawCorners(const minx, miny, minz, maxx, maxy, maxz: Single);

    procedure glDrawCornerLines(const x, y, z, dx, dy, dz: Single);
    begin
      Vertexes.AddRange([
        Vector4(x     , y     , z     , 1),
        Vector4(x + dx, y     , z     , 1),
        Vector4(x     , y     , z     , 1),
        Vector4(x     , y + dy, z     , 1),
        Vector4(x     , y     , z     , 1),
        Vector4(x     , y     , z + dz, 1)
      ]);
    end;

  var
    Xlength, Ylength, Zlength: Single;
  begin
    Xlength := (maxx - minx) * Proportion;
    Ylength := (maxy - miny) * Proportion;
    Zlength := (maxz - minz) * Proportion;

    glDrawCornerLines(minx, miny, minz,  Xlength,  Ylength,  Zlength);
    glDrawCornerLines(minx, miny, maxz,  Xlength,  Ylength, -Zlength);
    glDrawCornerLines(minx, maxy, minz,  Xlength, -Ylength,  Zlength);
    glDrawCornerLines(minx, maxy, maxz,  Xlength, -Ylength, -Zlength);
    glDrawCornerLines(maxx, miny, minz, -Xlength,  Ylength,  Zlength);
    glDrawCornerLines(maxx, miny, maxz, -Xlength,  Ylength, -Zlength);
    glDrawCornerLines(maxx, maxy, minz, -Xlength, -Ylength,  Zlength);
    glDrawCornerLines(maxx, maxy, maxz, -Xlength, -Ylength, -Zlength);
  end;

var
  Mesh: TCastleRenderUnlitMesh;
begin
  Vertexes := TVector4List.Create;
  try
    glDrawCorners(
      Box.Data[0].X, Box.Data[0].Y, Box.Data[0].Z,
      Box.Data[1].X, Box.Data[1].Y, Box.Data[1].Z
    );

    Mesh := TCastleRenderUnlitMesh.Create(true);
    try
      Mesh.ModelViewProjection := ModelViewProjection;
      Mesh.Color := Color;
      Mesh.SetVertexes(Vertexes, false);
      Mesh.Render(pmLines);
    finally FreeAndNil(Mesh) end;
  finally FreeAndNil(Vertexes) end;
end;

end.