| 1 |
{ |
| 2 |
Copyright 2023-2023 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ TCastleTransform descendant that renders unlit mesh using TCastleRenderUnlitMesh. } |
| 17 |
unit GameMyMesh; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses SysUtils, |
| 22 |
CastleRenderPrimitives, CastleBoxes, CastleTransform; |
| 23 |
|
| 24 |
type |
| 25 |
{ Mesh rendered using TCastleRenderUnlitMesh, not using TCastleScene. |
| 26 |
This is not generally advised, TCastleScene has much more features |
| 27 |
and is easier to use than TCastleRenderUnlitMesh. |
| 28 |
We do this only to test TCastleRenderUnlitMesh with rcForceFixedFunction here. } |
| 29 |
TMyMesh = class(TCastleTransform) |
| 30 |
strict private |
| 31 |
Mesh: TCastleRenderUnlitMesh; |
| 32 |
public |
| 33 |
procedure LocalRender(const Params: TRenderParams); override; |
| 34 |
procedure GLContextClose; override; |
| 35 |
function LocalBoundingBox: TBox3D; override; |
| 36 |
end; |
| 37 |
|
| 38 |
implementation |
| 39 |
|
| 40 |
uses CastleVectors, CastleRenderContext, CastleColors, CastleGLUtils; |
| 41 |
|
| 42 |
{ TMyMesh -------------------------------------------------------------------- } |
| 43 |
|
| 44 |
function TMyMesh.LocalBoundingBox: TBox3D; |
| 45 |
begin |
| 46 |
Result := inherited; |
| 47 |
Result := Result + Box3D( |
| 48 |
Vector3(-1, -1, -1), |
| 49 |
Vector3( 1, 1, 1) |
| 50 |
); |
| 51 |
end; |
| 52 |
|
| 53 |
procedure TMyMesh.LocalRender(const Params: TRenderParams); |
| 54 |
|
| 55 |
procedure CreateMesh; |
| 56 |
begin |
| 57 |
Mesh := TCastleRenderUnlitMesh.Create(true); |
| 58 |
Mesh.SetVertexes([ |
| 59 |
Vector4(-1, -1, -1, 1), |
| 60 |
Vector4( 1, -1, -1, 1), |
| 61 |
Vector4( 1, 1, -1, 1), |
| 62 |
Vector4(-1, 1, -1, 1), |
| 63 |
|
| 64 |
Vector4(-1, -1, 1, 1), |
| 65 |
Vector4( 1, -1, 1, 1), |
| 66 |
Vector4( 1, 1, 1, 1), |
| 67 |
Vector4(-1, 1, 1, 1) |
| 68 |
], false); |
| 69 |
Mesh.SetIndexes([ |
| 70 |
// line loop on Z = -1 |
| 71 |
0, 1, |
| 72 |
1, 2, |
| 73 |
2, 3, |
| 74 |
3, 0, |
| 75 |
|
| 76 |
// line loop on Z = 1 |
| 77 |
4, 5, |
| 78 |
5, 6, |
| 79 |
6, 7, |
| 80 |
7, 4, |
| 81 |
|
| 82 |
// connect Z = -1 with Z = 1 |
| 83 |
0, 4, |
| 84 |
1, 5, |
| 85 |
2, 6, |
| 86 |
3, 7 |
| 87 |
]); |
| 88 |
Mesh.Color := Yellow; |
| 89 |
end; |
| 90 |
|
| 91 |
var |
| 92 |
SavedDepthTest: Boolean; |
| 93 |
SavedLineWidth: Single; |
| 94 |
begin |
| 95 |
inherited; |
| 96 |
SavedDepthTest := RenderContext.DepthTest; |
| 97 |
SavedLineWidth := RenderContext.LineWidth; |
| 98 |
RenderContext.DepthTest := true; |
| 99 |
RenderContext.LineWidth := 5; |
| 100 |
|
| 101 |
if Mesh = nil then |
| 102 |
CreateMesh; |
| 103 |
Mesh.ModelViewProjection := RenderContext.ProjectionMatrix * |
| 104 |
Params.RenderingCamera.CurrentMatrix * WorldTransform; |
| 105 |
Mesh.Render(pmLines); |
| 106 |
|
| 107 |
RenderContext.DepthTest := SavedDepthTest; |
| 108 |
RenderContext.LineWidth := SavedLineWidth; |
| 109 |
end; |
| 110 |
|
| 111 |
procedure TMyMesh.GLContextClose; |
| 112 |
begin |
| 113 |
FreeAndNil(Mesh); |
| 114 |
inherited; |
| 115 |
end; |
| 116 |
|
| 117 |
end. |