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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
#include <sys/types.h>
#include <string.h>
#include <psxgte.h>
#include <psxgpu.h>
#include <inline_c.h>
#include "disp.h"
#include "smd.h"
extern MATRIX lgt_colmtx;
extern SMD *o_world;
extern SMD *o_lightbulb;
void sort_overlay(int showlotl);
void lightdemo() {
/*
The point lighting demo is perhaps the most impressive part of
n00bDEMO. A more streamlined version of this demo where you control
various attributes of the light source such as position, intensity
and color might be made as a dedicated example program in the future.
The point lighting trick is actually not that too complicated. You
basically calculate the distance and direction vector of two points
which are the light source and the vertex of a polygon.
Calculating the normal whose result can later be used to calculate
the distance between two points is achieved with:
vec_dir.vx = lgt_point.vx - pri_vert.vx;
vec_dir.vy = lgt_point.vy - pri_vert.vy;
vec_dir.vz = lgt_point.vz - pri_vert.vz;
The intensity is calculated with (this might not be accurate but this
is faster than applying a square root):
i = 4096 - ( (
(vec_dir.vx*vec_dir.vx) +
(vec_dir.vy*vec_dir.vy) +
(vec_dir.vz*vec_dir.vz) ) >> 7 );
// Clip minimum intensity
if( i < 0 )
i = 0;
This intensity value is then used to set the color of the light source
through the light color matrix.
col_mtx.m[0][0] = i;
col_mtx.m[1][0] = i;
col_mtx.m[2][0] = i;
gte_SetColorMatrix( &col_mtx );
The direction vector can then be used as the direction of the light
source. It is recommended to normalize it first to prevent possible
overflow related issues.
VectorNormalS( &vec_dir, &vec_norm );
lgt_mtx.m[0][0] = vec_norm.vx;
lgt_mtx.m[0][1] = vec_norm.vy;
lgt_mtx.m[0][2] = vec_norm.vz;
gte_SetLightMatrix( &lgt_mtx );
This operation is then performed for each point of a polygon to
achieve a nice smooth shaded point lighting effect. The macros used
are still the same as doing light source calculation with the GTE
the normal way.
3D geometry still requires normal data as with most lighting
processing operations. 'Flat' normals (faces with a single normal
vector) work best on flat surfaces while 'smooth' normals (faces with
normals on each point) work best on round or curved surfaces.
*/
int i,p_ang;
SC_OT s_ot;
SVECTOR rot;
VECTOR pos;
SMD_PRIM s_pri;
VECTOR l_point;
SVECTOR nrm;
MATRIX lmtx,llmtx,omtx;
SVECTOR orot = { 0 };
int timeout = SCENE_TIME;
// Set clear color to black
setRGB0( &draw, 0, 0, 0 );
// Base values for the environment geometry
setVector( &pos, 0, 0, 600 );
setVector( &rot, 512, 0, 0 );
// Set base tpage value for the SMD drawing routines
smdSetBaseTPage( 0x200 );
// Set back or ambient color to black for pure darkness
gte_SetBackColor( 0, 0, 0 );
memset( &llmtx, 0, sizeof(MATRIX) );
// demo loop
while( 1 ) {
char buff[32];
RotMatrix( &rot, &mtx );
TransMatrix( &mtx, &pos );
rot.vy += 4;
gte_SetRotMatrix( &mtx );
gte_SetTransMatrix( &mtx );
setVector( &l_point, (icos( p_ang )>>2)>>2, -350+(icos( p_ang<<1 )>>4), (isin( p_ang )>>2)>>2 );
p_ang += 16;
// Begin parsing the SMD data of the environment
OpenSMD( o_world );
// Prototype point lighting renderer
while( ReadSMD( &s_pri ) ) {
VECTOR v_dir;
SVECTOR v_nrm;
VECTOR v_sqr;
int flg;
if( s_pri.prim_id.texture ) {
POLY_GT4 *pri;
// Perform standard rotate, translate and perspective
// transformation of the geometry
pri = (POLY_GT4*)nextpri;
gte_ldv3(
&o_world->p_verts[s_pri.v0],
&o_world->p_verts[s_pri.v1],
&o_world->p_verts[s_pri.v2] );
gte_rtpt();
gte_nclip(); // Backface culling
gte_stopz( &flg );
if( flg < 0 )
continue;
gte_stsxy3( &pri->x0, &pri->x1, &pri->x2 );
gte_ldv0( &o_world->p_verts[s_pri.v3] );
gte_rtps();
gte_avsz4(); // Depth sort
gte_stotz( &flg );
if( (flg>>2) >= OT_LEN )
continue;
gte_stsxy( &pri->x3 );
// Load base color of polygon to GTE
gte_ldrgb( &s_pri.r0 );
// Load normal of polygon
gte_ldv0( &o_world->p_norms[s_pri.n0] );
// Calculate the direction between the vertex of the
// polygon and the light source
v_dir.vx = l_point.vx - o_world->p_verts[s_pri.v0].vx;
v_dir.vy = l_point.vy - o_world->p_verts[s_pri.v0].vy;
v_dir.vz = l_point.vz - o_world->p_verts[s_pri.v0].vz;
// Calculate distance and light intensity using square
i = 4096 - ( (
(v_dir.vx*v_dir.vx) +
(v_dir.vy*v_dir.vy) +
(v_dir.vz*v_dir.vz) ) >> 7 );
// Clip minimum intensity
if( i < 0 )
i = 0;
// Set intensity to color matrix
llmtx.m[0][0] = i;
llmtx.m[1][0] = i;
llmtx.m[2][0] = i;
gte_SetColorMatrix( &llmtx );
// Normalize light direction and set it to light matrix
VectorNormalS( &v_dir, &v_nrm );
lmtx.m[0][0] = v_nrm.vx;
lmtx.m[0][1] = v_nrm.vy;
lmtx.m[0][2] = v_nrm.vz;
gte_SetLightMatrix( &lmtx );
// Calculate (output is retrieved through gte_strgb)
gte_nccs();
// Repeat process for the next 3 vertices
v_dir.vx = l_point.vx - o_world->p_verts[s_pri.v1].vx;
v_dir.vy = l_point.vy - o_world->p_verts[s_pri.v1].vy;
v_dir.vz = l_point.vz - o_world->p_verts[s_pri.v1].vz;
i = 4096 - ( (
(v_dir.vx*v_dir.vx) +
(v_dir.vy*v_dir.vy) +
(v_dir.vz*v_dir.vz) ) >> 7 );
if( i < 0 )
i = 0;
llmtx.m[0][0] = i;
llmtx.m[1][0] = i;
llmtx.m[2][0] = i;
gte_strgb( &pri->r0 );
gte_SetColorMatrix( &llmtx );
VectorNormalS( &v_dir, &v_nrm );
lmtx.m[0][0] = v_nrm.vx;
lmtx.m[0][1] = v_nrm.vy;
lmtx.m[0][2] = v_nrm.vz;
gte_SetLightMatrix( &lmtx );
gte_nccs();
v_dir.vx = l_point.vx - o_world->p_verts[s_pri.v2].vx;
v_dir.vy = l_point.vy - o_world->p_verts[s_pri.v2].vy;
v_dir.vz = l_point.vz - o_world->p_verts[s_pri.v2].vz;
i = 4096 - ( (
(v_dir.vx*v_dir.vx) +
(v_dir.vy*v_dir.vy) +
(v_dir.vz*v_dir.vz) ) >> 7 );
if( i < 0 )
i = 0;
llmtx.m[0][0] = i;
llmtx.m[1][0] = i;
llmtx.m[2][0] = i;
gte_strgb( &pri->r1 );
gte_SetColorMatrix( &llmtx );
VectorNormalS( &v_dir, &v_nrm );
lmtx.m[0][0] = v_nrm.vx;
lmtx.m[0][1] = v_nrm.vy;
lmtx.m[0][2] = v_nrm.vz;
gte_SetLightMatrix( &lmtx );
gte_nccs();
v_dir.vx = l_point.vx - o_world->p_verts[s_pri.v3].vx;
v_dir.vy = l_point.vy - o_world->p_verts[s_pri.v3].vy;
v_dir.vz = l_point.vz - o_world->p_verts[s_pri.v3].vz;
i = 4096 - ( (
(v_dir.vx*v_dir.vx) +
(v_dir.vy*v_dir.vy) +
(v_dir.vz*v_dir.vz) ) >> 7 );
if( i < 0 )
i = 0;
llmtx.m[0][0] = i;
llmtx.m[1][0] = i;
llmtx.m[2][0] = i;
gte_strgb( &pri->r2 );
gte_SetColorMatrix( &llmtx );
VectorNormalS( &v_dir, &v_nrm );
lmtx.m[0][0] = v_nrm.vx;
lmtx.m[0][1] = v_nrm.vy;
lmtx.m[0][2] = v_nrm.vz;
gte_SetLightMatrix( &lmtx );
gte_nccs();
setUV4( pri,
s_pri.tu0, s_pri.tv0,
s_pri.tu1, s_pri.tv1,
s_pri.tu2, s_pri.tv2,
s_pri.tu3, s_pri.tv3 );
pri->tpage = s_pri.tpage;
pri->clut = s_pri.clut;
setPolyGT4( pri );
addPrim( ot[db]+(flg>>2), pri );
nextpri += sizeof(POLY_GT4);
gte_strgb( &pri->r3 );
} else {
POLY_G4 *pri;
pri = (POLY_G4*)nextpri;
gte_ldv3(
&o_world->p_verts[s_pri.v0],
&o_world->p_verts[s_pri.v1],
&o_world->p_verts[s_pri.v2] );
gte_rtpt();
gte_nclip();
gte_stopz( &flg );
if( flg < 0 )
continue;
gte_stsxy3( &pri->x0, &pri->x1, &pri->x2 );
gte_ldv0( &o_world->p_verts[s_pri.v3] );
gte_rtps();
gte_avsz4();
gte_stotz( &flg );
if( (flg>>2) >= OT_LEN )
continue;
gte_stsxy( &pri->x3 );
gte_ldrgb( &s_pri.r0 );
gte_ldv0( &o_world->p_norms[s_pri.n0] );
v_dir.vx = l_point.vx - o_world->p_verts[s_pri.v0].vx;
v_dir.vy = l_point.vy - o_world->p_verts[s_pri.v0].vy;
v_dir.vz = l_point.vz - o_world->p_verts[s_pri.v0].vz;
i = 4096 - ( (
(v_dir.vx*v_dir.vx) +
(v_dir.vy*v_dir.vy) +
(v_dir.vz*v_dir.vz) ) >> 7 );
if( i < 0 )
i = 0;
llmtx.m[0][0] = i;
llmtx.m[1][0] = i;
llmtx.m[2][0] = i;
gte_SetColorMatrix( &llmtx );
VectorNormalS( &v_dir, &v_nrm );
lmtx.m[0][0] = v_nrm.vx;
lmtx.m[0][1] = v_nrm.vy;
lmtx.m[0][2] = v_nrm.vz;
gte_SetLightMatrix( &lmtx );
gte_nccs();
v_dir.vx = l_point.vx - o_world->p_verts[s_pri.v1].vx;
v_dir.vy = l_point.vy - o_world->p_verts[s_pri.v1].vy;
v_dir.vz = l_point.vz - o_world->p_verts[s_pri.v1].vz;
i = 4096 - ( (
(v_dir.vx*v_dir.vx) +
(v_dir.vy*v_dir.vy) +
(v_dir.vz*v_dir.vz) ) >> 7 );
if( i < 0 )
i = 0;
llmtx.m[0][0] = i;
llmtx.m[1][0] = i;
llmtx.m[2][0] = i;
gte_strgb( &pri->r0 );
gte_SetColorMatrix( &llmtx );
VectorNormalS( &v_dir, &v_nrm );
lmtx.m[0][0] = v_nrm.vx;
lmtx.m[0][1] = v_nrm.vy;
lmtx.m[0][2] = v_nrm.vz;
gte_SetLightMatrix( &lmtx );
gte_nccs();
v_dir.vx = l_point.vx - o_world->p_verts[s_pri.v2].vx;
v_dir.vy = l_point.vy - o_world->p_verts[s_pri.v2].vy;
v_dir.vz = l_point.vz - o_world->p_verts[s_pri.v2].vz;
i = 4096 - ( (
(v_dir.vx*v_dir.vx) +
(v_dir.vy*v_dir.vy) +
(v_dir.vz*v_dir.vz) ) >> 7 );
if( i < 0 )
i = 0;
llmtx.m[0][0] = i;
llmtx.m[1][0] = i;
llmtx.m[2][0] = i;
gte_strgb( &pri->r1 );
gte_SetColorMatrix( &llmtx );
VectorNormalS( &v_dir, &v_nrm );
lmtx.m[0][0] = v_nrm.vx;
lmtx.m[0][1] = v_nrm.vy;
lmtx.m[0][2] = v_nrm.vz;
gte_SetLightMatrix( &lmtx );
gte_nccs();
v_dir.vx = l_point.vx - o_world->p_verts[s_pri.v3].vx;
v_dir.vy = l_point.vy - o_world->p_verts[s_pri.v3].vy;
v_dir.vz = l_point.vz - o_world->p_verts[s_pri.v3].vz;
i = 4096 - ( (
(v_dir.vx*v_dir.vx) +
(v_dir.vy*v_dir.vy) +
(v_dir.vz*v_dir.vz) ) >> 7 );
if( i < 0 )
i = 0;
llmtx.m[0][0] = i;
llmtx.m[1][0] = i;
llmtx.m[2][0] = i;
gte_strgb( &pri->r2 );
gte_SetColorMatrix( &llmtx );
VectorNormalS( &v_dir, &v_nrm );
lmtx.m[0][0] = v_nrm.vx;
lmtx.m[0][1] = v_nrm.vy;
lmtx.m[0][2] = v_nrm.vz;
gte_SetLightMatrix( &lmtx );
gte_nccs();
setPolyG4( pri );
addPrim( ot[db]+(flg>>2), pri );
nextpri += sizeof(POLY_G4);
gte_strgb( &pri->r3 );
}
}
// Sort the light bulb to represent the position of the light source
orot.vx += 32;
orot.vy += 32;
orot.vz += 32;
RotMatrix( &orot, &omtx );
TransMatrix( &omtx, &l_point );
CompMatrixLV( &mtx, &omtx, &mtx );
gte_SetRotMatrix( &mtx );
gte_SetTransMatrix( &mtx );
s_ot.ot = ot[db];
s_ot.otlen = OT_LEN;
s_ot.zdiv = 2;
s_ot.zoff = 0;
nextpri = smdSortModel( &s_ot, nextpri, o_lightbulb );
// Sort overlay and display
sort_overlay( 1 );
display();
timeout--;
if( timeout < 0 )
break;
}
}
|