-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathformgl.pas
151 lines (125 loc) · 3.44 KB
/
formgl.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
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
unit formgl;
interface
uses
Windows, Classes, Forms, gl, glu, Controls, Graphics;
type
TglForm = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
protected
{ Private declarations }
m_hRC: HGLRC; // rendering context
m_hDC: HDC; // device context
m_PFD: PIXELFORMATDESCRIPTOR;
m_fFOV: single;
m_fNear, m_fFar: double;
procedure SetupPixelFormat(dc: HDC);
procedure MyResize(Sender: TObject);
public
{ Public declarations }
procedure SetFOV(fFOV: glfloat);
procedure SetDistances(fNear, fFar: double);
procedure SetPerspective(fFOV, fNear, fFar: double);
procedure Flip;
property FOV: glfloat Read m_fFOV Write SetFOV;
end;
implementation
{$R *.dfm}
procedure TglForm.Flip;
begin
glFlush();
SwapBuffers(m_hDC);
end;
procedure TglForm.SetPerspective(fFOV, fNear, fFar: double);
begin
m_fFOV := fFOV;
m_fNear := fNear;
m_fFar := fFar;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(m_fFOV, ClientWidth / ClientHeight, m_fNear, m_fFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
end;
procedure TglForm.SetFOV(fFOV: glfloat);
begin
SetPerspective(fFOV, m_fNear, m_fFar);
end;
procedure TglForm.SetDistances(fNear, fFar: double);
begin
SetPerspective(m_fFOV, fNear, fFar);
end;
procedure TglForm.SetupPixelFormat(dc: HDC);
var
nPixelFormat: integer;
begin
fillchar(m_PFD, sizeof(m_PFD), 0);
m_PFD.nSize := sizeof(PIXELFORMATDESCRIPTOR);
m_PFD.nVersion := 1;
m_PFD.dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
m_PFD.iPixelType := PFD_TYPE_RGBA;
m_PFD.cColorBits := 16;
m_PFD.cDepthBits := 16;
m_PFD.iLayerType := PFD_MAIN_PLANE;
nPixelFormat := ChoosePixelFormat(m_hDC, @m_PFD);
SetPixelFormat(m_hDC, nPixelFormat, @m_PFD);
end;
procedure TglForm.MyResize(Sender: TObject);
begin
if (m_hRC <> 0) then
begin
if (ClientHeight = 0) then
begin
ClientHeight := 1;
end;
glViewport(0, 0, ClientWidth, ClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(m_fFOV, ClientWidth / ClientHeight, m_fNear, m_fFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
end;
end;
procedure TglForm.FormCreate(Sender: TObject);
begin
Parent := nil;
//OnResize := MyResize;
m_fFOV := 65.0;
m_fNear := 1.0;
m_fFar := 5000.0;
m_hDC := GetDC(Handle);
SetupPixelFormat(m_hDC);
m_hRC := wglCreateContext(m_hDC);
wglMakeCurrent(m_hDC, m_hRC);
glClearColor(1.0, 1.0, 1.0, 0.0);
glPointSize(3.0);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
glLineWidth(2.5);
ClientWidth := 320;
ClientHeight := 240;
Top := 0;
Left := 500;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-320.0, 320.0, 240.0, -240.0, -1.0, 1.0);
//glTranslatef(0.5, 0.5, 0.0);
glOrtho(-640.0, 640.0, 480.0, -480.0, -1.0, 1.0);
glTranslatef(-640, 240, 0.0);
end;
procedure TglForm.FormDestroy(Sender: TObject);
begin
if (m_hDC <> 0) then
begin
wglMakeCurrent(m_hDC, 0);
wglDeleteContext(m_hRC);
ReleaseDC(Handle, m_hDC);
end;
m_hDC := 0;
m_hRC := 0;
end;
end.