Path: chuka.playstation.co.uk!news From: Developer Support Newsgroups: scee.yaroze.programming.3d_graphics Subject: Re: Calculating Normals Date: Thu, 29 May 1997 14:36:03 +0100 Organization: Sony Computer Entertainment Europe Lines: 103 Message-ID: <338D8643.30D2@interactive.sony.com> References: <338D6EC6.7EC3@geocities.com> Reply-To: N/A-Use-Newsgroup NNTP-Posting-Host: 194.203.13.10 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 3.01 (Win95; I) Kev wrote: > > Hi, > > OK, I've finally got around to really playing with the Yaroze and I > like it. > > I designing my own 3D world system (wow! original!) and I'm starting > off with the basics... I'm having a problem calculating normals for > TMDVERTs for use in my own custom GPU primitives. > > This is the routine I'm using the calculate the normals for a triangle, > I'm a bit confused as it doesn't produce the correct results (when > compared to the normals produced by RSDTOOL for the same triangles): > > /* calculate normal for flat shaded triangle > IN: ptr to TriPoly structure > */ > void CalculateNormal(TriPoly *ptr) > { > // functions used > int GetSizeOfVector(TMDVERT *); > void ScaleVectorToUnit(TMDVERT *); > > // temp storage > TMDNORM normal; > > // convert vectors to unit vectors > ScaleVectorToUnit(&(ptr->vert[0])); > ScaleVectorToUnit(&(ptr->vert[1])); > ScaleVectorToUnit(&(ptr->vert[2])); > > // calc normal > normal.nx = (ptr->vert[1].vy - ptr->vert[0].vy) * (ptr->vert[0].vz - > ptr->vert[2].vz) - > (ptr->vert[1].vz - ptr->vert[0].vz) * (ptr->vert[0].vy - > ptr->vert[2].vy); > normal.ny = (ptr->vert[1].vz - ptr->vert[0].vz) * (ptr->vert[0].vx - > ptr->vert[2].vx) - > (ptr->vert[1].vx - ptr->vert[0].vx) * (ptr->vert[0].vz - > ptr->vert[2].vz); > normal.nz = (ptr->vert[1].vx - ptr->vert[0].vx) * (ptr->vert[0].vy - > ptr->vert[2].vy) - > (ptr->vert[1].vy - ptr->vert[0].vy) * (ptr->vert[0].vx - > ptr->vert[2].vx); > > ... > > } > > The normal produced by this routine is quite different from that the > correct normal. Am I using the right method??? I'm sure the > ScaleVectorToUnit function and GetSizeOfVector functions are OK as > they are from Sony code anyway. > > Can anyone help? > > Cheers, > > Kev. > -- > http://www.geocities.com/SiliconValley/Pines/7428 We're not suppose to be here to help with coding, only technical problems. What your doing is normalising the polygon vectors before calculating the cross product. This messes up everything, I think what you were trying to do is normalise the direction vectors that make up a side. Plus your calculating the clockwise vectors not the anticlockwise ones. Your code should look like this: void CalculateNormal(TriPoly *ptr) { // functions used int GetSizeOfVector(TMDVERT *); void ScaleVectorToUnit(TMDVERT *); // temp storage TMDNORM normal; TMDVERT v1,v2; // convert vectors to unit vectors v1.vx = ptr->vert[0].vx - ptr->vert[1].vx; v1.vy = ptr->vert[0].vy - ptr->vert[1].vy; v1.vz = ptr->vert[0].vz - ptr->vert[1].vz; v2.vx = ptr->vert[2].vx - ptr->vert[0].vx; v2.vy = ptr->vert[2].vy - ptr->vert[0].vy; v2.vz = ptr->vert[2].vz - ptr->vert[0].vz; ScaleVectorToUnit(&v1); ScaleVectorToUnit(&v2); // calc normal normal.nx = v1.vy * v2.vz - v1.vz * v2.vy; normal.ny = v1.vz * v2.vx - v1.vx * v2.vz; normal.nz = v1.vx * v2.vy - v1.vy * v2.vx; ... } Stuart