randtri.c 3.81 KB
/**************************************************************************
 *                                                                        *
 *               Copyright (C) 1995, Silicon Graphics, Inc.               *
 *                                                                        *
 *  These coded instructions, statements, and computer programs  contain  *
 *  unpublished  proprietary  information of Silicon Graphics, Inc., and  *
 *  are protected by Federal copyright  law.  They  may not be disclosed  *
 *  to  third  parties  or copied or duplicated in any form, in whole or  *
 *  in part, without the prior written consent of Silicon Graphics, Inc.  *
 *                                                                        *
 *************************************************************************/

#include <ultra64.h>
#include <PR/ramrom.h>  /* needed for argument passing into the app */
#include <assert.h>

#include "rtmonky.h"

void rand_tri( Vtx_t * );
int  xrand( void );

#define BIG_RTS

#ifdef BIG_RTS
#define BOXX    (10799)
#define BOXY    (13399)
#else
#define BOXX    (107)
#define BOXY    (131)
#endif

/* Both are prime and so is the result! */
#define GOOD_SEED       ((7789<<16)+13399)

unsigned int xseed = 174823885;

void
randtri( Gfx **pglistp, Dynamic *dynamicp, int num )
{
    Vtx *vcur;
    Gfx *gcur;
    Gfx *glistp;
    int i;

    glistp = *pglistp;

    vcur = &(dynamicp->vtlist[0]);
    gcur = &(dynamicp->gtlist[0]);

    gSPDisplayList(glistp++, gcur);

    gDPPipeSync(gcur++ );
    gSPClearGeometryMode(gcur++, G_ZBUFFER);
    gSPClearGeometryMode(gcur++, G_CULL_BACK);
    gSPClearGeometryMode(gcur++, G_CULL_FRONT);
    gSPSetGeometryMode(gcur++, G_SHADE);
    gDPSetCombineMode (gcur++, G_CC_SHADE, G_CC_SHADE);

    for(i=0;i<num;i++) {
	rand_tri( &(vcur->v) );
	gSPVertex(gcur++, &(vcur->v), 3, 0);
	gSP1Triangle(gcur++, 0, 1, 2, 0);

	vcur+= 3;
    };

    gSPEndDisplayList( gcur++ );

    *pglistp = glistp;
}


#ifdef SHOW_NUMS
void font_show_num( Gfx **, int, int, int );
        if( (count & 0x00) == 0 ) {
	    font_show_num( &glistp, 100, 100, count );

	    font_show_num( &glistp, 100, 200, xseed );
	}
	count ++;
}
#endif


#define K64	(65536)

#define R(y) (xrand()%y)

void
rand_tri( Vtx_t *vp )
{
    int x,y;
    int x1,y1;
    int x2,y2;
    int dx,dy;
    int base2, hscale;
    int rb, rh;


    x1 = vp->ob[0] = R(BOXX);
    y1 = vp->ob[1] = R(BOXY);
    vp->ob[2] = 0;

    vp->flag  = 0;

    vp->tc[0] = 0;
    vp->tc[1] = 0;

    vp->cn[0] = R(255);
    vp->cn[1] = R(255);
    vp->cn[2] = R(255);
    vp->cn[3] = R(255);

    vp++;

    x2 = vp->ob[0] = R(BOXX);
    y2 = vp->ob[1] = R(BOXY);
    vp->ob[2] = 0;

    vp->flag  = 0;

    vp->tc[0] = 0;
    vp->tc[1] = 0;

    vp->cn[0] = R(255);
    vp->cn[1] = R(255);
    vp->cn[2] = R(255);
    vp->cn[3] = R(255);

    vp++;

    dx = x2 - x1;
    dy = y2 - y1;

    base2 = (dx*dx+dy*dy + 32) >> 6;

    if( base2 < 2 )
	base2 = 1;

#define MAX_AREA (BOXX*BOXY)

    hscale = MAX_AREA/base2;

/*     emPrintf("dx=%d, dy=%d, hscale=%d/64K (%g)\n", dx, dy, hscale, hscale/65536.0 ); */

    rb = R(K64);
    rh = R(hscale);

    x = x1 + ((dx*rb + K64/2)>>16) + ((dy*rh +  K64/2)>>16);
    y = y1 + ((dy*rb + K64/2)>>16) - ((dx*rh +  K64/2)>>16);
    if( (y2 > y1) && (y > y2) )
	y = y2;
    else if( (y1 > y2) && (y > y1) )
	y = y1;

    if( (y2 < y1) && (y < y2) )
	y = y2;
    else if( (y1 < y2) && (y < y1) )
	y = y1;

    if( x <= 0 )
	x = 1;
    else if( x >= BOXX )
	x = BOXX-1;

    vp->ob[0] = x;
    vp->ob[1] = y;
    vp->ob[2] = -5;

    vp->flag  = 0;

    vp->tc[0] = 0;
    vp->tc[1] = 0;

    vp->cn[0] = R(255);
    vp->cn[1] = R(255);
    vp->cn[2] = R(255);
    vp->cn[3] = R(255);

}

int xrand( void )
{
    unsigned int x;

    x = (xseed<<2) + 2;

    x *= (x+1);
    x = x >> 2;

    xseed = x;

    return( x );
}