Skip to content

Commit

Permalink
add camera class, fix bug in diffuse reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
evan69 committed May 25, 2016
1 parent d98ee5a commit cda8882
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 49 deletions.
1 change: 1 addition & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
2016.5.23 ����������
2016.5.24 �����򵥰�Χ��
2016.5.24 ��������Ӱ���ֲھ��淴��
2016.5.26 ����������࣬�޸��ֲھ��淴���bug
1 change: 1 addition & 0 deletions all.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ typedef vector3 Color;

};

#include "camera.h"
#include "ray.h"
#include "primitive.h"
#include "box.h"
Expand Down
4 changes: 2 additions & 2 deletions box.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace HYF
class Box : public Primitive
{
public:
int getType() { return BOX; }
inline int getType() { return BOX; }
Box();
Box( BoundingBox& p_Box );
int Intersect( Ray& p_Ray, double& p_Dist );
Expand All @@ -18,7 +18,7 @@ class Box : public Primitive
bool Contains( vector3& p_Pos ) { return m_Box.Contains( p_Pos ); }
vector3& getPos() { return m_Box.getPos(); }
vector3& getSize() { return m_Box.getSize(); }
BoundingBox getBoundingBox() { return m_Box; }
inline BoundingBox getBoundingBox() { return m_Box; }
protected:
BoundingBox m_Box;
};
Expand Down
43 changes: 43 additions & 0 deletions camera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef CAMERA_H
#define CAMERA_H

#include "all.h"

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):
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;
}
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;//从整点像素到三维坐标系的缩放比,即图像单位像素对应投影平面在坐标系中的长度
};

}

#endif
8 changes: 4 additions & 4 deletions plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ class PlanePrim : public Primitive
public:
int getType() { return PLANE; }
PlanePrim( vector3& p_Normal, double p_D ) : m_Plane( plane( p_Normal, p_D ) ) {};
vector3& getNormal() { return m_Plane.N; }
double getD() { return m_Plane.D; }
inline vector3& getNormal() { return m_Plane.N; }
inline double getD() { return m_Plane.D; }
int Intersect( Ray& p_Ray, double& p_Dist );
bool H_IntersectBox( BoundingBox& );
vector3 getNormal( vector3& p_Pos ){return m_Plane.N;};
BoundingBox getBoundingBox() { return BoundingBox(vector3(-10000,-10000,-10000),vector3(20000,20000,20000));}
inline vector3 getNormal( vector3& p_Pos ){return m_Plane.N;};
inline BoundingBox getBoundingBox() { return BoundingBox(vector3(-10000,-10000,-10000),vector3(20000,20000,20000));}

private:
plane m_Plane;
Expand Down
36 changes: 18 additions & 18 deletions primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ class Material
{
public:
Material();
void setColor( Color& p_Color ) { m_Color = p_Color; }
Color getColor() { return m_Color; }
void setSpecular( double p_Spec ) { m_Spec = p_Spec; }//高光
void setDiffuse( double p_Diff ) { m_Diff = p_Diff; }
void setReflection( double p_Refl ) { m_Refl = p_Refl; }
void setDiffRefl(double p_DiffRefl ) { m_DiffRefl = p_DiffRefl; }
void setRefraction( double p_Refr ) { m_Refr = p_Refr; }
void setRefr_Rate( double p_Refr_Rate ) { m_Refr_Rate = p_Refr_Rate; }
double getSpecular() { return m_Spec; }//高光
double getDiffuse() { return m_Diff; }//漫反射
double getReflection() { return m_Refl;}
double getDiffRefl() { return m_DiffRefl;}
double getRefraction() { return m_Refr;}
double getRefr_Rate() { return m_Refr_Rate;}
inline void setColor( Color& p_Color ) { m_Color = p_Color; }
inline Color getColor() { return m_Color; }
inline void setSpecular( double p_Spec ) { m_Spec = p_Spec; }//高光
inline void setDiffuse( double p_Diff ) { m_Diff = p_Diff; }
inline void setReflection( double p_Refl ) { m_Refl = p_Refl; }
inline void setDiffRefl(double p_DiffRefl ) { m_DiffRefl = p_DiffRefl; }
inline void setRefraction( double p_Refr ) { m_Refr = p_Refr; }
inline void setRefr_Rate( double p_Refr_Rate ) { m_Refr_Rate = p_Refr_Rate; }
inline double getSpecular() { return m_Spec; }//高光
inline double getDiffuse() { return m_Diff; }//漫反射
inline double getReflection() { return m_Refl;}
inline double getDiffRefl() { return m_DiffRefl;}
inline double getRefraction() { return m_Refr;}
inline double getRefr_Rate() { return m_Refr_Rate;}
private:
Color m_Color;//材质颜色
double m_Refl;//反射系数
Expand All @@ -44,8 +44,8 @@ class Primitive
BOX = 3
};
Primitive() : m_Name( 0 ), m_Light( false ) {};
Material* getMaterial() { return &m_Material; }
void setMaterial( Material& p_Mat ) { m_Material = p_Mat; }
inline Material* getMaterial() { return &m_Material; }
inline void setMaterial( Material& p_Mat ) { m_Material = p_Mat; }

virtual int getType() = 0;
virtual int Intersect( Ray& p_Ray, double& p_Dist ) = 0;
Expand All @@ -55,9 +55,9 @@ class Primitive
virtual void Light( bool p_Light ) { m_Light = p_Light; }
virtual BoundingBox getBoundingBox() = 0;

bool IsLight() { return m_Light; }
inline bool IsLight() { return m_Light; }
void setName( char* p_Name );
char* getName() { return m_Name; }
inline char* getName() { return m_Name; }
protected:
Material m_Material;
char* m_Name;
Expand Down
8 changes: 4 additions & 4 deletions ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class Ray
public:
Ray() : m_Origin( vector3( 0, 0, 0 ) ), m_Direction( vector3( 0, 0, 0 ) ) {};
Ray( vector3& p_Origin, vector3& p_Dir ): m_Origin( p_Origin ), m_Direction( p_Dir ){}
void setOrigin( vector3& p_Origin ) { m_Origin = p_Origin; }
void setDirection( vector3& p_Direction ) { m_Direction = p_Direction; }
vector3& getOrigin() { return m_Origin; }
vector3& getDirection() { return m_Direction; }
inline void setOrigin( vector3& p_Origin ) { m_Origin = p_Origin; }
inline void setDirection( vector3& p_Direction ) { m_Direction = p_Direction; }
inline vector3& getOrigin() { return m_Origin; }
inline vector3& getDirection() { return m_Direction; }
private:
vector3 m_Origin;
vector3 m_Direction;
Expand Down
30 changes: 18 additions & 12 deletions raytracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,14 @@ Primitive* Engine::Runtracer( Ray& p_Ray, Color& p_Col, int p_Depth, double p_Re
double x0,y0;
while(1)
{
x0 = (double)rand() / RAND_MAX * drefl;
y0 = (double)rand() / RAND_MAX * drefl;
if((x0 * x0 + y0 * y0) < (drefl * drefl))
x0 = 2.0 * (double)rand() / RAND_MAX - 1.0;
y0 = 2.0 * (double)rand() / RAND_MAX - 1.0;
//if((x0 * x0 + y0 * y0) < (drefl * drefl))
if((x0 * x0 + y0 * y0) < 1.0)
break;
}
x0 *= drefl;
y0 *= drefl;
vector3 newR = R + component1 * x0 + component2 * y0;
NORMALIZE( newR );
double dist;
Expand Down Expand Up @@ -468,7 +471,8 @@ Primitive* Engine::Runtracer( Ray& p_Ray, Color& p_Col, int p_Depth, double p_Re
//#define SUPERSAMPLING
bool Engine::HYF_render(cv::Mat& colorim)
{
vector3 o( 0, 0, -5 );
//vector3 o( 0, 0, -5 );
Camera c = Camera();
Primitive* lastprim = 0;
for ( int y = 0; y < m_Height; y++ )
{
Expand All @@ -481,9 +485,10 @@ bool Engine::HYF_render(cv::Mat& colorim)
for(double i = -1.0;i < 1.5;++i)
for(double j = -1.0;j < 1.5;++j)
{
vector3 dir = vector3( (1.0 * x + i / 3) / 100 - 4.0 , (1.0 * y + j / 3) / 100 - 3.0, 0 ) - o;
NORMALIZE( dir );
Ray r( o, dir );
//vector3 dir = vector3( (1.0 * x + i / 3) / 100 - 4.0 , (1.0 * y + j / 3) / 100 - 3.0, 0 ) - o;
//NORMALIZE( dir );
vector3 dir = c.getDir(x,y);
Ray r( c.getEye(), dir );
double dist;
Primitive* prim = Runtracer( r, col, 1, 1.0, dist );
}
Expand All @@ -493,9 +498,10 @@ bool Engine::HYF_render(cv::Mat& colorim)
//super sampling
#endif
#ifndef SUPERSAMPLING
vector3 dir = vector3( (1.0 * x) / 100 - 4.0 , (1.0 * y) / 100 - 3.0, 0 ) - o;
NORMALIZE( dir );
Ray r( o, dir );
//vector3 dir = vector3( (1.0 * x) / 100 - 4.0 , (1.0 * y) / 100 - 3.0, 0 ) - o;
//NORMALIZE( dir );
vector3 dir = c.getDir(x,y);
Ray r( c.getEye(), dir );
double dist;
Primitive* prim = Runtracer( r, col, 1, 1.0, dist );
int red = (int)(col.r * 256);
Expand All @@ -508,8 +514,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(0);
imshow("test",colorim);
waitKey(10);
}
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion raytracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Engine
Engine();
~Engine();
void setTarget();
Scene* getScene() { return m_Scene; }
inline Scene* getScene() { return m_Scene; }
Primitive* Runtracer( Ray& p_Ray, Color& p_Acc, int p_Depth, double p_RIndex, double& p_Dist );
bool HYF_render(cv::Mat&);
double calShade(Primitive* p_Light, vector3 p_pi, vector3& p_Dir);
Expand Down
12 changes: 6 additions & 6 deletions scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class Scene
~Scene();
void init();
void BuildGrid();
std::vector<Primitive*>* getGrid() {return m_Grid;}
int getNrPrimitives() { return m_Primitives; }
int getNrLights() {return m_Lights;}
Primitive* getPrimitive( int p_Idx ) { return m_Primitive[p_Idx]; }
Primitive* getLight(int p_Idx) { return m_Light[p_Idx];}
BoundingBox& getBoundary() {return m_Boundary;}
inline std::vector<Primitive*>* getGrid() {return m_Grid;}
inline int getNrPrimitives() { return m_Primitives; }
inline int getNrLights() {return m_Lights;}
inline Primitive* getPrimitive( int p_Idx ) { return m_Primitive[p_Idx]; }
inline Primitive* getLight(int p_Idx) { return m_Light[p_Idx];}
inline BoundingBox& getBoundary() {return m_Boundary;}
private:
int m_Primitives,m_Lights;
Primitive** m_Primitive,**m_Light;
Expand Down
4 changes: 2 additions & 2 deletions sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Sphere : public Primitive
double getSqRadius() { return m_SqRadius; }
int Intersect( Ray& p_Ray, double& p_Dist );
bool H_IntersectBox(BoundingBox&);
vector3 getNormal( vector3& p_Pos ) { return (p_Pos - m_Centre) * m_RRadius; }
BoundingBox getBoundingBox()
inline vector3 getNormal( vector3& p_Pos ) { return (p_Pos - m_Centre) * m_RRadius; }
inline BoundingBox getBoundingBox()
{
vector3 vecR(m_Radius,m_Radius,m_Radius);
return BoundingBox(m_Centre - vecR,2*vecR);
Expand Down

0 comments on commit cda8882

Please sign in to comment.