2 * Glide64 - Glide video plugin for Nintendo 64 emulators.
3 * Copyright (c) 2002 Dave2001
4 * Copyright (c) 2003-2009 Sergey 'Gonetz' Lipski
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 //****************************************************************
23 // Glide64 - Glide Plugin for Nintendo 64 emulators
24 // Project started on December 29th, 2001
27 // Dave2001, original author, founded the project in 2001, left it in 2002
28 // Gugaman, joined the project in 2002, left it in 2002
29 // Sergey 'Gonetz' Lipski, joined the project in 2002, main author since fall of 2002
30 // Hiroshi 'KoolSmoky' Morii, joined the project in 2007
32 //****************************************************************
35 // * Write your name and (optional)email, commented by your work, so I know who did it, and so that you can find which parts you modified when it comes time to send it to me.
36 // * Do NOT send me the whole project or file that you modified. Take out your modified code sections, and tell me where to put them. If people sent the whole thing, I would have many different versions, but no idea how to combine them all.
38 //****************************************************************
47 #include "DepthBufferRender.h"
49 #define Vj rdp.vtxbuf2[j]
50 #define Vi rdp.vtxbuf2[i]
52 VERTEX *vtx_list1[32]; // vertex indexing
53 VERTEX *vtx_list2[32];
56 // util_init - initialize data for the functions in this file
61 for (int i=0; i<32; i++)
63 vtx_list1[i] = &rdp.vtx1[i];
64 vtx_list2[i] = &rdp.vtx2[i];
68 static wxUint32 u_cull_mode = 0;
70 //software backface culling. Gonetz
71 // mega modifications by Dave2001
72 int cull_tri(VERTEX **v) // type changed to VERTEX** [Dave2001]
76 if (v[0]->scr_off & v[1]->scr_off & v[2]->scr_off)
82 // Triangle can't be culled, if it need clipping
87 if (!v[i]->screen_translated)
89 v[i]->sx = rdp.view_trans[0] + v[i]->x_w * rdp.view_scale[0] + rdp.offset_x;
90 v[i]->sy = rdp.view_trans[1] + v[i]->y_w * rdp.view_scale[1] + rdp.offset_y;
91 v[i]->sz = rdp.view_trans[2] + v[i]->z_w * rdp.view_scale[2];
92 v[i]->screen_translated = 1;
94 if (v[i]->w < 0.01f) //need clip_z. can't be culled now
98 u_cull_mode = (rdp.flags & CULLMASK);
99 if (draw || u_cull_mode == 0 || u_cull_mode == CULLMASK) //no culling set
101 u_cull_mode >>= CULLSHIFT;
107 #if 1 // H.Morii - faster float comparisons with zero area check added
109 const float x1 = v[0]->sx - v[1]->sx;
110 const float y1 = v[0]->sy - v[1]->sy;
111 const float x2 = v[2]->sx - v[1]->sx;
112 const float y2 = v[2]->sy - v[1]->sy;
113 const float area = y1*x2 - x1*y2;
115 const int iarea = *(int*)&area;
116 const unsigned int mode = (u_cull_mode << 19UL);
117 u_cull_mode >>= CULLSHIFT;
119 if ((iarea & 0x7FFFFFFF) == 0)
121 LRDP (" zero area triangles\n");
125 if ((rdp.flags & CULLMASK) && ((int)(iarea ^ mode)) >= 0)
132 float x1 = v[0]->sx - v[1]->sx;
133 float y1 = v[0]->sy - v[1]->sy;
134 float x2 = v[2]->sx - v[1]->sx;
135 float y2 = v[2]->sy - v[1]->sy;
137 u_cull_mode >>= CULLSHIFT;
140 case 1: // cull front
141 // if ((x1*y2 - y1*x2) < 0.0f) //counter-clockwise, positive
142 if ((y1*x2-x1*y2) < 0.0f) //counter-clockwise, positive
149 // if ((x1*y2 - y1*x2) >= 0.0f) //clockwise, negative
150 if ((y1*x2-x1*y2) >= 0.0f) //clockwise, negative
164 void apply_shade_mods (VERTEX *v)
168 memcpy (col, rdp.col, 16);
172 if (v->shade_mod == 0)
173 v->color_backup = *(wxUint32*)(&(v->b));
175 *(wxUint32*)(&(v->b)) = v->color_backup;
179 if (col[0] > 1.0f) col[0] = 1.0f;
180 if (col[1] > 1.0f) col[1] = 1.0f;
181 if (col[2] > 1.0f) col[2] = 1.0f;
182 if (col[0] < 0.0f) col[0] = 0.0f;
183 if (col[1] < 0.0f) col[1] = 0.0f;
184 if (col[2] < 0.0f) col[2] = 0.0f;
185 v->r = (wxUint8)(255.0f * col[0]);
186 v->g = (wxUint8)(255.0f * col[1]);
187 v->b = (wxUint8)(255.0f * col[2]);
191 if (col[3] > 1.0f) col[3] = 1.0f;
192 if (col[3] < 0.0f) col[3] = 0.0f;
193 v->a = (wxUint8)(255.0f * col[3]);
195 if (mod & CMB_SETSHADE_SHADEALPHA)
197 v->r = v->g = v->b = v->a;
199 if (mod & CMB_MULT_OWN_ALPHA)
201 float percent = v->a / 255.0f;
202 v->r = (wxUint8)(v->r * percent);
203 v->g = (wxUint8)(v->g * percent);
204 v->b = (wxUint8)(v->b * percent);
208 if (col[0] > 1.0f) col[0] = 1.0f;
209 if (col[1] > 1.0f) col[1] = 1.0f;
210 if (col[2] > 1.0f) col[2] = 1.0f;
211 if (col[0] < 0.0f) col[0] = 0.0f;
212 if (col[1] < 0.0f) col[1] = 0.0f;
213 if (col[2] < 0.0f) col[2] = 0.0f;
214 v->r = (wxUint8)(v->r * col[0]);
215 v->g = (wxUint8)(v->g * col[1]);
216 v->b = (wxUint8)(v->b * col[2]);
218 if (mod & CMB_A_MULT)
220 if (col[3] > 1.0f) col[3] = 1.0f;
221 if (col[3] < 0.0f) col[3] = 0.0f;
222 v->a = (wxUint8)(v->a * col[3]);
226 int r = v->r - (int)(255.0f * rdp.coladd[0]);
227 int g = v->g - (int)(255.0f * rdp.coladd[1]);
228 int b = v->b - (int)(255.0f * rdp.coladd[2]);
238 int a = v->a - (int)(255.0f * rdp.coladd[3]);
244 int r = v->r + (int)(255.0f * rdp.coladd[0]);
245 int g = v->g + (int)(255.0f * rdp.coladd[1]);
246 int b = v->b + (int)(255.0f * rdp.coladd[2]);
247 if (r > 255) r = 255;
248 if (g > 255) g = 255;
249 if (b > 255) b = 255;
256 int a = v->a + (int)(255.0f * rdp.coladd[3]);
257 if (a > 255) a = 255;
260 if (mod & CMB_COL_SUB_OWN)
262 int r = (wxUint8)(255.0f * rdp.coladd[0]) - v->r;
263 int g = (wxUint8)(255.0f * rdp.coladd[1]) - v->g;
264 int b = (wxUint8)(255.0f * rdp.coladd[2]) - v->b;
272 v->shade_mod = cmb.shade_mod_hash;
274 if (rdp.cmb_flags_2 & CMB_INTER)
276 v->r = (wxUint8)(rdp.col_2[0] * rdp.shade_factor * 255.0f + v->r * (1.0f - rdp.shade_factor));
277 v->g = (wxUint8)(rdp.col_2[1] * rdp.shade_factor * 255.0f + v->g * (1.0f - rdp.shade_factor));
278 v->b = (wxUint8)(rdp.col_2[2] * rdp.shade_factor * 255.0f + v->b * (1.0f - rdp.shade_factor));
279 v->shade_mod = cmb.shade_mod_hash;
284 static int deltaZ = 0;
287 void draw_tri (VERTEX **vtx, wxUint16 linew)
290 if (linew == 0 && (fb_depth_render_enabled || (rdp.rm & 0xC00) == 0xC00))
292 float X0 = vtx[0]->sx / rdp.scale_x;
293 float Y0 = vtx[0]->sy / rdp.scale_y;
294 float X1 = vtx[1]->sx / rdp.scale_x;
295 float Y1 = vtx[1]->sy / rdp.scale_y;
296 float X2 = vtx[2]->sx / rdp.scale_x;
297 float Y2 = vtx[2]->sy / rdp.scale_y;
298 float diffy_02 = Y0 - Y2;
299 float diffy_12 = Y1 - Y2;
300 float diffx_02 = X0 - X2;
301 float diffx_12 = X1 - X2;
303 float denom = (diffx_02 * diffy_12 - diffx_12 * diffy_02);
304 if(denom*denom > 0.0f)
306 float diffz_02 = vtx[0]->sz - vtx[2]->sz;
307 float diffz_12 = vtx[1]->sz - vtx[2]->sz;
308 float fdzdx = (diffz_02 * diffy_12 - diffz_12 * diffy_02) / denom;
309 if ((rdp.rm & 0xC00) == 0xC00) {
310 // Calculate deltaZ per polygon for Decal z-mode
311 float fdzdy = (diffz_02 * diffx_12 - diffz_12 * diffx_02) / denom;
312 float fdz = fabs(fdzdx) + fabs(fdzdy);
313 if ((settings.hacks & hack_Zelda) && (rdp.rm & 0x800))
314 fdz *= 4.0f; // Decal mode in Zelda sometimes needs mutiplied deltaZ to work correct, e.g. roads
315 deltaZ = max(8, (int)fdz);
317 dzdx = (int)(fdzdx * 65536.0);
323 for (int i=0; i<3; i++)
327 if (v->uv_calculated != rdp.tex_ctr)
329 #ifdef EXTREME_LOGGING
330 FRDP(" * CALCULATING VERTEX U/V: %d\n", v->number);
332 v->uv_calculated = rdp.tex_ctr;
334 if (!(rdp.geom_mode & 0x00020000))
336 if (!(rdp.geom_mode & 0x00000200))
338 if (rdp.geom_mode & 0x00000004) // flat shading
340 int flag = min(2, (rdp.cmd1 >> 24) & 3);
345 #ifdef EXTREME_LOGGING
346 FRDP(" * Flat shaded, flag%d - r: %d, g: %d, b: %d, a: %d\n", flag, v->r, v->g, v->b, v->a);
351 #ifdef EXTREME_LOGGING
352 FRDP(" * Prim shaded %08lx\n", rdp.prim_color);
354 v->a = (wxUint8)(rdp.prim_color & 0xFF);
355 v->b = (wxUint8)((rdp.prim_color >> 8) & 0xFF);
356 v->g = (wxUint8)((rdp.prim_color >> 16) & 0xFF);
357 v->r = (wxUint8)((rdp.prim_color >> 24) & 0xFF);
362 // Fix texture coordinates
365 v->ou *= rdp.tiles[rdp.cur_tile].s_scale;
366 v->ov *= rdp.tiles[rdp.cur_tile].t_scale;
370 // v->oow = v->w = 1.0f;
375 v->u1 = v->u0 = v->ou;
376 v->v1 = v->v0 = v->ov;
378 if (rdp.tex >= 1 && rdp.cur_cache[0])
380 if (rdp.aTBuffTex[0])
382 v->u0 += rdp.aTBuffTex[0]->u_shift + rdp.aTBuffTex[0]->tile_uls;
383 v->v0 += rdp.aTBuffTex[0]->v_shift + rdp.aTBuffTex[0]->tile_ult;
386 if (rdp.tiles[rdp.cur_tile].shift_s)
388 if (rdp.tiles[rdp.cur_tile].shift_s > 10)
389 v->u0 *= (float)(1 << (16 - rdp.tiles[rdp.cur_tile].shift_s));
391 v->u0 /= (float)(1 << rdp.tiles[rdp.cur_tile].shift_s);
393 if (rdp.tiles[rdp.cur_tile].shift_t)
395 if (rdp.tiles[rdp.cur_tile].shift_t > 10)
396 v->v0 *= (float)(1 << (16 - rdp.tiles[rdp.cur_tile].shift_t));
398 v->v0 /= (float)(1 << rdp.tiles[rdp.cur_tile].shift_t);
401 if (rdp.aTBuffTex[0])
403 if (rdp.aTBuffTex[0]->tile_uls != (int)rdp.tiles[rdp.cur_tile].f_ul_s)
404 v->u0 -= rdp.tiles[rdp.cur_tile].f_ul_s;
405 if (rdp.aTBuffTex[0]->tile_ult != (int)rdp.tiles[rdp.cur_tile].f_ul_t || (settings.hacks&hack_Megaman))
406 v->v0 -= rdp.tiles[rdp.cur_tile].f_ul_t; //required for megaman (boss special attack)
407 v->u0 *= rdp.aTBuffTex[0]->u_scale;
408 v->v0 *= rdp.aTBuffTex[0]->v_scale;
409 #ifdef EXTREME_LOGGING
410 FRDP("tbuff_tex t0: (%f, %f)->(%f, %f)\n", v->ou, v->ov, v->u0, v->v0);
415 v->u0 -= rdp.tiles[rdp.cur_tile].f_ul_s;
416 v->v0 -= rdp.tiles[rdp.cur_tile].f_ul_t;
417 v->u0 = rdp.cur_cache[0]->c_off + rdp.cur_cache[0]->c_scl_x * v->u0;
418 v->v0 = rdp.cur_cache[0]->c_off + rdp.cur_cache[0]->c_scl_y * v->v0;
420 v->u0_w = v->u0 / v->w;
421 v->v0_w = v->v0 / v->w;
424 if (rdp.tex >= 2 && rdp.cur_cache[1])
426 if (rdp.aTBuffTex[1])
428 v->u1 += rdp.aTBuffTex[1]->u_shift + rdp.aTBuffTex[1]->tile_uls;
429 v->v1 += rdp.aTBuffTex[1]->v_shift + rdp.aTBuffTex[1]->tile_ult;
431 if (rdp.tiles[rdp.cur_tile+1].shift_s)
433 if (rdp.tiles[rdp.cur_tile+1].shift_s > 10)
434 v->u1 *= (float)(1 << (16 - rdp.tiles[rdp.cur_tile+1].shift_s));
436 v->u1 /= (float)(1 << rdp.tiles[rdp.cur_tile+1].shift_s);
438 if (rdp.tiles[rdp.cur_tile+1].shift_t)
440 if (rdp.tiles[rdp.cur_tile+1].shift_t > 10)
441 v->v1 *= (float)(1 << (16 - rdp.tiles[rdp.cur_tile+1].shift_t));
443 v->v1 /= (float)(1 << rdp.tiles[rdp.cur_tile+1].shift_t);
446 if (rdp.aTBuffTex[1])
448 if (rdp.aTBuffTex[1]->tile_uls != (int)rdp.tiles[rdp.cur_tile].f_ul_s)
449 v->u1 -= rdp.tiles[rdp.cur_tile].f_ul_s;
450 v->u1 *= rdp.aTBuffTex[1]->u_scale;
451 v->v1 *= rdp.aTBuffTex[1]->v_scale;
452 #ifdef EXTREME_LOGGING
453 FRDP("tbuff_tex t1: (%f, %f)->(%f, %f)\n", v->ou, v->ov, v->u1, v->v1);
458 v->u1 -= rdp.tiles[rdp.cur_tile+1].f_ul_s;
459 v->v1 -= rdp.tiles[rdp.cur_tile+1].f_ul_t;
460 v->u1 = rdp.cur_cache[1]->c_off + rdp.cur_cache[1]->c_scl_x * v->u1;
461 v->v1 = rdp.cur_cache[1]->c_off + rdp.cur_cache[1]->c_scl_y * v->v1;
464 v->u1_w = v->u1 / v->w;
465 v->v1_w = v->v1 / v->w;
467 // FRDP(" * CALCULATING VERTEX U/V: %d u0: %f, v0: %f, u1: %f, v1: %f\n", v->number, v->u0, v->v0, v->u1, v->v1);
469 #ifdef EXTREME_LOGGING
470 FRDP("draw_tri. v[%d] ou=%f, ov = %f\n", i, v->ou, v->ov);
472 if (v->shade_mod != cmb.shade_mod_hash)
473 apply_shade_mods (v);
478 if ((vtx[0]->scr_off & 16) ||
479 (vtx[1]->scr_off & 16) ||
480 (vtx[2]->scr_off & 16))
481 rdp.clip |= CLIP_WMIN;
483 vtx[0]->not_zclipped = vtx[1]->not_zclipped = vtx[2]->not_zclipped = 1;
485 if (rdp.cur_cache[0] && (rdp.tex & 1) && (rdp.cur_cache[0]->splits > 1) && !rdp.aTBuffTex[0] && !rdp.clip)
487 int index,i,j, min_256,max_256, cur_256,left_256,right_256;
490 min_256 = min((int)vtx[0]->u0,(int)vtx[1]->u0); // bah, don't put two mins on one line
491 min_256 = min(min_256,(int)vtx[2]->u0) >> 8; // or it will be calculated twice
493 max_256 = max((int)vtx[0]->u0,(int)vtx[1]->u0); // not like it makes much difference
494 max_256 = max(max_256,(int)vtx[2]->u0) >> 8; // anyway :P
496 for (cur_256=min_256; cur_256<=max_256; cur_256++)
498 left_256 = cur_256 << 8;
499 right_256 = (cur_256+1) << 8;
501 // Set vertex buffers
502 rdp.vtxbuf = rdp.vtx1; // copy from v to rdp.vtx1
503 rdp.vtxbuf2 = rdp.vtx2;
517 if (v1->u0 >= left_256)
519 if (v2->u0 >= left_256) // Both are in, save the last one
521 rdp.vtxbuf[index] = *v2;
522 rdp.vtxbuf[index].u0 -= left_256;
523 rdp.vtxbuf[index++].v0 += cur_256 * rdp.cur_cache[0]->splitheight;
525 else // First is in, second is out, save intersection
527 percent = (left_256 - v1->u0) / (v2->u0 - v1->u0);
528 rdp.vtxbuf[index].x = v1->x + (v2->x - v1->x) * percent;
529 rdp.vtxbuf[index].y = v1->y + (v2->y - v1->y) * percent;
530 rdp.vtxbuf[index].z = v1->z + (v2->z - v1->z) * percent;
531 rdp.vtxbuf[index].w = v1->w + (v2->w - v1->w) * percent;
532 rdp.vtxbuf[index].f = v1->f + (v2->f - v1->f) * percent;
533 rdp.vtxbuf[index].u0 = 0.5f;
534 rdp.vtxbuf[index].v0 = v1->v0 + (v2->v0 - v1->v0) * percent +
535 cur_256 * rdp.cur_cache[0]->splitheight;
536 rdp.vtxbuf[index].u1 = v1->u1 + (v2->u1 - v1->u1) * percent;
537 rdp.vtxbuf[index].v1 = v1->v1 + (v2->v1 - v1->v1) * percent;
538 rdp.vtxbuf[index].b = (wxUint8)(v1->b + (v2->b - v1->b) * percent);
539 rdp.vtxbuf[index].g = (wxUint8)(v1->g + (v2->g - v1->g) * percent);
540 rdp.vtxbuf[index].r = (wxUint8)(v1->r + (v2->r - v1->r) * percent);
541 rdp.vtxbuf[index++].a = (wxUint8)(v1->a + (v2->a - v1->a) * percent);
546 //if (v2->u0 < left_256) // Both are out, save nothing
547 if (v2->u0 >= left_256) // First is out, second is in, save intersection & in point
549 percent = (left_256 - v2->u0) / (v1->u0 - v2->u0);
550 rdp.vtxbuf[index].x = v2->x + (v1->x - v2->x) * percent;
551 rdp.vtxbuf[index].y = v2->y + (v1->y - v2->y) * percent;
552 rdp.vtxbuf[index].z = v2->z + (v1->z - v2->z) * percent;
553 rdp.vtxbuf[index].w = v2->w + (v1->w - v2->w) * percent;
554 rdp.vtxbuf[index].f = v2->f + (v1->f - v2->f) * percent;
555 rdp.vtxbuf[index].u0 = 0.5f;
556 rdp.vtxbuf[index].v0 = v2->v0 + (v1->v0 - v2->v0) * percent +
557 cur_256 * rdp.cur_cache[0]->splitheight;
558 rdp.vtxbuf[index].u1 = v2->u1 + (v1->u1 - v2->u1) * percent;
559 rdp.vtxbuf[index].v1 = v2->v1 + (v1->v1 - v2->v1) * percent;
560 rdp.vtxbuf[index].b = (wxUint8)(v2->b + (v1->b - v2->b) * percent);
561 rdp.vtxbuf[index].g = (wxUint8)(v2->g + (v1->g - v2->g) * percent);
562 rdp.vtxbuf[index].r = (wxUint8)(v2->r + (v1->r - v2->r) * percent);
563 rdp.vtxbuf[index++].a = (wxUint8)(v2->a + (v1->a - v2->a) * percent);
566 rdp.vtxbuf[index] = *v2;
567 rdp.vtxbuf[index].u0 -= left_256;
568 rdp.vtxbuf[index++].v0 += cur_256 * rdp.cur_cache[0]->splitheight;
572 rdp.n_global = index;
574 rdp.vtxbuf = rdp.vtx2; // now vtx1 holds the value, & vtx2 is the destination
575 rdp.vtxbuf2 = rdp.vtx1;
579 for (i=0; i<rdp.n_global; i++)
582 if (j == rdp.n_global) j = 0;
584 VERTEX *v1 = &rdp.vtxbuf2[i];
585 VERTEX *v2 = &rdp.vtxbuf2[j];
588 if (v1->u0 <= right_256)
590 if (v2->u0 <= right_256) // Both are in, save the last one
592 rdp.vtxbuf[index] = *v2;
593 rdp.vtxbuf[index++].not_zclipped = 0;
595 else // First is in, second is out, save intersection
597 percent = (right_256 - v1->u0) / (v2->u0 - v1->u0);
598 rdp.vtxbuf[index].x = v1->x + (v2->x - v1->x) * percent;
599 rdp.vtxbuf[index].y = v1->y + (v2->y - v1->y) * percent;
600 rdp.vtxbuf[index].z = v1->z + (v2->z - v1->z) * percent;
601 rdp.vtxbuf[index].w = v1->w + (v2->w - v1->w) * percent;
602 rdp.vtxbuf[index].f = v1->f + (v2->f - v1->f) * percent;
603 rdp.vtxbuf[index].u0 = 255.5f;
604 rdp.vtxbuf[index].v0 = v1->v0 + (v2->v0 - v1->v0) * percent;
605 rdp.vtxbuf[index].u1 = v1->u1 + (v2->u1 - v1->u1) * percent;
606 rdp.vtxbuf[index].v1 = v1->v1 + (v2->v1 - v1->v1) * percent;
607 rdp.vtxbuf[index].b = (wxUint8)(v1->b + (v2->b - v1->b) * percent);
608 rdp.vtxbuf[index].g = (wxUint8)(v1->g + (v2->g - v1->g) * percent);
609 rdp.vtxbuf[index].r = (wxUint8)(v1->r + (v2->r - v1->r) * percent);
610 rdp.vtxbuf[index].a = (wxUint8)(v1->a + (v2->a - v1->a) * percent);
611 rdp.vtxbuf[index++].not_zclipped = 0;
616 //if (v2->u0 > 256.0f) // Both are out, save nothing
617 if (v2->u0 <= right_256) // First is out, second is in, save intersection & in point
619 percent = (right_256 - v2->u0) / (v1->u0 - v2->u0);
620 rdp.vtxbuf[index].x = v2->x + (v1->x - v2->x) * percent;
621 rdp.vtxbuf[index].y = v2->y + (v1->y - v2->y) * percent;
622 rdp.vtxbuf[index].z = v2->z + (v1->z - v2->z) * percent;
623 rdp.vtxbuf[index].w = v2->w + (v1->w - v2->w) * percent;
624 rdp.vtxbuf[index].f = v2->f + (v1->f - v2->f) * percent;
625 rdp.vtxbuf[index].u0 = 255.5f;
626 rdp.vtxbuf[index].v0 = v2->v0 + (v1->v0 - v2->v0) * percent;
627 rdp.vtxbuf[index].u1 = v2->u1 + (v1->u1 - v2->u1) * percent;
628 rdp.vtxbuf[index].v1 = v2->v1 + (v1->v1 - v2->v1) * percent;
629 rdp.vtxbuf[index].b = (wxUint8)(v2->b + (v1->b - v2->b) * percent);
630 rdp.vtxbuf[index].g = (wxUint8)(v2->g + (v1->g - v2->g) * percent);
631 rdp.vtxbuf[index].r = (wxUint8)(v2->r + (v1->r - v2->r) * percent);
632 rdp.vtxbuf[index].a = (wxUint8)(v2->a + (v1->a - v2->a) * percent);
633 rdp.vtxbuf[index++].not_zclipped = 0;
636 rdp.vtxbuf[index] = *v2;
637 rdp.vtxbuf[index++].not_zclipped = 0;
641 rdp.n_global = index;
643 do_triangle_stuff (linew, TRUE);
648 // Set vertex buffers
649 rdp.vtxbuf = rdp.vtx1; // copy from v to rdp.vtx1
650 rdp.vtxbuf2 = rdp.vtx2;
654 rdp.vtxbuf[0] = *vtx[0];
655 rdp.vtxbuf[0].number = 1;
656 rdp.vtxbuf[1] = *vtx[1];
657 rdp.vtxbuf[1].number = 2;
658 rdp.vtxbuf[2] = *vtx[2];
659 rdp.vtxbuf[2].number = 4;
661 do_triangle_stuff (linew, FALSE);
665 #define interp2p(a, b, r) (a + (b - a) * r)
668 static void InterpolateColors(VERTEX & va, VERTEX & vb, VERTEX & res, float percent)
670 res.b = (wxUint8)interp2p(va.b, vb.b, percent);
671 res.g = (wxUint8)interp2p(va.g, vb.g, percent);;
672 res.r = (wxUint8)interp2p(va.r, vb.r, percent);;
673 res.a = (wxUint8)interp2p(va.a, vb.a, percent);;
674 res.f = interp2p(va.f, vb.f, percent);;
678 // clip_w - clips aint the z-axis
680 static void clip_w (int interpolate_colors)
682 int i,j,index,n=rdp.n_global;
684 // Swap vertex buffers
685 VERTEX *tmp = rdp.vtxbuf2;
686 rdp.vtxbuf2 = rdp.vtxbuf;
691 // Check the vertices for clipping
699 if (Vj.w >= 0.01f) // Both are in, save the last one
701 rdp.vtxbuf[index] = Vj;
702 rdp.vtxbuf[index++].not_zclipped = 1;
704 else // First is in, second is out, save intersection
706 percent = (-Vi.w) / (Vj.w - Vi.w);
707 rdp.vtxbuf[index].not_zclipped = 0;
708 rdp.vtxbuf[index].x = Vi.x + (Vj.x - Vi.x) * percent;
709 rdp.vtxbuf[index].y = Vi.y + (Vj.y - Vi.y) * percent;
710 rdp.vtxbuf[index].z = Vi.z + (Vj.z - Vi.z) * percent;
711 rdp.vtxbuf[index].w = 0.01f;
712 rdp.vtxbuf[index].u0 = Vi.u0 + (Vj.u0 - Vi.u0) * percent;
713 rdp.vtxbuf[index].v0 = Vi.v0 + (Vj.v0 - Vi.v0) * percent;
714 rdp.vtxbuf[index].u1 = Vi.u1 + (Vj.u1 - Vi.u1) * percent;
715 rdp.vtxbuf[index].v1 = Vi.v1 + (Vj.v1 - Vi.v1) * percent;
716 if (interpolate_colors)
717 InterpolateColors(Vi, Vj, rdp.vtxbuf[index++], percent);
719 rdp.vtxbuf[index++].number = Vi.number | Vj.number;
724 //if (Vj.w < 0.01f) // Both are out, save nothing
725 if (Vj.w >= 0.01f) // First is out, second is in, save intersection & in point
727 percent = (-Vj.w) / (Vi.w - Vj.w);
728 rdp.vtxbuf[index].not_zclipped = 0;
729 rdp.vtxbuf[index].x = Vj.x + (Vi.x - Vj.x) * percent;
730 rdp.vtxbuf[index].y = Vj.y + (Vi.y - Vj.y) * percent;
731 rdp.vtxbuf[index].z = Vj.z + (Vi.z - Vj.z) * percent;
732 rdp.vtxbuf[index].w = 0.01f;
733 rdp.vtxbuf[index].u0 = Vj.u0 + (Vi.u0 - Vj.u0) * percent;
734 rdp.vtxbuf[index].v0 = Vj.v0 + (Vi.v0 - Vj.v0) * percent;
735 rdp.vtxbuf[index].u1 = Vj.u1 + (Vi.u1 - Vj.u1) * percent;
736 rdp.vtxbuf[index].v1 = Vj.v1 + (Vi.v1 - Vj.v1) * percent;
737 if (interpolate_colors)
738 InterpolateColors(Vj, Vi, rdp.vtxbuf[index++], percent);
740 rdp.vtxbuf[index++].number = Vi.number | Vj.number;
743 rdp.vtxbuf[index] = Vj;
744 rdp.vtxbuf[index++].not_zclipped = 1;
748 rdp.n_global = index;
751 static void render_tri (wxUint16 linew, int old_interpolate);
753 void do_triangle_stuff (wxUint16 linew, int old_interpolate) // what else?? do the triangle stuff :P (to keep from writing code twice)
757 if (rdp.clip & CLIP_WMIN)
758 clip_w (old_interpolate);
760 float maxZ = (rdp.zsrc != 1) ? rdp.view_trans[2] + rdp.view_scale[2] : rdp.prim_depth;
763 for (i=0; i<rdp.n_global; i++)
765 if (rdp.vtxbuf[i].not_zclipped)// && rdp.zsrc != 1)
767 #ifdef EXTREME_LOGGING
768 FRDP (" * NOT ZCLIPPPED: %d\n", rdp.vtxbuf[i].number);
770 rdp.vtxbuf[i].x = rdp.vtxbuf[i].sx;
771 rdp.vtxbuf[i].y = rdp.vtxbuf[i].sy;
772 rdp.vtxbuf[i].z = rdp.vtxbuf[i].sz;
773 rdp.vtxbuf[i].q = rdp.vtxbuf[i].oow;
774 rdp.vtxbuf[i].u0 = rdp.vtxbuf[i].u0_w;
775 rdp.vtxbuf[i].v0 = rdp.vtxbuf[i].v0_w;
776 rdp.vtxbuf[i].u1 = rdp.vtxbuf[i].u1_w;
777 rdp.vtxbuf[i].v1 = rdp.vtxbuf[i].v1_w;
781 #ifdef EXTREME_LOGGING
782 FRDP (" * ZCLIPPED: %d\n", rdp.vtxbuf[i].number);
784 rdp.vtxbuf[i].q = 1.0f / rdp.vtxbuf[i].w;
785 rdp.vtxbuf[i].x = rdp.view_trans[0] + rdp.vtxbuf[i].x * rdp.vtxbuf[i].q * rdp.view_scale[0] + rdp.offset_x;
786 rdp.vtxbuf[i].y = rdp.view_trans[1] + rdp.vtxbuf[i].y * rdp.vtxbuf[i].q * rdp.view_scale[1] + rdp.offset_y;
787 rdp.vtxbuf[i].z = rdp.view_trans[2] + rdp.vtxbuf[i].z * rdp.vtxbuf[i].q * rdp.view_scale[2];
790 rdp.vtxbuf[i].u0 *= rdp.vtxbuf[i].q;
791 rdp.vtxbuf[i].v0 *= rdp.vtxbuf[i].q;
795 rdp.vtxbuf[i].u1 *= rdp.vtxbuf[i].q;
796 rdp.vtxbuf[i].v1 *= rdp.vtxbuf[i].q;
801 rdp.vtxbuf[i].z = rdp.prim_depth;
803 // Don't remove clipping, or it will freeze
804 if (rdp.vtxbuf[i].x > rdp.clip_max_x) rdp.clip |= CLIP_XMAX;
805 if (rdp.vtxbuf[i].x < rdp.clip_min_x) rdp.clip |= CLIP_XMIN;
806 if (rdp.vtxbuf[i].y > rdp.clip_max_y) rdp.clip |= CLIP_YMAX;
807 if (rdp.vtxbuf[i].y < rdp.clip_min_y) rdp.clip |= CLIP_YMIN;
808 if (rdp.vtxbuf[i].z > maxZ) rdp.clip |= CLIP_ZMAX;
809 if (rdp.vtxbuf[i].z < 0.0f) rdp.clip |= CLIP_ZMIN;
810 no_clip &= rdp.vtxbuf[i].screen_translated;
816 if (!settings.clip_zmin)
817 rdp.clip &= ~CLIP_ZMIN;
818 if (!settings.clip_zmax)
819 rdp.clip &= ~CLIP_ZMAX;
821 render_tri (linew, old_interpolate);
824 void do_triangle_stuff_2 (wxUint16 linew)
828 for (int i=0; i<rdp.n_global; i++)
830 // Don't remove clipping, or it will freeze
831 if (rdp.vtxbuf[i].x > rdp.clip_max_x) rdp.clip |= CLIP_XMAX;
832 if (rdp.vtxbuf[i].x < rdp.clip_min_x) rdp.clip |= CLIP_XMIN;
833 if (rdp.vtxbuf[i].y > rdp.clip_max_y) rdp.clip |= CLIP_YMAX;
834 if (rdp.vtxbuf[i].y < rdp.clip_min_y) rdp.clip |= CLIP_YMIN;
837 render_tri (linew, TRUE);
840 __inline wxUint8 real_to_char(double x)
842 return (wxUint8)(((int)floor(x+0.5))&0xFF);
846 static void InterpolateColors2(VERTEX & va, VERTEX & vb, VERTEX & res, float percent)
848 float w = 1.0f/(va.oow + (vb.oow-va.oow) * percent);
849 // res.oow = va.oow + (vb.oow-va.oow) * percent;
851 float ba = va.b * va.oow;
852 float bb = vb.b * vb.oow;
853 res.b = real_to_char(interp2p(ba, bb, percent) * w);
854 float ga = va.g * va.oow;
855 float gb = vb.g * vb.oow;
856 res.g = real_to_char(interp2p(ga, gb, percent) * w);
857 float ra = va.r * va.oow;
858 float rb = vb.r * vb.oow;
859 res.r = real_to_char(interp2p(ra, rb, percent) * w);
860 float aa = va.a * va.oow;
861 float ab = vb.a * vb.oow;
862 res.a = real_to_char(interp2p(aa, ab, percent) * w);
863 float fa = va.f * va.oow;
864 float fb = vb.f * vb.oow;
865 res.f = interp2p(fa, fb, percent) * w;
867 float u0a = va.u0_w * va.oow;
868 float u0b = vb.u0_w * vb.oow;
869 res.u0 = (u0a + (u0b - u0a) * percent) * w;
870 float v0a = va.v0_w * va.oow;
871 float v0b = vb.v0_w * vb.oow;
872 res.v0 = (v0a + (v0b - v0a) * percent) * w;
873 float u1a = va.u1_w * va.oow;
874 float u1b = vb.u1_w * vb.oow;
875 res.u1 = (u1a + (u1b - u1a) * percent) * w;
876 float v1a = va.v1_w * va.oow;
877 float v1b = vb.v1_w * vb.oow;
878 res.v1 = (v1a + (v1b - v1a) * percent) * w;
884 float d; //*SEB* was doubles
889 static float EvaLine(LineEuqationType &li, float x, float y) //*SEB* all double before
891 return li.x*x+li.y*y+li.d;
894 static void Create1LineEq(LineEuqationType &l, VERTEX &v1, VERTEX &v2, VERTEX &v3)
896 // Line between (x1,y1) to (x2,y2)
899 l.d = -(l.x*v2.sx+(l.y)*v2.sy);
900 if (EvaLine(l,v3.sx,v3.sy)*v3.oow < 0)
909 __inline float interp3p(float a, float b, float c, float r1, float r2) //*SEB* r1 and r2 and function was double
911 return (a)+(((b)+((c)-(b))*(r2))-(a))*(r1);
914 #define interp3p(a, b, c, r1, r2) \
915 (a+(((b)+((c)-(b))*(r2))-(a))*(r1))
918 static void InterpolateColors3(VERTEX &v1, VERTEX &v2, VERTEX &v3, VERTEX &out) //*SEB* all double before
921 LineEuqationType line;
922 Create1LineEq(line, v2, v3, v1);
924 float aDot = (out.x*line.x + out.y*line.y);
925 float bDot = (v1.sx*line.x + v1.sy*line.y);
927 float scale1 = ( - line.d - aDot) / ( bDot - aDot );
929 float tx = out.x + scale1 * (v1.sx - out.x);
930 float ty = out.y + scale1 * (v1.sy - out.y);
932 float s1 = 101.0, s2 = 101.0;
933 float den = tx - v1.sx;
934 if (fabsf(den) > 1.0)
935 s1 = (out.x-v1.sx)/den;
937 s1 = (out.y-v1.sy)/(ty-v1.sy);
940 if (fabsf(den) > 1.0)
943 s2 =(ty-v2.sy)/(v3.sy-v2.sy);
945 float w = 1.0/interp3p(v1.oow,v2.oow,v3.oow,s1,s2);
947 out.r = real_to_char(interp3p(v1.r*v1.oow,v2.r*v2.oow,v3.r*v3.oow,s1,s2)*w);
948 out.g = real_to_char(interp3p(v1.g*v1.oow,v2.g*v2.oow,v3.g*v3.oow,s1,s2)*w);
949 out.b = real_to_char(interp3p(v1.b*v1.oow,v2.b*v2.oow,v3.b*v3.oow,s1,s2)*w);
950 out.a = real_to_char(interp3p(v1.a*v1.oow,v2.a*v2.oow,v3.a*v3.oow,s1,s2)*w);
951 out.f = (float)(interp3p(v1.f*v1.oow,v2.f*v2.oow,v3.f*v3.oow,s1,s2)*w);
953 out.u0 = interp3p(v1.u0_w*v1.oow,v2.u0_w*v2.oow,v3.u0_w*v3.oow,s1,s2)/oow;
954 out.v0 = interp3p(v1.v0_w*v1.oow,v2.v0_w*v2.oow,v3.v0_w*v3.oow,s1,s2)/oow;
955 out.u1 = interp3p(v1.u1_w*v1.oow,v2.u1_w*v2.oow,v3.u1_w*v3.oow,s1,s2)/oow;
956 out.v1 = interp3p(v1.v1_w*v1.oow,v2.v1_w*v2.oow,v3.v1_w*v3.oow,s1,s2)/oow;
960 static void CalculateLOD(VERTEX *v, int n)
962 //rdp.update |= UPDATE_TEXTURE;
964 if (rdp.lod_calculated)
967 if (dc0_detailmax < 0.5)
968 detailmax = rdp.lod_fraction;
970 detailmax = 1.0f - rdp.lod_fraction;
971 grTexDetailControl (GR_TMU0, dc0_lodbias, dc0_detailscale, detailmax);
973 grTexDetailControl (GR_TMU1, dc1_lodbias, dc1_detailscale, detailmax);
977 float deltaS, deltaT;
978 float deltaX, deltaY;
979 float deltaTexels, deltaPixels, lodFactor = 0; //*SEB* double before
980 float intptr; //*SEB* double before
981 float s_scale = rdp.tiles[rdp.cur_tile].width / 255.0f;
982 float t_scale = rdp.tiles[rdp.cur_tile].height / 255.0f;
983 if (settings.lodmode == 1)
985 deltaS = (v[1].u0/v[1].q - v[0].u0/v[0].q) * s_scale;
986 deltaT = (v[1].v0/v[1].q - v[0].v0/v[0].q) * t_scale;
987 deltaTexels = sqrt( deltaS * deltaS + deltaT * deltaT );
989 deltaX = (v[1].x - v[0].x)/rdp.scale_x;
990 deltaY = (v[1].y - v[0].y)/rdp.scale_y;
991 deltaPixels = sqrt( deltaX * deltaX + deltaY * deltaY );
993 lodFactor = deltaTexels / deltaPixels;
998 for (i = 0; i < n; i++)
1000 j = (i < n-1) ? i + 1 : 0;
1002 deltaS = (v[j].u0/v[j].q - v[i].u0/v[i].q) * s_scale;
1003 deltaT = (v[j].v0/v[j].q - v[i].v0/v[i].q) * t_scale;
1004 // deltaS = v[j].ou - v[i].ou;
1005 // deltaT = v[j].ov - v[i].ov;
1006 deltaTexels = sqrt( deltaS * deltaS + deltaT * deltaT );
1008 deltaX = (v[j].x - v[i].x)/rdp.scale_x;
1009 deltaY = (v[j].y - v[i].y)/rdp.scale_y;
1010 deltaPixels = sqrt( deltaX * deltaX + deltaY * deltaY );
1012 lodFactor += deltaTexels / deltaPixels;
1014 // Divide by n (n edges) to find average
1015 lodFactor = lodFactor / n;
1017 int ilod = (int)lodFactor;
1018 int lod_tile = min((int)(log10f((float)ilod)/log10f(2.0f)), rdp.cur_tile + rdp.mipmap_level);
1019 float lod_fraction = 1.0f;
1020 if (lod_tile < rdp.cur_tile + rdp.mipmap_level)
1022 lod_fraction = max((float)modff(lodFactor / powf(2.,lod_tile),&intptr), (float)rdp.prim_lodmin / 255.0f);
1025 if (cmb.dc0_detailmax < 0.5f)
1026 detailmax = lod_fraction;
1028 detailmax = 1.0f - lod_fraction;
1029 grTexDetailControl (GR_TMU0, cmb.dc0_lodbias, cmb.dc0_detailscale, detailmax);
1030 if (voodoo.num_tmu == 2)
1031 grTexDetailControl (GR_TMU1, cmb.dc1_lodbias, cmb.dc1_detailscale, detailmax);
1032 FRDP("CalculateLOD factor: %f, tile: %d, lod_fraction: %f\n", (float)lodFactor, lod_tile, lod_fraction);
1035 float ScaleZ(float z)
1037 if (settings.n64_z_scale)
1039 int iz = (int)(z*8.0f+0.5f);
1041 else if (iz >= 0x40000) iz = 0x40000 - 1;
1042 return (float)zLUT[iz];
1044 if (z < 0.0f) return 0.0f;
1046 if (z > 65534.0f) return 65534.0f;
1050 static void DepthBuffer(VERTEX * vtx, int n)
1052 if (fb_depth_render_enabled && !(settings.hacks&hack_RE2) && dzdx && (rdp.flags & ZBUF_UPDATE))
1055 if (u_cull_mode == 1) //cull front
1057 for(int i=0; i<n; i++)
1059 v[i].x = (int)((vtx[n-i-1].x-rdp.offset_x) / rdp.scale_x * 65536.0);
1060 v[i].y = (int)((vtx[n-i-1].y-rdp.offset_y) / rdp.scale_y * 65536.0);
1061 v[i].z = (int)(vtx[n-i-1].z * 65536.0);
1066 for(int i=0; i<n; i++)
1068 v[i].x = (int)((vtx[i].x-rdp.offset_x) / rdp.scale_x * 65536.0);
1069 v[i].y = (int)((vtx[i].y-rdp.offset_y) / rdp.scale_y * 65536.0);
1070 v[i].z = (int)(vtx[i].z * 65536.0);
1073 Rasterize(v, n, dzdx);
1075 for(int i=0; i<n; i++)
1076 vtx[i].z = ScaleZ(vtx[i].z);
1081 #define LOGG(x) loga.open("glide_log.txt",std::ios::app); loga << x; loga.flush(); loga.close();
1082 __inline void FRDP2(char *fmt, ...)
1086 vsprintf(out_buf, fmt, ap);
1095 void clip_tri(int interpolate_colors)
1097 int i,j,index,n=rdp.n_global;
1100 // Check which clipping is needed
1101 if (rdp.clip & CLIP_XMAX) // right of the screen
1103 // Swap vertex buffers
1104 VERTEX *tmp = rdp.vtxbuf2;
1105 rdp.vtxbuf2 = rdp.vtxbuf;
1107 rdp.vtx_buffer ^= 1;
1110 // Check the vertices for clipping
1116 if (Vi.x <= rdp.clip_max_x)
1118 if (Vj.x <= rdp.clip_max_x) // Both are in, save the last one
1120 rdp.vtxbuf[index++] = Vj;
1122 else // First is in, second is out, save intersection
1124 percent = (rdp.clip_max_x - Vi.x) / (Vj.x - Vi.x);
1125 rdp.vtxbuf[index].x = rdp.clip_max_x;
1126 rdp.vtxbuf[index].y = Vi.y + (Vj.y - Vi.y) * percent;
1127 rdp.vtxbuf[index].z = Vi.z + (Vj.z - Vi.z) * percent;
1128 rdp.vtxbuf[index].q = Vi.q + (Vj.q - Vi.q) * percent;
1129 rdp.vtxbuf[index].u0 = Vi.u0 + (Vj.u0 - Vi.u0) * percent;
1130 rdp.vtxbuf[index].v0 = Vi.v0 + (Vj.v0 - Vi.v0) * percent;
1131 rdp.vtxbuf[index].u1 = Vi.u1 + (Vj.u1 - Vi.u1) * percent;
1132 rdp.vtxbuf[index].v1 = Vi.v1 + (Vj.v1 - Vi.v1) * percent;
1133 if (interpolate_colors)
1134 InterpolateColors(Vi, Vj, rdp.vtxbuf[index++], percent);
1136 rdp.vtxbuf[index++].number = Vi.number | Vj.number | 8;
1141 //if (Vj.x > rdp.clip_max_x) // Both are out, save nothing
1142 if (Vj.x <= rdp.clip_max_x) // First is out, second is in, save intersection & in point
1144 percent = (rdp.clip_max_x - Vj.x) / (Vi.x - Vj.x);
1145 rdp.vtxbuf[index].x = rdp.clip_max_x;
1146 rdp.vtxbuf[index].y = Vj.y + (Vi.y - Vj.y) * percent;
1147 rdp.vtxbuf[index].z = Vj.z + (Vi.z - Vj.z) * percent;
1148 rdp.vtxbuf[index].q = Vj.q + (Vi.q - Vj.q) * percent;
1149 rdp.vtxbuf[index].u0 = Vj.u0 + (Vi.u0 - Vj.u0) * percent;
1150 rdp.vtxbuf[index].v0 = Vj.v0 + (Vi.v0 - Vj.v0) * percent;
1151 rdp.vtxbuf[index].u1 = Vj.u1 + (Vi.u1 - Vj.u1) * percent;
1152 rdp.vtxbuf[index].v1 = Vj.v1 + (Vi.v1 - Vj.v1) * percent;
1153 if (interpolate_colors)
1154 InterpolateColors(Vj, Vi, rdp.vtxbuf[index++], percent);
1156 rdp.vtxbuf[index++].number = Vi.number | Vj.number | 8;
1158 // Save the in point
1159 rdp.vtxbuf[index++] = Vj;
1165 if (rdp.clip & CLIP_XMIN) // left of the screen
1167 // Swap vertex buffers
1168 VERTEX *tmp = rdp.vtxbuf2;
1169 rdp.vtxbuf2 = rdp.vtxbuf;
1171 rdp.vtx_buffer ^= 1;
1174 // Check the vertices for clipping
1180 if (Vi.x >= rdp.clip_min_x)
1182 if (Vj.x >= rdp.clip_min_x) // Both are in, save the last one
1184 rdp.vtxbuf[index++] = Vj;
1186 else // First is in, second is out, save intersection
1188 percent = (rdp.clip_min_x - Vi.x) / (Vj.x - Vi.x);
1189 rdp.vtxbuf[index].x = rdp.clip_min_x;
1190 rdp.vtxbuf[index].y = Vi.y + (Vj.y - Vi.y) * percent;
1191 rdp.vtxbuf[index].z = Vi.z + (Vj.z - Vi.z) * percent;
1192 rdp.vtxbuf[index].q = Vi.q + (Vj.q - Vi.q) * percent;
1193 rdp.vtxbuf[index].u0 = Vi.u0 + (Vj.u0 - Vi.u0) * percent;
1194 rdp.vtxbuf[index].v0 = Vi.v0 + (Vj.v0 - Vi.v0) * percent;
1195 rdp.vtxbuf[index].u1 = Vi.u1 + (Vj.u1 - Vi.u1) * percent;
1196 rdp.vtxbuf[index].v1 = Vi.v1 + (Vj.v1 - Vi.v1) * percent;
1197 if (interpolate_colors)
1198 InterpolateColors(Vi, Vj, rdp.vtxbuf[index++], percent);
1200 rdp.vtxbuf[index++].number = Vi.number | Vj.number | 8;
1205 //if (Vj.x < rdp.clip_min_x) // Both are out, save nothing
1206 if (Vj.x >= rdp.clip_min_x) // First is out, second is in, save intersection & in point
1208 percent = (rdp.clip_min_x - Vj.x) / (Vi.x - Vj.x);
1209 rdp.vtxbuf[index].x = rdp.clip_min_x;
1210 rdp.vtxbuf[index].y = Vj.y + (Vi.y - Vj.y) * percent;
1211 rdp.vtxbuf[index].z = Vj.z + (Vi.z - Vj.z) * percent;
1212 rdp.vtxbuf[index].q = Vj.q + (Vi.q - Vj.q) * percent;
1213 rdp.vtxbuf[index].u0 = Vj.u0 + (Vi.u0 - Vj.u0) * percent;
1214 rdp.vtxbuf[index].v0 = Vj.v0 + (Vi.v0 - Vj.v0) * percent;
1215 rdp.vtxbuf[index].u1 = Vj.u1 + (Vi.u1 - Vj.u1) * percent;
1216 rdp.vtxbuf[index].v1 = Vj.v1 + (Vi.v1 - Vj.v1) * percent;
1217 if (interpolate_colors)
1218 InterpolateColors(Vj, Vi, rdp.vtxbuf[index++], percent);
1220 rdp.vtxbuf[index++].number = Vi.number | Vj.number | 8;
1222 // Save the in point
1223 rdp.vtxbuf[index++] = Vj;
1229 if (rdp.clip & CLIP_YMAX) // top of the screen
1231 // Swap vertex buffers
1232 VERTEX *tmp = rdp.vtxbuf2;
1233 rdp.vtxbuf2 = rdp.vtxbuf;
1235 rdp.vtx_buffer ^= 1;
1238 // Check the vertices for clipping
1244 if (Vi.y <= rdp.clip_max_y)
1246 if (Vj.y <= rdp.clip_max_y) // Both are in, save the last one
1248 rdp.vtxbuf[index++] = Vj;
1250 else // First is in, second is out, save intersection
1252 percent = (rdp.clip_max_y - Vi.y) / (Vj.y - Vi.y);
1253 rdp.vtxbuf[index].x = Vi.x + (Vj.x - Vi.x) * percent;
1254 rdp.vtxbuf[index].y = rdp.clip_max_y;
1255 rdp.vtxbuf[index].z = Vi.z + (Vj.z - Vi.z) * percent;
1256 rdp.vtxbuf[index].q = Vi.q + (Vj.q - Vi.q) * percent;
1257 rdp.vtxbuf[index].u0 = Vi.u0 + (Vj.u0 - Vi.u0) * percent;
1258 rdp.vtxbuf[index].v0 = Vi.v0 + (Vj.v0 - Vi.v0) * percent;
1259 rdp.vtxbuf[index].u1 = Vi.u1 + (Vj.u1 - Vi.u1) * percent;
1260 rdp.vtxbuf[index].v1 = Vi.v1 + (Vj.v1 - Vi.v1) * percent;
1261 if (interpolate_colors)
1262 InterpolateColors(Vi, Vj, rdp.vtxbuf[index++], percent);
1264 rdp.vtxbuf[index++].number = Vi.number | Vj.number | 16;
1269 //if (Vj.y > rdp.clip_max_y) // Both are out, save nothing
1270 if (Vj.y <= rdp.clip_max_y) // First is out, second is in, save intersection & in point
1272 percent = (rdp.clip_max_y - Vj.y) / (Vi.y - Vj.y);
1273 rdp.vtxbuf[index].x = Vj.x + (Vi.x - Vj.x) * percent;
1274 rdp.vtxbuf[index].y = rdp.clip_max_y;
1275 rdp.vtxbuf[index].z = Vj.z + (Vi.z - Vj.z) * percent;
1276 rdp.vtxbuf[index].q = Vj.q + (Vi.q - Vj.q) * percent;
1277 rdp.vtxbuf[index].u0 = Vj.u0 + (Vi.u0 - Vj.u0) * percent;
1278 rdp.vtxbuf[index].v0 = Vj.v0 + (Vi.v0 - Vj.v0) * percent;
1279 rdp.vtxbuf[index].u1 = Vj.u1 + (Vi.u1 - Vj.u1) * percent;
1280 rdp.vtxbuf[index].v1 = Vj.v1 + (Vi.v1 - Vj.v1) * percent;
1281 if (interpolate_colors)
1282 InterpolateColors(Vj, Vi, rdp.vtxbuf[index++], percent);
1284 rdp.vtxbuf[index++].number = Vi.number | Vj.number | 16;
1286 // Save the in point
1287 rdp.vtxbuf[index++] = Vj;
1293 if (rdp.clip & CLIP_YMIN) // bottom of the screen
1295 // Swap vertex buffers
1296 VERTEX *tmp = rdp.vtxbuf2;
1297 rdp.vtxbuf2 = rdp.vtxbuf;
1299 rdp.vtx_buffer ^= 1;
1302 // Check the vertices for clipping
1308 if (Vi.y >= rdp.clip_min_y)
1310 if (Vj.y >= rdp.clip_min_y) // Both are in, save the last one
1312 rdp.vtxbuf[index++] = Vj;
1314 else // First is in, second is out, save intersection
1316 percent = (rdp.clip_min_y - Vi.y) / (Vj.y - Vi.y);
1317 rdp.vtxbuf[index].x = Vi.x + (Vj.x - Vi.x) * percent;
1318 rdp.vtxbuf[index].y = rdp.clip_min_y;
1319 rdp.vtxbuf[index].z = Vi.z + (Vj.z - Vi.z) * percent;
1320 rdp.vtxbuf[index].q = Vi.q + (Vj.q - Vi.q) * percent;
1321 rdp.vtxbuf[index].u0 = Vi.u0 + (Vj.u0 - Vi.u0) * percent;
1322 rdp.vtxbuf[index].v0 = Vi.v0 + (Vj.v0 - Vi.v0) * percent;
1323 rdp.vtxbuf[index].u1 = Vi.u1 + (Vj.u1 - Vi.u1) * percent;
1324 rdp.vtxbuf[index].v1 = Vi.v1 + (Vj.v1 - Vi.v1) * percent;
1325 if (interpolate_colors)
1326 InterpolateColors(Vi, Vj, rdp.vtxbuf[index++], percent);
1328 rdp.vtxbuf[index++].number = Vi.number | Vj.number | 16;
1333 //if (Vj.y < rdp.clip_min_y) // Both are out, save nothing
1334 if (Vj.y >= rdp.clip_min_y) // First is out, second is in, save intersection & in point
1336 percent = (rdp.clip_min_y - Vj.y) / (Vi.y - Vj.y);
1337 rdp.vtxbuf[index].x = Vj.x + (Vi.x - Vj.x) * percent;
1338 rdp.vtxbuf[index].y = rdp.clip_min_y;
1339 rdp.vtxbuf[index].z = Vj.z + (Vi.z - Vj.z) * percent;
1340 rdp.vtxbuf[index].q = Vj.q + (Vi.q - Vj.q) * percent;
1341 rdp.vtxbuf[index].u0 = Vj.u0 + (Vi.u0 - Vj.u0) * percent;
1342 rdp.vtxbuf[index].v0 = Vj.v0 + (Vi.v0 - Vj.v0) * percent;
1343 rdp.vtxbuf[index].u1 = Vj.u1 + (Vi.u1 - Vj.u1) * percent;
1344 rdp.vtxbuf[index].v1 = Vj.v1 + (Vi.v1 - Vj.v1) * percent;
1345 if (interpolate_colors)
1346 InterpolateColors(Vj, Vi, rdp.vtxbuf[index++], percent);
1348 rdp.vtxbuf[index++].number = Vi.number | Vj.number | 16;
1350 // Save the in point
1351 rdp.vtxbuf[index++] = Vj;
1357 if (rdp.clip & CLIP_ZMAX) // far plane
1359 // Swap vertex buffers
1360 VERTEX *tmp = rdp.vtxbuf2;
1361 rdp.vtxbuf2 = rdp.vtxbuf;
1363 rdp.vtx_buffer ^= 1;
1365 float maxZ = rdp.view_trans[2] + rdp.view_scale[2];
1367 // Check the vertices for clipping
1375 if (Vj.z < maxZ) // Both are in, save the last one
1377 rdp.vtxbuf[index++] = Vj;
1379 else // First is in, second is out, save intersection
1381 percent = (maxZ - Vi.z) / (Vj.z - Vi.z);
1382 rdp.vtxbuf[index].x = Vi.x + (Vj.x - Vi.x) * percent;
1383 rdp.vtxbuf[index].y = Vi.y + (Vj.y - Vi.y) * percent;
1384 rdp.vtxbuf[index].z = maxZ - 0.001f;
1385 rdp.vtxbuf[index].q = Vi.q + (Vj.q - Vi.q) * percent;
1386 rdp.vtxbuf[index].u0 = Vi.u0 + (Vj.u0 - Vi.u0) * percent;
1387 rdp.vtxbuf[index].v0 = Vi.v0 + (Vj.v0 - Vi.v0) * percent;
1388 rdp.vtxbuf[index].u1 = Vi.u1 + (Vj.u1 - Vi.u1) * percent;
1389 rdp.vtxbuf[index].v1 = Vi.v1 + (Vj.v1 - Vi.v1) * percent;
1390 if (interpolate_colors)
1391 InterpolateColors(Vi, Vj, rdp.vtxbuf[index++], percent);
1393 rdp.vtxbuf[index++].number = Vi.number | Vj.number;
1398 //if (Vj.z > maxZ) // Both are out, save nothing
1399 if (Vj.z < maxZ) // First is out, second is in, save intersection & in point
1401 percent = (maxZ - Vj.z) / (Vi.z - Vj.z);
1402 rdp.vtxbuf[index].x = Vj.x + (Vi.x - Vj.x) * percent;
1403 rdp.vtxbuf[index].y = Vj.y + (Vi.y - Vj.y) * percent;
1404 rdp.vtxbuf[index].z = maxZ - 0.001f;;
1405 rdp.vtxbuf[index].q = Vj.q + (Vi.q - Vj.q) * percent;
1406 rdp.vtxbuf[index].u0 = Vj.u0 + (Vi.u0 - Vj.u0) * percent;
1407 rdp.vtxbuf[index].v0 = Vj.v0 + (Vi.v0 - Vj.v0) * percent;
1408 rdp.vtxbuf[index].u1 = Vj.u1 + (Vi.u1 - Vj.u1) * percent;
1409 rdp.vtxbuf[index].v1 = Vj.v1 + (Vi.v1 - Vj.v1) * percent;
1410 if (interpolate_colors)
1411 InterpolateColors(Vj, Vi, rdp.vtxbuf[index++], percent);
1413 rdp.vtxbuf[index++].number = Vi.number | Vj.number;
1415 // Save the in point
1416 rdp.vtxbuf[index++] = Vj;
1423 if (rdp.clip & CLIP_ZMIN) // near Z
1425 // Swap vertex buffers
1426 VERTEX *tmp = rdp.vtxbuf2;
1427 rdp.vtxbuf2 = rdp.vtxbuf;
1429 rdp.vtx_buffer ^= 1;
1432 // Check the vertices for clipping
1440 if (Vj.z >= 0.0f) // Both are in, save the last one
1442 rdp.vtxbuf[index++] = Vj;
1444 else // First is in, second is out, save intersection
1446 percent = (-Vi.z) / (Vj.z - Vi.z);
1447 rdp.vtxbuf[index].x = Vi.x + (Vj.x - Vi.x) * percent;
1448 rdp.vtxbuf[index].y = Vi.y + (Vj.y - Vi.y) * percent;
1449 rdp.vtxbuf[index].z = 0.0f;
1450 rdp.vtxbuf[index].q = Vi.q + (Vj.q - Vi.q) * percent;
1451 rdp.vtxbuf[index].u0 = Vi.u0 + (Vj.u0 - Vi.u0) * percent;
1452 rdp.vtxbuf[index].v0 = Vi.v0 + (Vj.v0 - Vi.v0) * percent;
1453 rdp.vtxbuf[index].u1 = Vi.u1 + (Vj.u1 - Vi.u1) * percent;
1454 rdp.vtxbuf[index].v1 = Vi.v1 + (Vj.v1 - Vi.v1) * percent;
1455 if (interpolate_colors)
1456 InterpolateColors(Vi, Vj, rdp.vtxbuf[index++], percent);
1458 rdp.vtxbuf[index++].number = Vi.number | Vj.number;
1463 //if (Vj.z < 0.0f) // Both are out, save nothing
1464 if (Vj.z >= 0.0f) // First is out, second is in, save intersection & in point
1466 percent = (-Vj.z) / (Vi.z - Vj.z);
1467 rdp.vtxbuf[index].x = Vj.x + (Vi.x - Vj.x) * percent;
1468 rdp.vtxbuf[index].y = Vj.y + (Vi.y - Vj.y) * percent;
1469 rdp.vtxbuf[index].z = 0.0f;;
1470 rdp.vtxbuf[index].q = Vj.q + (Vi.q - Vj.q) * percent;
1471 rdp.vtxbuf[index].u0 = Vj.u0 + (Vi.u0 - Vj.u0) * percent;
1472 rdp.vtxbuf[index].v0 = Vj.v0 + (Vi.v0 - Vj.v0) * percent;
1473 rdp.vtxbuf[index].u1 = Vj.u1 + (Vi.u1 - Vj.u1) * percent;
1474 rdp.vtxbuf[index].v1 = Vj.v1 + (Vi.v1 - Vj.v1) * percent;
1475 if (interpolate_colors)
1476 InterpolateColors(Vj, Vi, rdp.vtxbuf[index++], percent);
1478 rdp.vtxbuf[index++].number = Vi.number | Vj.number;
1480 // Save the in point
1481 rdp.vtxbuf[index++] = Vj;
1491 static void render_tri (wxUint16 linew, int old_interpolate)
1494 clip_tri(old_interpolate);
1495 int n = rdp.n_global;
1498 FRDP (" * render_tri: n < 3\n");
1503 if ((rdp.clip & CLIP_ZMIN) && (rdp.othermode_l & 0x00000030))
1506 int to_render = FALSE;
1507 for (i = 0; i < n; i++)
1509 if (rdp.vtxbuf[i].z >= 0.0f)
1515 if (!to_render) //all z < 0
1517 FRDP (" * render_tri: all z < 0\n");
1522 if (rdp.clip && !old_interpolate)
1524 for (i = 0; i < n; i++)
1526 float percent = 101.0f;
1527 VERTEX * v1 = 0, * v2 = 0;
1528 switch (rdp.vtxbuf[i].number&7)
1548 InterpolateColors3(*org_vtx[0], *org_vtx[1], *org_vtx[2], rdp.vtxbuf[i]);
1552 switch (rdp.vtxbuf[i].number&24)
1555 percent = (rdp.vtxbuf[i].x-v1->sx)/(v2->sx-v1->sx);
1558 percent = (rdp.vtxbuf[i].y-v1->sy)/(v2->sy-v1->sy);
1562 float d = (v2->sx-v1->sx);
1564 percent = (rdp.vtxbuf[i].x-v1->sx)/d;
1565 if (percent > 100.0f)
1566 percent = (rdp.vtxbuf[i].y-v1->sy)/(v2->sy-v1->sy);
1569 InterpolateColors2(*v1, *v2, rdp.vtxbuf[i], percent);
1575 LOGG("Colors before clipping:\n");
1579 FRDP2("V%d: r=%d, g=%d, b=%d, a=%d, f=%d\n", k, org_vtx[k]->r, org_vtx[k]->g, org_vtx[k]->b, org_vtx[k]->a, (short)org_vtx[k]->f);
1581 FRDP("Got %d vertex after clipping\n", n);
1584 FRDP("V%d: r=%d, g=%d, b=%d, a=%d, f=%d\n", k, rdp.vtxbuf[k].r, rdp.vtxbuf[k].g, rdp.vtxbuf[k].b, rdp.vtxbuf[k].a, (short)rdp.vtxbuf[k].f);
1589 ConvertCoordsConvert (rdp.vtxbuf, n);
1590 if (rdp.fog_mode == RDP::fog_enabled)
1592 for (i = 0; i < n; i++)
1594 rdp.vtxbuf[i].f = 1.0f/max(4.0f, rdp.vtxbuf[i].f);
1597 else if (rdp.fog_mode == RDP::fog_blend)
1599 float fog = 1.0f/max(1, rdp.fog_color&0xFF);
1600 for (i = 0; i < n; i++)
1602 rdp.vtxbuf[i].f = fog;
1605 else if (rdp.fog_mode == RDP::fog_blend_inverse)
1607 float fog = 1.0f/max(1, (~rdp.fog_color)&0xFF);
1608 for (i = 0; i < n; i++)
1610 rdp.vtxbuf[i].f = fog;
1614 if (settings.lodmode > 0 && rdp.cur_tile < rdp.mipmap_level)
1615 CalculateLOD(rdp.vtxbuf, n);
1617 cmb.cmb_ext_use = cmb.tex_cmb_ext_use = 0;
1622 for (int k = 0; k < 3; k++)
1624 FRDP("v%d %f->%f, width: %d. height: %d, tex_width: %d, tex_height: %d, lr_u: %f, lr_v: %f\n", k, vv0[k], pv[k]->v1, rdp.tbuff_tex->width, rdp.tbuff_tex->height, rdp.tbuff_tex->tex_width, rdp.tbuff_tex->tex_height, rdp.tbuff_tex->lr_u, rdp.tbuff_tex->lr_v);
1630 if (settings.wireframe)
1637 grDrawLine (&rdp.vtxbuf[i], &rdp.vtxbuf[j]);
1643 // VERTEX ** pv = rdp.vtx_buffer?(vtx_list2):(vtx_list1);
1644 // for (int k = 0; k < n; k ++)
1645 // FRDP ("DRAW[%d]: v.x = %f, v.y = %f, v.z = %f, v.u = %f, v.v = %f\n", k, pv[k]->x, pv[k]->y, pv[k]->z, pv[k]->coord[rdp.t0<<1], pv[k]->coord[(rdp.t0<<1)+1]);
1646 // pv[k]->y = settings.res_y - pv[k]->y;
1650 VERTEX *V0 = &rdp.vtxbuf[0];
1651 VERTEX *V1 = &rdp.vtxbuf[1];
1652 if (fabs(V0->x - V1->x) < 0.01 && fabs(V0->y - V1->y) < 0.01)
1653 V1 = &rdp.vtxbuf[2];
1654 V0->z = ScaleZ(V0->z);
1655 V1->z = ScaleZ(V1->z);
1661 float width = linew * 0.25f;
1662 if (fabs(V0->y - V1->y) < 0.0001)
1664 v[0].x = v[1].x = V0->x;
1665 v[2].x = v[3].x = V1->x;
1667 width *= rdp.scale_y;
1668 v[0].y = v[2].y = V0->y - width;
1669 v[1].y = v[3].y = V0->y + width;
1671 else if (fabs(V0->x - V1->x) < 0.0001)
1673 v[0].y = v[1].y = V0->y;
1674 v[2].y = v[3].y = V1->y;
1676 width *= rdp.scale_x;
1677 v[0].x = v[2].x = V0->x - width;
1678 v[1].x = v[3].x = V0->x + width;
1682 float dx = V1->x - V0->x;
1683 float dy = V1->y - V0->y;
1684 float len = sqrtf(dx*dx + dy*dy);
1685 float wx = dy * width * rdp.scale_x / len;
1686 float wy = dx * width * rdp.scale_y / len;
1687 v[0].x = V0->x + wx;
1688 v[0].y = V0->y - wy;
1689 v[1].x = V0->x - wx;
1690 v[1].y = V0->y + wy;
1691 v[2].x = V1->x + wx;
1692 v[2].y = V1->y - wy;
1693 v[3].x = V1->x - wx;
1694 v[3].y = V1->y + wy;
1696 grDrawTriangle(&v[0], &v[1], &v[2]);
1697 grDrawTriangle(&v[1], &v[2], &v[3]);
1701 DepthBuffer(rdp.vtxbuf, n);
1702 if ((rdp.rm & 0xC10) == 0xC10)
1703 grDepthBiasLevel (-deltaZ);
1704 grDrawVertexArray (GR_TRIANGLE_FAN, n, rdp.vtx_buffer?(&vtx_list2):(&vtx_list1));
1709 if (_debugger.capture) add_tri (rdp.vtxbuf, n, TRI_TRIANGLE);
1712 void add_tri (VERTEX *v, int n, int type)
1714 //FRDP ("ENTER (%f, %f, %f), (%f, %f, %f), (%f, %f, %f)\n", v[0].x, v[0].y, v[0].w,
1715 // v[1].x, v[1].y, v[1].w, v[2].x, v[2].y, v[2].w);
1718 if (_debugger.capture)
1722 TRI_INFO *info = new TRI_INFO;
1724 info->v = new VERTEX [n];
1725 memcpy (info->v, v, sizeof(VERTEX)*n);
1726 info->cycle_mode = rdp.cycle_mode;
1727 info->cycle1 = rdp.cycle1;
1728 info->cycle2 = rdp.cycle2;
1729 info->uncombined = rdp.uncombined;
1730 info->geom_mode = rdp.geom_mode;
1731 info->othermode_h = rdp.othermode_h;
1732 info->othermode_l = rdp.othermode_l;
1733 info->tri_n = rdp.tri_n;
1736 for (int i=0; i<2; i++)
1738 int j = rdp.cur_tile+i;
1740 info->t[i].tmu = rdp.t0;
1742 info->t[i].tmu = rdp.t1;
1743 info->t[i].cur_cache[0] = rdp.cur_cache_n[rdp.t0];
1744 info->t[i].cur_cache[1] = rdp.cur_cache_n[rdp.t1];
1745 info->t[i].format = rdp.tiles[j].format;
1746 info->t[i].size = rdp.tiles[j].size;
1747 info->t[i].width = rdp.tiles[j].width;
1748 info->t[i].height = rdp.tiles[j].height;
1749 info->t[i].line = rdp.tiles[j].line;
1750 info->t[i].palette = rdp.tiles[j].palette;
1751 info->t[i].clamp_s = rdp.tiles[j].clamp_s;
1752 info->t[i].clamp_t = rdp.tiles[j].clamp_t;
1753 info->t[i].mirror_s = rdp.tiles[j].mirror_s;
1754 info->t[i].mirror_t = rdp.tiles[j].mirror_t;
1755 info->t[i].shift_s = rdp.tiles[j].shift_s;
1756 info->t[i].shift_t = rdp.tiles[j].shift_t;
1757 info->t[i].mask_s = rdp.tiles[j].mask_s;
1758 info->t[i].mask_t = rdp.tiles[j].mask_t;
1759 info->t[i].ul_s = rdp.tiles[j].ul_s;
1760 info->t[i].ul_t = rdp.tiles[j].ul_t;
1761 info->t[i].lr_s = rdp.tiles[j].lr_s;
1762 info->t[i].lr_t = rdp.tiles[j].lr_t;
1763 info->t[i].t_ul_s = rdp.tiles[7].t_ul_s;
1764 info->t[i].t_ul_t = rdp.tiles[7].t_ul_t;
1765 info->t[i].t_lr_s = rdp.tiles[7].t_lr_s;
1766 info->t[i].t_lr_t = rdp.tiles[7].t_lr_t;
1767 info->t[i].scale_s = rdp.tiles[j].s_scale;
1768 info->t[i].scale_t = rdp.tiles[j].t_scale;
1771 info->fog_color = rdp.fog_color;
1772 info->fill_color = rdp.fill_color;
1773 info->prim_color = rdp.prim_color;
1774 info->blend_color = rdp.blend_color;
1775 info->env_color = rdp.env_color;
1776 info->prim_lodmin = rdp.prim_lodmin;
1777 info->prim_lodfrac = rdp.prim_lodfrac;
1779 info->pNext = _debugger.tri_list;
1780 _debugger.tri_list = info;
1782 if (_debugger.tri_last == NULL)
1783 _debugger.tri_last = _debugger.tri_list;
1787 void update_scissor ()
1789 if (rdp.update & UPDATE_SCISSOR)
1791 rdp.update ^= UPDATE_SCISSOR;
1793 // KILL the floating point error with 0.01f
1794 rdp.scissor.ul_x = (wxUint32)max(min((rdp.scissor_o.ul_x * rdp.scale_x + rdp.offset_x + 0.01f),settings.res_x),0);
1795 rdp.scissor.lr_x = (wxUint32)max(min((rdp.scissor_o.lr_x * rdp.scale_x + rdp.offset_x + 0.01f),settings.res_x),0);
1796 rdp.scissor.ul_y = (wxUint32)max(min((rdp.scissor_o.ul_y * rdp.scale_y + rdp.offset_y + 0.01f),settings.res_y),0);
1797 rdp.scissor.lr_y = (wxUint32)max(min((rdp.scissor_o.lr_y * rdp.scale_y + rdp.offset_y + 0.01f),settings.res_y),0);
1798 //grClipWindow specifies the hardware clipping window. Any pixels outside the clipping window are rejected.
1799 //Values are inclusive for minimum x and y values and exclusive for maximum x and y values.
1800 // grClipWindow (rdp.scissor.ul_x?rdp.scissor.ul_x+1:0, rdp.scissor.ul_y?rdp.scissor.ul_y+1:0, rdp.scissor.lr_x, rdp.scissor.lr_y);
1802 grClipWindow (rdp.scissor.ul_x, rdp.scissor.ul_y, rdp.scissor.lr_x, rdp.scissor.lr_y);
1803 FRDP (" |- scissor - (%d, %d) -> (%d, %d)\n", rdp.scissor.ul_x, rdp.scissor.ul_y,
1804 rdp.scissor.lr_x, rdp.scissor.lr_y);
1809 // update - update states if they need it
1814 unsigned int c2_m2b:2;
1815 unsigned int c1_m2b:2;
1816 unsigned int c2_m2a:2;
1817 unsigned int c1_m2a:2;
1818 unsigned int c2_m1b:2;
1819 unsigned int c1_m1b:2;
1820 unsigned int c2_m1a:2;
1821 unsigned int c1_m1a:2;
1822 } rdp_blender_setting;
1826 LRDP ("-+ update called\n");
1827 // Check for rendermode changes
1829 if (rdp.render_mode_changed & 0x00000C30)
1831 FRDP (" |- render_mode_changed zbuf - decal: %s, update: %s, compare: %s\n",
1832 str_yn[(rdp.othermode_l & 0x00000400)?1:0],
1833 str_yn[(rdp.othermode_l&0x00000020)?1:0],
1834 str_yn[(rdp.othermode_l&0x00000010)?1:0]);
1836 rdp.render_mode_changed &= ~0x00000C30;
1837 rdp.update |= UPDATE_ZBUF_ENABLED;
1840 if ((rdp.othermode_l & 0x00000020))
1841 rdp.flags |= ZBUF_UPDATE;
1843 rdp.flags &= ~ZBUF_UPDATE;
1846 if (rdp.othermode_l & 0x00000010)
1847 rdp.flags |= ZBUF_COMPARE;
1849 rdp.flags &= ~ZBUF_COMPARE;
1853 if (rdp.render_mode_changed & 0x00001000)
1855 FRDP (" |- render_mode_changed alpha compare - on: %s\n",
1856 str_yn[(rdp.othermode_l&0x00001000)?1:0]);
1857 rdp.render_mode_changed &= ~0x00001000;
1858 rdp.update |= UPDATE_ALPHA_COMPARE;
1860 if (rdp.othermode_l & 0x00001000)
1861 rdp.flags |= ALPHA_COMPARE;
1863 rdp.flags &= ~ALPHA_COMPARE;
1866 if (rdp.render_mode_changed & 0x00002000) // alpha cvg sel
1868 FRDP (" |- render_mode_changed alpha cvg sel - on: %s\n",
1869 str_yn[(rdp.othermode_l&0x00002000)?1:0]);
1870 rdp.render_mode_changed &= ~0x00002000;
1871 rdp.update |= UPDATE_COMBINE;
1872 rdp.update |= UPDATE_ALPHA_COMPARE;
1876 if (rdp.render_mode_changed & 0xFFFF0000)
1878 FRDP (" |- render_mode_changed force_blend - %08lx\n", rdp.othermode_l&0xFFFF0000);
1879 rdp.render_mode_changed &= 0x0000FFFF;
1881 rdp.fbl_a0 = (wxUint8)((rdp.othermode_l>>30)&0x3);
1882 rdp.fbl_b0 = (wxUint8)((rdp.othermode_l>>26)&0x3);
1883 rdp.fbl_c0 = (wxUint8)((rdp.othermode_l>>22)&0x3);
1884 rdp.fbl_d0 = (wxUint8)((rdp.othermode_l>>18)&0x3);
1885 rdp.fbl_a1 = (wxUint8)((rdp.othermode_l>>28)&0x3);
1886 rdp.fbl_b1 = (wxUint8)((rdp.othermode_l>>24)&0x3);
1887 rdp.fbl_c1 = (wxUint8)((rdp.othermode_l>>20)&0x3);
1888 rdp.fbl_d1 = (wxUint8)((rdp.othermode_l>>16)&0x3);
1890 rdp.update |= UPDATE_COMBINE;
1893 // Combine MUST go before texture
1894 if ((rdp.update & UPDATE_COMBINE) && rdp.allow_combine)
1896 TBUFF_COLOR_IMAGE * aTBuff[2] = {0, 0};
1897 if (rdp.aTBuffTex[0])
1898 aTBuff[rdp.aTBuffTex[0]->tile] = rdp.aTBuffTex[0];
1899 if (rdp.aTBuffTex[1])
1900 aTBuff[rdp.aTBuffTex[1]->tile] = rdp.aTBuffTex[1];
1901 rdp.aTBuffTex[0] = aTBuff[0];
1902 rdp.aTBuffTex[1] = aTBuff[1];
1904 LRDP (" |-+ update_combine\n");
1908 if (rdp.update & UPDATE_TEXTURE) // note: UPDATE_TEXTURE and UPDATE_COMBINE are the same
1911 if (rdp.tex_ctr == 0xFFFFFFFF)
1915 if (rdp.noise == RDP::noise_none)
1916 rdp.update ^= UPDATE_TEXTURE;
1922 if (rdp.update & UPDATE_ZBUF_ENABLED)
1924 // already logged above
1925 rdp.update ^= UPDATE_ZBUF_ENABLED;
1927 if (((rdp.flags & ZBUF_ENABLED) || rdp.zsrc == 1) && rdp.cycle_mode < 2)
1929 if (rdp.flags & ZBUF_COMPARE)
1931 switch ((rdp.rm & 0xC00)>>10) {
1933 grDepthBiasLevel(0);
1934 grDepthBufferFunction (settings.zmode_compare_less ? GR_CMP_LESS : GR_CMP_LEQUAL);
1937 grDepthBiasLevel(-4);
1938 grDepthBufferFunction (settings.zmode_compare_less ? GR_CMP_LESS : GR_CMP_LEQUAL);
1941 grDepthBiasLevel(settings.ucode == 7 ? -4 : 0);
1942 grDepthBufferFunction (GR_CMP_LESS);
1945 // will be set dynamically per polygon
1946 //grDepthBiasLevel(-deltaZ);
1947 grDepthBufferFunction (GR_CMP_LEQUAL);
1953 grDepthBiasLevel(0);
1954 grDepthBufferFunction (GR_CMP_ALWAYS);
1957 if (rdp.flags & ZBUF_UPDATE)
1958 grDepthMask (FXTRUE);
1960 grDepthMask (FXFALSE);
1964 grDepthBiasLevel(0);
1965 grDepthBufferFunction (GR_CMP_ALWAYS);
1966 grDepthMask (FXFALSE);
1971 if (rdp.update & UPDATE_ALPHA_COMPARE)
1973 // already logged above
1974 rdp.update ^= UPDATE_ALPHA_COMPARE;
1976 // if (rdp.acmp == 1 && !(rdp.othermode_l & 0x00002000) && !force_full_alpha)
1977 // if (rdp.acmp == 1 && !(rdp.othermode_l & 0x00002000) && (rdp.blend_color&0xFF))
1978 if (rdp.acmp == 1 && !(rdp.othermode_l & 0x00002000) && (!(rdp.othermode_l & 0x00004000) || (rdp.blend_color&0xFF)))
1980 wxUint8 reference = (wxUint8)(rdp.blend_color&0xFF);
1981 grAlphaTestFunction (reference ? GR_CMP_GEQUAL : GR_CMP_GREATER);
1982 grAlphaTestReferenceValue (reference);
1983 FRDP (" |- alpha compare: blend: %02lx\n", reference);
1987 if (rdp.flags & ALPHA_COMPARE)
1989 if ((rdp.othermode_l & 0x5000) != 0x5000)
1991 grAlphaTestFunction (GR_CMP_GEQUAL);
1992 grAlphaTestReferenceValue (0x20);//0xA0);
1993 LRDP (" |- alpha compare: 0x20\n");
1997 grAlphaTestFunction (GR_CMP_GREATER);
2000 grAlphaTestReferenceValue ((wxUint8)(rdp.blend_color&0xFF));
2001 FRDP (" |- alpha compare: blend: %02lx\n", rdp.blend_color&0xFF);
2005 grAlphaTestReferenceValue (0x00);
2006 LRDP (" |- alpha compare: 0x00\n");
2012 grAlphaTestFunction (GR_CMP_ALWAYS);
2013 LRDP (" |- alpha compare: none\n");
2016 if (rdp.acmp == 3 && rdp.cycle_mode < 2)
2018 if (grStippleModeExt != 0)
2020 if (settings.old_style_adither || rdp.alpha_dither_mode != 3) {
2021 LRDP (" |- alpha compare: dither\n");
2022 grStippleModeExt(settings.stipple_mode);
2025 grStippleModeExt(GR_STIPPLE_DISABLE);
2030 if (grStippleModeExt)
2032 //LRDP (" |- alpha compare: dither disabled\n");
2033 grStippleModeExt(GR_STIPPLE_DISABLE);
2037 // Cull mode (leave this in for z-clipped triangles)
2038 if (rdp.update & UPDATE_CULL_MODE)
2040 rdp.update ^= UPDATE_CULL_MODE;
2041 wxUint32 mode = (rdp.flags & CULLMASK) >> CULLSHIFT;
2042 FRDP (" |- cull_mode - mode: %s\n", str_cull[mode]);
2045 case 0: // cull none
2046 case 3: // cull both
2047 grCullMode(GR_CULL_DISABLE);
2049 case 1: // cull front
2050 // grCullMode(GR_CULL_POSITIVE);
2051 grCullMode(GR_CULL_NEGATIVE);
2053 case 2: // cull back
2054 // grCullMode (GR_CULL_NEGATIVE);
2055 grCullMode (GR_CULL_POSITIVE);
2061 if (settings.fog && (rdp.update & UPDATE_FOG_ENABLED))
2063 rdp.update ^= UPDATE_FOG_ENABLED;
2065 wxUint16 blender = (wxUint16)(rdp.othermode_l >> 16);
2066 if (rdp.flags & FOG_ENABLED)
2068 rdp_blender_setting &bl = *(rdp_blender_setting*)(&(blender));
2069 if((rdp.fog_multiplier > 0) && (bl.c1_m1a==3 || bl.c1_m2a == 3 || bl.c2_m1a == 3 || bl.c2_m2a == 3))
2071 grFogColorValue(rdp.fog_color);
2072 grFogMode (GR_FOG_WITH_TABLE_ON_FOGCOORD_EXT);
2073 rdp.fog_mode = RDP::fog_enabled;
2074 LRDP("fog enabled \n");
2078 LRDP("fog disabled in blender\n");
2079 rdp.fog_mode = RDP::fog_disabled;
2080 grFogMode (GR_FOG_DISABLE);
2083 else if (blender == 0xc410 || blender == 0xc411 || blender == 0xf500)
2085 grFogColorValue(rdp.fog_color);
2086 grFogMode (GR_FOG_WITH_TABLE_ON_FOGCOORD_EXT);
2087 rdp.fog_mode = RDP::fog_blend;
2088 LRDP("fog blend \n");
2090 else if (blender == 0x04d1)
2092 grFogColorValue(rdp.fog_color);
2093 grFogMode (GR_FOG_WITH_TABLE_ON_FOGCOORD_EXT);
2094 rdp.fog_mode = RDP::fog_blend_inverse;
2095 LRDP("fog blend \n");
2099 LRDP("fog disabled\n");
2100 rdp.fog_mode = RDP::fog_disabled;
2101 grFogMode (GR_FOG_DISABLE);
2106 if (rdp.update & UPDATE_VIEWPORT)
2108 rdp.update ^= UPDATE_VIEWPORT;
2111 float scale_x = (float)fabs(rdp.view_scale[0]);
2112 float scale_y = (float)fabs(rdp.view_scale[1]);
2114 rdp.clip_min_x = max((rdp.view_trans[0] - scale_x + rdp.offset_x) / rdp.clip_ratio, 0.0f);
2115 rdp.clip_min_y = max((rdp.view_trans[1] - scale_y + rdp.offset_y) / rdp.clip_ratio, 0.0f);
2116 rdp.clip_max_x = min((rdp.view_trans[0] + scale_x + rdp.offset_x) * rdp.clip_ratio, settings.res_x);
2117 rdp.clip_max_y = min((rdp.view_trans[1] + scale_y + rdp.offset_y) * rdp.clip_ratio, settings.res_y);
2119 FRDP (" |- viewport - (%d, %d, %d, %d)\n", (wxUint32)rdp.clip_min_x, (wxUint32)rdp.clip_min_y, (wxUint32)rdp.clip_max_x, (wxUint32)rdp.clip_max_y);
2120 if (!rdp.scissor_set)
2122 rdp.scissor.ul_x = (wxUint32)rdp.clip_min_x;
2123 rdp.scissor.lr_x = (wxUint32)rdp.clip_max_x;
2124 rdp.scissor.ul_y = (wxUint32)rdp.clip_min_y;
2125 rdp.scissor.lr_y = (wxUint32)rdp.clip_max_y;
2126 grClipWindow (rdp.scissor.ul_x, rdp.scissor.ul_y, rdp.scissor.lr_x, rdp.scissor.lr_y);
2131 if (rdp.update & UPDATE_SCISSOR)
2134 LRDP (" + update end\n");
2137 void set_message_combiner ()
2139 grColorCombine (GR_COMBINE_FUNCTION_SCALE_OTHER,
2140 GR_COMBINE_FACTOR_ONE,
2141 GR_COMBINE_LOCAL_NONE,
2142 GR_COMBINE_OTHER_TEXTURE,
2144 grAlphaCombine (GR_COMBINE_FUNCTION_SCALE_OTHER,
2145 GR_COMBINE_FACTOR_ONE,
2146 GR_COMBINE_LOCAL_NONE,
2147 GR_COMBINE_OTHER_TEXTURE,
2149 if (settings.buff_clear && (settings.show_fps & 0x08))
2150 grAlphaBlendFunction (GR_BLEND_SRC_ALPHA,
2151 GR_BLEND_ONE_MINUS_SRC_ALPHA,
2155 grAlphaBlendFunction (GR_BLEND_ONE,
2159 grAlphaTestFunction (GR_CMP_ALWAYS);
2160 if (grStippleModeExt)
2162 grStippleModeExt(GR_STIPPLE_DISABLE);
2164 grTexFilterMode (0, GR_TEXTUREFILTER_BILINEAR, GR_TEXTUREFILTER_BILINEAR);
2165 grTexCombine (GR_TMU1,
2166 GR_COMBINE_FUNCTION_NONE,
2167 GR_COMBINE_FACTOR_NONE,
2168 GR_COMBINE_FUNCTION_NONE,
2169 GR_COMBINE_FACTOR_NONE,
2171 grTexCombine (GR_TMU0,
2172 GR_COMBINE_FUNCTION_LOCAL,
2173 GR_COMBINE_FACTOR_NONE,
2174 GR_COMBINE_FUNCTION_LOCAL,
2175 GR_COMBINE_FACTOR_NONE,
2177 grTexSource(GR_TMU0,
2178 voodoo.tex_min_addr[GR_TMU0] + offset_font,
2179 GR_MIPMAPLEVELMASK_BOTH,
2181 grFogMode (GR_FOG_DISABLE);