Path: chuka.playstation.co.uk!news From: Craig Graham Newsgroups: scee.yaroze.mydemos Subject: Re: Environment mapping Date: Wed, 03 Feb 1999 09:54:53 +0000 Organization: PlayStation Net Yaroze (SCEE) Lines: 159 Message-ID: <36B81CED.E06DF09B@hinge.mistral.co.uk> References: <36B254E3.6637@mdx.ac.uk> <36B5894C.4CA9@bristol.ac.uk> <36B741A2.463C@mdx.ac.uk> <36B81CE3.BC8@bristol.ac.uk> NNTP-Posting-Host: d3-s9-165-telehouse.mistral.co.uk Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.5 [en] (Win95; I) X-Accept-Language: en Here's how I did it.......dunno if this is faster than the way Tom is doing it.... It only works for one primitive type and requires that the normal vectors were pre-calculated (as they should be anyway for the gourard shading to work). Craig. #include #include #include #include #include #include #include #include #include #include "tmd.h" #include "envmap.h" #define FAST_ENVIRONMENT_MODE 1 //#define ENV_TX_SHIFT 7 //#define ENV_TY_SHIFT 8 // Setup for a 64*64 env map in fast mode #define ENV_TX_SHIFT 7 #define ENV_TY_SHIFT 7 /* * Environment Map An Object * ------------------------- * Parameters: * object = object to map. * i = image description of the environment texture. must be aligned to top left of a texture * page, and must not go beyond the texture page boundary. * axis_flag = environment mapping mode: * EM_AXIS_X : only the X texture axis is mapped * EM_AXIS_Y : only the Y texture axis is mapped * EM_AXIS_ALL : both X & Y texture axis are mapped * NOTES: * 1) Only TMD_GTP3N primitives work at the moment (Gourard,Texture Poly3's) * 2) If FAST_ENVIRONMENT_MODE is set then the texture is assumed to be a single * texture page in width, and shifting is used instead of multiply/divide to * improve performance. * 3) If possible, this routine puts it's temporary buffers in the scratch pad. * this only works if you've got less than 512 normals in the model.... * 4) Inline GTE macro's are used to speed up the calculation of the rotated normals... * ****No they aren't, I've replaced it with ApplyMatrixSV() so you can use it on the Yaroze**** */ void EnvMap(GsDOBJ2 *object,GsIMAGE *i, int axis_flag) { TMD_OBJECT *ob=(TMD_OBJECT*)object->tmd; TMD_PACKET_HEADER *h; VECTOR t; int p; register TMD_GTP3N *prim; register short temp; register unsigned char *tu_buf,*tv_buf; long flg; h=(TMD_PACKET_HEADER*)ob->primitive_top; // If we can, we put the normals into the scratchpad for fast access if(ob->n_normal>512) { // Allocate a temporary buffer for the u's, v's and normals tu_buf=(unsigned char*)malloc(ob->n_normal); tv_buf=(unsigned char*)malloc(ob->n_normal); }else{ // Get scratch pad address tu_buf=(unsigned char*)getScratchAddr(0); tv_buf=(unsigned char*)getScratchAddr(512/4); } gte_SetRotMatrix(&object->coord2->workm); // Calculate texture UV's for all vertices, and build a lookup for(p=0; pn_normal; p++) { // Rotate the normal ApplyMatrixSV(&object->coord2->workm,(SVECTOR*)&ob->normal_top[p],&t); // Do it Yaroze style #if FAST_ENVIRONMENT_MODE if(axis_flag&EM_AXIS_X) { temp=t.vx; temp>>=ENV_TX_SHIFT; temp+=32; tu_buf[p]=temp; } if(axis_flag&EM_AXIS_Y) { temp=t.vy; temp>>=ENV_TY_SHIFT; temp+=32; tv_buf[p]=temp; } #else if(axis_flag&EM_AXIS_X) { temp=t.vx; temp*=i->pw/2; temp/=ONE; temp+=(i->pw/2)-1; tu_buf[p]=temp; } if(axis_flag&EM_AXIS_Y) { temp=t.vy; temp*=i->ph/2; temp/=ONE; temp+=(i->ph/2)-1; tv_buf[p]=temp; } #endif } // Now go round all poly's and fill in the UV's from the lookup we've already built for(p=0; pn_primitive; p++) { if((h->mode&~2)==0x34) { prim=(TMD_GTP3N*)h; if(axis_flag&EM_AXIS_X) { prim->u0=tu_buf[prim->n0]; prim->u1=tu_buf[prim->n1]; prim->u2=tu_buf[prim->n2]; } if(axis_flag&EM_AXIS_Y) { prim->v0=tv_buf[prim->n0]; prim->v1=tv_buf[prim->n1]; prim->v2=tv_buf[prim->n2]; } h=(TMD_PACKET_HEADER*)(prim+1); } } if(ob->n_normal>512) { free(tu_buf); free(tv_buf); } }