光线与球的求交代码

None.gif // -------------------------------------------------------------------------
None.gif
eiBool eiSphere::intersect(eiRay  * ray, eiObject  * po, eiFloat  &  oldt,
None.gif                            eiPrimitive
* & ret_pri, eiFloat  * cust_data,
None.gif                            HitParam 
* hparam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if( oldt == 0.0f || ray_box( ray->src, hparam->hitPoint )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//compute intersecting pointdot.gif
InBlock.gif

InBlock.gif        eiVector    oc 
= add( ray->src, - center );
InBlock.gif        eiFloat        oc_len 
= dist( oc );
InBlock.gif            
InBlock.gif        
if( oc_len > radius || po->material->doubleSide )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            eiFloat    a 
= dot( ray->dir, ray->dir );
InBlock.gif            eiFloat    b 
= 2.0f * dot( oc, ray->dir);
InBlock.gif            eiFloat    c 
= dot( oc, oc ) - RR;
InBlock.gif            eiFloat    dt 
= b * b - 4.0f * a * c;
InBlock.gif
InBlock.gif            eiFloat    t;
InBlock.gif
InBlock.gif            
if(a == 0.0f || dt < 0.0f)
InBlock.gif                
return false;
InBlock.gif            
else if(dt == 0.0f)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                t 
= - b / a * 0.5f;
InBlock.gif
InBlock.gif                
if(t < 0.0f)
InBlock.gif                    
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                a 
= 0.5f / a;
InBlock.gif                eiFloat t1 
= (- b + sqrtf( dt )) * a;
InBlock.gif                eiFloat t2 
= (- b - sqrtf( dt )) * a;
InBlock.gif
InBlock.gif                
if(t1 > 0.0f && t2 < 0.0f)
InBlock.gif                    t 
= t1;
InBlock.gif                
else if(t1 < 0.0f && t2 > 0.0f)
InBlock.gif                    t 
= t2;
InBlock.gif                
else if(t1 > 0.0f && t2 > 0.0f)
InBlock.gif                    t 
= (t1 < t2) ? t1 : t2;
InBlock.gif                
else
InBlock.gif                    
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            eiVector tmpIntersection 
= add(ray->src, mulvf(ray->dir,t));
InBlock.gif
InBlock.gif            
//affect shadow factordot.gif
InBlock.gif

InBlock.gif            
if(ray->type == RAY_SHADOW)
InBlock.gif                hparam
->shadow_factor *= 1.0f - po->material->opacity;
InBlock.gif
InBlock.gif            
//find nearest hit pointdot.gif
InBlock.gif

InBlock.gif            
if(oldt == 0.0f || t < oldt)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                hparam
->hitPoint = tmpIntersection;
InBlock.gif                hparam
->hitObj = po;
InBlock.gif                ret_pri 
= this;
InBlock.gif                oldt 
= t;
InBlock.gif
InBlock.gif                
//fill custom datadot.gif
InBlock.gif

InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return false;
ExpandedBlockEnd.gif}
posted on 2005-07-28 22:16 Len3d 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/len3d/archive/2005/07/28/202305.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值