Skip to content

Commit

Permalink
add texture on plane
Browse files Browse the repository at this point in the history
  • Loading branch information
evan69 committed May 26, 2016
1 parent cda8882 commit 169f007
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
2016.5.24 �����򵥰�Χ��
2016.5.24 ��������Ӱ���ֲھ��淴��
2016.5.26 ����������࣬�޸��ֲھ��淴���bug
2016.5.26 ����ƽ���������ͼ
12 changes: 12 additions & 0 deletions plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
namespace HYF
{

Color PlanePrim::getColor(vector3& p_Pos)
{
if(m_Material.getTexure() == NULL)
{
return m_Material.getColor();
}
double u = DOT(p_Pos,x_Dir) * m_Material.getTexRatioDao();
double v = DOT(p_Pos,y_Dir) * m_Material.getTexRatioDao();
return m_Material.getTexure()->getUVColor(u,v);
//return m_Material.getColor();
}

int PlanePrim::Intersect( Ray& p_Ray, double& p_Dist )
{
double d = DOT( m_Plane.N, p_Ray.getDirection() );
Expand Down
16 changes: 15 additions & 1 deletion plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,30 @@ class PlanePrim : public Primitive
{
public:
int getType() { return PLANE; }
PlanePrim( vector3& p_Normal, double p_D ) : m_Plane( plane( p_Normal, p_D ) ) {};
PlanePrim( vector3& p_Normal, double p_D ) : m_Plane( plane( p_Normal, p_D ) )
{
//x_Dir = vector3( m_Plane.N.y, m_Plane.N.z, -m_Plane.N.x );

x_Dir = m_Plane.N.Cross(vector3(0,1,0));
y_Dir = x_Dir.Cross( m_Plane.N );
NORMALIZE(x_Dir);
NORMALIZE(y_Dir);
O_H = - m_Plane.N * m_Plane.D;

}
inline vector3& getNormal() { return m_Plane.N; }
inline double getD() { return m_Plane.D; }
Color getColor(vector3& p_Pos);
int Intersect( Ray& p_Ray, double& p_Dist );
bool H_IntersectBox( BoundingBox& );
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;
vector3 x_Dir;
vector3 y_Dir;
vector3 O_H;//Ô­µãÔÚƽÃæÉϵĴ¹×ã
};

}
Expand Down
54 changes: 54 additions & 0 deletions primitive.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,62 @@
#include "all.h"
#include <assert.h>

namespace HYF
{

Texture::Texture( Color* p_Bitmap, int p_Width, int p_Height ) :
m_Bitmap( p_Bitmap ),m_Width( p_Width ), m_Height( p_Height )
{
}

Texture::Texture( char* p_File )
{
FILE* f = fopen( p_File, "rb" );
if (f)
{
// extract width and height from file header
unsigned char buffer[20];
fread( buffer, 1, 20, f );
m_Width = *(buffer + 12) + 256 * *(buffer + 13);
m_Height = *(buffer + 14) + 256 * *(buffer + 15);
fclose( f );
// read pixel data
f = fopen( p_File, "rb" );
unsigned char* t = new unsigned char[m_Width * m_Height * 3 + 1024];
fread( t, 1, m_Width * m_Height * 3 + 1024, f );
fclose( f );
// convert RGB 8:8:8 pixel data to doubleing point RGB
m_Bitmap = new Color[m_Width * m_Height];
double rec = 1.0f / 256;
for ( int size = m_Width * m_Height, i = 0; i < size; i++ )
m_Bitmap[i] = Color( t[i * 3 + 20] * rec, t[i * 3 + 19] * rec, t[i * 3 + 18] * rec );
delete t;
}
}

Color Texture::getUVColor(double p_u,double p_v)//对应于uv平面的坐标,不一定在0-1之间
{
double tmp_u = p_u + 2048.0;
double tmp_v = p_v + 2048.0;
assert(tmp_u > 0);
assert(tmp_v > 0);
double u_W = tmp_u * m_Width;
double v_H = tmp_v * m_Height;
int u1 = (int)u_W % m_Width,u2 = (u1 + 1) % m_Width;
int v1 = (int)v_H % m_Height,v2 = (v1 + 1) % m_Height;
double deltp_u = u_W - floor(u_W);
double deltp_v = v_H - floor(v_H);
double w11 = (1 - deltp_u) * (1 - deltp_v);
double w12 = (1 - deltp_u) * deltp_v;
double w21 = deltp_u * (1 - deltp_v);
double w22 = deltp_u * deltp_v;
Color c11 = m_Bitmap[u1 + v1 * m_Width];
Color c21 = m_Bitmap[u2 + v1 * m_Width];
Color c12 = m_Bitmap[u1 + v2 * m_Width];
Color c22 = m_Bitmap[u2 + v2 * m_Width];
return w11 * c11 + w12 * c12 + w21 * c21 + w22 * c22;
}

void Primitive::setName( char* p_Name )
{
delete m_Name;
Expand Down
27 changes: 25 additions & 2 deletions primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,25 @@
namespace HYF
{

class Texture
{
public:
Texture( Color* p_Bitmap, int p_Width, int p_Height );
Texture( char* p_File );
inline Color* getBitmap() { return m_Bitmap; }
Color getUVColor( double p_U, double p_V );
inline int getWidth() { return m_Width; }
inline int getHeight() { return m_Height; }
private:
Color* m_Bitmap;
int m_Width, m_Height;
};

class Material
{
public:
Material();
inline void setColor( Color& p_Color ) { m_Color = p_Color; }
inline void setColor( Color& p_Color ) { m_Color = p_Color; m_Tex = NULL;m_TexRatio = m_TexRatioDao = 1.0;}
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; }
Expand All @@ -24,6 +38,12 @@ class Material
inline double getDiffRefl() { return m_DiffRefl;}
inline double getRefraction() { return m_Refr;}
inline double getRefr_Rate() { return m_Refr_Rate;}

inline void setTexure(Texture* p_Tex){m_Tex = p_Tex;}
inline Texture* getTexure() {return m_Tex;}
inline void setTexRatio(double p_TexRatio){m_TexRatio = p_TexRatio; m_TexRatioDao = 1.0 / p_TexRatio;}
inline double getTexRatio() {return m_TexRatio;}
inline double getTexRatioDao() {return m_TexRatioDao;}
private:
Color m_Color;//材质颜色
double m_Refl;//反射系数
Expand All @@ -32,6 +52,9 @@ class Material
double m_Spec;//高光
double m_Refr;//透射率
double m_Refr_Rate;//折射率
Texture* m_Tex;
double m_TexRatio;//一块纹理的放大倍数
double m_TexRatioDao;//一块纹理的放大倍数的倒数
};

class Primitive
Expand All @@ -51,7 +74,7 @@ class Primitive
virtual int Intersect( Ray& p_Ray, double& p_Dist ) = 0;
virtual bool H_IntersectBox( BoundingBox& ) = 0;
virtual vector3 getNormal( vector3& p_Pos ) = 0;
virtual Color getColor() { return m_Material.getColor(); }
virtual Color getColor(vector3& p_Pos) { return m_Material.getColor(); }
virtual void Light( bool p_Light ) { m_Light = p_Light; }
virtual BoundingBox getBoundingBox() = 0;

Expand Down
12 changes: 8 additions & 4 deletions raytracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ Primitive* Engine::Runtracer( Ray& p_Ray, Color& p_Col, int p_Depth, double p_Re
{
double diff = dot * prim->getMaterial()->getDiffuse() * shade;
// add diffuse component to ray color
p_Col += diff * light->getMaterial()->getColor() * prim->getMaterial()->getColor();
//p_Col += diff * light->getMaterial()->getColor() * prim->getMaterial()->getColor();
p_Col += diff * light->getMaterial()->getColor() * prim->getColor(pi);
}
}
// determine specular component
Expand Down Expand Up @@ -424,15 +425,17 @@ Primitive* Engine::Runtracer( Ray& p_Ray, Color& p_Col, int p_Depth, double p_Re
double dist;
Color rcol( 0, 0, 0 );
Runtracer( Ray( pi + newR * EPS, newR ), rcol, p_Depth + 1, p_Refr_Rate, dist );
p_Col += refl * rcol * prim->getMaterial()->getColor() * (1.0 / (double)num);
//p_Col += refl * rcol * prim->getMaterial()->getColor() * (1.0 / (double)num);
p_Col += refl * rcol * prim->getColor(pi) * (1.0 / (double)num);
}
}
else if (p_Depth < TRACEDEPTH)//光滑镜面反射
{
Color rcol( 0, 0, 0 );
double dist;
Runtracer( Ray( pi + R * EPS, R ), rcol, p_Depth + 1, p_Refr_Rate, dist );
p_Col += refl * rcol * prim->getMaterial()->getColor();
//p_Col += refl * rcol * prim->getMaterial()->getColor();
p_Col += refl * rcol * prim->getColor(pi);
}
}
//计算折射
Expand All @@ -459,7 +462,8 @@ Primitive* Engine::Runtracer( Ray& p_Ray, Color& p_Col, int p_Depth, double p_Re
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);
Color absorbance = prim->getMaterial()->getColor() * 0.15 * -dist;
//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 ) );
p_Col += rcol * transparency;
}
Expand Down
6 changes: 4 additions & 2 deletions scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Scene::~Scene()
delete m_Primitive;
}

#define SCENE_CORNELLBOX
//#define SCENE_5
//#define SCENE_CORNELLBOX
#define SCENE_3
void Scene::init()
{
m_Primitive = 0;
Expand Down Expand Up @@ -64,6 +64,8 @@ void Scene::init()
m_Primitive[6]->getMaterial()->setSpecular( 0 );
m_Primitive[6]->getMaterial()->setDiffuse( 0.6 );
m_Primitive[6]->getMaterial()->setColor( Color( 0.5, 0.3, 0.5 ) );
m_Primitive[6]->getMaterial()->setTexure(new Texture( "textures/marble.tga" ));
m_Primitive[6]->getMaterial()->setTexRatio(2.0);
// ceiling plane
m_Primitive[7] = new PlanePrim( vector3( 0, -1, 0 ), 7.4f );
m_Primitive[7]->setName( "back plane" );
Expand Down
Binary file added textures/marble.tga
Binary file not shown.
Binary file added textures/wood.tga
Binary file not shown.

0 comments on commit 169f007

Please sign in to comment.