Skip to content

Commit

Permalink
debug kdtree in ray tracing. bug in field of depth
Browse files Browse the repository at this point in the history
  • Loading branch information
evan69 committed Jun 13, 2016
1 parent 207d5e4 commit 5a8e7fd
Show file tree
Hide file tree
Showing 10 changed files with 474 additions and 53 deletions.
1 change: 1 addition & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
2016.6.9 ���������Σ�obj�ļ�������
2016.6.12 ������path tracing
2016.6.12 ����path tracing�µ�obj�Լ�kdtree����ray tracing��kdtree����bug
2016.6.13 ray tracing��kdtree��debug��ɣ���������bug
8 changes: 6 additions & 2 deletions all.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ inline double erand48(short unsigned int*){return (double)rand()/RAND_MAX;}
//#define SQRLENGTH(A) (A.x*A.x+A.y*A.y+A.z*A.z)
//#define SQRDISTANCE(A,B) ((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)+(A.z-B.z)*(A.z-B.z))

#define PATHTRACING
//#define PATHTRACING
#define PTSAMP 2
#define KD
//#define DEPTH_OF_FIELD
#define IMPORTANCE_SAMPLING
//#define SUPERSAMPLING
#define SAMPLES 128

#define EPS 0.0001f
#define TRACEDEPTH 6
Expand Down Expand Up @@ -82,8 +86,8 @@ typedef vector3 Color;

};

#include "camera.h"
#include "ray.h"
#include "camera.h"
#include "primitive.h"
#include "triangle.h"
#include "object.h"
Expand Down
57 changes: 56 additions & 1 deletion camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

namespace HYF
{

/*
class Camera
{
public:
Camera():m_Eye(0,0,-5),m_Target(0,0,0),m_Dist(5.0),m_Width(8.0),m_Height(6.0),m_Ratio(0.01){}
Camera(vector3 p_Eye,vector3 p_Target,double p_Dist,double p_Width,double p_Height,double p_Ratio):
Expand Down Expand Up @@ -37,7 +38,61 @@ class Camera
vector3 m_y;//投影平面的y方向基向量
double m_Ratio;//从整点像素到三维坐标系的缩放比,即图像单位像素对应投影平面在坐标系中的长度
};
*/
class Camera
{
public:
Camera():m_Eye(0,0,-5),m_Target(0,0,0),m_Dist(5.0),m_Width(8.0),m_Height(6.0),m_Ratio(0.01){}
Camera(vector3 p_Eye,vector3 p_Target,double p_Dist,double p_Width,double p_Height,double p_Ratio):
m_Eye(p_Eye),m_Target(p_Target),m_Dist(p_Dist),m_Width(p_Width),m_Height(p_Width),m_Ratio(p_Ratio){}
inline vector3 getEye(){return m_Eye;}
vector3 getDir(double x,double y)//像素坐标,整数,也可以在超采样中取小数
{
vector3 dir = m_Target - m_Eye;
NORMALIZE(dir);
dir = dir * m_Dist;
//return dir;
m_x = dir.Cross(vector3(0,1,0));
NORMALIZE(m_x);
m_y = m_x.Cross(dir);
NORMALIZE(m_y);
vector3 ret = dir + (x * m_Ratio - 0.5 * m_Width) * m_x + (y * m_Ratio - 0.5 * m_Height) * m_y;
NORMALIZE(ret);
return ret;
}
inline void setRV(double p_R,double p_V){ m_R = p_R; m_V = p_V; }
Ray getRandRay(double x,double y)//景深,获取光圈内随机的一个光线,用以计算颜色
{
//printf("%llf\n",LENGTH(m_x));
double x0,y0;
do
{
x0 = 2.0 * (double)rand() / RAND_MAX - 1.0;
y0 = 2.0 * (double)rand() / RAND_MAX - 1.0;
}while(x0 * x0 + y0 * y0 > 1);
x0 *= m_R;
y0 *= m_R;
vector3 randPos = m_Eye + x0 * m_x + y0 * m_y;
vector3 dir = getDir(x,y);
vector3 focusPos = m_Eye + dir * m_V * (1.0 / m_Dist);
vector3 rayDir = focusPos - randPos;
NORMALIZE(rayDir);
//return Ray(focusPos,rayDir);
return Ray(randPos,rayDir);
}
private:
vector3 m_Eye;
vector3 m_Target;
double m_Dist;
double m_Width;
double m_Height;
vector3 m_x;//投影平面的x方向基向量
vector3 m_y;//投影平面的y方向基向量
double m_Ratio;//从整点像素到三维坐标系的缩放比,即图像单位像素对应投影平面在坐标系中的长度

double m_R;//光圈的半径
double m_V;//眼睛到焦平面的距离
};
}

#endif
4 changes: 3 additions & 1 deletion object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ void Object::readin(char* filename,Primitive** p_Prim,int& p_PrimNum,std::vector
{
//std::cout << "face:";
int tmp[3];
fin >> tmp[0] >> tmp[1] >> tmp[2];
//fin >> tmp[0] >> tmp[1] >> tmp[2];
//´Ë´¦×¢Òâ·¨Ïò£¡£¡£¡
fin >> tmp[0] >> tmp[2] >> tmp[1];
//std::cout << tmp[0] << tmp[1] << tmp[2] << '\n';
ObjTriangle* tri = new ObjTriangle(PointList[tmp[0]],PointList[tmp[1]],PointList[tmp[2]]);
if(tri->getNormal().Length() < EPS) continue;
Expand Down
1 change: 1 addition & 0 deletions plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Color PlanePrim::getColor(vector3& p_Pos)
}
double u = DOT(p_Pos,x_Dir) * m_Material.getTexRatioDao();
double v = DOT(p_Pos,y_Dir) * m_Material.getTexRatioDao();
//std::cout << u << " " << v << std::endl;
return m_Material.getTexure()->getUVColor(u,v);
//return m_Material.getColor();
}
Expand Down
7 changes: 5 additions & 2 deletions primitive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ Texture::Texture( char* p_File )

Color Texture::getUVColor(double p_u,double p_v)//对应于uv平面的坐标,不一定在0-1之间
{
double tmp_u = p_u + 204800.0;
double tmp_v = p_v + 204800.0;
//double tmp_u = p_u + 204800.0;
//double tmp_v = p_v + 204800.0;
double tmp_u = p_u + 2048;
double tmp_v = p_v + 2048;
//std::cout << tmp_u << "\n";
assert(tmp_u > 0);
assert(tmp_v > 0);
double u_W = tmp_u * m_Width;
Expand Down
114 changes: 101 additions & 13 deletions raytracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ int Engine::findNearestKD(Ray& ray, double& p_Dist, Primitive*& p_Prim)
if (res = pr->Intersect( ray, p_Dist ))
{
p_Prim = pr;
in_out = res;
//std::cout << "intersect\n";
//result = res; // 0 = miss, 1 = hit, -1 = hit from inside primitive
}
}
Expand Down Expand Up @@ -301,13 +303,53 @@ int Engine::FindNearest( Ray& p_Ray, double& p_Dist, Primitive*& p_Prim )
return retval;
}

double Engine::calShade(Primitive* p_Light, vector3 p_pi, vector3& p_Dir)
double Engine::calShade(Primitive* p_Light, vector3 p_pi, vector3& p_Dir,double p_Sample,double p_SampleRange)
{

//vector3 delta = vector3();
double shade = 0.0;
Primitive* prim = 0;
if(p_Light->getType() == Primitive::SPHERE)
{
Sphere* light = (Sphere*)p_Light;
vector3 O = light->getCentre();
p_Dir = O - p_pi;
NORMALIZE(p_Dir);
double R = light->getRadius();
vector3 dir = O - p_pi;
double dist = LENGTH(dir);
NORMALIZE(dir);
if (FindNearest( Ray( p_pi + dir * EPS, dir), dist, prim ))
if (prim == p_Light)
shade += 1.0;
}
else if(p_Light->getType() == Primitive::BOX)
{
shade = 0.0;
Box* light = (Box*)p_Light;
vector3 P = light->getPos();
vector3 size = light->getSize();
p_Dir = P + 0.5 * size - p_pi;
NORMALIZE(p_Dir);
int n = (int)floor(sqrt(p_Sample + 0.5));
for(int i = 0;i < n;++i)
for(int j = 0;j < n;++j)
{
vector3 d = vector3(size.x / n,0.0,size.z / n);
vector3 pos = P + vector3(d.x * (i + (double)rand() / RAND_MAX),0.0,d.z * (j + (double)rand() / RAND_MAX));
vector3 dir = pos - p_pi;
double dist = LENGTH(dir);
NORMALIZE(dir);
if (FindNearest( Ray( p_pi + dir * EPS, dir), dist, prim ))
if (prim == p_Light)
shade += 1.0 / (n * n);
}
}
return shade;

//--------------------------------------------
//vector3 delta = vector3();
//double shade = 0.0;
//Primitive* prim = 0;
if(p_Light->getType() == Primitive::SPHERE)
{
//int max_R = 2,max_C = 1;
Sphere* light = (Sphere*)p_Light;
Expand Down Expand Up @@ -376,14 +418,16 @@ double Engine::calShade(Primitive* p_Light, vector3 p_pi, vector3& p_Dir)
*/
}
//printf("%llf\n",shade);
//return 1.0;
//for debug
return shade;
}




#ifndef PATHTRACING
Primitive* Engine::Runtracer( Ray& p_Ray, Color& p_Col, int p_Depth, double p_Refr_Rate, double& p_Dist )
Primitive* Engine::Runtracer( Ray& p_Ray, Color& p_Col, int p_Depth, double p_Refr_Rate, double& p_Dist ,double p_Sample,double p_SampleRange)
{
if (p_Depth > TRACEDEPTH) return 0;
// trace primary ray
Expand All @@ -404,10 +448,15 @@ Primitive* Engine::Runtracer( Ray& p_Ray, Color& p_Col, int p_Depth, double p_Re
}
}
*/
#ifndef KD
if (!(result = FindNearest( p_Ray, p_Dist, prim ))) return 0;
#else
if (!(result = findNearestKD( p_Ray, p_Dist, prim ))) return 0;
#endif
//计算一根光线Ray最近的交汇点和距离
// no hit, terminate ray
if (!prim) return 0;//光线不和任何物体相交
//std::cout << "PRIM!=0" << std::endl;
// handle intersection
if (prim->IsLight())//如果光线最近遇到光源,直接返回光源颜色
{
Expand All @@ -418,10 +467,13 @@ Primitive* Engine::Runtracer( Ray& p_Ray, Color& p_Col, int p_Depth, double p_Re
}
else//否则,最近的是不发光物体
{
//std::cout << "is not light\n";
// determine color at point of intersection
pi = p_Ray.getOrigin() + p_Ray.getDirection() * p_Dist;//pi为光线与最近物体交汇的地方
//std::cout << pi.x << " " << pi.y << " " << pi.z << "\n";
// trace lights
//for ( int l = 0; l < m_Scene->getNrPrimitives(); l++ )//再次枚举所有物体
//std::cout << m_Scene->getNrLights() << "\n";
for(int l = 0;l < m_Scene->getNrLights() ;++l)
{
//Primitive* p = m_Scene->getPrimitive( l );
Expand Down Expand Up @@ -456,7 +508,7 @@ Primitive* Engine::Runtracer( Ray& p_Ray, Color& p_Col, int p_Depth, double p_Re
}
*/
vector3 L;
shade = calShade(light,pi,L);
shade = calShade(light,pi,L,p_Sample,p_SampleRange);
// calculate diffuse shading
//vector3 L = ((Sphere*)light)->getCentre() - pi;
//NORMALIZE( L );
Expand Down Expand Up @@ -523,7 +575,11 @@ Primitive* Engine::Runtracer( Ray& p_Ray, Color& p_Col, int p_Depth, double p_Re
NORMALIZE( newR );
double dist;
Color rcol( 0, 0, 0 );
Runtracer( Ray( pi + newR * EPS, newR ), rcol, p_Depth + 1, p_Refr_Rate, dist );
#ifdef IMPORTANCE_SAMPLING
Runtracer( Ray( pi + newR * EPS, newR ), rcol, p_Depth + 1, p_Refr_Rate, dist ,p_Sample * 0.25,p_SampleRange * 4.0);
#else
Runtracer( Ray( pi + newR * EPS, newR ), rcol, p_Depth + 1, p_Refr_Rate, dist ,p_Sample,p_SampleRange);
#endif
//p_Col += refl * rcol * prim->getMaterial()->getColor() * (1.0 / (double)num);
p_Col += refl * rcol * prim->getColor(pi) * (1.0 / (double)num);
}
Expand All @@ -532,7 +588,13 @@ Primitive* Engine::Runtracer( Ray& p_Ray, Color& p_Col, int p_Depth, double p_Re
{
Color rcol( 0, 0, 0 );
double dist;
Runtracer( Ray( pi + R * EPS, R ), rcol, p_Depth + 1, p_Refr_Rate, dist );
#ifdef IMPORTANCE_SAMPLING
//Runtracer( Ray( pi + newR * EPS, newR ), rcol, p_Depth + 1, p_Refr_Rate, dist ,p_Sample * 0.25,p_SampleRange * 4.0);
Runtracer( Ray( pi + R * EPS, R ), rcol, p_Depth + 1, p_Refr_Rate, dist ,p_Sample * 0.25,p_SampleRange * 4.0);
#else
//Runtracer( Ray( pi + newR * EPS, newR ), rcol, p_Depth + 1, p_Refr_Rate, dist ,p_Sample,p_SampleRange);
Runtracer( Ray( pi + R * EPS, R ), rcol, p_Depth + 1, p_Refr_Rate, dist ,p_Sample,p_SampleRange);
#endif
//p_Col += refl * rcol * prim->getMaterial()->getColor();
p_Col += refl * rcol * prim->getColor(pi);
}
Expand Down Expand Up @@ -561,7 +623,13 @@ Primitive* Engine::Runtracer( Ray& p_Ray, Color& p_Col, int p_Depth, double p_Re
double sinr = sqrt(sinr2);
double cosr = sqrt(cosr2);
vector3 T = (V * (1/n)) + (cosi / n - sqrt( cosr2 )) * N;
Runtracer(Ray(pi + T * EPS , T),rcol,p_Depth + 1, tmp_Refr_rate,dist);
#ifdef IMPORTANCE_SAMPLING
//Runtracer( Ray( pi + newR * EPS, newR ), rcol, p_Depth + 1, p_Refr_Rate, dist ,p_Sample * 0.25,p_SampleRange * 4.0);
Runtracer(Ray(pi + T * EPS , T),rcol,p_Depth + 1, tmp_Refr_rate,dist,p_Sample * 0.25,p_SampleRange * 4.0);
#else
//Runtracer( Ray( pi + newR * EPS, newR ), rcol, p_Depth + 1, p_Refr_Rate, dist ,p_Sample,p_SampleRange);
Runtracer(Ray(pi + T * EPS , T),rcol,p_Depth + 1, tmp_Refr_rate,dist,p_Sample,p_SampleRange);
#endif
//Color absorbance = prim->getMaterial()->getColor() * 0.15 * -dist;
Color absorbance = prim->getColor(pi) * 0.15 * -dist;
Color transparency = Color( exp( absorbance.r ), exp( absorbance.g ), exp( absorbance.b ) );
Expand All @@ -577,6 +645,7 @@ bool Engine::HYF_render(cv::Mat& colorim)
{
//vector3 o( 0, 0, -5 );
Camera c = Camera();
//c.setRV(0.005,16);
Primitive* lastprim = 0;
for ( int y = 0; y < m_Height; y++ )
{
Expand All @@ -594,7 +663,7 @@ bool Engine::HYF_render(cv::Mat& colorim)
vector3 dir = c.getDir(x,y);
Ray r( c.getEye(), dir );
double dist;
Primitive* prim = Runtracer( r, col, 1, 1.0, dist );
Primitive* prim = Runtracer( r, col, 1, 1.0, dist ,SAMPLES,(1.0 / SAMPLES));
}
int red = (int)(col.r * 256 / 9);
int green = (int)(col.g * 256 / 9);
Expand All @@ -607,7 +676,19 @@ bool Engine::HYF_render(cv::Mat& colorim)
vector3 dir = c.getDir(x,y);
Ray r( c.getEye(), dir );
double dist;
Primitive* prim = Runtracer( r, col, 1, 1.0, dist );
Primitive* prim = Runtracer( r, col, 1, 1.0, dist ,SAMPLES,(1.0 / SAMPLES));
#ifdef DEPTH_OF_FIELD
c.setRV(0.02,15.0);
for(int oo = 0;oo < 9;++oo)
{
//Color tmpCol(0.0,0.0,0.0);
//prim = Runtracer( c.getRandRay(x,y), tmpCol, 1, 1.0, dist ,SAMPLES,(1.0 / SAMPLES));
prim = Runtracer( c.getRandRay(x,y), col, 1, 1.0, dist ,SAMPLES,(1.0 / SAMPLES));
//prim = Runtracer( r, tmpCol, 1, 1.0, dist ,SAMPLES,(1.0 / SAMPLES));
//col += tmpCol;
}
col *= (1.0 / 10);
#endif
int red = (int)(col.r * 256);
int green = (int)(col.g * 256);
int blue = (int)(col.b * 256);
Expand All @@ -618,8 +699,8 @@ bool Engine::HYF_render(cv::Mat& colorim)
colorim.at<Vec3b>(m_Height - y - 1,x) = Vec3b(blue,green,red);
}
printf("rendering %dth row...\n",y+1);
imshow("test",colorim);
waitKey(10);
cv::imshow("test",colorim);
cv::waitKey(10);
}
return true;
}
Expand Down Expand Up @@ -702,6 +783,7 @@ void Engine::PTintersect(Ray &r,double& t,Primitive* prim)
Color Engine::Runtracer(Ray &r, int depth, unsigned short *Xi)
{
//std::cout << depth << "\n";
if(depth > 1400) return Color(0,0,0);
double t = 1e20; // distance to intersection
//int id=0; // id of intersected object
Primitive* prim = 0;
Expand All @@ -721,7 +803,11 @@ Color Engine::Runtracer(Ray &r, int depth, unsigned short *Xi)
}
}
*/
#ifndef KD
FindNearest( r, t, prim );
#else
findNearestKD(r,t,prim);
#endif
//std::cout << t << std::endl;
if (prim == 0) {return vector3();} // if miss, return black
//if (!intersect(r, t, id)) {return vector3();} // if miss, return black
Expand Down Expand Up @@ -796,7 +882,9 @@ bool Engine::HYF_render(cv::Mat& colorim)
c[i] = c[i] + vector3(clamp(r.x),clamp(r.y),clamp(r.z))*.25;
colorim.at<cv::Vec3b>(h - y - 1,x) = cv::Vec3b(toInt(c[i].x), toInt(c[i].y), toInt(c[i].z));
}
cv::imshow("test",colorim);
//cv::imshow("test",colorim);
if(y == 100 || y == 200 || y == 300 || y == 400 || y == 500 || y == 600)
cv::imwrite("tmp.png",colorim);
cv::waitKey(10);
}
/*
Expand Down
4 changes: 2 additions & 2 deletions raytracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class Engine
~Engine();
void setTarget();
inline Scene* getScene() { return m_Scene; }
Primitive* Runtracer( Ray& p_Ray, Color& p_Acc, int p_Depth, double p_RIndex, double& p_Dist );
Primitive* Runtracer( Ray& p_Ray, Color& p_Acc, int p_Depth, double p_RIndex, double& p_Dist ,double p_Sample,double p_SampleRange);
Color Runtracer(Ray &r, int depth, unsigned short *Xi);//Path Tracing
bool HYF_render(cv::Mat&);
double calShade(Primitive* p_Light, vector3 p_pi, vector3& p_Dir);
double calShade(Primitive* p_Light, vector3 p_pi, vector3& p_Dir,double p_Sample,double p_SampleRange);
int findNearestKD(Ray& ray, double& p_Dist, Primitive*& p_Prim);
int FindNearest(Ray& p_Ray, double& p_Dist, Primitive*& p_Prim);
void PTintersect(Ray &r,double& t,Primitive* prim);
Expand Down
Loading

0 comments on commit 5a8e7fd

Please sign in to comment.