flat_shade_dl.c 5.03 KB

/**************************************************************************
 *                                                                        *
 *               Copyright (C) 1994, 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.  *
 *                                                                        *
 *************************************************************************/

/*
 * File:	flat_shade_dl.c
 *
 * This program reads in a RDP command stream and changes smooth shaded prims
 * to be flat shaded using random colors.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef __sgi__
#include <bstring.h>
#endif
#include <string.h>

#include "mbi.h"

#define UsageString	"<rdp binfile> <outfile>"

#ifndef TRUE
#   define TRUE		1
#endif

#ifndef FALSE
#   define FALSE	0
#endif

#ifndef ABS
#define ABS(x)		((x) > 0 ? (x) : (-(x)))
#endif

typedef int			bool;
typedef char 	  		i8;
typedef unsigned char 	  	u8;
typedef short int  		i16;
typedef unsigned short int  	u16;
typedef long int 		i32;
typedef unsigned long int 	u32;


FILE *Outfp = NULL;

static bool	do_verbose = FALSE;
static u32	NumPrimitives = 0;

/*
 * Parse and dump a RDP-type display list command.
 */
static void
read_rdp(u32 gfx0, u32 gfx1, FILE *in)
{
    u32		addr, buffer[44];
    char	op, b0, tstring[32];
    int i;

    /* every command has at least 64-bits... */
    buffer[0] = gfx0;
    buffer[1] = gfx1;

    op = (char) ((gfx0 & 0xff000000) >> 24);

    switch (op) {

      case (char) G_SETCIMG:
      case (char) G_SETZIMG:
      case (char) G_SETTIMG:
      case (char) G_SETENVCOLOR:
      case (char) G_SETPRIMCOLOR:
      case (char) G_SETBLENDCOLOR:
      case (char) G_SETFOGCOLOR:
      case (char) G_SETFILLCOLOR:
      case (char) G_FILLRECT:
      case (char) G_SETTILE:
      case (char) G_LOADTILE:
      case (char) G_LOADBLOCK:
      case (char) G_SETTILESIZE:
      case (char) G_LOADTLUT:
      case (char) G_RDPSETOTHERMODE:
      case (char) G_SETPRIMDEPTH:
      case (char) G_SETSCISSOR:
      case (char) G_SETCONVERT:
      case (char) G_SETKEYR:
      case (char) G_SETKEYGB:
      case (char) G_RDPFULLSYNC:
      case (char) G_RDPTILESYNC:
      case (char) G_RDPPIPESYNC:
      case (char) G_NOOP:
      case (char) G_TEXRECTFLIP:
      case (char) G_TEXRECT:
      case (char) G_SETCOMBINE:
        fwrite( &gfx0, sizeof(u32), 1, Outfp);
        fwrite( &gfx1, sizeof(u32), 1, Outfp);
	break;

      case (char) G_TRI_FILL:
	fread(&buffer[2], sizeof(u32), 6, in);
        fwrite( buffer, sizeof(u32), 8, Outfp);
        break;

      case (char) G_TRI_SHADE:
	fread(&buffer[2], sizeof(u32), 22, in);
        buffer[8] = random() & 0x00ff00ff;
        buffer[9] = (random() & 0x00ff0000) | 0xff;
        for(i = 10; i < 24; i++)
          buffer[i] = 0;
        fwrite( buffer, sizeof(u32), 24, Outfp);
	break;

      case (char) G_TRI_TXTR:
	fread(&buffer[2], sizeof(u32), 22, in);
        fwrite( buffer, sizeof(u32), 24, Outfp);
	break;

      case (char) G_TRI_SHADE_TXTR:
	fread(&buffer[2], sizeof(u32), 38, in);
        fwrite( buffer, sizeof(u32), 40, Outfp);
	break;

      case (char) G_TRI_FILL_ZBUFF:
	fread(&buffer[2], sizeof(u32), 10, in);
        fwrite( buffer, sizeof(u32), 12, Outfp);
	break;

      case (char) G_TRI_SHADE_ZBUFF:
	fread(&buffer[2], sizeof(u32), 26, in);
        fwrite( buffer, sizeof(u32), 28, Outfp);
	break;

      case (char) G_TRI_TXTR_ZBUFF:
	fread(&buffer[2], sizeof(u32), 26, in);
        fwrite( buffer, sizeof(u32), 28, Outfp);
	break;

      case (char) G_TRI_SHADE_TXTR_ZBUFF:
	fread(&buffer[2], sizeof(u32), 42, in);
        fwrite( buffer, sizeof(u32), 44, Outfp);
	break;

      default:
	break;
    }

}

/*
 * main routine.
 */
int
main(int argc, char *argv[])
{
    FILE	*bin_file;
    u32		gfx0, gfx1, *gfxp;
    int		i;

    while ((argc > 1) && (argv[1][0] == '-')) {
	switch(argv[1][1]) {

	  case 'v':
	    do_verbose = FALSE;
	    break;

	  default:
	    fprintf(stderr,"%s : unknown argument [%s].\n",argv[0],argv[1]);
	    break;
	}
	argc--;
	argv++;
    }

    if (argc != 3) {
	fprintf(stderr,"usage: %s %s.\n",argv[0],UsageString);
	exit(-1);
    }

    if ((bin_file=fopen(argv[1],"r"))==NULL) {
	fprintf(stderr,"can't open bin file [%s].\n",argv[1]);
	exit(-1);
    }

    if (argc > 2) {
	if ((Outfp=fopen(argv[2],"w"))==NULL) {
	    fprintf(stderr,"can't open output file [%s].\n",argv[2]);
	    exit(-1);
	}
    }

    fread(&gfx0, sizeof(u32), 1, bin_file);
    fread(&gfx1, sizeof(u32), 1, bin_file);
    while (!feof(bin_file)) {

	read_rdp(gfx0, gfx1, bin_file);

	fread(&gfx0, sizeof(u32), 1, bin_file);
	fread(&gfx1, sizeof(u32), 1, bin_file);
    }

    fclose(Outfp);
}