//IShader.cuh#ifndef I_SHADER_H#define I_SHADER_H#include"Vector3D.cuh"
#include"Color.cuh"
#include"IGeometry.cuh"
__device__ extern Vector cameraPos;
__device__ extern Vector lightPos;
__device__ extern Color lightColor;
__device__ extern float lightPower;
__device__ extern Color ambientLight;
class Shader
{
protected:
Color _color;
public:
__device__ Shader(const Color& color);
__device__ virtual ~Shader();
__device__ virtual Color shade(Ray ray, const IntersectionData& data) = 0;
};
#endif
//IShader.cu
#include"IShader.cuh"
__device__ Shader::Shader(const Color& color)
{
this->_color = color;
}
// Lambert.cuh
#ifndef LAMBERT_H
#define LAMBERT_H
#include"IShader.cuh"
class Lambert : public Shader
{
public:
__device__ Lambert(const Color& diffuseColor);
__device__ Color shade(Ray ray, const IntersectionData& data);
};
#endif
//Lambert.cu
#include"Lambert.cuh"
Vector cameraPos;
Vector lightPos;
Color lightColor;
float lightPower;
Color ambientLight;
__device__ Lambert::Lambert(const Color& diffuseColor)
: Shader(diffuseColor)
{
}
__device__ Color Lambert::shade(Ray ray, const IntersectionData& data)
{
Color result = _color;
result = result * lightColor * lightPower / (data.p - lightPos).lengthSqr();
Vector lightDir = lightPos - data.p;
lightDir.normalize();
double cosTheta = dot(lightDir, data.normal);
result = result * cosTheta;
return result;
}