Exemplo n.º 1
0
static void
init_effect (void *params)
{
  pvr_mem_print_list ();
  // pvr_mem_reset ();

  warp_texture[0] = pvr_mem_malloc (1024 * 512 * 2);
  warp_texture[1] = pvr_mem_malloc (1024 * 512 * 2);
  memset (warp_texture[0], 0, 1024 * 512 * 2);
  memset (warp_texture[1], 0, 1024 * 512 * 2);

  if (cooltmp)
    {
      cooling_texture = pvr_mem_malloc (COOL_X * COOL_Y * 2);
      pvr_txr_load_ex (cooltmp, cooling_texture, COOL_X, COOL_Y,
		       PVR_TXRLOAD_16BPP);
      free (cooltmp);
      cooltmp = 0;
    }

  flame_texture = pvr_mem_malloc (256 * 256 * 2);
  png_to_texture ("/rd/flametex.png", flame_texture, PNG_NO_ALPHA);
  
  backdrop_texture = pvr_mem_malloc (512 * 256 * 2);
  png_to_texture ("/rd/backdrop.png", backdrop_texture, PNG_NO_ALPHA);
  //printf ("memory free: %d bytes\n", pvr_mem_available ());
}
Exemplo n.º 2
0
/* Load a PCX texture 
 * 
 * Parameters : none
 * 
 * */
void GraphicalElement::createTexturePCX(char *fileName, int indiceTex)
{
	kos_img_t img;
  	pvr_ptr_t txaddr;
  	GLuint txr;
  	
  	if (pcx_to_img(fileName, &img) < 0) 
  	{
    	printf("can't load %s\n", fileName);
    	return;
  	}
  
  	txaddr = pvr_mem_malloc(img.w * img.h * 2);
  	pvr_txr_load_kimg(&img, txaddr, PVR_TXRLOAD_INVERT_Y);
  	kos_img_free(&img, 0);

  	
  	glGenTextures(1, &txr);
  	
  	/* insertion in the vector */
  	listeTexture.push_back(txr);
  	glBindTexture(GL_TEXTURE_2D, listeTexture.back());
  
  	glKosTex2D(GL_RGB565_TWID, img.w, img.h, txaddr);
  	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_FILTER, GL_FILTER_BILINEAR);
}
Exemplo n.º 3
0
/* Load a texture from a 2D image */
void glTexImage2D(GLenum target, GLint level,
		GLint internal_fmt,
		GLsizei width, GLsizei height,
		GLint border, GLenum format, GLenum type,
		const GLvoid *pixels) {
	pvr_ptr_t txr;
	
	assert_msg(border == 0 && level == 0, "Borders and levels not supported.");
	assert_msg((internal_fmt & ~1) == (format & ~1), "Pixel conversion not supported.");

	/* Allocate space for it */
	txr = pvr_mem_malloc(width * height * 2);

	/* Load the texture data */
	if ((internal_fmt & 1) != (format & 1))
	 	pvr_txr_load_ex((GLvoid *)pixels, txr, width, height, PVR_TXRLOAD_16BPP);
	else
		pvr_txr_load((GLvoid *)pixels, txr, width * height * 2);
		
	/* Store texture state in context */
	gl_cur_texture->txr.width = width; 
	gl_cur_texture->txr.height =  height;
	gl_cur_texture->txr.format =  internal_fmt;
	gl_cur_texture->txr.base = txr;

	gl_pbuf_submitted = GL_FALSE;
}
Exemplo n.º 4
0
FontMemory FontManager::getFont(FONTS i){
	if(font[i].fntPtr == NULL){
		char tmpbuf[256];
		sprintf(tmpbuf, "/rd/fonts/%i.txf", i);
		FontMemory fmem;
		fmem.memPtr = pvr_mem_malloc(512 * 512 * 2);
		fmem.fntPtr = new fntTexFont(tmpbuf);
		font[i] = fmem;
	}
	return font[i];
}
Exemplo n.º 5
0
/* initialize draw stuff: get our texture of the font, etc */
void conio_draw_init() {
	uint16 *vram;
	int x, y;

	font_texture = pvr_mem_malloc(256*256*2);
	vram = (uint16 *)font_texture;

	for (y = 0; y < 8; y++) {
		for (x = 0; x < 16; x++) {
			bfont_draw(vram, 256, 0, y*16 + x);
			vram += 16;
		}
		vram += 23*256;
	}
}
Exemplo n.º 6
0
/* Load a texture using pcx_load_texture and glKosTex2D */
void loadtxr(const char *fn, GLuint *txr) {
	kos_img_t img;
	pvr_ptr_t txaddr;
	
	if (pcx_to_img(fn, &img) < 0) {
		Logger::log(LOG_ERROR,"can't load %s\n", fn);
		return;
	}
	
	txaddr = pvr_mem_malloc(img.w * img.h * 2);
	pvr_txr_load_kimg(&img, txaddr, PVR_TXRLOAD_INVERT_Y);
	kos_img_free(&img, 0);
	
	glKosTex2D(GL_RGB565_TWID, img.w, img.h, txaddr);
}
Exemplo n.º 7
0
/* Load a texture using pcx_load_texture and glKosTex2D */
void loadtxr(const char *fn, GLuint *txr) {
	kos_img_t img;
	pvr_ptr_t txaddr;

	if (pcx_to_img(fn, &img) < 0) {
		printf("can't load %s\n", fn);
		return;
	}

	txaddr = pvr_mem_malloc(img.w * img.h * 2);
	pvr_txr_load_kimg(&img, txaddr, PVR_TXRLOAD_INVERT_Y);
	kos_img_free(&img, 0);

	glGenTextures(1, txr);
	glBindTexture(GL_TEXTURE_2D, *txr);
	glKosTex2D(GL_RGB565_TWID, img.w, img.h, txaddr);
}
Exemplo n.º 8
0
/*
	Load in a VQ compressed texture with twiddling, format ARGB4444
	fn - File pointer
	text - texture structure to load into
*/
void Load_VQTexture(const char* fn,Texture* text) {

	kos_img_t img;
	if(text->Allocated == 1){
		printf("TEXTURE ALREADY ALLOCATED!\n");
		return;
	}
	if(kmg_to_img(fn,&img)){
		return;
	}
	text->Allocated = 1;
	text->w = img.w;
	text->h = img.h;
	text->fmt = PVR_TXRFMT_ARGB4444 | PVR_TXRFMT_VQ_ENABLE | PVR_TXRFMT_TWIDDLED;
	text->txt = pvr_mem_malloc(img.byte_count);
	
    pvr_txr_load_kimg(&img, text->txt, 0);
    kos_img_free(&img, 0);
}
Exemplo n.º 9
0
void setup_util_texture() {
	uint16	*vram;
	int	x, y;
	pvr_poly_cxt_t	cxt;

	util_texture = pvr_mem_malloc(256*256*2);
	printf("util_texture at %08x\n", util_texture);
	vram = (uint16 *)util_texture;
	
	/* First dump in the mouse cursor */
	for (y=0; y<16; y++) {
		for (x=0; x<10; x++) {
			if (mouse1_xpm[y*10+x] == '.')
				*vram = 0xffff;
			else if (mouse1_xpm[y*10+x] == '+')
				*vram = 0xf000;
			else
				*vram = 0x0000;
			vram++;
		}
		vram += 256 - 10;
	}
	
	/* Now add the rest as ASCII characters */
	vram = (uint16 *)util_texture;
	for (y=0; y<8; y++) {
		for (x=0; x<16; x++) {
			/* Skip the first (it's a mouse pointer) */
			if (x != 0 || y != 0) 
				bfont_draw(vram, 256, 0, y*16+x);
			vram += 16;
		}
		vram += 23*256;
	}

	/* Setup a polygon header for the util texture */
	pvr_poly_cxt_txr(&cxt, PVR_LIST_TR_POLY, PVR_TXRFMT_ARGB4444 | PVR_TXRFMT_NONTWIDDLED,
		256, 256, util_texture, PVR_FILTER_NONE);
	pvr_poly_compile(&util_txr_hdr, &cxt);
}
Exemplo n.º 10
0
static pvr_ptr_t
load_bumpmap (const char *filename)
{
  void *data = malloc (128 * 128 * 2);
  pvr_ptr_t pvraddr;
  FILE *fp = fopen (filename, "rb");

  if (!fp)
    {
      printf ("Couldn't load bump map '%s'\n", filename);
      exit (1);
    }
  
  fread (data, 1, 128 * 128 * 2, fp);
  fclose (fp);
  
  pvraddr = pvr_mem_malloc (128 * 128 * 2);
  pvr_txr_load (data, pvraddr, 128 * 128 * 2);
  free (data);
  
  return pvraddr;
}
Exemplo n.º 11
0
static void MakeVideoTexture(video_txr_t *txr, int width, int height, int format, int filler) {
	pvr_poly_cxt_t tmp;

	int tw,th;
	for(tw = 8; tw < width ; tw <<= 1);
	for(th = 8; th < height; th <<= 1);

	txr->width = width;
	txr->height = height;
	txr->tw = tw;
	txr->th = th;
	
	if(txr->addr || txr->backbuf) {
		FreeVideoTexture(txr);
	}
	
	txr->backbuf = memalign(32, txr->tw * txr->height * 2);
	txr->addr = pvr_mem_malloc(tw * th * 2);
	
	pvr_poly_cxt_txr(&tmp, PVR_LIST_OP_POLY, format, tw, th, txr->addr, filler);
	pvr_poly_compile(&txr->hdr, &tmp);
}
Exemplo n.º 12
0
void setup() {
    pvr_poly_cxt_t cxt;
    int i;
    float x, y, z;
    int w1, h1, w2, h2;
    uint32 fmt1, fmt2;
    kos_img_t img;

    if(pcx_to_img("/rd/crate.pcx", &img)) {
        printf("Failed to load /rd/crate.pcx\n");
        exit(1);
    }

    w1 = img.w;
    h1 = img.h;
    fmt1 = PVR_TXRFMT_RGB565 | PVR_TXRFMT_TWIDDLED;
    txr1 = pvr_mem_malloc(img.byte_count);
    pvr_txr_load_kimg(&img, txr1, 0);
    kos_img_free(&img, 0);

    if(kmg_to_img("/rd/fruit.kmg", &img)) {
        printf("Failed to load /rd/fruit.kmg\n");
        exit(1);
    }

    w2 = img.w;
    h2 = img.h;
    fmt2 = PVR_TXRFMT_RGB565 | PVR_TXRFMT_VQ_ENABLE | PVR_TXRFMT_TWIDDLED;
    txr2 = pvr_mem_malloc(img.byte_count);
    pvr_txr_load_kimg(&img, txr2, 0);
    kos_img_free(&img, 0);

    printf("Loaded textures\n");

    pvr_poly_cxt_txr_mod(&cxt, list, fmt1, w1, h1, txr1, PVR_FILTER_BILINEAR,
                         fmt2, w2, h2, txr2, PVR_FILTER_NONE);
    pvr_poly_mod_compile(&phdr, &cxt);

    pvr_mod_compile(&mhdr, list + 1, PVR_MODIFIER_OTHER_POLY, PVR_CULLING_NONE);
    pvr_mod_compile(&mhdr2, list + 1, PVR_MODIFIER_INCLUDE_LAST_POLY,
                    PVR_CULLING_NONE);

    for(i = 0; i < NUM_POLYS; ++i) {
        x = rand() % 640;
        y = rand() % 480;
        z = rand() % 100 + 1;

        verts[i * 4].flags = PVR_CMD_VERTEX;
        verts[i * 4].x = x - 50;
        verts[i * 4].y = y + 50;
        verts[i * 4].z = z;
        verts[i * 4].u0 = 0.0f;
        verts[i * 4].v0 = 1.0f;
        verts[i * 4].argb0 = 0xFFFFFFFF;
        verts[i * 4].oargb0 = 0xFF000000;
        verts[i * 4].u1 = 0.0f;
        verts[i * 4].v1 = 1.0f;
        verts[i * 4].argb1 = 0xFFFFFFFF;
        verts[i * 4].oargb1 = 0xFF000000;
        verts[i * 4].d1 = verts[i * 4].d2 = verts[i * 4].d3 =
                                                verts[i * 4].d4 = 0;

        verts[i * 4 + 1].flags = PVR_CMD_VERTEX;
        verts[i * 4 + 1].x = x - 50;
        verts[i * 4 + 1].y = y - 50;
        verts[i * 4 + 1].z = z;
        verts[i * 4 + 1].u0 = 0.0f;
        verts[i * 4 + 1].v0 = 0.0f;
        verts[i * 4 + 1].argb0 = 0xFFFFFFFF;
        verts[i * 4 + 1].oargb0 = 0xFF000000;
        verts[i * 4 + 1].u1 = 0.0f;
        verts[i * 4 + 1].v1 = 0.0f;
        verts[i * 4 + 1].argb1 = 0xFFFFFFFF;
        verts[i * 4 + 1].oargb1 = 0xFF000000;
        verts[i * 4 + 1].d1 = verts[i * 4 + 1].d2 = verts[i * 4 + 1].d3 =
                                  verts[i * 4 + 1].d4 = 0;

        verts[i * 4 + 2].flags = PVR_CMD_VERTEX;
        verts[i * 4 + 2].x = x + 50;
        verts[i * 4 + 2].y = y + 50;
        verts[i * 4 + 2].z = z;
        verts[i * 4 + 2].u0 = 1.0f;
        verts[i * 4 + 2].v0 = 1.0f;
        verts[i * 4 + 2].argb0 = 0xFFFFFFFF;
        verts[i * 4 + 2].oargb0 = 0xFF000000;
        verts[i * 4 + 2].u1 = 1.0f;
        verts[i * 4 + 2].v1 = 1.0f;
        verts[i * 4 + 2].argb1 = 0xFFFFFFFF;
        verts[i * 4 + 2].oargb1 = 0xFF000000;
        verts[i * 4 + 2].d1 = verts[i * 4 + 2].d2 = verts[i * 4 + 2].d3 =
                                  verts[i * 4 + 2].d4 = 0;

        verts[i * 4 + 3].flags = PVR_CMD_VERTEX_EOL;
        verts[i * 4 + 3].x = x + 50;
        verts[i * 4 + 3].y = y - 50;
        verts[i * 4 + 3].z = z;
        verts[i * 4 + 3].u0 = 1.0f;
        verts[i * 4 + 3].v0 = 0.0f;
        verts[i * 4 + 3].argb0 = 0xFFFFFFFF;
        verts[i * 4 + 3].oargb0 = 0xFF000000;
        verts[i * 4 + 3].u1 = 1.0f;
        verts[i * 4 + 3].v1 = 0.0f;
        verts[i * 4 + 3].argb1 = 0xFFFFFFFF;
        verts[i * 4 + 3].oargb1 = 0xFF000000;
        verts[i * 4 + 3].d1 = verts[i * 4 + 3].d2 = verts[i * 4 + 3].d3 =
                                  verts[i * 4 + 3].d4 = 0;
    }
}
Exemplo n.º 13
0
static void
preinit_skybox (void)
{
  static int initialised = 0;
  kos_img_t txr;
  
  if (initialised)
    return;

  printf ("Loading sky box texture");
  fflush (stdout);
#if 0
  skytex[0] = pvr_mem_malloc (512 * 512 * 2);
  png_to_texture ("/rd/sky23.png", skytex[0], PNG_NO_ALPHA);
  putchar ('.');
  fflush (stdout);
  skytex[1] = pvr_mem_malloc (512 * 512 * 2);
  png_to_texture ("/rd/sky24.png", skytex[1], PNG_NO_ALPHA);
  putchar ('.');
  fflush (stdout);
  skytex[2] = pvr_mem_malloc (512 * 512 * 2);
  png_to_texture ("/rd/sky27.png", skytex[2], PNG_NO_ALPHA);
  putchar ('.');
  fflush (stdout);
  skytex[3] = pvr_mem_malloc (512 * 512 * 2);
  png_to_texture ("/rd/sky28.png", skytex[3], PNG_NO_ALPHA);
  putchar ('.');
  fflush (stdout);
  skytex[4] = pvr_mem_malloc (512 * 512 * 2);
  png_to_texture ("/rd/sky25.png", skytex[4], PNG_NO_ALPHA);
  putchar ('.');
  fflush (stdout);
  skytex[5] = pvr_mem_malloc (512 * 512 * 2);
  png_to_texture ("/rd/sky26.png", skytex[5], PNG_NO_ALPHA);
  putchar ('.');
#elif 0
  skytex[0] = pvr_mem_malloc (512 * 512 * 2);
  jpeg_to_texture ("/rd/sky23.jpg", skytex[0], 512, 1);
  putchar ('.');
  fflush (stdout);
  skytex[1] = pvr_mem_malloc (512 * 512 * 2);
  jpeg_to_texture ("/rd/sky24.jpg", skytex[1], 512, 1);
  putchar ('.');
  fflush (stdout);
  skytex[2] = pvr_mem_malloc (512 * 512 * 2);
  jpeg_to_texture ("/rd/sky27.jpg", skytex[2], 512, 1);
  putchar ('.');
  fflush (stdout);
  skytex[3] = pvr_mem_malloc (512 * 512 * 2);
  jpeg_to_texture ("/rd/sky28.jpg", skytex[3], 512, 1);
  putchar ('.');
  fflush (stdout);
  skytex[4] = pvr_mem_malloc (512 * 512 * 2);
  jpeg_to_texture ("/rd/sky25.jpg", skytex[4], 512, 1);
  putchar ('.');
  fflush (stdout);
  skytex[5] = pvr_mem_malloc (512 * 512 * 2);
  jpeg_to_texture ("/rd/sky26.jpg", skytex[5], 512, 1);
  putchar ('.');
#else
  kmg_to_img ("/rd/sky23.kmg", &txr);
  skytex[0] = pvr_mem_malloc (txr.byte_count);
  pvr_txr_load_kimg (&txr, skytex[0], PVR_TXRLOAD_SQ);
  kos_img_free (&txr, 0);
  
  kmg_to_img ("/rd/sky24.kmg", &txr);
  skytex[1] = pvr_mem_malloc (txr.byte_count);
  pvr_txr_load_kimg (&txr, skytex[1], PVR_TXRLOAD_SQ);
  kos_img_free (&txr, 0);
  
  kmg_to_img ("/rd/sky27.kmg", &txr);
  skytex[2] = pvr_mem_malloc (txr.byte_count);
  pvr_txr_load_kimg (&txr, skytex[2], PVR_TXRLOAD_SQ);
  kos_img_free (&txr, 0);
  
  kmg_to_img ("/rd/sky28.kmg", &txr);
  skytex[3] = pvr_mem_malloc (txr.byte_count);
  pvr_txr_load_kimg (&txr, skytex[3], PVR_TXRLOAD_SQ);
  kos_img_free (&txr, 0);
  
  kmg_to_img ("/rd/sky25.kmg", &txr);
  skytex[4] = pvr_mem_malloc (txr.byte_count);
  pvr_txr_load_kimg (&txr, skytex[4], PVR_TXRLOAD_SQ);
  kos_img_free (&txr, 0);
  
  kmg_to_img ("/rd/sky26.kmg", &txr);
  skytex[5] = pvr_mem_malloc (txr.byte_count);
  pvr_txr_load_kimg (&txr, skytex[5], PVR_TXRLOAD_SQ);
  kos_img_free (&txr, 0);
#endif
  putchar ('\n');

  skybox = create_skybox (200, skytex, 512, 512, PVR_TXRFMT_RGB565
			  | PVR_TXRFMT_TWIDDLED | PVR_TXRFMT_VQ_ENABLE);

  initialised = 1;
}
Exemplo n.º 14
0
void Load_Texture(const char* fn, Texture* t){
	FILE* fp;
	header_t  hdr;
	fp = fopen(fn,"r");
	
	fread(&hdr,sizeof(hdr),1,fp);	// read in the header
	
	t->w = hdr.width;
	t->h = hdr.height;
	
	t->fmt = hdr.type;
	//Allocate texture memory
	t->txt = pvr_mem_malloc(hdr.size);
	//Temporary ram storage of texture
	void* temp = malloc(hdr.size);
	// Load texture into ram
	fread(temp,hdr.size,1,fp);
	// SQ copy into VRAM
	pvr_txr_load(temp,t->txt,hdr.size);
	//Free RAM
	free(temp);
	temp = NULL;
	fclose(fp);
	
	/*
		Palette loading and management
	*/
	if( ((t->fmt >> 27) & 7) > 4 ) {
		if(t->fmt & PVR_TXRFMT_PAL4BPP){
			// Append palette suffix to filepath
			char pf[64];
			strcpy(pf,fn);
			strcat(pf,".pal");
			fp = fopen(pf,"r");
			pal_header_t phdr;
			//read in the 8-byte header
			fread(&phdr,sizeof(pal_header_t),1,fp);
			//setup buffer
			void *palette = malloc(phdr.numcolors*4);
			Uint32 i;
			//Make entries readable to PVR
			Uint32* packed = (Uint32*)palette;
			//Load entries in to buffer
			fread(packed,phdr.numcolors*4,1,fp);
			//Load palette entries into correct location ( first 512 bank)
			for(i = Pindex4*16; i < (Pindex4*16) + phdr.numcolors*4;i++){
				pvr_set_pal_entry(i,packed[i]);
			}
			//Set palette #
			t->palette = Pindex4;
		
			t->fmt |=  PVR_TXRFMT_4BPP_PAL(Pindex4);
		
		//Increase palettte index to prevent overwrite
			Pindex4++;
			//32 possible palettes in 512 bank
			if(Pindex4 == 32){
				Pindex4 = 0; // overwrite
			}

			packed = NULL;
			free(palette);
			fclose(fp);
		} else if(t->fmt & PVR_TXRFMT_PAL8BPP){
			char pf[64];
			strcpy(pf,fn);
			strcat(pf,".pal");
			fp = fopen(pf,"r");
			pal_header_t phdr;
			fread(&phdr,sizeof(pal_header_t),1,fp);
			void * palette = malloc(phdr.numcolors*4);
			Uint32 i;
			Uint32* packed = (Uint32*)palette;
			fread(packed,phdr.numcolors*4,1,fp);
			
			//Load palette entries into the second 512 bank
			for(i = (512 + Pindex8*256); i < (Pindex8*256 + 512) + phdr.numcolors*4;i++){
				pvr_set_pal_entry(i,packed[i]);
			}
			
			t->palette = Pindex8 | 0x80;
			t->fmt |=  PVR_TXRFMT_8BPP_PAL(Pindex8+2);
			Pindex8++;
			
			//Only 2 8-bit palettes can fit into second 512 bank
			if(Pindex8 == 2){
				Pindex8 = 0;
			}
		
			packed = NULL;
			free(palette);
			fclose(fp);
		}
	}
	
}
Exemplo n.º 15
0
/* Open the pvr texture and send it to VRAM, then bind it to the glKosTex2D context */
void loadpvr( const char * file_name, GLuint *txr ) {

  /* set up the files and buffers */
  FILE * pvr_file;
  char * pvr_header, * pvr_buffer;
  int header_len;
  struct pvr_tex pvr;
  
  /* open the pvr file */
  pvr_file = fopen( file_name ,"rb");
  if (pvr_file==NULL) printf ("File error",stderr);	

  /* Read the possible 0x00100000 byte header */  
  pvr_header = (char*)malloc( 32 );
  fread( pvr_header, 16, 2, pvr_file);

  /* obtain the PVR file size using fseek */
  fseek (pvr_file , 0 , SEEK_END);
  int pvrSize = ftell (pvr_file);

  /* Move to the begining of the PVR file */
  fseek (pvr_file , 0, SEEK_SET);
    
  /* Allocate RAM to contain the PVR file */
  if( pvr_buffer ) free( pvr_buffer );
  pvr_buffer = (char*)memalign( 32, pvrSize );
  //pvr_buffer = (char*)malloc( pvrSize );
  if ( pvr_buffer == NULL ) printf ("Memory error\n");

  /* GBIX = 0x00100000 byte header */
  if( (char)pvr_header[0] == 'G' && (char)pvr_header[1] == 'B'
      && (char)pvr_header[2] == 'I' && (char)pvr_header[3] == 'X' ) 
    header_len = 32;
  /* PVRT = 0x00010000 byte header */
  else if ( (char)pvr_header[0] == 'P' && (char)pvr_header[1] == 'V' 
            && (char)pvr_header[2] == 'R' )  
    header_len = 16;  
           
  /* Move past the header */
  fseek (pvr_file , header_len, SEEK_SET);

  /* Read the pvr texture data into RAM and close the file */
  fread (pvr_buffer,16,(pvrSize-header_len)/16,pvr_file);
  fclose( pvr_file );

  /* Get PVR Colorspace */
  unsigned int pvr_color = (unsigned int)pvr_header[header_len-8];
  switch( pvr_color ) {
          case 0x00: pvr.txrColor = PVR_TXRFMT_ARGB1555; break; //(bilevel translucent alpha 0,255)
          case 0x01: pvr.txrColor = PVR_TXRFMT_RGB565;   break; //(non translucent RGB565 )
          case 0x02: pvr.txrColor = PVR_TXRFMT_ARGB4444; break; //(translucent alpha 0-255)
          case 0x03: pvr.txrColor = PVR_TXRFMT_YUV422;   break; //(non translucent UYVY )
          case 0x04: pvr.txrColor = PVR_TXRFMT_BUMP;     break; //(special bump-mapping format)
          case 0x05: pvr.txrColor = PVR_TXRFMT_PAL4BPP;  break; //(4-bit palleted texture)
          case 0x06: pvr.txrColor = PVR_TXRFMT_PAL8BPP;  break; //(8-bit palleted texture)
          default:   break;            
  }

  /* Get PVR Format. Mip-Maps and Palleted Textures not Currently handled */
  unsigned int pvr_fmt = (unsigned int)pvr_header[header_len-7];
  switch( pvr_fmt ) {
          case 0x01: pvr.txrFmt = PVR_TXRFMT_TWIDDLED;                           break;//SQUARE TWIDDLED
          //case 0x02: pvr.txrFmt = SQUARE TWIDDLED & MIPMAP
          case 0x03: pvr.txrFmt = PVR_TXRFMT_VQ_ENABLE | PVR_TXRFMT_TWIDDLED;    break;//VQ TWIDDLED
          //case 0x04: pvr.txrFmt = VQ & MIPMAP
          //case 0X05: pvr.txrFmt = 8-BIT CLUT TWIDDLED
          //case 0X06: pvr.txrFmt = 4-BIT CLUT TWIDDLED
          //case 0x07: pvr.txrFmt = 8-BIT DIRECT TWIDDLED
          //case 0X08: pvr.txrFmt = 4-BIT DIRECT TWIDDLED
          case 0x09: pvr.txrFmt = PVR_TXRFMT_NONTWIDDLED;                        break;//RECTANGLE    
          case 0x0B: pvr.txrFmt = PVR_TXRFMT_STRIDE | PVR_TXRFMT_NONTWIDDLED;    break;//RECTANGULAR STRIDE
          case 0x0D: pvr.txrFmt = PVR_TXRFMT_TWIDDLED;                           break;//RECTANGULAR TWIDDLED
          case 0x10: pvr.txrFmt = PVR_TXRFMT_VQ_ENABLE | PVR_TXRFMT_NONTWIDDLED; break;//SMALL VQ
          //case 0x11: pvr.txrFmt = SMALL VQ & MIPMAP
          //case 0x12: pvr.txrFmt = SQUARE TWIDDLED & MIPMAP
          default:   pvr.txrFmt = PVR_TXRFMT_NONE; break;
  }
  
  /* Get PVR Texture Width */
  if( (unsigned int)pvr_header[header_len-4] == 0x08 && (unsigned int)pvr_header[header_len-3] == 0x00 )
        pvr.texWidth = 8; 
  else if( (unsigned int)pvr_header[header_len-4] == 0x10 && (unsigned int)pvr_header[header_len-3] == 0x00 )
        pvr.texWidth = 16; 
  else if( (unsigned int)pvr_header[header_len-4] == 0x20 && (unsigned int)pvr_header[header_len-3] == 0x00 )
        pvr.texWidth = 32; 
  else if( (unsigned int)pvr_header[header_len-4] == 0x40 && (unsigned int)pvr_header[header_len-3] == 0x00 )
        pvr.texWidth = 64;   
  else if( (unsigned int)pvr_header[header_len-4] == -0x80 && (unsigned int)pvr_header[header_len-3] == 0x00 )
        pvr.texWidth = 128;       
  else if( (unsigned int)pvr_header[header_len-4] == 0x00 && (unsigned int)pvr_header[header_len-3] == 0x01 )
        pvr.texWidth = 256;
  else if( (unsigned int)pvr_header[header_len-4] == 0x00 && (unsigned int)pvr_header[header_len-3] == 0x02 )
        pvr.texWidth = 512;
  else if( (unsigned int)pvr_header[header_len-4] == 0x00 && (unsigned int)pvr_header[header_len-3] == 0x04 )
        pvr.texWidth = 1024;
   /* Get PVR Texture  Height */
  if( (unsigned int)pvr_header[header_len-2] == 0x08 && (unsigned int)pvr_header[header_len-1] == 0x00 )
        pvr.texHeight = 8;
  else if( (unsigned int)pvr_header[header_len-2] == 0x10 && (unsigned int)pvr_header[header_len-1] == 0x00 )
        pvr.texHeight = 16;
  else if( (unsigned int)pvr_header[header_len-2] == 0x20 && (unsigned int)pvr_header[header_len-1] == 0x00 )
        pvr.texHeight = 32;
  else if( (unsigned int)pvr_header[header_len-2] == 0x40 && (unsigned int)pvr_header[header_len-1] == 0x00 )
        pvr.texHeight = 64;
  else if( (unsigned int)pvr_header[header_len-2] == -0x80 && (unsigned int)pvr_header[header_len-1] == 0x00 )
        pvr.texHeight = 128;
  else if( (unsigned int)pvr_header[header_len-2] == 0x00 && (unsigned int)pvr_header[header_len-1] == 0x01 )
        pvr.texHeight = 256; 
  else if( (unsigned int)pvr_header[header_len-2] == 0x00 && (unsigned int)pvr_header[header_len-1] == 0x02 )
        pvr.texHeight = 512; 
  else if( (unsigned int)pvr_header[header_len-2] == 0x00 && (unsigned int)pvr_header[header_len-1] == 0x04 )
        pvr.texHeight = 1024; 
                  
  printf("PVR TXR Size: %i bytes, %ix%i pixels\n", pvrSize, pvr.texWidth, pvr.texHeight );
  
  /* Allocate VRAM */
  if(pvr.txrAddr) pvr_mem_free(pvr.txrAddr);
  pvr.txrAddr = pvr_mem_malloc(pvrSize);
  
  /* Transfer the texture from RAM to VRAM */
  /* SH4->PVR DMA Transfer */
  if( header_len != 16 ) {
     if( pvrSize > 16384)
        dcache_flush_range((uint32)pvr_buffer, 16384);
     else
        dcache_flush_range((uint32)pvr_buffer, pvrSize);
     while (!pvr_dma_ready());
     pvr_dma_transfer( (void*)pvr_buffer, (uint32)pvr.txrAddr, pvrSize,
                       PVR_DMA_VRAM64, PVR_TXRLOAD_NONBLOCK, NULL, 0);
  }
  /* SH4->PVR SQ Transfer */
  else {
     pvr_txr_load( pvr_buffer, pvr.txrAddr, pvrSize);
  }

  /* Free the RAM */
  if( pvr_header ) free( pvr_header );  
  if( pvr_buffer ) free( pvr_buffer );
  
  /* Bind the texture data to the glTex2D context */
  glKosTex2D( pvr.txrColor | pvr.txrFmt, pvr.texWidth, pvr.texHeight, pvr.txrAddr);  

}
Exemplo n.º 16
0
int
main (int argc, char* argv[])
{
  int cable_type, quit = 0;
  float rot1 = 0.0, rot2 = 0.0, rot3 = 0.0;
  kos_img_t front_txr, back_txr, tmp_img;
  pvr_ptr_t texaddr;
  GLuint texture[8];
  int blendfunc = 2;
  float eye_rot = 0;

  cable_type = vid_check_cable ();
  
  if (cable_type == CT_VGA)
    vid_init (DM_640x480_VGA, PM_RGB565);
  else
    vid_init (DM_640x480_PAL_IL, PM_RGB565);
  
  init_pvr ();

  glKosInit ();

  sphere = mkspheredata (3, &nstrip, &stripl);

  glGenTextures (8, &texture[0]);

#if 1
  kmg_to_img ("/rd/sky1.kmg", &front_txr);
  texaddr = pvr_mem_malloc (front_txr.byte_count);
  pvr_txr_load_kimg (&front_txr, texaddr, PVR_TXRFMT_VQ_ENABLE);
  kos_img_free (&front_txr, 0);
  
  glBindTexture (GL_TEXTURE_2D, texture[0]);
  glKosTex2D (GL_RGB565_TWID | GL_VQ_ENABLE, front_txr.w, front_txr.h,
	      texaddr);

  kmg_to_img ("/rd/sky2o.kmg", &back_txr);
  texaddr = pvr_mem_malloc (back_txr.byte_count);
  pvr_txr_load_kimg (&back_txr, texaddr, PVR_TXRFMT_VQ_ENABLE);
  kos_img_free (&back_txr, 0);

  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

  glBindTexture (GL_TEXTURE_2D, texture[1]);
  glKosTex2D (GL_ARGB4444_TWID | GL_VQ_ENABLE, back_txr.w, back_txr.h, texaddr);

  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  
  glBindTexture (GL_TEXTURE_2D, texture[2]);
  kmg_to_img ("/rd/sky3.kmg", &tmp_img);
  texaddr = pvr_mem_malloc (tmp_img.byte_count);
  pvr_txr_load_kimg (&tmp_img, texaddr, PVR_TXRFMT_VQ_ENABLE);
  glKosTex2D (GL_RGB565_TWID | GL_VQ_ENABLE, tmp_img.w, tmp_img.h, texaddr);

  glBindTexture (GL_TEXTURE_2D, texture[3]);
  kmg_to_img ("/rd/sky4.kmg", &tmp_img);
  texaddr = pvr_mem_malloc (tmp_img.byte_count);
  pvr_txr_load_kimg (&tmp_img, texaddr, PVR_TXRFMT_VQ_ENABLE);
  glKosTex2D (GL_RGB565_TWID | GL_VQ_ENABLE, tmp_img.w, tmp_img.h, texaddr);

  glBindTexture (GL_TEXTURE_2D, texture[4]);
  kmg_to_img ("/rd/sky5.kmg", &tmp_img);
  texaddr = pvr_mem_malloc (tmp_img.byte_count);
  pvr_txr_load_kimg (&tmp_img, texaddr, PVR_TXRFMT_VQ_ENABLE);
  glKosTex2D (GL_RGB565_TWID | GL_VQ_ENABLE, tmp_img.w, tmp_img.h, texaddr);

  glBindTexture (GL_TEXTURE_2D, texture[5]);
  kmg_to_img ("/rd/sky6.kmg", &tmp_img);
  texaddr = pvr_mem_malloc (tmp_img.byte_count);
  pvr_txr_load_kimg (&tmp_img, texaddr, PVR_TXRFMT_VQ_ENABLE);
  glKosTex2D (GL_RGB565_TWID | GL_VQ_ENABLE, tmp_img.w, tmp_img.h, texaddr);

  glBindTexture (GL_TEXTURE_2D, texture[6]);
  kmg_to_img ("/rd/sky7.kmg", &tmp_img);
  texaddr = pvr_mem_malloc (tmp_img.byte_count);
  pvr_txr_load_kimg (&tmp_img, texaddr, PVR_TXRFMT_VQ_ENABLE);
  glKosTex2D (GL_RGB565_TWID | GL_VQ_ENABLE, tmp_img.w, tmp_img.h, texaddr);

  glBindTexture (GL_TEXTURE_2D, texture[7]);
  kmg_to_img ("/rd/sky8.kmg", &tmp_img);
  texaddr = pvr_mem_malloc (tmp_img.byte_count);
  pvr_txr_load_kimg (&tmp_img, texaddr, PVR_TXRFMT_VQ_ENABLE);
  glKosTex2D (GL_RGB565_TWID | GL_VQ_ENABLE, tmp_img.w, tmp_img.h, texaddr);

#else  
  png_to_img ("/rd/sky1.png", PNG_MASK_ALPHA, &front_txr);
  texaddr = pvr_mem_malloc (front_txr.w * front_txr.h * 2);
  pvr_txr_load_kimg (&front_txr, texaddr, PVR_TXRLOAD_INVERT_Y);
  kos_img_free (&front_txr, 0);
  
  glBindTexture (GL_TEXTURE_2D, texture[0]);
  glKosTex2D (GL_ARGB1555_TWID, front_txr.w, front_txr.h, texaddr);

  png_to_img ("/rd/sky2o.png", PNG_MASK_ALPHA, &back_txr);
  texaddr = pvr_mem_malloc (back_txr.w * back_txr.h * 2);
  pvr_txr_load_kimg (&back_txr, texaddr, PVR_TXRLOAD_INVERT_Y);
  kos_img_free (&back_txr, 0);

  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

  glBindTexture (GL_TEXTURE_2D, texture[1]);
  glKosTex2D (GL_ARGB1555_TWID, back_txr.w, back_txr.h, texaddr);

  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  
  glBindTexture (GL_TEXTURE_2D, texture[2]);
  texaddr = pvr_mem_malloc (TEXSIZE * TEXSIZE * 2);
  png_to_texture ("/rd/sky3.png", texaddr, PNG_NO_ALPHA);
  glKosTex2D (GL_RGB565_TWID, TEXSIZE, TEXSIZE, texaddr);

  glBindTexture (GL_TEXTURE_2D, texture[3]);
  texaddr = pvr_mem_malloc (TEXSIZE * TEXSIZE * 2);
  png_to_texture ("/rd/sky4.png", texaddr, PNG_NO_ALPHA);
  glKosTex2D (GL_RGB565_TWID, TEXSIZE, TEXSIZE, texaddr);

  glBindTexture (GL_TEXTURE_2D, texture[4]);
  texaddr = pvr_mem_malloc (TEXSIZE * TEXSIZE * 2);
  png_to_texture ("/rd/sky5.png", texaddr, PNG_NO_ALPHA);
  glKosTex2D (GL_RGB565_TWID, TEXSIZE, TEXSIZE, texaddr);

  glBindTexture (GL_TEXTURE_2D, texture[5]);
  texaddr = pvr_mem_malloc (TEXSIZE * TEXSIZE * 2);
  png_to_texture ("/rd/sky6.png", texaddr, PNG_NO_ALPHA);
  glKosTex2D (GL_RGB565_TWID, TEXSIZE, TEXSIZE, texaddr);

  glBindTexture (GL_TEXTURE_2D, texture[6]);
  texaddr = pvr_mem_malloc (TEXSIZE * TEXSIZE * 2);
  png_to_texture ("/rd/sky7.png", texaddr, PNG_NO_ALPHA);
  glKosTex2D (GL_RGB565_TWID, TEXSIZE, TEXSIZE, texaddr);

  glBindTexture (GL_TEXTURE_2D, texture[7]);
  texaddr = pvr_mem_malloc (TEXSIZE * TEXSIZE * 2);
  png_to_texture ("/rd/sky8.png", texaddr, PNG_NO_ALPHA);
  glKosTex2D (GL_RGB565_TWID, TEXSIZE, TEXSIZE, texaddr);
#endif
  
  glEnable (GL_DEPTH_TEST);
  glEnable (GL_CULL_FACE);
  glEnable (GL_TEXTURE_2D);
  glShadeModel (GL_SMOOTH);
  glClearDepth (1.0f);
  glDepthFunc (GL_LEQUAL);
  
  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  gluPerspective (60.0,			/* Field of view in degrees.  */
		  640.0 / 480.0,	/* Aspect ratio.  */
		  1.0,			/* Z near.  */
		  50.0);		/* Z far.  */

  glMatrixMode (GL_MODELVIEW);
  
  while (!quit)
    {
      MAPLE_FOREACH_BEGIN (MAPLE_FUNC_CONTROLLER, cont_state_t, st)
        {
	  if (st->buttons & CONT_START)
	    quit = 1;
	  
	  eye_pos[0] = st->joyx / 25.0;
	  eye_pos[1] = st->joyy / 25.0;
	  eye_pos[2] = 5 * sin (eye_rot);
	  
	  if (st->buttons & CONT_A)
	    blendfunc = 0;
	  else if (st->buttons & CONT_B)
	    blendfunc = 1;
	  else if (st->buttons & CONT_X)
	    blendfunc = 2;
	}
      MAPLE_FOREACH_END ()
      
      draw_vectors = 10;
      
      glLoadIdentity ();
      gluLookAt (eye_pos[0], eye_pos[1], eye_pos[2],	/* Eye position.  */
		 0.0,   0.0,   0.0,			/* Centre.  */
		 0.0,   1.0,   0.0);			/* Up.  */

      glGetFloatv (GL_MODELVIEW_MATRIX, &camera[0][0]);

      invcamera[0][0] = camera[0][0];
      invcamera[0][1] = camera[1][0];
      invcamera[0][2] = camera[2][0];
      invcamera[0][3] = 0.0;

      invcamera[1][0] = camera[0][1];
      invcamera[1][1] = camera[1][1];
      invcamera[1][2] = camera[2][1];
      invcamera[1][3] = 0.0;

      invcamera[2][0] = camera[0][2];
      invcamera[2][1] = camera[1][2];
      invcamera[2][2] = camera[2][2];
      invcamera[2][3] = 0.0;

      invcamera[3][0] = 0.0;
      invcamera[3][1] = 0.0;
      invcamera[3][2] = 0.0;
      invcamera[3][3] = 1.0;

      /*dbgio_printf ("inverted camera orthogonality: %f %f %f\n",
		    (double) vec_dot (invcamera[0], invcamera[1]),
		    (double) vec_dot (invcamera[1], invcamera[2]),
		    (double) vec_dot (invcamera[2], invcamera[0]));*/

      /*dbgio_printf ("Inverted camera matrix:\n");
      for (i = 0; i < 4; i++)
	{
	  dbgio_printf ("[ %f %f %f %f ]\n",
                	(double) invcamera[0][i], (double) invcamera[1][i],
	        	(double) invcamera[2][i], (double) invcamera[3][i]);
	}*/

      glKosBeginFrame ();

      /*glPushMatrix ();
      glTranslatef (-camera[3][0], -camera[3][1], -camera[3][2]);*/
      render_cube (&texture[2]);
      /*glPopMatrix ();*/
      
      glPushMatrix ();
      
      #if 1
      glRotatef (rot1, 0.0, 1.0, 0.0);
      rot1 += 0.7;
      glRotatef (rot2, 1.0, 0.0, 0.0);
      rot2 += 0.29;
      #else
      glRotatef (rot1, 0.0, 0.0, 1.0);
      rot1 += 0.7;
      #endif
      
      if (rot1 >= 360)
        rot1 -= 360;

      if (rot2 >= 360)
        rot2 -= 360;
      
      eye_rot += 0.05;
      if (eye_rot >= 2 * M_PI)
        eye_rot -= 2 * M_PI;
      
      blob_phase += 0.05;
      if (blob_phase >= 24 * M_PI)
        blob_phase -= 24 * M_PI;
      
      /* Render front.  */
      glBindTexture (GL_TEXTURE_2D, texture[0]);
      glTexEnvi (GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE);

      #if 1
      render_blob (sphere, nstrip, stripl, 1);
      #else
      render_torus (1.0, 0.6, 1);
      #endif
            
      glPushMatrix ();
      glRotatef (rot3, 1.0, 0.0, 0.0);
      glTranslatef (2.9, 0.0, 0.0);
      render_torus (0.8, 0.5, 1);
      glPopMatrix ();
      glPushMatrix ();
      glRotatef (-rot3, 1.0, 0.0, 0.0);
      glTranslatef (-2.9, 0.0, 0.0);
      render_torus (0.8, 0.5, 1);
      glPopMatrix ();
      
      glKosFinishList ();

      /* Render back.  */
      glBindTexture (GL_TEXTURE_2D, texture[1]);
      glTexEnvi (GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE);
      if (blendfunc == 0)
        glBlendFunc (GL_SRC_ALPHA, GL_ZERO);
      else if (blendfunc == 1)
        glBlendFunc (GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);
      else
        glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
      #if 1
      //glDisable (GL_TEXTURE_2D);
      render_blob (sphere, nstrip, stripl, 0);
      //glEnable (GL_TEXTURE_2D);
      #else
      render_torus (1.0, 0.6, 0);
      #endif

      glPushMatrix ();
      glRotatef (rot3, 1.0, 0.0, 0.0);
      glTranslatef (2.9, 0.0, 0.0);
      render_torus (0.8, 0.5, 0);
      glPopMatrix ();
      glPushMatrix ();
      glRotatef (-rot3, 1.0, 0.0, 0.0);
      glTranslatef (-2.9, 0.0, 0.0);
      render_torus (0.8, 0.5, 0);
      glPopMatrix ();

      rot3 += 1;
      if (rot3 >= 360)
        rot3 -= 360;

      glPopMatrix ();
      
      glKosFinishFrame ();
    }
  
  glKosShutdown ();
  
  pvr_shutdown ();
  
  vid_shutdown ();
  /* Make dc-load look nicer.  */
  vid_init (DM_640x480_PAL_IL, PM_RGB565);

  return 0;
}
Exemplo n.º 17
0
int
main (int argc, char *argv[])
{
  int cable_type;
  int quit = 0;
  float rot1 = 0.0, rot2 = 0.0, rot3 = 0.0;
  kos_img_t cubetxr;
  GLuint texture[2];
  pvr_ptr_t texaddr;
  pvr_ptr_t bumpmap;
  /* Consider this as the vector from the object to the light, for now.  */
  GLfloat light_position[3] = { 0.7, 0.7, 2.0 };
  
  vec_normalize (&light_position[0], &light_position[0]);
  
  cable_type = vid_check_cable ();
  
  printf ("KOS says M_PI is: %f\n", M_PI);
  
  if (cable_type == CT_VGA)
    vid_init (DM_640x480_VGA, PM_RGB565);
  else
    vid_init (DM_640x480_PAL_IL, PM_RGB565);
  
  init_pvr ();
  
  auto_orient ();
  
  png_to_img ("/rd/cube.png", PNG_NO_ALPHA, &cubetxr);
  
  texaddr = pvr_mem_malloc (cubetxr.w * cubetxr.h * 2);
  pvr_txr_load_kimg (&cubetxr, texaddr, PVR_TXRLOAD_INVERT_Y);
  kos_img_free (&cubetxr, 0);
  
  bumpmap = load_bumpmap ("/rd/bump.raw");
  
  vid_border_color (0, 0, 0);
  pvr_set_bg_color (0.0, 0.0, 0.0);
    
  glKosInit ();
  
  glEnable (GL_DEPTH_TEST);
  glEnable (GL_CULL_FACE);
  glEnable (GL_TEXTURE_2D);
  glShadeModel (GL_SMOOTH);
  glClearDepth (1.0f);
  glDepthFunc (GL_LEQUAL);

  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  gluPerspective (45.0,			/* Field of view in degrees.  */
		  640.0 / 480.0,	/* Aspect ratio.  */
		  1.0,			/* Z near.  */
		  50.0);		/* Z far.  */

  glMatrixMode (GL_MODELVIEW);
  glLoadIdentity ();
  gluLookAt (0.0,   0.0,  -4.5,		/* Eye position.  */
	     0.0,   0.0,   0.0,		/* Centre.  */
	     0.0,   1.0,   0.0);	/* Up.  */

  glGenTextures (2, &texture[0]);

  /* Ordinary texture.  */
  glBindTexture (GL_TEXTURE_2D, texture[0]);
  glKosTex2D (GL_RGB565_TWID, cubetxr.w, cubetxr.h, texaddr);

  /* Bump texture.  */
  glBindTexture (GL_TEXTURE_2D, texture[1]);

  /*pvr_poly_cxt_txr (cxt, PVR_LIST_OP_POLY,
                    PVR_TXRFMT_BUMP | PVR_TXRFMT_TWIDDLED,
                    128, 128, bumpmap, PVR_FILTER_BILINEAR);*/
  glKosTex2D (GL_BUMP_TWID, 128, 128, bumpmap);

  glTexEnvi (GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE);

  /* Break the nice abstraction.  Tweak for bump mapping.  */
  /*cxt = (pvr_poly_cxt_t *) texture[1];
  cxt->gen.specular = PVR_SPECULAR_ENABLE;*/
  /*pvr_poly_compile (&bumphdr, cxt);*/
  
  printf ("objmatrix at %p\n", objmatrix);
  
  while (!quit)
    {
      int faces;
      GLfloat transformed_normals
        [sizeof (face_normals) / (3 * sizeof (GLfloat))][3];
      GLfloat transformed_orient
        [sizeof (face_orient) / (3 * sizeof (GLfloat))][3];
      
      MAPLE_FOREACH_BEGIN (MAPLE_FUNC_CONTROLLER, cont_state_t, st)
        if (st->buttons & CONT_START)
	  quit = 1;
      MAPLE_FOREACH_END ()
      
      glKosBeginFrame ();
      
      glPushMatrix ();
      
      glRotatef (rot1, 1.0, 0.0, 0.0);
      glRotatef (rot2, 0.0, 1.0, 0.0);
      glRotatef (rot3, 0.0, 0.0, 1.0);
      
      rot1 += 0.1;
      rot2 += 0.2;
      rot3 += 0.3;

      /* Get the object's transformation matrix.  */
      glGetFloatv (GL_MODELVIEW_MATRIX, &objmatrix[0][0]);
      /* We care only about rotation for now.  */
      objmatrix[0][3] = objmatrix[1][3] = objmatrix[2][3] = 0.0;
      objmatrix[3][0] = objmatrix[3][1] = objmatrix[3][2] = 0.0;
      objmatrix[3][3] = 1.0;

      /*printf ("Got matrix:\n");
      printf ("[ %f %f %f %f ]\n", objmatrix[0][0], objmatrix[0][1],
				   objmatrix[0][2], objmatrix[0][3]);
      printf ("[ %f %f %f %f ]\n", objmatrix[1][0], objmatrix[1][1],
				   objmatrix[1][2], objmatrix[1][3]);
      printf ("[ %f %f %f %f ]\n", objmatrix[2][0], objmatrix[2][1],
				   objmatrix[2][2], objmatrix[2][3]);
      printf ("[ %f %f %f %f ]\n", objmatrix[3][0], objmatrix[3][1],
				   objmatrix[3][2], objmatrix[3][3]);*/

      /* Do these all in one go.  */
      mat_load ((matrix_t *) &objmatrix[0][0]);

      /* Note: mat_transform is only for 3D->2D (perspective)
         transformations!  This won't be quite as quick, most likely.  */
      for (faces = 0; faces < 6; faces++)
        {
	  GLfloat x = face_normals[faces][0];
	  GLfloat y = face_normals[faces][1];
	  GLfloat z = face_normals[faces][2];
	  GLfloat w = 1.0;
	  mat_trans_nodiv (x, y, z, w);
	  transformed_normals[faces][0] = x;
	  transformed_normals[faces][1] = y;
	  transformed_normals[faces][2] = z;

	  x = face_orient[faces][0];
	  y = face_orient[faces][1];
	  z = face_orient[faces][2];
	  w = 1.0;
	  mat_trans_nodiv (x, y, z, w);
	  transformed_orient[faces][0] = x;
	  transformed_orient[faces][1] = y;
	  transformed_orient[faces][2] = z;
	}
      glKosMatrixDirty ();

      glDisable (GL_KOS_OFFSET_COLOR);
      glBindTexture (GL_TEXTURE_2D, texture[0]);
      glDisable (GL_TEXTURE_2D);
      //glBlendFunc (GL_DST_COLOR, GL_ZERO);
      glBlendFunc (GL_ONE, GL_ZERO);

      for (faces = 0; faces < 6; faces++)
        {
	  int strip;
	  	  
	  glBegin (GL_TRIANGLE_STRIP);

	  glColor4ub (colour[faces][0], colour[faces][1], colour[faces][2],
		      colour[faces][3]);

	  for (strip = 0; strip < 4; strip++)
	    {
	      glTexCoord2fv (texcoords[strip]);
	      glVertex3fv (points[tristrips[faces][strip]]);
	    }
	  
	  glEnd ();
	}

      /* Finish opaque list, start transparent list.  */
      glKosFinishList ();

      glBindTexture (GL_TEXTURE_2D, texture[1]);
      glEnable (GL_TEXTURE_2D);
      glEnable (GL_KOS_OFFSET_COLOR);
      glBlendFunc (GL_ZERO, GL_SRC_ALPHA);
      
      for (faces = 0; faces < 6; faces++)
        {
	  int strip, over_pi, over_2pi;
	  GLfloat s_dot_n, f[3], d[3], t, q;
	  GLfloat d_cross_r[3], dxr_len, d_len;

	  s_dot_n = vec_dot (&transformed_normals[faces][0],
			     &light_position[0]);
	  /* Elevation (T) angle:
	     s.n = |s| |n| cos T
	     T = acos (s.n / (|s| * |n|))
	     |s| and |n| are both 1.  */
	  t = M_PI / 2 - acosf (s_dot_n);
	  
	  if (t < 0)
	    t = 0;
	  
	  /* Rotation (Q) angle:
	     d x r = (|d| |r| sin Q) n
	     |d x r| / (|d| |r|) = sin Q.  */
	  vec_scale (&f[0], &transformed_normals[faces][0], s_dot_n);
	  vec_sub (&d[0], &light_position[0], &f[0]);
	  vec_cross (&d_cross_r[0], &d[0], &transformed_orient[faces][0]);
	  dxr_len = vec_length (&d_cross_r[0]);
	  d_len = vec_length (&d[0]);
	  q = asinf (dxr_len / d_len);
	  
	  over_pi = vec_dot (&d[0], &transformed_orient[faces][0]) < 0;
	  if (over_pi)
	    q = M_PI - q;

	  over_2pi
	    = vec_dot (&d_cross_r[0], &transformed_normals[faces][0]) < 0;
	  if (over_2pi)
	    q = 2 * M_PI - q;

	  /*printf ("length of n: %f\n",
		 length (&transformed_normals[faces][0]));
	  printf ("%d: [ %f %f %f ]\n", faces, transformed_normals[faces][0],
	    transformed_normals[faces][1], transformed_normals[faces][2]);

	  printf ("length of r: %f\n", length (&transformed_orient[faces][0]));
	  printf ("%d: [ %f %f %f ]\n", faces, transformed_orient[faces][0],
	    transformed_orient[faces][1], transformed_orient[faces][2]);*/

	  glBegin (GL_TRIANGLE_STRIP);

	  set_bump_direction (t, 2 * M_PI - q, 1.0);
	  glColor4ub (255, 255, 255, 255);

	  for (strip = 0; strip < 4; strip++)
	    {
	      glTexCoord2fv (texcoords[strip]);
	      glVertex3fv (points[tristrips[faces][strip]]);
	    }

	  glEnd ();
	}

      /* Finish opaque polygon list, start translucent polygon list.  */
     /* glKosFinishList ();*/

      glPopMatrix ();
                  
      glKosFinishFrame ();
    }

  glKosShutdown ();
  
  pvr_shutdown ();
  
  vid_shutdown ();
  
  return 0;
}