-
Notifications
You must be signed in to change notification settings - Fork 1
/
Unit_Render.pas
220 lines (171 loc) · 5.39 KB
/
Unit_Render.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
unit Unit_Render;
interface
uses Classes, Controls, dglOpenGL, KromOGLUtils, KromUtils, Math, Windows, SysUtils,
Unit_ColorCoder, Unit_Vector;
type
TRenderMode = (rm2D, rmDeck, rm3D);
TRenderPass = (rpFrame, rpSelection);
TRender = class
private
PrevTime, PrevFrameTimes, FrameTime, FrameCount: Cardinal;
fFPS: string;
h_DC: HDC;
h_RC: HGLRC;
fOpenGL_Vendor, fOpenGL_Renderer, fOpenGL_Version: AnsiString;
fWidth, fHeight: Word;
fViewport: TVector4i; //Store the viewport properties
fProjection: TMatrix4d; //to use with glUnProject
fModelview: TMatrix4d;
function GetVersionInfo: string;
public
IsNormal: Boolean;
constructor Create(RenderFrame: HWND; aLeft, aTop, aWidth, aHeight: integer);
destructor Destroy; override;
property Width: Word read fWidth;
property Height: Word read fHeight;
property FPS: string read fFPS;
procedure Resize(aWidth, aHeight: Integer);
property VersionInfo: string read GetVersionInfo;
function CodeBelow(X,Y: Integer): TColorCodeId;
procedure GetRay(aScreenX, aScreenY: Integer; out RayStart, RayEnd: TVector3f);
procedure BeginFrame;
procedure Switch(aMode: TRenderMode);
procedure EndFrame;
end;
const
FPS_INTERVAL = 1000;
LightPos: array [0..3] of GLfloat = (-20, 20, 20, 0);
LightSpec: array [0..3] of GLfloat = (0.7, 0.7, 0.7, 0);
LightDiff: array [0..3] of GLfloat = (1, 0.9, 1, 1);
var
fRender: TRender;
implementation
uses
Unit_Defaults;
constructor TRender.Create(RenderFrame: HWND; aLeft, aTop, aWidth, aHeight: Integer);
begin
SetRenderFrame(RenderFrame, h_DC, h_RC);
Resize(aWidth, aHeight);
fOpenGL_Vendor := glGetString(GL_VENDOR);
fOpenGL_Renderer := glGetString(GL_RENDERER);
fOpenGL_Version := glGetString(GL_VERSION);
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT, GL_FILL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, @LightPos);
glLightfv(GL_LIGHT0, GL_SPECULAR, @LightSpec);
glLightfv(GL_LIGHT0, GL_DIFFUSE, @LightDiff);
BuildFont(h_DC, 12, FW_BOLD);
SetupVSync(True);
end;
destructor TRender.Destroy;
begin
wglMakeCurrent(0, 0);
wglDeleteContext(h_RC);
inherited;
end;
procedure TRender.Resize(aWidth, aHeight: Integer);
begin
fWidth := Max(aWidth, 1);
fHeight := Max(aHeight, 1);
glViewport(0, 0, fWidth, fHeight);
end;
procedure TRender.Switch(aMode: TRenderMode);
begin
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
case aMode of
rm2D: begin
//Whole screen overlay
glViewport(0, 0, fWidth, fHeight);
glOrtho(0, fWidth, fHeight, 0, -10, 30);
end;
rmDeck: begin
//Deck
glViewport(0, 0, fWidth, DECK_HEIGHT);
gluOrtho2D(0, fWidth, DECK_HEIGHT/2, -DECK_HEIGHT/2);
end;
rm3D: begin
//Ingot view
glViewport(0, DECK_HEIGHT, fWidth, fHeight - DECK_HEIGHT);
gluPerspective(85, fWidth / (fHeight - DECK_HEIGHT), 0.1, 10000);
end;
end;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
if aMode = rm3D then
begin
glTranslatef(0, 0, -1);
//Remember the matrices to use with gluUnproject later on
glGetIntegerv(GL_VIEWPORT, @fViewport);
glGetDoublev(GL_PROJECTION_MATRIX, @fProjection);
glGetDoublev(GL_MODELVIEW_MATRIX, @fModelview);
end;
end;
function TRender.CodeBelow(X, Y: Integer): TColorCodeId;
var
PosX, PosY: Integer;
Pixel: Cardinal;
begin
PosX := X;
PosY := fHeight - Y - 1;
glReadPixels(PosX, PosY, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, @Pixel);
Pixel := Pixel and $FFFFFF;
Result := GetColorCode(Pixel);
end;
procedure TRender.BeginFrame;
begin
if IsNormal then
glClearColor(0.3, 0.3, 0.3, 1)
else
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
end;
procedure TRender.EndFrame;
begin
glFinish;
SwapBuffers(h_DC);
//Calculate FPS
FrameTime := GetTickCount - PrevTime;
PrevTime := GetTickCount;
if FrameTime > 1000 then
FrameTime := 1000;
Inc(PrevFrameTimes, FrameTime);
Inc(FrameCount);
if PrevFrameTimes >= FPS_INTERVAL then
begin
fFPS := FloatToStr(RoundTo(1000 / (PrevFrameTimes / FrameCount), -2)) + ' fps';
PrevFrameTimes := 0;
FrameCount := 0;
end;
end;
procedure TRender.GetRay(aScreenX, aScreenY: Integer; out RayStart, RayEnd: TVector3f);
var
PosX, PosY: Integer;
X,Y,Z: GLdouble;
begin
//fViewport already contains viewport offsets
PosX := aScreenX;
PosY := fHeight - aScreenY; //In OpenGL Y axis is inverted and starts from bottom
//Take far coord
gluUnProject(PosX, PosY, 0, fModelview, fProjection, fViewport, @X, @Y, @Z);
RayStart.X := X;
RayStart.Y := Y;
RayStart.Z := Z;
gluUnProject(PosX, PosY, 1, fModelview, fProjection, fViewport, @X, @Y, @Z);
RayEnd.X := X;
RayEnd.Y := Y;
RayEnd.Z := Z;
end;
function TRender.GetVersionInfo: string;
begin
Result := fOpenGL_Vendor + ' / ' + fOpenGL_Renderer + ' / ' + fOpenGL_Version;
end;
end.