• 光线与球的求交代码


    //-------------------------------------------------------------------------
    eiBool eiSphere::intersect(eiRay *ray, eiObject *po, eiFloat & oldt,
                                eiPrimitive
    * & ret_pri, eiFloat *cust_data,
                                HitParam 
    *hparam)
    {
        
    if( oldt == 0.0f || ray_box( ray->src, hparam->hitPoint )
        
    {
            
    //compute intersecting point

            eiVector    oc 
    = add( ray->src, - center );
            eiFloat        oc_len 
    = dist( oc );
                
            
    if( oc_len > radius || po->material->doubleSide )
            
    {
                eiFloat    a 
    = dot( ray->dir, ray->dir );
                eiFloat    b 
    = 2.0f * dot( oc, ray->dir);
                eiFloat    c 
    = dot( oc, oc ) - RR;
                eiFloat    dt 
    = b * b - 4.0f * a * c;

                eiFloat    t;

                
    if(a == 0.0f || dt < 0.0f)
                    
    return false;
                
    else if(dt == 0.0f)
                
    {
                    t 
    = - b / a * 0.5f;

                    
    if(t < 0.0f)
                        
    return false;
                }

                
    else
                
    {
                    a 
    = 0.5f / a;
                    eiFloat t1 
    = (- b + sqrtf( dt )) * a;
                    eiFloat t2 
    = (- b - sqrtf( dt )) * a;

                    
    if(t1 > 0.0f && t2 < 0.0f)
                        t 
    = t1;
                    
    else if(t1 < 0.0f && t2 > 0.0f)
                        t 
    = t2;
                    
    else if(t1 > 0.0f && t2 > 0.0f)
                        t 
    = (t1 < t2) ? t1 : t2;
                    
    else
                        
    return false;
                }


                eiVector tmpIntersection 
    = add(ray->src, mulvf(ray->dir,t));

                
    //affect shadow factor

                
    if(ray->type == RAY_SHADOW)
                    hparam
    ->shadow_factor *= 1.0f - po->material->opacity;

                
    //find nearest hit point

                
    if(oldt == 0.0f || t < oldt)
                
    {
                    hparam
    ->hitPoint = tmpIntersection;
                    hparam
    ->hitObj = po;
                    ret_pri 
    = this;
                    oldt 
    = t;

                    
    //fill custom data

                    
    return true;
                }

            }

        }


        
    return false;
    }
  • 相关阅读:
    Iframe和Frame中实现cookie跨域的方法(转载)
    android中拷贝assets下的资源文件到SD卡中(可以超过1M)
    OpenSL ES 查询设备支持的SL Profiles
    NDK开发中的一个HTTP下载实例附带下载进度
    android中配置文件property的用途以及使用<转>
    Eclipse 工程使用相对路径导入Jar包设置
    Android 解压zip文件(支持中文)
    c++实现一个比较两个string类型的版本号的小demo
    linux c++下载http文件并显示进度<转>
    Linux下类似windows下_beginthread和_endthread 的多线程开发
  • 原文地址:https://www.cnblogs.com/len3d/p/202305.html
Copyright © 2020-2023  润新知