-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunit1.pas
201 lines (180 loc) · 6.75 KB
/
unit1.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
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
(******************************************************************************)
(* OpenGL Clear Engine ??.??.???? *)
(* *)
(* Version : 0.01 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : All thats needed to start a OpenGL Application *)
(* *)
(* License : See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(* Warranty : There is no warranty, neither in correctness of the *)
(* implementation, nor anything other that could happen *)
(* or go wrong, use at your own risk. *)
(* *)
(* Known Issues: none *)
(* *)
(* History : 0.01 - Initial version *)
(* *)
(******************************************************************************)
Unit Unit1;
{$MODE objfpc}{$H+}
{$DEFINE DebuggMode}
Interface
Uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
ExtCtrls,
OpenGlcontext,
(*
* Kommt ein Linkerfehler wegen OpenGL dann: sudo apt-get install freeglut3-dev
*)
dglOpenGL // http://wiki.delphigl.com/index.php/dglOpenGL.pas
//, uopengl_graphikengine // Die OpenGLGraphikengine ist eine Eigenproduktion von www.Corpsman.de, und kann getrennt auf https://github.com/PascalCorpsman/Examples/tree/master/OpenGL geladen werden.
;
Type
{ TForm1 }
TForm1 = Class(TForm)
OpenGLControl1: TOpenGLControl;
Timer1: TTimer;
Procedure FormCreate(Sender: TObject);
Procedure OpenGLControl1MakeCurrent(Sender: TObject; Var Allow: boolean);
Procedure OpenGLControl1Paint(Sender: TObject);
Procedure OpenGLControl1Resize(Sender: TObject);
Procedure Timer1Timer(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
Procedure Go2d();
Procedure Exit2d();
End;
Var
Form1: TForm1;
Initialized: Boolean = false; // Wenn True dann ist OpenGL initialisiert
Implementation
{$R *.lfm}
{ TForm1 }
Procedure Tform1.Go2d();
Begin
glMatrixMode(GL_PROJECTION);
glPushMatrix(); // Store The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// glOrtho(0, 640, 0, 480, -1, 1); // Set Up An Ortho Screen
glOrtho(0, OpenGLControl1.Width, OpenGLControl1.height, 0, -1, 1); // Set Up An Ortho Screen
glMatrixMode(GL_MODELVIEW);
glPushMatrix(); // Store old Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
End;
Procedure Tform1.Exit2d();
Begin
glMatrixMode(GL_PROJECTION);
glPopMatrix(); // Restore old Projection Matrix
glMatrixMode(GL_MODELVIEW);
glPopMatrix(); // Restore old Projection Matrix
End;
Var
allowcnt: Integer = 0;
Procedure TForm1.OpenGLControl1MakeCurrent(Sender: TObject; Var Allow: boolean);
Begin
If allowcnt > 2 Then Begin
exit;
End;
inc(allowcnt);
// Sollen Dialoge beim Starten ausgeführt werden ist hier der Richtige Zeitpunkt
If allowcnt = 1 Then Begin
// Init dglOpenGL.pas , Teil 2
ReadExtensions; // Anstatt der Extentions kann auch nur der Core geladen werden. ReadOpenGLCore;
ReadImplementationProperties;
End;
If allowcnt = 2 Then Begin // Dieses If Sorgt mit dem obigen dafür, dass der Code nur 1 mal ausgeführt wird.
(*
Man bedenke, jedesmal wenn der Renderingcontext neu erstellt wird, müssen sämtliche Graphiken neu Geladen werden.
Bei Nutzung der TOpenGLGraphikengine, bedeutet dies, das hier ein clear durchgeführt werden mus !!
*)
{
OpenGL_GraphikEngine.clear;
glenable(GL_TEXTURE_2D); // Texturen
glEnable(GL_DEPTH_TEST); // Tiefentest
glDepthFunc(gl_less);
}
// Der Anwendung erlauben zu Rendern.
Initialized := True;
OpenGLControl1Resize(Nil);
End;
Form1.Invalidate;
End;
Procedure TForm1.OpenGLControl1Paint(Sender: TObject);
Begin
If Not Initialized Then Exit;
// Render Szene
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// gluLookAt(5, 11, -20, 5, 5, 0, 0, 1, 0);
// { Render etwas ---
go2d;
glcolor3f(1, 0, 0);
glbegin(gl_lines);
glvertex3f(10, 10, 0);
glvertex3f(100, 100, 0);
glend;
//}
exit2d;
OpenGLControl1.SwapBuffers;
End;
Procedure TForm1.OpenGLControl1Resize(Sender: TObject);
Begin
If Initialized Then Begin
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, OpenGLControl1.Width, OpenGLControl1.Height);
gluPerspective(45.0, OpenGLControl1.Width / OpenGLControl1.Height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
End;
End;
Procedure TForm1.FormCreate(Sender: TObject);
Begin
// Init dglOpenGL.pas , Teil 1
If Not InitOpenGl Then Begin
showmessage('Error, could not init dglOpenGL.pas');
Halt;
End;
(*
60 - FPS entsprechen
0.01666666 ms
Ist Interval auf 16 hängt das gesamte system, bei 17 nicht.
Generell sollte die Interval Zahl also dynamisch zum Rechenaufwand, mindestens aber immer 17 sein.
*)
Timer1.Interval := 17;
End;
Procedure TForm1.Timer1Timer(Sender: TObject);
{$IFDEF DebuggMode}
Var
i: Cardinal;
p: Pchar;
{$ENDIF}
Begin
If Initialized Then Begin
OpenGLControl1.Invalidate;
{$IFDEF DebuggMode}
i := glGetError();
If i <> 0 Then Begin
Timer1.Enabled := false;
p := gluErrorString(i);
showmessage('OpenGL Error (' + inttostr(i) + ') occured.' + LineEnding + LineEnding +
'OpenGL Message : "' + p + '"' + LineEnding + LineEnding +
'Applikation will be terminated.');
close;
End;
{$ENDIF}
End;
End;
End.