-
Notifications
You must be signed in to change notification settings - Fork 2
/
i3d_gl.pas
308 lines (251 loc) · 8.75 KB
/
i3d_gl.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//------------------------------------------------------------------------------
//
// I3D_Viewer - Model Viewer for Speed Haste models
// Copyright (C) 2020-2021 by Jim Valavanis
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// DESCRIPTION:
// OpenGL Rendering
//
//------------------------------------------------------------------------------
// Site : https://sourceforge.net/projects/i3dviewer/
//------------------------------------------------------------------------------
unit i3d_gl;
interface
uses
Windows,
Graphics,
dglOpenGL,
i3d_model;
var
gld_max_texturesize: integer = 0;
gl_tex_format: integer = GL_RGBA8;
gl_tex_filter: integer = GL_LINEAR;
procedure glInit;
procedure ResetCamera;
procedure glBeginScene(const Width, Height: integer);
procedure glEndScene(dc: HDC);
procedure glRenderEnviroment;
procedure glRenderI3D(const t: TI3DModel);
type
TCDCamera = record
x, y, z: glfloat;
ax, ay, az: glfloat;
end;
var
camera: TCDCamera;
var
i3d_rendredtriangles: integer = 0;
implementation
uses
SysUtils,
Classes,
Math,
i3d_defs;
procedure ResetCamera;
begin
camera.x := 0.0;
camera.y := -3.0;
camera.z := -10.0;
camera.ax := 0.0;
camera.ay := 0.0;
camera.az := 0.0;
end;
{------------------------------------------------------------------}
{ Initialise OpenGL }
{------------------------------------------------------------------}
procedure glInit;
begin
glClearColor(0.0, 0.0, 0.0, 0.0); // Black Background
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glClearDepth(1.0); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enable Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_POINT_SIZE);
glGetIntegerv(GL_MAX_TEXTURE_SIZE, @gld_max_texturesize);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Realy Nice perspective calculations
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
end;
procedure infinitePerspective(fovy: GLdouble; aspect: GLdouble; znear: GLdouble);
var
left, right, bottom, top: GLdouble;
m: array[0..15] of GLdouble;
begin
top := znear * tan(fovy * pi / 360.0);
bottom := -top;
left := bottom * aspect;
right := top * aspect;
m[ 0] := (2 * znear) / (right - left);
m[ 4] := 0;
m[ 8] := (right + left) / (right - left);
m[12] := 0;
m[ 1] := 0;
m[ 5] := (2 * znear) / (top - bottom);
m[ 9] := (top + bottom) / (top - bottom);
m[13] := 0;
m[ 2] := 0;
m[ 6] := 0;
m[10] := -1;
m[14] := -2 * znear;
m[ 3] := 0;
m[ 7] := 0;
m[11] := -1;
m[15] := 0;
glMultMatrixd(@m);
end;
procedure glBeginScene(const Width, Height: integer);
begin
glDisable(GL_CULL_FACE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
infinitePerspective(64.0, width / height, 0.01);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity; // Reset The View
glTranslatef(camera.x, camera.y, camera.z);
glRotatef(camera.az, 0, 0, 1);
glRotatef(camera.ay, 0, 1, 0);
glRotatef(camera.ax, 1, 0, 0);
end;
procedure glEndScene(dc: HDC);
begin
SwapBuffers(dc); // Display the scene
end;
procedure glRenderEnviroment;
const
DRUNIT = 5;
DREPEATS = 10;
DWORLD = DREPEATS + 1;
var
i: integer;
begin
if opt_renderevniroment then
begin
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
for i := -DREPEATS to DREPEATS do
begin
if i = 0 then
glColor3f(1.0, 1.0, 0.0);
glVertex3f((DREPEATS + 1) * DRUNIT, 0.0, i * DRUNIT);
glVertex3f(-(DREPEATS + 1) * DRUNIT, 0.0, i * DRUNIT);
if i = 0 then
glColor3f(1.0, 1.0, 1.0);
end;
for i := -DREPEATS to DREPEATS do
begin
if i = 0 then
glColor3f(1.0, 1.0, 0.0);
glVertex3f(i * DRUNIT, 0.0, (DREPEATS + 1) * DRUNIT);
glVertex3f(i * DRUNIT, 0.0, -(DREPEATS + 1) * DRUNIT);
if i = 0 then
glColor3f(1.0, 1.0, 1.0);
end;
glColor3f(1.0, 1.0, 0.0);
glVertex3f(0.0, (DREPEATS + 1) * DRUNIT, 0.0);
glVertex3f(0.0, -(DREPEATS + 1) * DRUNIT, 0.0);
glEnd;
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glColor3f(0.0, 0.5, 0.0);
glBegin(GL_QUADS);
glVertex3f(DWORLD * DRUNIT, -DWORLD * DRUNIT, DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, -DWORLD * DRUNIT, -DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, -DWORLD * DRUNIT, -DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, -DWORLD * DRUNIT, DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, 0.0, DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, 0.0, -DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, -DWORLD * DRUNIT, -DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, -DWORLD * DRUNIT, DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, 0.0, -DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, 0.0, -DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, -DWORLD * DRUNIT, -DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, -DWORLD * DRUNIT, -DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, 0.0, -DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, 0.0, DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, -DWORLD * DRUNIT, DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, -DWORLD * DRUNIT, -DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, 0.0, DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, 0.0, DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, -DWORLD * DRUNIT, DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, -DWORLD * DRUNIT, DWORLD * DRUNIT);
glEnd;
glDisable(GL_CULL_FACE);
glColor3f(0.4, 0.4, 1.0);
glBegin(GL_QUADS);
glVertex3f(DWORLD * DRUNIT, DWORLD * DRUNIT, DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, DWORLD * DRUNIT, -DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, DWORLD * DRUNIT, -DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, DWORLD * DRUNIT, DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, -DRUNIT / 50, DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, -DRUNIT / 50, -DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, DWORLD * DRUNIT, -DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, DWORLD * DRUNIT, DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, -DRUNIT / 50, -DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, -DRUNIT / 50, -DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, DWORLD * DRUNIT, -DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, DWORLD * DRUNIT, -DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, -DRUNIT / 50, -DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, -DRUNIT / 50, DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, DWORLD * DRUNIT, DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, DWORLD * DRUNIT, -DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, -DRUNIT / 50, DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, -DRUNIT / 50, DWORLD * DRUNIT);
glVertex3f(DWORLD * DRUNIT, DWORLD * DRUNIT, DWORLD * DRUNIT);
glVertex3f(-DWORLD * DRUNIT, DWORLD * DRUNIT, DWORLD * DRUNIT);
glEnd;
end;
end;
procedure glRenderI3D(const t: TI3DModel);
const
DEF_SCALE = 0.001;
begin
if opt_renderwireframe then
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE )
else
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
// Render selected object
glColor4f(1.0, 0.0, 0.0, 1.0);
glDisable(GL_TEXTURE_2D);
t.RenderSelectionCubeGL(DEF_SCALE);
// Render model
glColor4f(1.0, 1.0, 1.0, 1.0);
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDisable(GL_ALPHA_TEST);
i3d_rendredtriangles := t.RenderGL(DEF_SCALE);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
end;
type
PLongWordArray = ^TLongWordArray;
TLongWordArray = array[0..$FFFF] of LongWord;
function RGBSwap(const l: LongWord): LongWord;
var
A: packed array[0..3] of byte;
tmp: byte;
begin
PLongWord(@A)^ := l;
tmp := A[0];
A[0] := A[2];
A[2] := tmp;
Result := PLongWord(@A)^;
end;
end.