📄 sphere.cpp
字号:
// Sphere.cpp: implementation of the CSphere class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RayTrace.h"
#include "Sphere.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSphere::CSphere()
{
}
CSphere::CSphere(CVector center, double radius)
:
vecCenter ( center ),
dRadius ( radius )
{
}
CSphere::~CSphere()
{
}
double CSphere::Intersect()
{
//求交测试,场景原点O(0,0,0),入射光线HitRay方向向量为单位向量
CVector temp = vecCenter;
double dintersect;
double I_b,I_c,I_disc;
temp -= HitRay->StartPoint;
I_b = -2.0 * (temp*HitRay->Direction);
I_c = temp*temp - dRadius*dRadius;
I_disc = I_b*I_b - 4.0*I_c;
if( !( I_disc > 0.0 ) )
dintersect=0.0;
else
{
double I_sqrt,I_t1,I_t2;
I_sqrt = sqrt(I_disc);
I_t1 = ( -I_b - I_sqrt ) * 0.5;
I_t2 = ( -I_b + I_sqrt ) * 0.5;
if(( !( I_t1 > SMALL ) ) && ( !( I_t2 > SMALL ) ))
dintersect = 0.0;
else
{
if( I_t1 > I_t2 )
{
if( I_t2 < SMALL )
I_t2 = I_t1;
}
else
{
if( I_t1 > SMALL )
I_t2 = I_t1;
}
dintersect = I_t2;
}
}
if( dintersect != 0.0 )
{
IntersectPoint = HitRay->Direction;
IntersectPoint *= dintersect;
IntersectPoint += HitRay->StartPoint;
}
return dintersect;
}
CVector CSphere::GetNormalVec()
{
CVector N = IntersectPoint;
N -= vecCenter;
N.Normalize();
return N;
}
CVector CSphere::Mapping()
{
double theta,phi;
double x,y,z;
x = IntersectPoint.GetX() - vecCenter.GetX();
y = IntersectPoint.GetY() - vecCenter.GetY();
z = IntersectPoint.GetZ() - vecCenter.GetZ();
phi = acos( z / dRadius );
theta = acos( x / ( dRadius * sin( phi ) ) );
if( y < 0 )
theta = 2.0 * PI - theta;
double u,v;
u = theta / PI / 2.0;
v = 1.0 - phi / PI;
return Texture->GetColor(u,v);
}
void CSphere::Transform(CVector offset, CVector rotate)
{
vecCenter.Rotate( rotate );
vecCenter.Translate( offset );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -