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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
#include "global.h"
#include "z64shrink_window.h"
#include "z64view.h"
s32 View_ApplyPerspective(View* view);
s32 View_ApplyOrtho(View* view);
void View_ViewportToVp(Vp* dest, Viewport* src) {
s32 width = src->rightX - src->leftX;
s32 height = src->bottomY - src->topY;
dest->vp.vscale[0] = width * 2;
dest->vp.vscale[1] = height * 2;
dest->vp.vscale[2] = 0x01FF;
dest->vp.vscale[3] = 0;
dest->vp.vtrans[0] = ((src->leftX * 2) + width) * 2;
dest->vp.vtrans[1] = ((src->topY * 2) + height) * 2;
dest->vp.vtrans[2] = 0x01FF;
dest->vp.vtrans[3] = 0;
if ((src->leftX == 0) && (src->rightX == HIRES_BUFFER_WIDTH) && (src->topY == 0) &&
(src->bottomY == HIRES_BUFFER_HEIGHT)) {}
}
void View_Init(View* view, GraphicsContext* gfxCtx) {
view->gfxCtx = gfxCtx;
view->viewport.topY = 0;
view->viewport.bottomY = SCREEN_HEIGHT;
view->viewport.leftX = 0;
view->viewport.rightX = SCREEN_WIDTH;
view->magic = 0x56494557; // "VIEW"
if (1) {}
view->scale = 1.0f;
view->fovy = 60.0f;
view->zNear = 10.0f;
view->zFar = 12800.0f;
view->eye.x = 0.0f;
view->eye.y = 0.0f;
view->eye.z = -1.0f;
view->at.x = 0.0f;
view->up.x = 0.0f;
view->up.y = 1.0f;
view->up.z = 0.0f;
view->unk164 = 0;
view->flags = VIEW_VIEWING | VIEW_VIEWPORT | VIEW_PROJECTION_PERSPECTIVE;
View_InitDistortion(view);
}
void View_LookAt(View* view, Vec3f* eye, Vec3f* at, Vec3f* up) {
if ((eye->x == at->x) && (eye->z == at->z)) {
eye->z += 0.1f;
up->z = 0.0f;
up->x = 0.0f;
up->y = 1.0f;
}
view->eye = *eye;
view->at = *at;
view->up = *up;
view->flags |= VIEW_VIEWING;
}
/*
* Unused. View_LookAt is always used instead. This version is similar but
* is missing the input sanitization and the update to the flags.
*/
void View_LookAtUnsafe(View* view, Vec3f* eye, Vec3f* at, Vec3f* up) {
view->eye = *eye;
view->at = *at;
view->up = *up;
}
void View_SetScale(View* view, f32 scale) {
view->flags |= VIEW_PROJECTION_PERSPECTIVE;
view->scale = scale;
}
void View_GetScale(View* view, f32* scale) {
*scale = view->scale;
}
void View_SetPerspective(View* view, f32 fovy, f32 zNear, f32 zFar) {
view->fovy = fovy;
view->zNear = zNear;
view->zFar = zFar;
view->flags |= VIEW_PROJECTION_PERSPECTIVE;
}
void View_GetPerspective(View* view, f32* fovy, f32* zNear, f32* zFar) {
*fovy = view->fovy;
*zNear = view->zNear;
*zFar = view->zFar;
}
void View_SetOrtho(View* view, f32 fovy, f32 zNear, f32 zFar) {
view->fovy = fovy;
view->zNear = zNear;
view->zFar = zFar;
view->flags |= VIEW_PROJECTION_ORTHO;
view->scale = 1.0f;
}
/*
* Identical to View_GetPerspective, and never called.
* Named as it seems to fit the "set, get" pattern.
*/
void View_GetOrtho(View* view, f32* fovy, f32* zNear, f32* zFar) {
*fovy = view->fovy;
*zNear = view->zNear;
*zFar = view->zFar;
}
void View_SetViewport(View* view, Viewport* viewport) {
view->viewport = *viewport;
view->flags |= VIEW_VIEWPORT;
}
void View_GetViewport(View* view, Viewport* viewport) {
*viewport = view->viewport;
}
void View_SetScissor(Gfx** gfx, s32 ulx, s32 uly, s32 lrx, s32 lry) {
Gfx* gfxP = *gfx;
gDPSetScissor(gfxP++, G_SC_NON_INTERLACE, ulx, uly, lrx, lry);
*gfx = gfxP;
}
void View_ClearScissor(View* view, Gfx** gfx) {
Gfx* gfxP = *gfx;
s32 ulx = view->viewport.leftX;
s32 uly = view->viewport.topY;
s32 lrx = view->viewport.rightX;
s32 lry = view->viewport.bottomY;
gDPPipeSync(gfxP++);
View_SetScissor(&gfxP, ulx, uly, lrx, lry);
*gfx = gfxP;
}
void View_ApplyLetterbox(View* view) {
s32 letterboxY;
s32 letterboxX;
s32 pad1;
s32 ulx;
s32 uly;
s32 lrx;
s32 lry;
OPEN_DISPS(view->gfxCtx);
letterboxY = ShrinkWindow_Letterbox_GetSize();
letterboxX = -1; // The following is optimized to varX = 0 but affects codegen
if (letterboxX < 0) {
letterboxX = 0;
}
if (letterboxX > (SCREEN_WIDTH / 2)) {
letterboxX = SCREEN_WIDTH / 2;
}
if (letterboxY < 0) {
letterboxY = 0;
} else if (letterboxY > (SCREEN_HEIGHT / 2)) {
letterboxY = SCREEN_HEIGHT / 2;
}
ulx = view->viewport.leftX + letterboxX;
uly = view->viewport.topY + letterboxY;
lrx = view->viewport.rightX - letterboxX;
lry = view->viewport.bottomY - letterboxY;
gDPPipeSync(POLY_OPA_DISP++);
{
s32 pad2;
Gfx* polyOpa;
polyOpa = POLY_OPA_DISP;
View_SetScissor(&polyOpa, ulx, uly, lrx, lry);
POLY_OPA_DISP = polyOpa;
}
gDPPipeSync(POLY_XLU_DISP++);
{
Gfx* polyXlu;
s32 pad3;
polyXlu = POLY_XLU_DISP;
View_SetScissor(&polyXlu, ulx, uly, lrx, lry);
POLY_XLU_DISP = polyXlu;
}
CLOSE_DISPS(view->gfxCtx);
}
s32 View_SetDistortionOrientation(View* view, f32 rotX, f32 rotY, f32 rotZ) {
view->distortionOrientation.x = rotX;
view->distortionOrientation.y = rotY;
view->distortionOrientation.z = rotZ;
return 1;
}
s32 View_SetDistortionScale(View* view, f32 scaleX, f32 scaleY, f32 scaleZ) {
view->distortionScale.x = scaleX;
view->distortionScale.y = scaleY;
view->distortionScale.z = scaleZ;
return 1;
}
s32 View_SetDistortionSpeed(View* view, f32 speed) {
view->distortionSpeed = speed;
return 1;
}
s32 View_InitDistortion(View* view) {
view->distortionOrientation.x = 0.0f;
view->distortionOrientation.y = 0.0f;
view->distortionOrientation.z = 0.0f;
view->distortionScale.x = 1.0f;
view->distortionScale.y = 1.0f;
view->distortionScale.z = 1.0f;
view->curDistortionOrientation = view->distortionOrientation;
view->curDistortionScale = view->distortionScale;
view->distortionSpeed = 0.0f;
return 1;
}
s32 View_ClearDistortion(View* view) {
view->distortionOrientation.x = 0.0f;
view->distortionOrientation.y = 0.0f;
view->distortionOrientation.z = 0.0f;
view->distortionScale.x = 1.0f;
view->distortionScale.y = 1.0f;
view->distortionScale.z = 1.0f;
view->distortionSpeed = 1.0f;
return 1;
}
s32 View_SetDistortion(View* view, Vec3f orientation, Vec3f scale, f32 speed) {
view->distortionOrientation = orientation;
view->distortionScale = scale;
view->distortionSpeed = speed;
return 1;
}
s32 View_StepDistortion(View* view, Mtx* projectionMtx) {
MtxF projectionMtxF;
if (view->distortionSpeed == 0.0f) {
return false;
}
if (view->distortionSpeed == 1.0f) {
view->curDistortionOrientation = view->distortionOrientation;
view->curDistortionScale = view->distortionScale;
view->distortionSpeed = 0.0f;
} else {
view->curDistortionOrientation.x =
F32_LERPIMP(view->curDistortionOrientation.x, view->distortionOrientation.x, view->distortionSpeed);
view->curDistortionOrientation.y =
F32_LERPIMP(view->curDistortionOrientation.y, view->distortionOrientation.y, view->distortionSpeed);
view->curDistortionOrientation.z =
F32_LERPIMP(view->curDistortionOrientation.z, view->distortionOrientation.z, view->distortionSpeed);
view->curDistortionScale.x =
F32_LERPIMP(view->curDistortionScale.x, view->distortionScale.x, view->distortionSpeed);
view->curDistortionScale.y =
F32_LERPIMP(view->curDistortionScale.y, view->distortionScale.y, view->distortionSpeed);
view->curDistortionScale.z =
F32_LERPIMP(view->curDistortionScale.z, view->distortionScale.z, view->distortionSpeed);
}
Matrix_MtxToMtxF(projectionMtx, &projectionMtxF);
Matrix_Put(&projectionMtxF);
Matrix_RotateXFApply(view->curDistortionOrientation.x);
Matrix_RotateYF(view->curDistortionOrientation.y, MTXMODE_APPLY);
Matrix_RotateZF(view->curDistortionOrientation.z, MTXMODE_APPLY);
Matrix_Scale(view->curDistortionScale.x, view->curDistortionScale.y, view->curDistortionScale.z, MTXMODE_APPLY);
Matrix_RotateZF(-view->curDistortionOrientation.z, MTXMODE_APPLY);
Matrix_RotateYF(-view->curDistortionOrientation.y, MTXMODE_APPLY);
Matrix_RotateXFApply(-view->curDistortionOrientation.x);
Matrix_ToMtx(projectionMtx);
return true;
}
/**
* Apply view to POLY_OPA_DISP, POLY_XLU_DISP (and OVERLAY_DISP if ortho)
*/
s32 View_Apply(View* view, s32 mask) {
mask = (view->flags & mask) | (mask >> 4);
if (mask & VIEW_PROJECTION_ORTHO) {
return View_ApplyOrtho(view);
} else {
return View_ApplyPerspective(view);
}
}
s32 View_ApplyPerspective(View* view) {
f32 aspect;
s32 width;
s32 height;
Vp* vp;
Mtx* projection;
Mtx* viewing;
GraphicsContext* gfxCtx = view->gfxCtx;
OPEN_DISPS(gfxCtx);
// Viewport
vp = GRAPH_ALLOC(gfxCtx, sizeof(Vp));
View_ViewportToVp(vp, &view->viewport);
view->vp = *vp;
View_ApplyLetterbox(view);
gSPViewport(POLY_OPA_DISP++, vp);
gSPViewport(POLY_XLU_DISP++, vp);
// Perspective projection
projection = GRAPH_ALLOC(gfxCtx, sizeof(Mtx));
view->projectionPtr = projection;
width = view->viewport.rightX - view->viewport.leftX;
height = view->viewport.bottomY - view->viewport.topY;
aspect = (f32)width / (f32)height;
guPerspective(projection, &view->perspNorm, view->fovy, aspect, view->zNear, view->zFar, view->scale);
view->projection = *projection;
View_StepDistortion(view, projection);
gSPPerspNormalize(POLY_OPA_DISP++, view->perspNorm);
gSPMatrix(POLY_OPA_DISP++, projection, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_PROJECTION);
gSPPerspNormalize(POLY_XLU_DISP++, view->perspNorm);
gSPMatrix(POLY_XLU_DISP++, projection, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_PROJECTION);
// View matrix (look-at)
viewing = GRAPH_ALLOC(gfxCtx, sizeof(Mtx));
view->viewingPtr = viewing;
if ((view->eye.x == view->at.x) && (view->eye.y == view->at.y) && (view->eye.z == view->at.z)) {
view->eye.z += 2.0f;
}
guLookAt(viewing, view->eye.x, view->eye.y, view->eye.z, view->at.x, view->at.y, view->at.z, view->up.x, view->up.y,
view->up.z);
view->viewing = *viewing;
gSPMatrix(POLY_OPA_DISP++, viewing, G_MTX_NOPUSH | G_MTX_MUL | G_MTX_PROJECTION);
gSPMatrix(POLY_XLU_DISP++, viewing, G_MTX_NOPUSH | G_MTX_MUL | G_MTX_PROJECTION);
CLOSE_DISPS(gfxCtx);
return 1;
}
s32 View_ApplyOrtho(View* view) {
Vp* vp;
Mtx* projection;
GraphicsContext* gfxCtx = view->gfxCtx;
OPEN_DISPS(gfxCtx);
vp = GRAPH_ALLOC(gfxCtx, sizeof(Vp));
View_ViewportToVp(vp, &view->viewport);
view->vp = *vp;
View_ApplyLetterbox(view);
gSPViewport(POLY_OPA_DISP++, vp);
gSPViewport(POLY_XLU_DISP++, vp);
gSPViewport(OVERLAY_DISP++, vp);
projection = GRAPH_ALLOC(gfxCtx, sizeof(Mtx));
view->projectionPtr = projection;
guOrtho(projection, gScreenWidth * -0.5f, gScreenWidth * 0.5f, gScreenHeight * -0.5f, gScreenHeight * 0.5f,
view->zNear, view->zFar, view->scale);
view->projection = *projection;
gSPMatrix(POLY_OPA_DISP++, projection, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_PROJECTION);
gSPMatrix(POLY_XLU_DISP++, projection, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_PROJECTION);
CLOSE_DISPS(gfxCtx);
return 1;
}
/**
* Apply scissor, viewport and projection (ortho) to OVERLAY_DISP.
*/
s32 View_ApplyOrthoToOverlay(View* view) {
Vp* vp;
Mtx* projection;
GraphicsContext* gfxCtx;
gfxCtx = view->gfxCtx;
OPEN_DISPS(gfxCtx);
vp = GRAPH_ALLOC(gfxCtx, sizeof(Vp));
View_ViewportToVp(vp, &view->viewport);
view->vp = *vp;
gDPPipeSync(OVERLAY_DISP++);
{
Gfx* overlay;
s32 pad;
overlay = OVERLAY_DISP;
View_SetScissor(&overlay, view->viewport.leftX, view->viewport.topY, view->viewport.rightX,
view->viewport.bottomY);
OVERLAY_DISP = overlay;
}
gSPViewport(OVERLAY_DISP++, vp);
projection = GRAPH_ALLOC(gfxCtx, sizeof(Mtx));
view->projectionPtr = projection;
guOrtho(projection, gScreenWidth * -0.5f, gScreenWidth * 0.5f, gScreenHeight * -0.5f, gScreenHeight * 0.5f,
view->zNear, view->zFar, view->scale);
view->projection = *projection;
gSPMatrix(OVERLAY_DISP++, projection, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_PROJECTION);
CLOSE_DISPS(gfxCtx);
return 1;
}
/**
* Apply scissor, viewport, view and projection (perspective) to OVERLAY_DISP.
*/
s32 View_ApplyPerspectiveToOverlay(View* view) {
f32 aspect;
s32 width;
s32 height;
Vp* vp;
Mtx* projection;
Mtx* viewing;
GraphicsContext* gfxCtx;
s32 pad;
gfxCtx = view->gfxCtx;
OPEN_DISPS(gfxCtx);
vp = GRAPH_ALLOC(gfxCtx, sizeof(Vp));
View_ViewportToVp(vp, &view->viewport);
view->vp = *vp;
gDPPipeSync(OVERLAY_DISP++);
{
s32 pad;
Gfx* overlay;
overlay = OVERLAY_DISP;
View_SetScissor(&overlay, view->viewport.leftX, view->viewport.topY, view->viewport.rightX,
view->viewport.bottomY);
OVERLAY_DISP = overlay;
}
gSPViewport(OVERLAY_DISP++, vp);
projection = GRAPH_ALLOC(gfxCtx, sizeof(Mtx));
view->projectionPtr = projection;
width = view->viewport.rightX - view->viewport.leftX;
height = view->viewport.bottomY - view->viewport.topY;
aspect = (f32)width / (f32)height;
guPerspective(projection, &view->perspNorm, view->fovy, aspect, view->zNear, view->zFar, view->scale);
view->projection = *projection;
gSPPerspNormalize(OVERLAY_DISP++, view->perspNorm);
gSPMatrix(OVERLAY_DISP++, projection, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_PROJECTION);
viewing = GRAPH_ALLOC(gfxCtx, sizeof(Mtx));
view->viewingPtr = viewing;
// This check avoids a divide-by-zero in guLookAt if eye == at
if (view->eye.x == view->at.x && view->eye.y == view->at.y && view->eye.z == view->at.z) {
view->eye.z += 2.0f;
}
guLookAt(viewing, view->eye.x, view->eye.y, view->eye.z, view->at.x, view->at.y, view->at.z, view->up.x, view->up.y,
view->up.z);
view->viewing = *viewing;
gSPMatrix(OVERLAY_DISP++, viewing, G_MTX_NOPUSH | G_MTX_MUL | G_MTX_PROJECTION);
CLOSE_DISPS(gfxCtx);
return 1;
}
/**
* Just updates view's view matrix from its eye/at/up vectors.
*/
s32 View_UpdateViewingMatrix(View* view) {
guLookAt(view->viewingPtr, view->eye.x, view->eye.y, view->eye.z, view->at.x, view->at.y, view->at.z, view->up.x,
view->up.y, view->up.z);
view->unkE0 = *view->viewingPtr;
view->viewingPtr = &view->unkE0;
return 1;
}
s32 View_ApplyTo(View* view, Gfx** gfxP) {
Gfx* gfx = *gfxP;
GraphicsContext* gfxCtx = view->gfxCtx;
Viewport* viewport = &view->viewport;
Mtx* projection;
Vp* vp;
vp = GRAPH_ALLOC(gfxCtx, sizeof(Vp));
View_ViewportToVp(vp, viewport);
view->vp = *vp;
View_ClearScissor(view, &gfx);
gSPViewport(gfx++, vp);
projection = GRAPH_ALLOC(gfxCtx, sizeof(Mtx));
view->projectionPtr = projection;
guOrtho(projection, gScreenWidth * -0.5f, gScreenWidth * 0.5f, gScreenHeight * -0.5f, gScreenHeight * 0.5f,
view->zNear, view->zFar, view->scale);
view->projection = *projection;
gSPMatrix(gfx++, projection, G_MTX_NOPUSH | G_MTX_LOAD | G_MTX_PROJECTION);
*gfxP = gfx;
return 1;
}
|