forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help with clipping code - also question about rounding fp!

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
subbie



Joined: 05 May 2005
Posts: 122

PostPosted: Wed Mar 01, 2006 12:58 am    Post subject: Help with clipping code - also question about rounding fp! Reply with quote

Hey, a while back I made a 3d engine based on the quake 3 file formats. A while back I added in code to clip my surfaces but I have a few problems with it. For some reason the cordinates & UV clip fine but the colors dont clip properly. like if far away I could have a surface with a shadow starting from the bottom left going to the top right. Yet when I get closer and closer (when my clipping kicks in) the colors shift to the oposite side (going from |/| to |\|).

Here is my code. Any idea's?

As you can guess from the code the cordinates & UV's are floating point but the color is 32bit 8888 RGBA.

Code:
int ClipLineToPlane( Quake3BspPlane* pPlane, BspClipVertex* vStart, BspClipVertex* vEnd )
{
   //* Clip Return Status
   //* 00 = Point Not Visable (save neither point)
   //* 01 = Save Start Point
   //* 10 = Save End Point
   //* 11 = Save Both Points

   float fStartDistance = IsPointVisableFloat( pPlane, vStart->x, vStart->y, vStart->z );
   float fEndDistance = IsPointVisableFloat( pPlane, vEnd->x, vEnd->y, vEnd->z );

   float fInverDistance = 1.0f;
   float fFraction = 0.0f;

   float uColor1[4];
   float uColor2[4];
   float uColor3[4];

   BspClipVertex vNewPoint;

   //* Both Points Visable
   if( ( fStartDistance >= 0 ) && ( fEndDistance >= 0 ) )
      return 1;

   //* Both Points Not Visable
   if( ( fStartDistance < 0 ) && ( fEndDistance < 0 ) )
      return 0;

   //* Calculate Inverse
   if( ( fStartDistance - fEndDistance ) != 0 )
      fInverDistance = 1.0f / ( fStartDistance - fEndDistance );

   //* Get Fractional Value
   fFraction = ( fStartDistance ) * fInverDistance;

   //* Let it go outside the bounds a bit so we dont clip too early
   if( fStartDistance > fEndDistance )
      fFraction += EPSILON2;
   else
      fFraction -= EPSILON2;

   //* Verify it's valid
   if( fFraction < 0.0f )
      fFraction = 0.0f;
   else if( fFraction > 1.0f )
      fFraction = 1.0f;

   //* Generate Point of Intersection
   vNewPoint.x = vStart->x + ( fFraction * ( vEnd->x - vStart->x ) );
   vNewPoint.y = vStart->y + ( fFraction * ( vEnd->y - vStart->y ) );
   vNewPoint.z = vStart->z + ( fFraction * ( vEnd->z - vStart->z ) );

   //* Generate UVs
   vNewPoint.u = vStart->u + ( fFraction * ( vEnd->u - vStart->u ) );
   vNewPoint.v = vStart->v + ( fFraction * ( vEnd->v - vStart->v ) );

   //* Generate Color
   uColor2[0] = (float)vStart->color[0] / 255.0f;
   uColor2[1] = (float)vStart->color[1] / 255.0f;
   uColor2[2] = (float)vStart->color[2] / 255.0f;
   uColor2[3] = (float)vStart->color[3] / 255.0f;

   uColor3[0] = (float)vEnd->color[0] / 255.0f;
   uColor3[1] = (float)vEnd->color[1] / 255.0f;
   uColor3[2] = (float)vEnd->color[2] / 255.0f;
   uColor3[3] = (float)vEnd->color[3] / 255.0f;

   uColor1[0] = uColor2[0] + ( fFraction * (uColor3[0] - uColor2[0]) );
   uColor1[1] = uColor2[1] + ( fFraction * (uColor3[1] - uColor2[1]) );
   uColor1[2] = uColor2[2] + ( fFraction * (uColor3[2] - uColor2[2]) );
   uColor1[3] = uColor2[3] + ( fFraction * (uColor3[3] - uColor2[3]) );

   //* Store Point
   if( fStartDistance > fEndDistance )
   {
      vEnd->x = vNewPoint.x;
      vEnd->y = vNewPoint.y;
      vEnd->z = vNewPoint.z;
      vEnd->u = vNewPoint.u;
      vEnd->v = vNewPoint.v;

      //vEnd->color = uColor1[0] + ( uColor1[1] << 8) + ( uColor1[2] << 16) + ( uColor1[3] << 24);
      vEnd->color[0] = (unsigned char)( uColor1[0] * 255.0f );
      vEnd->color[1] = (unsigned char)( uColor1[1] * 255.0f );
      vEnd->color[2] = (unsigned char)( uColor1[2] * 255.0f );
      vEnd->color[3] = (unsigned char)( uColor1[3] * 255.0f );

      //* Line Clipped (Save both Points)
      return 3;
   }
   else
   {
      vStart->x = vNewPoint.x;
      vStart->y = vNewPoint.y;
      vStart->z = vNewPoint.z;
      vStart->u = vNewPoint.u;
      vStart->v = vNewPoint.v;

      //vStart->color = uColor1[0] + ( uColor1[1] << 8) + ( uColor1[2] << 16) + ( uColor1[3] << 24);
      vStart->color[0] = (unsigned char)( uColor1[0] * 255.0f );
      vStart->color[1] = (unsigned char)( uColor1[1] * 255.0f );
      vStart->color[2] = (unsigned char)( uColor1[2] * 255.0f );
      vStart->color[3] = (unsigned char)( uColor1[3] * 255.0f );
   }

   //* Line Clipped
   return 1;
}


--edit--

I have another question. I have also been working on a n64 emulator called monkey64. Some of the n64 floating point unit seems to control how it wants values rounded. Is there a way I can set and controll this on the psp?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group