資源簡介
實現光線跟蹤算法,構造一個虛擬場景。

代碼片段和文件信息
//?-----------------------------------------------------------
//?raytracer.cpp
//?2004?-?Jacco?Bikker?-?jacco@bik5.com?-?www.bik5.com?-???<><
//?-----------------------------------------------------------
#include?“raytracer.h“
#include?“scene.h“
#include?“common.h“
#include?“windows.h“
#include?“winbase.h“
namespace?Raytracer?{
Ray::Ray(?vector3&?a_Origin?vector3&?a_Dir?)?:?
m_Origin(?a_Origin?)?
m_Direction(?a_Dir?)
{
}
Engine::Engine()
{
m_Scene?=?new?Scene();
}
Engine::~Engine()
{
delete?m_Scene;
}
//?-----------------------------------------------------------
//?Engine::SetTarget
//?Sets?the?render?target?canvas
//?-----------------------------------------------------------
void?Engine::SetTarget(?Pixel*?a_Dest?int?a_Width?int?a_Height?)
{
//?set?pixel?buffer?address?&?size
m_Dest?=?a_Dest;
m_Width?=?a_Width;
m_Height?=?a_Height;
}
//?-----------------------------------------------------------
//?Engine::Raytrace
//?Naive?ray?tracing:?Intersects?the?ray?with?every?primitive
//?in?the?scene?to?determine?the?closest?intersection
//?-----------------------------------------------------------
Primitive*?Engine::Raytrace(?Ray&?a_Ray?Color&?a_Acc?int?a_Depth?float?a_RIndex?float&?a_Dist?)
{
if?(a_Depth?>?TRACEDEPTH)?return?0;
//?trace?primary?ray
a_Dist?=?1000000.0f;
vector3?pi;
Primitive*?prim?=?0;
int?result;
//?find?the?nearest?intersection
for?(?int?s?=?0;?s?GetNrPrimitives();?s++?)
{
Primitive*?pr?=?m_Scene->GetPrimitive(?s?);
int?res;
if?(res?=?pr->Intersect(?a_Ray?a_Dist?))?
{
prim?=?pr;
result?=?res;?//?0?=?miss?1?=?hit?-1?=?hit?from?inside?primitive
}
}
//?no?hit?terminate?ray
if?(!prim)?return?0;
//?handle?intersection
if?(prim->IsLight())
{
//?we?hit?a?light?stop?tracing
a_Acc?=?Color(?1?1?1?);
}
else
{
//?determine?color?at?point?of?intersection
pi?=?a_Ray.GetOrigin()?+?a_Ray.GetDirection()?*?a_Dist;
//?trace?lights
for?(?int?l?=?0;?l?GetNrPrimitives();?l++?)
{
Primitive*?p?=?m_Scene->GetPrimitive(?l?);
if?(p->IsLight())?
{
Primitive*?light?=?p;
//?calculate?diffuse?shading
vector3?L?=?((Sphere*)light)->GetCentre()?-?pi;
NORMALIZE(?L?);
vector3?N?=?prim->GetNormal(?pi?);
if?(prim->GetMaterial()->GetDiffuse()?>?0)
{
float?dot?=?DOT(?N?L?);
if?(dot?>?0)
{
float?diff?=?dot?*?prim->GetMaterial()->GetDiffuse();
//?add?diffuse?component?to?ray?color
a_Acc?+=?diff?*?prim->GetMaterial()->GetColor()?*?light->GetMaterial()->GetColor();
}
}
}
}
}
//?return?pointer?to?primitive?hit?by?primary?ray
return?prim;
}
//?-----------------------------------------------------------
//?Engine::InitRender
//?Initializes?the?renderer?by?resetting?the?line?/?tile
//?counters?and?precalculating?some?values
//?----------------------------------------------------------
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????3294??2004-09-16?08:47??common.h
?????文件????????4911??2004-09-21?11:03??raytracer.cpp
?????文件????????4185??2004-09-16?08:58??raytracer.dsp
?????文件?????????541??2004-07-08?16:04??raytracer.dsw
?????文件????????1627??2004-09-16?10:58??raytracer.h
?????文件????????3756??2004-09-21?11:04??scene.cpp
?????文件????????3646??2004-09-16?11:10??scene.h
?????文件????????4921??2004-08-30?21:38??surface.cpp
?????文件????????1064??2004-08-30?21:39??surface.h
?????文件????????3974??2004-09-16?08:47??testapp.cpp
- 上一篇:徑向基函數插值的代碼
- 下一篇:veriog語言實現UART
評論
共有 條評論