/
UnitRayTraceOpenGL.pas
105 lines (85 loc) · 2.39 KB
/
UnitRayTraceOpenGL.pas
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
unit UnitRayTraceOpenGL;
// Copyright (c) 2019 Sergey-KoRJiK, Belarus
interface
uses
SysUtils, Windows, Classes, OpenGL;
type AGLDouble = array of GLdouble;
type tGLMatrix4d = array[0..15] of GLdouble;
const
IdentityMatrix4d: tGLMatrix4d = (
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
);
type CRayTracer = class
private
ModelMatrix, ProjMatrix: tGLMatrix4d; // 4x4 GLdouble matrix
ViewPort: array[0..3] of GLint; // list of glViewport() input variables
public
constructor CreateRayTracer();
destructor DeleteRayTracer();
//
// ScreenX, ScreenY - "Mouse Click" integer windows display coordinates
// in range [0..Width, 0..Height].
// DepthZ - value of Depth Buffer from [0 to 1];
// "DepthZ = 0" mean pixel at Near Clip Plane, at camera pos;
// "DepthZ = 1" mead pixel at Far Clip Plane, at max render pos;
// lpVertex3d is Pointer on first element of array[0..2] of GLdouble;
procedure UnProjectVertex(const ScreenX, ScreenY, DepthZ: GLdouble;
const lpVertex3d: PGLdouble);
//
procedure UpdateViewPort();
procedure UpdateProjectMatrix();
procedure UpdateModelMatrix();
end;
implementation
constructor CRayTracer.CreateRayTracer();
begin
{$R-}
// Set Identity matrix
Self.ModelMatrix:=IdentityMatrix4d;
Self.ProjMatrix:=IdentityMatrix4d;
// Set Identity Viewport
Self.ViewPort[0]:=0;
Self.ViewPort[1]:=0;
Self.ViewPort[2]:=1;
Self.ViewPort[3]:=1;
{$R+}
end;
destructor CRayTracer.DeleteRayTracer();
begin
{$R-}
{$R+}
end;
procedure CRayTracer.UnProjectVertex(const ScreenX, ScreenY, DepthZ: GLdouble;
const lpVertex3d: PGLdouble);
begin
{$R-}
gluUnProject(
ScreenX, Self.ViewPort[3] - ScreenY - 1, DepthZ,
@Self.ModelMatrix[0], @Self.ProjMatrix[0], @Self.ViewPort[0],
AGLDouble(lpVertex3d)[0],
AGLDouble(lpVertex3d)[1],
AGLDouble(lpVertex3d)[2]);
{$R+}
end;
procedure CRayTracer.UpdateViewPort();
begin
{$R-}
glGetIntegerv(GL_VIEWPORT, @Self.ViewPort[0]);
{$R+}
end;
procedure CRayTracer.UpdateProjectMatrix();
begin
{$R-}
glGetDoublev(GL_PROJECTION_MATRIX, @Self.ProjMatrix[0]);
{$R+}
end;
procedure CRayTracer.UpdateModelMatrix();
begin
{$R-}
glGetDoublev(GL_MODELVIEW_MATRIX, @Self.ModelMatrix[0]);
{$R+}
end;
end.