This repository has been archived by the owner on Oct 31, 2022. It is now read-only.
forked from Yoshimaster96/N64BrewGameJam2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.c
133 lines (130 loc) · 3.85 KB
/
graphics.c
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
#include "graphics.h"
////////////
//GRAPHICS//
////////////
const Vp vp = {
SCREEN_WD*2,SCREEN_HT*2,G_MAXZ/2,0,
SCREEN_WD*2,SCREEN_HT*2,G_MAXZ/2,0,
};
Lights1 lights = gdSPDefLights1(0x00,0x33,0x66,
0xFF,0xCC,0x99,
64,90,64);
const Gfx gfxInit[] = {
//Init RSP state
gsSPViewport(&vp),
gsSPClearGeometryMode(0xFFFFFFFF),
gsSPTexture(0,0,0,0,G_OFF),
gsSPSetLights1(lights),
gsSPFogPosition(996,1000),
//Init RDP state
gsDPSetRenderMode(G_RM_OPA_SURF,G_RM_OPA_SURF2),
gsDPSetColorDither(G_CD_BAYER),
gsDPSetFogColor(0x00,0x00,0x00,0xFF),
gsDPSetScissor(G_SC_NON_INTERLACE,0,0,SCREEN_WD-1,SCREEN_HT-1),
gsDPPipeSync(),
gsSPEndDisplayList(),
};
Gfx dlBuf[GFX_GTASK_NUM][GFX_GLIST_LEN];
Gfx * dlPtr;
TDynamic dynBuf[GFX_GTASK_NUM];
TDynamic * dynPtr;
u8 bufferId = 0;
//Init display
void graphics_init() {
dlPtr = &dlBuf[bufferId][0];
dynPtr = &dynBuf[bufferId];
gSPSegment(dlPtr++,0x00,0);
gSPDisplayList(dlPtr++,OS_K0_TO_PHYSICAL(gfxInit));
}
//Clear buffers
void graphics_clear() {
gDPSetDepthImage(dlPtr++,OS_K0_TO_PHYSICAL(nuGfxZBuffer));
gDPPipeSync(dlPtr++);
gDPSetCycleType(dlPtr++,G_CYC_FILL);
gDPSetColorImage(dlPtr++,G_IM_FMT_RGBA,G_IM_SIZ_16b,SCREEN_WD,
OS_K0_TO_PHYSICAL(nuGfxZBuffer));
gDPSetFillColor(dlPtr++,GPACK_ZDZ(G_MAXFBZ,0)<<16 | GPACK_ZDZ(G_MAXFBZ,0));
gDPFillRectangle(dlPtr++,0,0,SCREEN_WD-1,SCREEN_HT-1);
gDPPipeSync(dlPtr++);
gDPSetColorImage(dlPtr++,G_IM_FMT_RGBA,G_IM_SIZ_16b,SCREEN_WD,
osVirtualToPhysical(nuGfxCfb_ptr));
gDPSetFillColor(dlPtr++,GPACK_RGBA5551(0x00,0x00,0x00,1)<<16 | GPACK_RGBA5551(0x00,0x00,0x00,1));
gDPFillRectangle(dlPtr++,0,0,SCREEN_WD-1,SCREEN_HT-1);
gDPPipeSync(dlPtr++);
}
//Setup render settings
void graphics_setup() {
gDPSetCycleType(dlPtr++,G_CYC_1CYCLE);
gSPClearGeometryMode(dlPtr++,0xFFFFFFFF);
gSPSetGeometryMode(dlPtr++,G_LIGHTING|G_ZBUFFER|G_SHADE|G_SHADING_SMOOTH|G_CULL_BACK);
gDPSetRenderMode(dlPtr++,G_RM_AA_ZB_OPA_SURF,G_RM_AA_ZB_OPA_SURF2);
gDPSetColorDither(dlPtr++,G_CD_BAYER);
gDPSetCombineMode(dlPtr++,G_CC_MODULATERGBA,G_CC_MODULATERGBA);
gDPSetTexturePersp(dlPtr++,G_TP_PERSP);
gDPSetTextureLOD(dlPtr++,G_TL_TILE);
gDPSetTextureFilter(dlPtr++,G_TF_POINT);
gDPSetTextureConvert(dlPtr++,G_TC_FILT);
gDPSetTextureLUT(dlPtr++,G_TT_NONE);
gSPTexture(dlPtr++,0x8000,0x8000,0,G_TX_RENDERTILE,G_ON);
}
//Setup view matrix
void graphics_view(float* camEye,float* camCenter,float* camUp) {
u16 norm;
LookAt lookat;
//Calculate matrix
guPerspective(&dynPtr->mProj,&norm,
45,(float)SCREEN_WD/(float)SCREEN_HT,10,10000,1.0);
guLookAtReflect(&dynPtr->mView,&lookat,
camEye [0],camEye [1],camEye [2],
camCenter[0],camCenter[1],camCenter[2],
camUp [0],camUp [1],camUp [2]);
gSPPerspNormalize(dlPtr++,norm);
gSPLookAt(dlPtr++,&lookat);
gSPMatrix(dlPtr++,&dynPtr->mProj,G_MTX_PROJECTION|G_MTX_LOAD|G_MTX_NOPUSH);
gSPMatrix(dlPtr++,&dynPtr->mView,G_MTX_PROJECTION|G_MTX_MUL |G_MTX_NOPUSH);
}
//Setup rotate matrix
void graphics_rotate(Mtx* m,float x,float y,float z) {
float mf[4][4];
float sx,cx;
float sy,cy;
float sz,cz;
//Calculate sin/cos
sx = sin(x);
cx = cos(x);
sy = sin(y);
cy = cos(y);
sz = sin(z);
cz = cos(z);
//Calculate matrix
guMtxIdentF(mf);
mf[0][0] = (cy*cz)-(sx*sy*sz);
mf[0][1] = (cy*sz)+(cz*sx*sy);
mf[0][2] = -cx*sy;
mf[1][0] = -cx*sz;
mf[1][1] = cx*cz;
mf[1][2] = sx;
mf[2][0] = (cz*sy)+(cy*sx*sz);
mf[2][1] = (sy*sz)-(cy*cz*sx);
mf[2][2] = cx*cy;
guMtxF2L(mf,m);
}
//End display
void graphics_end(u32 flag) {
gDPFullSync(dlPtr++);
gSPEndDisplayList(dlPtr++);
//assert((dlPtr-dlBuf[bufferId])<GFX_GLIST_LEN);
nuGfxTaskStart(&dlBuf[bufferId][0],(s32)(dlPtr-dlBuf[bufferId])*sizeof(Gfx),NU_GFX_UCODE_F3DEX,flag);
if(flag==NU_SC_SWAPBUFFER) {
bufferId++;
bufferId %= GFX_GTASK_NUM;
}
}
//End display (debug console)
void graphics_end_deb_con(u32 flag) {
nuDebConDisp(flag);
if(flag==NU_SC_SWAPBUFFER) {
bufferId++;
bufferId %= GFX_GTASK_NUM;
}
}