DOOM64-RE

DOOM 64 Reverse Engineering
Log | Files | Refs | README | LICENSE

r_main.c (10716B)


      1 /* r_main.c */
      2 
      3 #include "doomdef.h"
      4 #include "r_local.h"
      5 
      6 /*===================================== */
      7 
      8 /* */
      9 /* subsectors */
     10 /* */
     11 //subsector_t		*vissubsectors[MAXVISSSEC], **lastvissubsector;
     12 
     13 /* */
     14 /* walls */
     15 /* */
     16 //viswall_t	viswalls[MAXWALLCMDS], *lastwallcmd;
     17 
     18 /* */
     19 /* planes */
     20 /* */
     21 //visplane_t	visplanes[MAXVISPLANES], *lastvisplane;
     22 
     23 /* */
     24 /* sprites */
     25 /* */
     26 //vissprite_t	vissprites[MAXVISSPRITES], *lastsprite_p, *vissprite_p;
     27 
     28 /* */
     29 /* openings / misc refresh memory */
     30 /* */
     31 //unsigned short	openings[MAXOPENINGS], *lastopening;
     32 
     33 
     34 /*===================================== */
     35 
     36 fixed_t		viewx, viewy, viewz;    // 800A6890, 800A6894, 800A6898
     37 angle_t		viewangle;              // 800A689C
     38 fixed_t		viewcos, viewsin;       // 800A68A0,
     39 player_t	*viewplayer;            // 800A688C, 800a68a4
     40 
     41 int			validcount;		/* increment every time a check is made */ // 800A6900
     42 //int			framecount;		    /* incremented every frame */
     43 
     44 /* */
     45 /* sky mapping */
     46 /* */
     47 boolean     rendersky; // 800A68A8
     48 
     49 byte        solidcols[320];                     // 800A6348
     50 subsector_t *solidsubsectors[MAXSUBSECTORS];	// 800A6488  /* List of valid ranges to scan through */
     51 subsector_t **endsubsector;				        // 800A6888    /* Pointer to the first free entry */
     52 int numdrawsubsectors;                          // 800A68AC
     53 
     54 vissprite_t	vissprites[MAXVISSPRITES];          // 800A6908
     55 vissprite_t	*visspritehead;                     // 800A8108
     56 int numdrawvissprites;                          // 800A68B0
     57 
     58 int globallump;                                 // 800A68f8
     59 int globalcm;                                   // 800A68FC
     60 
     61 Mtx R_ProjectionMatrix;                         // 800A68B8
     62 /*Mtx R_ProjectionMatrix =                          // 800A68B8
     63 {
     64     0x00010000,	0x00000000,
     65     0x00000001,	0x00000000,
     66     0x00000000,	0xfffeffff,
     67     0x00000000,	0xffef0000,
     68     0x00000000,	0x00000000,
     69     0x00005555,	0x00000000,
     70     0x00000000,	0xfeed0000,
     71     0x00000000,	0xf7610000
     72 };*/
     73 
     74 Mtx R_ModelMatrix =                             // 8005b0C8
     75 {
     76     0x00010000,	0x00000000,
     77     0x00000001,	0x00000000,
     78     0x00000000,	0x00010000,
     79     0x00000000,	0x00000001,
     80     0x00000000,	0x00000000,
     81     0x00000000,	0x00000000,
     82     0x00000000,	0x00000000,
     83     0x00000000,	0x00000000
     84 };
     85 
     86 /* */
     87 /* precalculated math */
     88 /* */
     89 fixed_t*    finecosine = &finesine[FINEANGLES / 4]; // 8005B890
     90 
     91 int         infraredFactor; // 800A810C
     92 int         FlashEnvColor;  // 800A8110
     93 fixed_t     quakeviewx;     // 800A8118
     94 fixed_t     quakeviewy;     // 800A8114
     95 mobj_t      *cameratarget;  // 800A5D70
     96 angle_t     camviewpitch;   // 800A811C
     97 
     98 fixed_t     scrollfrac;     // 800A812C
     99 sector_t    *frontsector;	// 800A6340
    100 
    101 /*============================================================================= */
    102 
    103 /*
    104 ==============
    105 =
    106 = R_Init
    107 =
    108 ==============
    109 */
    110 
    111 void R_Init(void) // 800233E0
    112 {
    113     R_InitData();
    114     guFrustum(&R_ProjectionMatrix, -8.0f, 8.0f, -6.0f, 6.0f, 8.0f, 3808.0f, 1.0f);
    115 
    116     /*PRINTF_D2(WHITE, 0, 10, "R_ProjectionMatrix[0][0] %x", R_ProjectionMatrix.m[0][0]);
    117     PRINTF_D2(WHITE, 0, 11, "R_ProjectionMatrix[0][1] %x", R_ProjectionMatrix.m[0][1]);
    118     PRINTF_D2(WHITE, 0, 12, "R_ProjectionMatrix[0][2] %x", R_ProjectionMatrix.m[0][2]);
    119     PRINTF_D2(WHITE, 0, 13, "R_ProjectionMatrix[0][3] %x", R_ProjectionMatrix.m[0][3]);
    120     PRINTF_D2(WHITE, 0, 14, "R_ProjectionMatrix[1][0] %x", R_ProjectionMatrix.m[1][0]);
    121     PRINTF_D2(WHITE, 0, 15, "R_ProjectionMatrix[1][1] %x", R_ProjectionMatrix.m[1][1]);
    122     PRINTF_D2(WHITE, 0, 16, "R_ProjectionMatrix[1][2] %x", R_ProjectionMatrix.m[1][2]);
    123     PRINTF_D2(WHITE, 0, 17, "R_ProjectionMatrix[1][3] %x", R_ProjectionMatrix.m[1][3]);
    124     PRINTF_D2(WHITE, 0, 18, "R_ProjectionMatrix[2][0] %x", R_ProjectionMatrix.m[2][0]);
    125     PRINTF_D2(WHITE, 0, 19, "R_ProjectionMatrix[2][1] %x", R_ProjectionMatrix.m[2][1]);
    126     PRINTF_D2(WHITE, 0, 20, "R_ProjectionMatrix[2][2] %x", R_ProjectionMatrix.m[2][2]);
    127     PRINTF_D2(WHITE, 0, 21, "R_ProjectionMatrix[2][3] %x", R_ProjectionMatrix.m[2][3]);
    128     PRINTF_D2(WHITE, 0, 22, "R_ProjectionMatrix[3][0] %x", R_ProjectionMatrix.m[3][0]);
    129     PRINTF_D2(WHITE, 0, 23, "R_ProjectionMatrix[3][1] %x", R_ProjectionMatrix.m[3][1]);
    130     PRINTF_D2(WHITE, 0, 24, "R_ProjectionMatrix[3][2] %x", R_ProjectionMatrix.m[3][2]);
    131     PRINTF_D2(WHITE, 0, 25, "R_ProjectionMatrix[3][3] %x", R_ProjectionMatrix.m[3][3]);
    132     while(1){}*/
    133 }
    134 
    135 
    136 /*
    137 ==============
    138 =
    139 = R_RenderView
    140 =
    141 ==============
    142 */
    143 
    144 void R_RenderPlayerView(void) // 80023448
    145 {
    146 	fixed_t pitch;
    147 	fixed_t Fnear, FnearA, FnearB;
    148 	fixed_t sin, cos;
    149 
    150 	viewplayer = &players[0];
    151 
    152     if (cameratarget == players[0].mo)
    153     {
    154         viewz = players[0].viewz;
    155         pitch = players[0].recoilpitch >> ANGLETOFINESHIFT;
    156     }
    157     else
    158     {
    159         viewz = cameratarget->z;
    160         pitch = camviewpitch >> ANGLETOFINESHIFT;
    161     }
    162 
    163     viewx = cameratarget->x;
    164     viewy = cameratarget->y;
    165     viewz += quakeviewy;
    166 
    167 	viewangle = cameratarget->angle + quakeviewx;
    168 	viewcos = finecosine[viewangle >> ANGLETOFINESHIFT];
    169 	viewsin = finesine[viewangle >> ANGLETOFINESHIFT];
    170 
    171 	// Phase 1
    172 	R_BSP();
    173 
    174 	gDPSetEnvColorD64(GFX1++, FlashEnvColor);
    175 
    176 	// Phase 2
    177 	if (rendersky)
    178     {
    179         R_RenderSKY();
    180         gDPPipeSync(GFX1++);
    181     }
    182 
    183     gDPSetCycleType(GFX1++, G_CYC_2CYCLE);
    184     gDPSetTextureLOD(GFX1++, G_TL_TILE);
    185     gDPSetTextureLUT(GFX1++, G_TT_RGBA16);
    186     gDPSetTexturePersp(GFX1++, G_TP_PERSP);
    187     gDPSetAlphaCompare(GFX1++, G_AC_THRESHOLD);
    188     gDPSetBlendColor(GFX1++, 0, 0, 0, 0);
    189 
    190     gDPSetCombineMode(GFX1++, G_CC_D64COMB07, G_CC_D64COMB08);
    191 
    192     gDPSetRenderMode(GFX1++, G_RM_FOG_SHADE_A, G_RM_TEX_EDGE2);
    193 
    194     FnearA = (1000 - FogNear);
    195     FnearB = ((0-FogNear) << 8) + 128000;
    196     Fnear  = (((128000 / FnearA) << 16) | ((FnearB / FnearA) & 0xffff));
    197     gMoveWd(GFX1++, G_MW_FOG, G_MWO_FOG, Fnear);
    198 
    199     // Apply Fog Color
    200     gDPSetFogColorD64(GFX1++, FogColor);
    201 
    202     sin = finesine[pitch];
    203     cos = finecosine[pitch];
    204 
    205     gSPMatrix(GFX1++, OS_K0_TO_PHYSICAL(MTX1), G_MTX_MODELVIEW| G_MTX_LOAD | G_MTX_NOPUSH);
    206     MTX1->m[0][0] = 0x10000;
    207     MTX1->m[0][1] = 0;
    208     MTX1->m[0][2] = ((cos & 0xffff0000) >> 16);
    209     MTX1->m[0][3] = ((-sin) & 0xffff0000);
    210     MTX1->m[1][0] = ((sin & 0xffff0000) >> 16);
    211     MTX1->m[1][1] = (cos & 0xffff0000);
    212     MTX1->m[1][2] = 0;
    213     MTX1->m[1][3] = 1;
    214     MTX1->m[2][0] = 0;
    215     MTX1->m[2][1] = 0;
    216     MTX1->m[2][2] = (cos & 0xffff);
    217     MTX1->m[2][3] = (((-sin) << 16) & 0xffff0000);
    218     MTX1->m[3][0] = (sin & 0xffff);
    219     MTX1->m[3][1] = ((cos << 16) & 0xffff0000);
    220     MTX1->m[3][2] = 0;
    221     MTX1->m[3][3] = 0;
    222     MTX1++;
    223 
    224     sin = viewsin;
    225     cos = viewcos;
    226 
    227     gSPMatrix(GFX1++, OS_K0_TO_PHYSICAL(MTX1), G_MTX_MODELVIEW| G_MTX_MUL | G_MTX_NOPUSH);
    228     MTX1->m[0][0] = (sin & 0xffff0000);
    229     MTX1->m[0][1] = ((-cos) & 0xffff0000);
    230     MTX1->m[0][2] = 1;
    231     MTX1->m[0][3] = 0;
    232     MTX1->m[1][0] = (cos & 0xffff0000);
    233     MTX1->m[1][1] = (sin & 0xffff0000);
    234     MTX1->m[1][2] = 0;
    235     MTX1->m[1][3] = 1;
    236     MTX1->m[2][0] = ((sin << 16) & 0xffff0000);
    237     MTX1->m[2][1] = (((-cos) << 16) & 0xffff0000);
    238     MTX1->m[2][2] = 0;
    239     MTX1->m[2][3] = 0;
    240     MTX1->m[3][0] = ((cos << 16) & 0xffff0000);
    241     MTX1->m[3][1] = ((sin << 16) & 0xffff0000);
    242     MTX1->m[3][2] = 0;
    243     MTX1->m[3][3] = 0;
    244     MTX1++;
    245 
    246     gSPMatrix(GFX1++, OS_K0_TO_PHYSICAL(MTX1), G_MTX_MODELVIEW| G_MTX_MUL | G_MTX_NOPUSH);
    247     MTX1->m[0][0] = 0x10000;
    248     MTX1->m[0][1] = 0;
    249     MTX1->m[0][2] = 1;
    250     MTX1->m[0][3] = 0;
    251     MTX1->m[1][0] = 0;
    252     MTX1->m[1][1] = 0x10000;
    253     MTX1->m[1][2] = ((-viewx) & 0xffff0000) | (((-viewz) >> 16) & 0xffff);
    254     MTX1->m[1][3] = (viewy & 0xffff0000) | 1;
    255     MTX1->m[2][0] = 0;
    256     MTX1->m[2][1] = 0;
    257     MTX1->m[2][2] = 0;
    258     MTX1->m[2][3] = 0;
    259     MTX1->m[3][0] = 0;
    260     MTX1->m[3][1] = 0;
    261     MTX1->m[3][2] = (((-viewx) << 16) & 0xffff0000) | ((-viewz) & 0xffff);
    262     MTX1->m[3][3] = ((viewy << 16) & 0xffff0000);
    263     MTX1++;
    264 
    265     // Phase 3
    266     R_RenderAll();
    267 
    268     if (cameratarget == viewplayer->mo)
    269         R_RenderPSprites();
    270 }
    271 
    272 /*============================================================================= */
    273 
    274 /*
    275 ===============================================================================
    276 =
    277 = R_PointOnSide
    278 =
    279 = Returns side 0 (front) or 1 (back)
    280 ===============================================================================
    281 */
    282 int	R_PointOnSide(int x, int y, node_t *node) // 80023B6C
    283 {
    284 	fixed_t	dx, dy;
    285 	fixed_t	left, right;
    286 
    287 	if (!node->line.dx)
    288 	{
    289 		if (x <= node->line.x)
    290 			return (node->line.dy > 0);
    291 		return (node->line.dy < 0);
    292 	}
    293 	if (!node->line.dy)
    294 	{
    295 		if (y <= node->line.y)
    296 			return (node->line.dx < 0);
    297 		return (node->line.dx > 0);
    298 	}
    299 
    300 	dx = (x - node->line.x);
    301 	dy = (y - node->line.y);
    302 
    303 	left = (node->line.dy >> 16) * (dx >> 16);
    304 	right = (dy >> 16) * (node->line.dx >> 16);
    305 
    306 	if (right < left)
    307 		return 0;		/* front side */
    308 	return 1;			/* back side */
    309 }
    310 
    311 /*
    312 ==============
    313 =
    314 = R_PointInSubsector
    315 =
    316 ==============
    317 */
    318 
    319 struct subsector_s *R_PointInSubsector(fixed_t x, fixed_t y) // 80023C44
    320 {
    321 	node_t	*node;
    322 	int		side, nodenum;
    323 
    324 	if (!numnodes)				/* single subsector is a special case */
    325 		return subsectors;
    326 
    327 	nodenum = numnodes - 1;
    328 
    329 	while (!(nodenum & NF_SUBSECTOR))
    330 	{
    331 		node = &nodes[nodenum];
    332 		side = R_PointOnSide(x, y, node);
    333 		nodenum = node->children[side];
    334 	}
    335 
    336 	return &subsectors[nodenum & ~NF_SUBSECTOR];
    337 }
    338 
    339 /*
    340 ===============================================================================
    341 =
    342 = R_PointToAngle
    343 =
    344 ===============================================================================
    345 */
    346 
    347 extern	angle_t	tantoangle[SLOPERANGE + 1];
    348 
    349 int SlopeDiv(unsigned num, unsigned den) // 80023D10
    350 {
    351 	unsigned ans;
    352 	if (den < 512)
    353 		return SLOPERANGE;
    354 	ans = (num << 3) / (den >> 8);
    355 	return ans <= SLOPERANGE ? ans : SLOPERANGE;
    356 }
    357 
    358 angle_t R_PointToAngle2(fixed_t x1, fixed_t y1, fixed_t x2, fixed_t y2) // 80023D60
    359 {
    360 	int		x;
    361 	int		y;
    362 
    363 	x = x2 - x1;
    364 	y = y2 - y1;
    365 
    366 	if ((!x) && (!y))
    367 		return 0;
    368 
    369 	if (x >= 0)
    370 	{	/* x >=0 */
    371 		if (y >= 0)
    372 		{	/* y>= 0 */
    373 			if (x>y)
    374 				return tantoangle[SlopeDiv(y, x)];     /* octant 0 */
    375 			else
    376 				return ANG90 - 1 - tantoangle[SlopeDiv(x, y)];  /* octant 1 */
    377 		}
    378 		else
    379 		{	/* y<0 */
    380 			y = -y;
    381 			if (x>y)
    382 				return -tantoangle[SlopeDiv(y, x)];  /* octant 8 */
    383 			else
    384 				return ANG270 + tantoangle[SlopeDiv(x, y)];  /* octant 7 */
    385 		}
    386 	}
    387 	else
    388 	{	/* x<0 */
    389 		x = -x;
    390 		if (y >= 0)
    391 		{	/* y>= 0 */
    392 			if (x>y)
    393 				return ANG180 - 1 - tantoangle[SlopeDiv(y, x)]; /* octant 3 */
    394 			else
    395 				return ANG90 + tantoangle[SlopeDiv(x, y)];  /* octant 2 */
    396 		}
    397 		else
    398 		{	/* y<0 */
    399 			y = -y;
    400 			if (x>y)
    401 				return ANG180 + tantoangle[SlopeDiv(y, x)];  /* octant 4 */
    402 			else
    403 				return ANG270 - 1 - tantoangle[SlopeDiv(x, y)];  /* octant 5 */
    404 		}
    405 	}
    406 }