/*---------------------------------------------------------------------------------------------
Eclinse
Len3d.All Rights Reserved.
---------------------------------------------------------------------------------------------*/
#ifndef _BASE_H
#define _BASE_H
//---------------------------------------------------------------------------------------------
# include "os.h"
# ifdef _BASE_DLL
# define DLLPORT DLLEXPORT
# else
# pragma comment( lib, "base.lib" )
# ifdef _ECLINSE
# define DLLPORT
# else
# define DLLPORT DLLIMPORT
# endif
# endif
# define DECLARE_TYPE( _datatype ) _datatype type;
# ifdef INLINE
# undef INLINE
# endif
# ifdef _DEBUG
# define _CRTDBG_MAP_ALLOC
# include <crtdbg.h>
# include "debugnew.h"
# define INLINE
# define VIRTUAL virtual
# define VIRTUAL_BEGIN( _funcdef ) _funcdef {
# define VOID_DERIVED_CLASS( _class, _enum )
# define DERIVED_CLASS( _class, _enum )
# define VIRTUAL_END }
# else
# //define USING_SSE
# define INLINE //inline
# define VIRTUAL INLINE
# define VIRTUAL_BEGIN( _funcdef ) _funcdef { switch( type ) { default:
# define VOID_DERIVED_CLASS( _class, _enum ) break; case _enum: ((_class*)this)->##FUNCCALL;
# define DERIVED_CLASS( _class, _enum ) break; case _enum: return ((_class*)this)->##FUNCCALL;
# define VIRTUAL_END break; } }
# endif
//---------------------------------------------------------------------------------------------
namespace Eclinse
{
class MemoryManager;
class Entity;
class Reference;
class Instance;
template <typename T> class Property;
class Texture;
class TextureHandler;
class Light;
class PointLight;
class DistantLight;
class SpotLight;
# define Group GroupNode
class Group;
class Primitive;
class NudeVertex;
class AnimatableNudeVertex;
class Vertex;
class TradRayVertex;
class RayVertex;
class Triangle;
class RayPrimitive;
class BaseRayTriangle;
class TradRayTriangle;
class RayTriangle;
class Object;
class Polygon;
class Sphere;
class RayObject;
class TradRayPolygon;
class RayPolygon;
class BSPNode;
class Material;
class Camera;
class Photon;
class Ray;
class RayState;
class RayTracer;
class Scene;
class PixelBuffer;
template <typename T> class Buffer;
class Sampler;
class Mender;
class Bucket;
class Tracer;
class Renderer;
class Interface;
class Variable;
class ShaderInstance;
class SurfaceShaderInstance;
class LightShaderInstance;
class ShadowShaderInstance;
class DisplacementShaderInstance;
class PhotonShaderInstance;
class EmitterShaderInstance;
class VolumeShaderInstance;
class LensShaderInstance;
class ImagerShaderInstance;
class MainWindow;
class PhotonMap;
class Parser;
struct _Option;
//---------------------------------------------------------------------------------------------
typedef union _Vector2f {
struct { float x, y; };
struct { float u, v; };
float arr[2];
void operator += ( const _Vector2f & b ) {
x += b.x; y += b.y;
}
void operator -= ( const _Vector2f & b ) {
x -= b.x; y -= b.y;
}
void operator *= ( float s ) {
x *= s; y *= s;
}
} Vector2f;
//---------------------------------------------------------------------------------------------
typedef union _Vector3f {
struct { float x, y, z; };
struct { float r, g, b; };
float arr[3];
void operator += ( const _Vector3f & b ) {
x += b.x; y += b.y; z += b.z;
}
void operator -= ( const _Vector3f & b ) {
x -= b.x; y -= b.y; z -= b.z;
}
void operator *= ( float s ) {
x *= s; y *= s; z *= s;
}
} Vector3f, Color3f;
//---------------------------------------------------------------------------------------------
typedef union _Vector4f {
struct { float x, y, z, w; };
struct { float r, g, b, a; };
struct { Vector3f abc; float d; };
struct { Vector2f uv; float du, dv; };
float arr[4];
__m128 mm;
void operator += ( const _Vector4f & b ) {
x += b.x; y += b.y; z += b.z; w += b.w;
}
void operator -= ( const _Vector4f & b ) {
x -= b.x; y -= b.y; z -= b.z; w -= b.w;
}
void operator *= ( float s ) {
x *= s; y *= s; z *= s; w *= s;
}
} Vector4f, Color4f;
//---------------------------------------------------------------------------------------------
typedef union _UnalignedVector4f {
struct { float x, y, z, w; };
struct { Vector3f abc; float d; };
float arr[4];
void operator = ( const Vector4f & b ) {
arr[0] = b.arr[0]; arr[1] = b.arr[1]; arr[2] = b.arr[2]; arr[3] = b.arr[3];
}
Vector4f aligned() {
Vector4f r;
r.arr[0] = arr[0]; r.arr[1] = arr[1]; r.arr[2] = arr[2]; r.arr[3] = arr[3];
return r;
}
} UnalignedVector4f;
//---------------------------------------------------------------------------------------------
typedef union _Vector2i {
struct { int x, y; };
int arr[2];
} Vector2i;
//---------------------------------------------------------------------------------------------
typedef union _Vector3i {
struct { int x, y, z; };
struct { int r, g, b; };
int arr[3];
} Vector3i, Color3i;
//---------------------------------------------------------------------------------------------
typedef union _Vector4i {
struct { int x, y, z, w; };
struct { int r, g, b, a; };
int arr[4];
} Vector4i, Color4i;
//---------------------------------------------------------------------------------------------
typedef union _Vector2b {
struct { BYTE x, y; };
BYTE arr[2];
} Vector2b;
//---------------------------------------------------------------------------------------------
typedef union _Vector3b {
struct { BYTE x, y, z; };
struct { BYTE r, g, b; };
BYTE arr[3];
} Vector3b, Color3b;
//---------------------------------------------------------------------------------------------
typedef union _Vector4b {
struct { BYTE x, y, z, w; };
struct { BYTE r, g, b, a; };
BYTE arr[4];
} Vector4b, Color4b;
//---------------------------------------------------------------------------------------------
typedef union _Matrix33f {
struct { float m1, m2, m3,
m4, m5, m6,
m7, m8, m9; };
float arr[9];
float m[3][3];
} Matrix33f;
//---------------------------------------------------------------------------------------------
typedef union _Matrix44f {
struct { float m1, m2, m3, m4,
m5, m6, m7, m8,
m9, m10, m11, m12,
m13, m14, m15, m16; };
struct { float rs[12]; Vector4f translation; };
float arr[16];
float m[4][4];
__m128 mm[4];
} Matrix44f;
//---------------------------------------------------------------------------------------------
typedef union _Rect4i {
struct { int left, right, top, bottom; };
int arr[4];
} Rect4i;
//---------------------------------------------------------------------------------------------
typedef union _Rect6i {
struct { int left, right, top, bottom,
width, height; };
int arr[6];
} Rect6i;
//---------------------------------------------------------------------------------------------
typedef union _Rect4f {
struct { float xmin, xmax, ymin, ymax; };
float arr[4];
} Rect4f;
//---------------------------------------------------------------------------------------------
typedef union _Rect6f {
struct { float xmin, xmax, ymin, ymax,
width, height; };
float arr[6];
} Rect6f;
//---------------------------------------------------------------------------------------------
typedef union _Range2f {
struct { float min, max; };
float arr[2];
bool cover( float _s );
} Range2f;
//---------------------------------------------------------------------------------------------
typedef union _Bound6f {
struct { float xmin, xmax, ymin, ymax, zmin, zmax; };
struct { Range2f xrange, yrange, zrange; };
float arr[6];
void normalize();
void operator += ( const _Bound6f & b ) {
if( xmin > b.xmin ) xmin = b.xmin;
if( xmax < b.xmax ) xmax = b.xmax;
if( ymin > b.ymin ) ymin = b.ymin;
if( ymax < b.ymax ) ymax = b.ymax;
if( zmin > b.zmin ) zmin = b.zmin;
if( zmax < b.zmax ) zmax = b.zmax;
}
void operator += ( const Vector3f & v ) {
if( xmin > v.x ) xmin = v.x;
if( xmax < v.x ) xmax = v.x;
if( ymin > v.y ) ymin = v.y;
if( ymax < v.y ) ymax = v.y;
if( zmin > v.z ) zmin = v.z;
if( zmax < v.z ) zmax = v.z;
}
void operator = ( const Vector3f & v ) {
xmax = xmin = v.x;
ymax = ymin = v.y;
zmax = zmin = v.z;
}
} Bound6f;
//---------------------------------------------------------------------------------------------
typedef union _Bound9f {
struct { float xmin, xmax, ymin, ymax, zmin, zmax,
width, height, length; };
struct { Bound6f box; float width, height, length; };
float arr[9];
void normalize();
} Bound9f;
//---------------------------------------------------------------------------------------------
typedef union _MemoryHeader {
unsigned int index;
_MemoryHeader *next;
} MemoryHeader;
//---------------------------------------------------------------------------------------------
# ifdef _DEBUG
# ifdef new
# undef new
# endif
# endif
class MemoryManager {
public:
MemoryManager();
virtual ~MemoryManager();
static void new_page( unsigned int size );
#ifndef _DEBUG
void *operator new( size_t );
void *operator new[]( size_t );
void operator delete( void * );
void operator delete[]( void * );
#else
void *operator new( size_t, const char *, int );
void *operator new[]( size_t, const char *, int );
void operator delete( void *, const char *, int );
void operator delete[]( void *, const char *, int );
#endif
void *operator new( size_t, void *p ) { return p; }
void operator delete( void *, void * ) {}
public:
static MemoryHeader *memoryChunks[ NUM_CHUNKS ];
static unsigned char *memoryPage;
static unsigned int memoryAvailable;
static unsigned int memoryUsage;
static MemoryHeader *memoryAllPages;
static int memoryManagerInited;
};
# ifdef _DEBUG
# ifndef new
# define new new( __FILE__, __LINE__ )
# endif
# endif
//---------------------------------------------------------------------------------------------
# include "templates.h"
//---------------------------------------------------------------------------------------------
class Entity : public MemoryManager {
public:
Entity() {
memset( name, 0, MAX_NAME_LEN );
}
virtual ~Entity() {
}
public:
Name name;
DECLARE_TYPE( EntityType )
};
//---------------------------------------------------------------------------------------------
class Reference : public Entity {
public:
Reference() {
}
virtual ~Reference() {
}
VIRTUAL void inherit( const Matrix44f & )/* = 0 */;
};
//---------------------------------------------------------------------------------------------
class Instance : public MemoryManager {
public:
Instance();
~Instance();
void instantiate( Scene *, const Matrix44f & );
public:
Object *object;
Light *light;
};
//---------------------------------------------------------------------------------------------
template <typename T>
class Property {
public:
Property() {
}
~Property() {
}
DLLPORT T &operator[]( int _i ) {
if( _i < 1 )
return k[0];
else
return k[1];
}
void init( float _s, float _begin, float _len ) {
k[1] -= k[0];
T d = k[1];
d *= _begin;
k[0] += d;
k[1] *= _len * _s;
}
void move() {
k[0] += k[1];
}
private:
T k[2];
};
//---------------------------------------------------------------------------------------------
# include "imagefile.h"
//---------------------------------------------------------------------------------------------
typedef enum _TextureFormat {
NONE, RAW, BMP, JPG, TIF,
} TextureFormat;
//---------------------------------------------------------------------------------------------
typedef enum _TextureType {
SRC, MIP_MAP,
} TextureType;
//---------------------------------------------------------------------------------------------
typedef struct _TextureInfo {
FileName name;
TextureType type;
int id;
} TextureInfo;
//---------------------------------------------------------------------------------------------
class TextureTile : public MemoryManager {
public:
TextureTile();
~TextureTile();
bool load( ImageFile *imgFile, int x1, int y1, int x2, int y2, TextureType t );
bool loaded();
int get_width() const;
int get_height() const;
void get_pixel( Vector3f & r, float x, float y, float dx, float dy );
void get_pixel( float & r, float x, float y, float dx, float dy );
private:
bool scale( int width, int height );
void normalize();
void get_color( Vector3b & r, BYTE *dibbits, int x, int y );
void get_color( BYTE & alpha, BYTE *dibbits, int x, int y );
void get_bilinear_color( Vector3f & r, BYTE *dibbits, float x, float y );
void get_bilinear_color( float & r, BYTE *dibbits, float x, float y );
void get_src_color( Vector3b & color, int x, int y );
void get_src_color( BYTE & alpha, int x, int y );
void get_src_bilinear_color( Vector3f & color, float x, float y );
void get_src_bilinear_color( float & alpha, float x, float y );
void blt( int x1, int y1, int x2, int y2,
int width, int height );
void bltB( int x1, int y1, int x2, int y2,
int width, int height );
void bltG( int x1, int y1, int x2, int y2,
int width, int height );
void bltR( int x1, int y1, int x2, int y2,
int width, int height );
void bltA( BYTE *begin, int width, int height );
bool make_mip_map();
float get_mm_bilinear( float x, float y );
float get_mm_bilinear( BYTE *begin, float x, float y, int width );
void get_pixel( Vector3f & clr, float x, float y, int d );
void get_pixel( float & clr, float x, float y, int d );
private:
BYTE *bits, *Abits, *mip_map, *Amip_map;
int m_width, m_height, m_width1, m_height1, m_area;
TextureType type;
};
//---------------------------------------------------------------------------------------------
class Texture : public Entity {
public:
Texture( TextureInfo * );
~Texture();
bool load();
int get_width() const;
int get_height() const;
void lookup( Vector3f & r, const Vector4f & uv );
void lookup( float & r, const Vector4f & uv );
private:
TextureInfo *info;
TextureFormat format;
Buffer<TextureTile> *tiles;
int m_width, m_height, m_width1, m_height1;
ImageFile *imgFile;
};
//---------------------------------------------------------------------------------------------
class TextureHandler {
public:
TextureHandler();
~TextureHandler();
bool load( int id );
void release();
DLLPORT void lookup( Vector3f & r, const Vector4f & uv );
DLLPORT void lookup( float & r, const Vector4f & uv );
private:
Texture *texture;
};
//---------------------------------------------------------------------------------------------
class Light : public Reference {
public:
Light();
virtual ~Light();
virtual void precompute();
DLLPORT VIRTUAL void illuminate( RayState * );
virtual void inherit( const Matrix44f & );
virtual void attrib_copy( const Light * );
public:
bool visible;
Vector3f i;
Vector3f pos;
bool shadowCast,
useAttenuation;
Vector3f attenuation;
float shadowOpacity;
// global illumination.
float giEnergy, giExponent;
UINT giPhotonNum;
// caustics.
float cstEnergy, cstExponent;
UINT cstPhotonNum;
LightShaderInstance *lightShader;
EmitterShaderInstance *emitterShader;
};
//---------------------------------------------------------------------------------------------
class PointLight : public Light {
public:
PointLight();
~PointLight();
void inherit( const Matrix44f & );
void precompute();
DLLPORT void illuminate( RayState * );
};
//---------------------------------------------------------------------------------------------
class DistantLight : public Light {
public:
DistantLight();
~DistantLight();
void inherit( const Matrix44f & );
void precompute();
DLLPORT void illuminate( RayState * );
void attrib_copy( const Light * );
public:
Vector3f dir;
float nearRadius, farRadius, outBright;
float inv_radius;
};
//---------------------------------------------------------------------------------------------
class SpotLight : public Light {
public:
SpotLight();
~SpotLight();
void inherit( const Matrix44f & );
void precompute();
DLLPORT void illuminate( RayState * );
void attrib_copy( const Light * );
public:
Vector3f dir;
float nearAngle, farAngle, outBright;
float near_ang, far_ang, inv_ang;
};
//---------------------------------------------------------------------------------------------
typedef SmartPtr<Light> LightPtr;
typedef SmartPtr<Group> GroupPtr;
typedef SmartPtr<Object> ObjectPtr;
typedef SmartPtr<RayObject> RayObjectPtr;
//---------------------------------------------------------------------------------------------
class Group : public Reference {
public:
Group();
~Group();
void translate( const Vector3f & );
void rotate( const Vector3f & );
void scale( const Vector3f & );
void inherit( const Matrix44f & );
void inherit( Scene *scene, const Matrix44f & );
void push_matrix( const Matrix44f & );
void apply_transform( Scene *scene );
bool find( const char *f_nam, Group* &grp );
bool erase( const char *f_nam );
void erase_dynamic_children();
public:
Vector3f pos;
Vector3f x_axis, y_axis, z_axis;
std::vector<GroupPtr> children;
Group *parent;
Instance *instance;
};
//---------------------------------------------------------------------------------------------
class Primitive : public MemoryManager {
public:
Primitive();
virtual ~Primitive();
public:
Object *object;
};
//---------------------------------------------------------------------------------------------
class NudeVertex {
public:
Vector3f pos;
Vector2f uv;
};
//---------------------------------------------------------------------------------------------
class AnimatableNudeVertex {
public:
Property<Vector3f> pos;
Property<Vector2f> uv;
};
//---------------------------------------------------------------------------------------------
typedef class Vertex : public AnimatableNudeVertex {
public:
Property<Vector3f> normal;
} *VertexPtr;
//---------------------------------------------------------------------------------------------
class RayVertex : public NudeVertex {
public:
Vector3f normal;
void operator = ( Vertex & vtx ) {
uv = vtx.uv[0];
}
};
//---------------------------------------------------------------------------------------------
class Triangle : public Primitive {
public:
Triangle();
~Triangle();
public:
UINT v[3];
Property<Vector3f> normal;
};
//---------------------------------------------------------------------------------------------
typedef union _HitType {
int type;
RayState *state;
} HitType;
//---------------------------------------------------------------------------------------------
class RayPrimitive : public MemoryManager {
public:
RayPrimitive();
virtual ~RayPrimitive();
VIRTUAL bool intersect( Ray *ray, RayState *state )/* = 0*/;
VIRTUAL void subdivide( RayObjectPtr & MObj, RayObjectPtr & LObj,
RayObjectPtr & RObj, bool *isExist,
UINT axis, float plane )/* = 0*/;
VIRTUAL void detail( RayState *state )/* = 0*/;
virtual void precompute() = 0;
virtual void bound() = 0;
virtual void boundDisplaced();
virtual float priority() = 0;
virtual void freeTemp();
void operator()( RayState *state );
public:
DECLARE_TYPE( RayPrimitiveType )
Primitive *primitive;
RayObject *object;
Bound6f box;
HitType hitType;
int hitHistory;
USHORT hitTime;
};
//---------------------------------------------------------------------------------------------
typedef struct _DisplacedTriangle {
RayVertex *v1, *v2, *v3;
} DisplacedTriangle;
//---------------------------------------------------------------------------------------------
typedef struct _DisplacedCell {
bool intersect( Ray *ray, RayState *state );
void precompute();
DisplacedTriangle tri[ DISPLACED_CELL_SIZE ];
__m128 lax, lay, laz, law,
lbx, lby, lbz, lbw,
lcx, lcy, lcz, lcw,
nx, ny, nz, nw;
Vector4f sphere;
} DisplacedCell;
//---------------------------------------------------------------------------------------------
class DisplacementData : public MemoryManager {
public:
DisplacementData();
~DisplacementData();
public:
RayVertex *dpVtx;
DisplacedCell *dpGrid;
float tolerance;
int noEdgeVtx, noDpVtx, noDpTri,
noDpCell, dpLevel, refCount;
};
//---------------------------------------------------------------------------------------------
class RayTriangle : public RayPrimitive {
public:
RayTriangle();
~RayTriangle();
bool intersect( Ray *ray, RayState *state );
void subdivide( RayObjectPtr & MObj, RayObjectPtr & LObj,
RayObjectPtr & RObj, bool *isExist,
UINT axis, float plane );
void detail( RayState *state );
void precompute();
void bound();
void boundDisplaced();
float priority();
void freeTemp();
private:
bool baseIntersect( Ray *ray, RayState *state );
bool intersectDisplaced( Ray *ray, RayState *state );
void tessellate();
void displace( DisplacementShaderInstance *shader, RayState *state );
bool ray_box_1d( float p1, float p2, float p3, float start, float end );
bool ray_box( const Vector3f & start, const Vector3f & end );
void getWrap( const Vector3f &, const Vector3f &, const Vector3f & );
void getWrap( const Vector3f &, const Vector3f &,
const Vector3f &, const Vector3f & );
void getWrap( const Vector3f &, const Vector3f &,
const Vector3f &, const Vector3f &,
const Vector3f &, const Vector3f & );
void edgeXplane( float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float & y3, float & z3 );
public:
UnalignedVector4f la, lb, lc, normal;
FakePtr<RayVertex> v1, v2, v3;
Vector3f uv_para1, uv_para2, uv_para3;
SafePtr<DisplacementData> dpd;
};
//---------------------------------------------------------------------------------------------
class Object : public Reference {
public:
Object();
virtual ~Object();
virtual void inherit( const Matrix44f & ) = 0;
virtual void initialize( const _Option & ) = 0;
virtual RayObject *newRayObject( const Matrix44f & ) = 0;
virtual void move() = 0;
virtual void clearDynamicData();
public:
Material *material;
bool visible;
bool rebuild_normals;
float shadingRate, maxDisplace;
int displacePeriod;
UINT mbSegment;
// global illumination.
bool giRecv, giCast;
// caustics.
bool cstRecv, cstCast;
// shadow.
bool shadowRecv, shadowCast;
};
//---------------------------------------------------------------------------------------------
class Polygon : public Object {
public:
Polygon();
~Polygon();
void inherit( const Matrix44f & );
void initialize( const _Option & );
RayObject *newRayObject( const Matrix44f & );
void move();
public:
std::vector<Vertex> vtxlist;
std::vector<Triangle> prilist;
};
//---------------------------------------------------------------------------------------------
class RayObject : public Entity {
public:
RayObject();
virtual ~RayObject();
VIRTUAL void illumIndirect( RayState *state );
VIRTUAL RayPrimitive *pri_at( int id )/* = 0*/;
VIRTUAL int pri_size()/* = 0*/;
VIRTUAL void pri_add( const RayPrimitive * )/* = 0*/;
VIRTUAL void pri_clear()/* = 0*/;
VIRTUAL void pri_copy( const RayObject * )/* = 0*/;
virtual void attrib_copy( const RayObject * ) = 0;
virtual void bound() = 0;
virtual void sort() = 0;
DLLPORT void operator()( RayState *state );
public:
Object *object;
Bound9f box;
PhotonMap *giMap, *cstMap;
Range2f time;
};
//---------------------------------------------------------------------------------------------
class RayPolygon : public RayObject {
public:
RayPolygon();
~RayPolygon();
void illumIndirect( RayState *state );
RayPrimitive *pri_at( int id );
int pri_size();
void pri_add( const RayPrimitive * );
void pri_clear();
void pri_copy( const RayObject * );
void attrib_copy( const RayObject * );
void bound();
void sort();
private:
std::vector<RayTriangle> prilist;
};
//---------------------------------------------------------------------------------------------
class BSPNode : public MemoryManager {
public:
BSPNode();
~BSPNode();
public:
BSPNode *LChild,
*RChild;
std::vector<RayObjectPtr> pObj;
UINT priCount;
std::vector<RayPrimitive*> refPri;
USHORT axis;
Bound6f box;
#ifdef _DEBUG
Vector3f color;
#endif
};
//---------------------------------------------------------------------------------------------
class Material : public Entity {
public:
Material();
~Material();
void precompute();
public:
SurfaceShaderInstance *surfaceShader;
ShadowShaderInstance *shadowShader;
DisplacementShaderInstance *displaceShader;
PhotonShaderInstance *photonShader;
bool twoFaced,
smooth;
};
//---------------------------------------------------------------------------------------------
class Camera : public Reference {
public:
Camera();
~Camera();
void inherit( const Matrix44f & );
void initialize( const _Option & );
void move();
void prepare( Vector3f &, Vector3f &, Vector3f &, Vector3f &,
Vector3f &, Vector3f &, int, int );
public:
Property<Vector3f> pos, tgt;
Vector3f dir, up;
Property<float> angle, roll;
Range2f clip;
UINT mbSegment;
float pixelSize;
};
//---------------------------------------------------------------------------------------------
class Photon {
public:
Vector3f pos, energy;
Light *light;
};
//---------------------------------------------------------------------------------------------
typedef enum _RayType {
RAY_EYE, // eye ray
RAY_TRANSPARENT, // transparency ray
RAY_REFLECT, // reflection ray
RAY_REFRACT, // refraction ray
RAY_LIGHT, // light ray
RAY_SHADOW, // shadow ray
RAY_ENVIRONMENT, // ray only into environment/volume
RAY_NONE, // other ray
PHOTON_ABSORB, // photon is absorbed (RIP)
PHOTON_LIGHT, // photon emitted from a light source
PHOTON_REFLECT_SPECULAR, // specular reflection of a photon
PHOTON_REFLECT_GLOSSY, // glossy reflection of a photon
PHOTON_REFLECT_DIFFUSE, // diffuse reflection of a photon
PHOTON_TRANSMIT_SPECULAR, // specular transmission of a photon
PHOTON_TRANSMIT_GLOSSY, // glossy transmission of a photon
PHOTON_TRANSMIT_DIFFUSE, // diffuse transmission of a photon
RAY_DISPLACE, // displacement during tesselation
RAY_OUTPUT, // output shader
PHOTON_SCATTER_VOLUME, // volume scattering of a photon
PHOTON_TRANSPARENT, // transparency photon
RAY_FINALGATHER, // final gather ray
RAY_LM_VERTEX, // light map vertex rendering
RAY_LM_MESH, // light map mesh rendering
PHOTON_EMIT_GI, // globillum photons (emitters only)
PHOTON_EMIT_CAUSTIC, // caustic photons (emitters only)
RAY_PROBE,
} RayType;
//---------------------------------------------------------------------------------------------
class Ray {
public:
Ray( RayType _t );
~Ray();
public:
Vector3f src, pos, dir, axis;
RayType type;
Photon *photon;
Ray *neighbors[4];
float displace_tol;
int id;
std::list<RayState> potentials;
};
//---------------------------------------------------------------------------------------------
# define QUERY_UV 0x4
# define QUERY_DU_DV 0x8
# define QUERY_NG 0x10
# define QUERY_N 0x20
# define QUERY_DPDU_DPDV 0x40
# define QUERY_BARY 0x80
# define QUERY_DOT_ND 0x100
# define QUERY_GI_LUM 0x200
# define QUERY_SHADOW_COLOR 0x400
# define QUERY_SHADING 0x800
//---------------------------------------------------------------------------------------------
typedef RayObject *Illuminator;
typedef RayPrimitive *GetDetails;
typedef Tracer *TextureLoader;
typedef Light* (*GetLight)( int id );
DLLPORT Light *getLightInstance( int id );
//---------------------------------------------------------------------------------------------
typedef union _CustomData {
float scalar;
int integer;
void *pointer;
} CustomData;
//---------------------------------------------------------------------------------------------
class RayState {
public:
RayState();
~RayState();
public:
// input :
Primitive *limitPri;
Object *limitObj;
BSPNode *limitBSP;
Vector2f rasterPos;
float time;
// output :
int query;
Primitive *hitPri;
Object *hitObj;
Illuminator illumIndirect;
GetDetails detail;
TextureLoader loadTex;
GetLight lightInst;
BSPNode *hitBSP;
float t, dtime;
Vector4f uv, shadowColor;
Vector3f P, Ng, N,
dPdu, dPdv,
bary, L;
float dotNd;
Vector3f I, giLum;
// state :
int depth;
float ior, prev_ior;
struct _Option *opt;
// custom :
CustomData cust_data[ MAX_CUSTOM_DATA_SIZE ];
};
//---------------------------------------------------------------------------------------------
class RayTracer : public MemoryManager {
public:
RayTracer();
~RayTracer();
DLLPORT static float distributed_trace( Vector4f & color, Ray *ray,
int depth, float time, Vector2f & rasterPos, int );
DLLPORT static float trace( Vector4f & color, Ray *ray,
int depth, float time, Vector2f & rasterPos, int );
DLLPORT static void trace( Vector4f & color, Ray *ray,
int depth, float time, Vector2f & rasterPos,
TextureLoader & );
static bool trace( Ray *ray, RayState *state );
static bool intersect( const Vector3f & pos, const Vector3f & dir,
const Bound6f & box, float & rt, Vector3f & hit );
static bool intersect( const Vector3f & pos, const Vector3f & dir,
const Bound6f & box, float & rt );
static bool intersect( const Vector3f & pos, const Vector3f & dir,
const Vector4f & sphere, float DDD );
static bool intersect( Ray *ray, RayState *state );
static bool near_far_clip( const Ray *ray, float t );
static void shade( Vector4f & c, Ray *ray, RayState *state );
static void prepare_intersect( const Vector3f & normal, const Vector3f & v1,
const Vector3f & v2, const Vector3f & v3,
UnalignedVector4f & plane );
static void prepare_uv_dir( const Vector2f & uv1, const Vector2f & uv2,
const Vector2f & uv3, Vector3f & para );
static void get_normal( const Vector3f & v1, const Vector3f & v2,
const Vector3f & v3, UnalignedVector4f & normal );
};
//---------------------------------------------------------------------------------------------
typedef enum _FilterType {
BOX, TRIANGLE, CATMULLROM, GAUSSIAN, SINC,
} FilterType;
//---------------------------------------------------------------------------------------------
typedef struct _Option {
// info
FileName fileName;
short noThread;
Camera view;
// sampling
UINT maxAntialias, // max depth of anti-aliasing
minAntialias; // min depth of anti-aliasing
Vector4f contrast,
timeContrast;
float filterSize;
FilterType filter,
timeFilter;
// parameters
USHORT coord;
UINT bucketSize;
Vector4f backgroundColor;
Vector3f ia;
float ior;
UINT traceDepth; // max depth of raytracing
UINT maxSubdivision;
bool memoryPinch,
diagnostic;
// bsp
USHORT bspMethod;
UINT bspSize;
UINT bspDepth;
// global illumination
bool useGI;
UINT giAccuracy;
float giRadius;
UINT giTraceDepth;
// caustics
bool useCst;
UINT cstAccuracy;
float cstRadius;
UINT cstTraceDepth;
// final gather
bool useFG;
UINT fgAccuracy;
UINT fgTraceDepth;
float fgMaxRadius,
fgMinRadius;
// motion blur
bool useMB;
Range2f mbShutter;
// depth of field
bool useDOF;
float fStop;
// rendering
UINT imageWidth, imageHeight;
} Option;
//---------------------------------------------------------------------------------------------
class Scene {
public:
Scene();
~Scene();
public:
Option opt;
// substance
Group root;
std::vector<ObjectPtr> objects;
std::vector<LightPtr> lights;
std::vector<LightPtr> lightInstances;
std::list<Material> materials;
std::list<PhotonMap> photonMaps;
std::vector<TextureInfo> texInfo;
BSPNode *bsp;
LensShaderInstance *lensShader;
ImagerShaderInstance *imagerShader;
// reserves
Bound9f box;
Vector3f origin[ MAX_MOTIONBLUR_SEGMENT ],
right_dir[ MAX_MOTIONBLUR_SEGMENT ],
down_dir[ MAX_MOTIONBLUR_SEGMENT ],
pixel_right_dir[ MAX_MOTIONBLUR_SEGMENT ],
pixel_down_dir[ MAX_MOTIONBLUR_SEGMENT ],
sdir[ MAX_MOTIONBLUR_SEGMENT ];
};
//---------------------------------------------------------------------------------------------
class PixelBuffer : public MemoryManager {
public:
PixelBuffer( int w, int h );
~PixelBuffer();
int getWidth() const;
int getHeight() const;
void set( int x, int y, const BYTE *clr );
void set( int x, int y, const Vector3f & clr );
void set( int x, int y, COLORREF clr );
void set4( int x, int y, const BYTE *clr );
void set4( int x, int y, const Vector4f & clr );
void add( int x, int y, const BYTE *clr );
void add( int x, int y, const Vector3f & clr );
void add4( int x, int y, const BYTE *clr );
void add4( int x, int y, const Vector4f & clr );
void mix4( int x, int y, const BYTE *clr, float t );
void mix4( int x, int y, const Vector4f & clr, float t );
void mul( int x, int y, float f );
void mul4( int x, int y, float f );
void get( BYTE *clr, int x, int y ) const;
void get( Vector3f & clr, int x, int y ) const;
void get4( BYTE *clr, int x, int y ) const;
void get4( Vector4f & clr, int x, int y ) const;
void gamma( BYTE *dst, const Vector3f & src ) const;
void gamma( BYTE *dst, const Vector4f & src ) const;
void fill( int x1, int x2, int y1, int y2, const Vector3f & clr );
bool filled( int x, int y );
bool saveBmp( const char *file_name, int frame_id, int w, int h ) const;
BYTE *getHandle() const;
private:
BYTE *bitmap, *Abitmap;
int m_width, m_height, m_width1, m_height1;
};
//---------------------------------------------------------------------------------------------
class Sampler : public MemoryManager {
public:
Sampler();
~Sampler();
void init( float pixel_size, FilterType filter );
void jitter();
void reg( int x, int y, float & sx, float & sy, float & time );
float getWeight( int x, int y );
int getFilterRadius();
private:
typedef struct _SamplePosition {
float x, y;
float jx, jy;
} SamplePosition;
SamplePosition table[9][9];
float **weights;
int filter_radius;
};
//---------------------------------------------------------------------------------------------
class Locker {
public:
Locker();
~Locker();
bool lock();
bool unlock();
static void time_out( DWORD _limit );
private:
bool entered;
TMutex mutex;
};
//---------------------------------------------------------------------------------------------
class Mender : public MemoryManager {
public:
Mender();
virtual ~Mender();
public:
static Locker g_locker, buckets_locker,
pBuffer_locker, dofBuffer_locker, scene_locker;
};
//---------------------------------------------------------------------------------------------
class Bucket : public Mender {
public:
Bucket();
~Bucket();
void init( int x, int y, PixelBuffer *b, Buffer<BYTE> *d, Rect6i *r );
void release();
void render();
void supersample( const Vector4f & avg,
const Vector4f & c1, const Vector4f & c2,
const Vector4f & c3, const Vector4f & c4,
const Rect4i & rct, USHORT depth );
BYTE sample( Vector4f & c, int x, int y, USHORT depth );
void timeSample( Vector4f & result, float & totalWeight, float & confusion,
const Vector4f & c1, const Vector4f & c2,
const Vector3f & org1, const Vector3f & org2,
const Vector3f & dir1, const Vector3f & dir2,
const Vector3f & axis1, const Vector3f & axis2,
float time1, float time2, float displace_tol,
Vector2f & rasterPos, USHORT depth );
bool compare( const Vector4f & c1, const Vector4f & c2 );
bool timeCompare( const Vector4f & c1, const Vector4f & c2 );
void average( Vector4f & avg,
const Vector4f & c1, const Vector4f & c2,
const Vector4f & c3, const Vector4f & c4 );
void contribute( int x, int y, int tx, int ty );
void setRasterPos( int x, int y );
public:
PixelBuffer *pBuffer, *gBuffer;
Buffer<BYTE> *dofBuffer;
Vector2i pos, l_rasterPos;
Vector2f rasterPos;
Rect6i rect;
bool caught;
Sampler sampler;
Scene *scene;
int thread;
};
//---------------------------------------------------------------------------------------------
class Tracer : public Mender {
public:
Tracer();
~Tracer();
void setID( int id );
int getID();
void walk();
void catchBucket();
void findBucket();
void trace();
void pause();
void resume();
void stop();
void releaseTextures();
static TThreadSpec proc( LPVOID lpthis );
TextureHandler *loadTexture( int id );
DLLPORT TextureHandler *operator()( int id ) {
return loadTexture( id );
}
public:
typedef enum _Step {
RIGHT, DOWN, LEFT, UP,
} Step;
Bucket *target;
Step lastStep;
TThread thread;
DWORD threadID;
bool stopped,
isRunning;
std::vector<TextureHandler> textureCache;
};
//---------------------------------------------------------------------------------------------
typedef enum _OSType {
Windows32s, WindowsNT3, Windows95, Windows98,
WindowsME, WindowsNT4, Windows2000, WindowsXP,
} OSType;
//---------------------------------------------------------------------------------------------
# include "random.h"
//---------------------------------------------------------------------------------------------
class Renderer : public Mender {
public:
DLLPORT Renderer();
DLLPORT virtual ~Renderer();
void begin();
void pause();
void resume();
void end();
void print( int msg_id, );
void registerPixels(int count);
BOOL post( UINT Msg, WPARAM wParam, LPARAM lParam );
void getSysInfo();
void getOSType( OSType & );
bool isRightOS();
void initScene();
void wrapScene();
void stuffBSP();
void buildBSP( BSPNode *curNode, USHORT method, UINT curDepth, UINT curAxis );
void initBSP( BSPNode *node );
void sortBSP( BSPNode *node );
void register_displaced_primitive( RayPrimitive *prim,
BSPNode *node, BSPNode *exclude );
void renderPhotonMap();
void buildKdtree();
void precompute();
void wait();
void release();
void clearDynamicData();
void dispatch( TThread & thread, int _id_ );
Bucket *getBucket( int x, int y );
public:
Bucket **buckets;
Tracer tracers[ MAX_THREAD_AMOUNT ];
TThread threads[ MAX_THREAD_AMOUNT ];
Scene *scene;
int noXBuckets, noYBuckets;
PixelBuffer *pBuffer;
float bias;
float div_100_render_area;
UINT pixel_count;
int last_percent;
UINT frameID;
int id_count;
char appPath[ MAX_PATH_LEN ];
// random generators
CSobol<4> traceGen, giGen, fgGen;
};
//---------------------------------------------------------------------------------------------
typedef enum _RenderState {
RENDERING, NOT_RENDERING, PAUSE,
} RenderState;
//---------------------------------------------------------------------------------------------
// _FLOAT :
# define FILTER_SIZE 100
# define REFRACTIVE_INDEX 101
# define GLOBALILLUM_RADIUS 102
# define CAUSTICS_RADIUS 103
# define FINALGATHER_MAX_RADIUS 104
# define FINALGATHER_MIN_RADIUS 105
# define F_STOP 106
# define CAMERA_ANGLE 107
# define CAMERA_ROLL 108
# define SHADING_RATE 109
# define MAX_DISPLACEMENT 110
# define GLOBALILLUM_ENERGY 111
# define GLOBALILLUM_DECAY 112
# define CAUSTICS_ENERGY 113
# define CAUSTICS_DECAY 114
# define SHADOW_OPACITY 115
# define OUTSIDE_BRIGHTNESS 116
# define RADIUS 117
// _INTEGER :
# define THREAD_NUMBER 200
# define MAX_ANTIALIASING 201
# define MIN_ANTIALIASING 202
# define BUCKET_SIZE 203
# define TRACE_DEPTH 204
# define MAX_SUBDIVISION 205
# define BSP_SIZE 206
# define BSP_DEPTH 207
# define GLOBALILLUM_ACCURACY 208
# define GLOBALILLUM_TRACE_DEPTH 209
# define CAUSTICS_ACCURACY 210
# define CAUSTICS_TRACE_DEPTH 211
# define FINALGATHER_ACCURACY 212
# define FINALGATHER_TRACE_DEPTH 213
# define MOTIONBLUR_SEGMENT 214
# define DISPLACEMENT_PERIOD 215
# define GLOBALILLUM_PHOTON_NUMBER 216
# define CAUSTICS_PHOTON_NUMBER 217
# define VERTEX_BUFFER_SIZE 218
# define TRIANGLE_BUFFER_SIZE 219
# define IMAGE_WIDTH 220
# define IMAGE_HEIGHT 221
# define SEGMENT 222
// _STRING :
# define NAME 300
# define FILE_NAME 301
# define FILTER 302
# define TIME_FILTER 303
# define BSP_METHOD 304
# define SURFACE_SHADER 305
# define LIGHT_SHADER 306
# define SHADOW_SHADER 307
# define DISPLACEMENT_SHADER 308
# define PHOTON_SHADER 309
# define EMITTER_SHADER 310
# define VOLUME_SHADER 311
# define COORDINATE_SYSTEM 312
# define MATERIAL 313
# define LENS_SHADER 314
# define IMAGER_SHADER 315
// _BOOLEAN :
# define MEMORY_PINCH 400
# define ENABLE_GLOBALILLUM 401
# define ENABLE_CAUSTICS 402
# define ENABLE_FINALGATHER 403
# define ENABLE_MOTIONBLUR 404
# define ENABLE_DEPTHOFFIELD 405
# define RECEIVE_GLOBALILLUM 406
# define CAST_GLOBALILLUM 407
# define RECEIVE_CAUSTICS 408
# define CAST_CAUSTICS 409
# define RECEIVE_SHADOW 410
# define CAST_SHADOW 411
# define VISIBLE 412
# define REBUILD_NORMALS 413
# define TWO_FACED 414
# define SMOOTH 415
# define ENABLE_ATTENUATION 416
# define DIAGNOSTIC_MODE 417
// _VECTOR2:
# define SHUTTER 500
# define CLIP 501
# define DISTANTLIGHT_RADIUS 502
# define SPOTLIGHT_ANGLE 503
// _VECTOR3 :
# define AMBIENT_COLOR 600
# define CAMERA_POSITION 601
# define CAMERA_TARGET 602
# define ILLUMINATION 603
# define ATTENUATION 604
// _VECTOR4 :
# define CONTRAST 700
# define TIME_CONTRAST 701
# define BACKGROUND_COLOR 702
//---------------------------------------------------------------------------------------------
typedef enum _PrimitiveType {
TRIANGLES,
} PrimitiveType;
//---------------------------------------------------------------------------------------------
class Interface : public Renderer {
friend class Parser;
public:
DLLPORT Interface();
DLLPORT ~Interface();
DLLPORT int read_scene( const char *name );
/* 1*/DLLPORT int world_begin();
/* 2*/DLLPORT int world_end();
/* 3*/DLLPORT int frame_begin();
/* 4*/DLLPORT int frame_end();
/* 5*/DLLPORT int group_begin( const char *name );
/* 6*/DLLPORT int group_end();
/* 7*/DLLPORT int init_instance( const char *name );
/* 8*/DLLPORT int object_begin( const char *name, const char *type );
/* 9*/DLLPORT int object_end();
/*10*/DLLPORT int motion_begin();
/*11*/DLLPORT int motion_end();
/*12*/DLLPORT int vertex( float x, float y, float z, float u, float v );
/*13*/DLLPORT int vertex( int id );
/*14*/DLLPORT int normal( float x, float y, float z );
/*15*/DLLPORT int primitive_begin( const char *type );
/*16*/DLLPORT int primitive_end();
/*17*/DLLPORT int texture( const char *name, const char *type );
/*18*/DLLPORT int translate( float x, float y, float z );
/*19*/DLLPORT int rotate( float x, float y, float z );
/*20*/DLLPORT int scale( float x, float y, float z );
/*21*/DLLPORT int material_begin( const char *name );
/*22*/DLLPORT int material_end();
/*23*/DLLPORT int light_begin( const char *name, const char *type );
/*24*/DLLPORT int light_end();
/*25*/DLLPORT int set_option( int name, float value );
/*26*/DLLPORT int set_parameter( const char *name, float value );
/*27*/DLLPORT int set_option( int name, int value );
/*28*/DLLPORT int set_parameter( const char *name, int value );
/*29*/DLLPORT int set_option( int name, const char *value );
/*30*/DLLPORT int set_parameter( const char *name, const char *str );
/*31*/DLLPORT int set_option( int name, bool value );
/*32*/DLLPORT int set_parameter( const char *name, bool value );
/*33*/DLLPORT int set_option( int name, float x, float y );
/*34*/DLLPORT int set_parameter( const char *name, float x, float y );
/*35*/DLLPORT int set_option( int name, float x, float y, float z );
/*36*/DLLPORT int set_parameter( const char *name, float x, float y, float z );
/*37*/DLLPORT int set_option( int name, float x, float y, float z, float w );
/*38*/DLLPORT int set_parameter( const char *name,
float x, float y, float z, float w );
/*39*/DLLPORT int select_group( const char *name );
/*40*/DLLPORT int delete_group( const char *name );
/*41*/DLLPORT int select_object( const char *name );
/*42*/DLLPORT int delete_object( const char *name );
/*43*/DLLPORT int select_material( const char *name );
/*44*/DLLPORT int delete_material( const char *name );
/*45*/DLLPORT int select_light( const char *name );
/*46*/DLLPORT int delete_light( const char *name );
/*47*/DLLPORT int select_end();
DLLPORT int render_pause();
DLLPORT int render_resume();
DLLPORT int render_end();
DLLPORT static void error( int id, char* & desc );
private:
static TThreadSpec loadProc( LPVOID );
private:
TThread mainThread;
bool worldBegin,
frameBegin,
groupBegin,
objectBegin,
motionBegin,
materialBegin,
lightBegin,
primitiveBegin;
RenderState renderState;
Material *pMtl;
Group *pGrp;
Object *pObj;
Light *pLight;
Vertex *pVtx;
Triangle *pTri;
int key, vtx_id;
PrimitiveType primType;
Triangle tri;
};
}
//---------------------------------------------------------------------------------------------
// Eclinse files :
# include "algebra.h"
# include "extern.h"
# include "shader.h"
# include "parser.h"
# include "opengl.h"
# include "window.h"
# include "geometry.h"
# include "photonmap.h"
# include "resource.h"
//---------------------------------------------------------------------------------------------
#endif
Eclinse
Len3d.All Rights Reserved.
---------------------------------------------------------------------------------------------*/
#ifndef _BASE_H
#define _BASE_H
//---------------------------------------------------------------------------------------------
# include "os.h"
# ifdef _BASE_DLL
# define DLLPORT DLLEXPORT
# else
# pragma comment( lib, "base.lib" )
# ifdef _ECLINSE
# define DLLPORT
# else
# define DLLPORT DLLIMPORT
# endif
# endif
# define DECLARE_TYPE( _datatype ) _datatype type;
# ifdef INLINE
# undef INLINE
# endif
# ifdef _DEBUG
# define _CRTDBG_MAP_ALLOC
# include <crtdbg.h>
# include "debugnew.h"
# define INLINE
# define VIRTUAL virtual
# define VIRTUAL_BEGIN( _funcdef ) _funcdef {
# define VOID_DERIVED_CLASS( _class, _enum )
# define DERIVED_CLASS( _class, _enum )
# define VIRTUAL_END }
# else
# //define USING_SSE
# define INLINE //inline
# define VIRTUAL INLINE
# define VIRTUAL_BEGIN( _funcdef ) _funcdef { switch( type ) { default:
# define VOID_DERIVED_CLASS( _class, _enum ) break; case _enum: ((_class*)this)->##FUNCCALL;
# define DERIVED_CLASS( _class, _enum ) break; case _enum: return ((_class*)this)->##FUNCCALL;
# define VIRTUAL_END break; } }
# endif
//---------------------------------------------------------------------------------------------
namespace Eclinse
{
class MemoryManager;
class Entity;
class Reference;
class Instance;
template <typename T> class Property;
class Texture;
class TextureHandler;
class Light;
class PointLight;
class DistantLight;
class SpotLight;
# define Group GroupNode
class Group;
class Primitive;
class NudeVertex;
class AnimatableNudeVertex;
class Vertex;
class TradRayVertex;
class RayVertex;
class Triangle;
class RayPrimitive;
class BaseRayTriangle;
class TradRayTriangle;
class RayTriangle;
class Object;
class Polygon;
class Sphere;
class RayObject;
class TradRayPolygon;
class RayPolygon;
class BSPNode;
class Material;
class Camera;
class Photon;
class Ray;
class RayState;
class RayTracer;
class Scene;
class PixelBuffer;
template <typename T> class Buffer;
class Sampler;
class Mender;
class Bucket;
class Tracer;
class Renderer;
class Interface;
class Variable;
class ShaderInstance;
class SurfaceShaderInstance;
class LightShaderInstance;
class ShadowShaderInstance;
class DisplacementShaderInstance;
class PhotonShaderInstance;
class EmitterShaderInstance;
class VolumeShaderInstance;
class LensShaderInstance;
class ImagerShaderInstance;
class MainWindow;
class PhotonMap;
class Parser;
struct _Option;
//---------------------------------------------------------------------------------------------
typedef union _Vector2f {
struct { float x, y; };
struct { float u, v; };
float arr[2];
void operator += ( const _Vector2f & b ) {
x += b.x; y += b.y;
}
void operator -= ( const _Vector2f & b ) {
x -= b.x; y -= b.y;
}
void operator *= ( float s ) {
x *= s; y *= s;
}
} Vector2f;
//---------------------------------------------------------------------------------------------
typedef union _Vector3f {
struct { float x, y, z; };
struct { float r, g, b; };
float arr[3];
void operator += ( const _Vector3f & b ) {
x += b.x; y += b.y; z += b.z;
}
void operator -= ( const _Vector3f & b ) {
x -= b.x; y -= b.y; z -= b.z;
}
void operator *= ( float s ) {
x *= s; y *= s; z *= s;
}
} Vector3f, Color3f;
//---------------------------------------------------------------------------------------------
typedef union _Vector4f {
struct { float x, y, z, w; };
struct { float r, g, b, a; };
struct { Vector3f abc; float d; };
struct { Vector2f uv; float du, dv; };
float arr[4];
__m128 mm;
void operator += ( const _Vector4f & b ) {
x += b.x; y += b.y; z += b.z; w += b.w;
}
void operator -= ( const _Vector4f & b ) {
x -= b.x; y -= b.y; z -= b.z; w -= b.w;
}
void operator *= ( float s ) {
x *= s; y *= s; z *= s; w *= s;
}
} Vector4f, Color4f;
//---------------------------------------------------------------------------------------------
typedef union _UnalignedVector4f {
struct { float x, y, z, w; };
struct { Vector3f abc; float d; };
float arr[4];
void operator = ( const Vector4f & b ) {
arr[0] = b.arr[0]; arr[1] = b.arr[1]; arr[2] = b.arr[2]; arr[3] = b.arr[3];
}
Vector4f aligned() {
Vector4f r;
r.arr[0] = arr[0]; r.arr[1] = arr[1]; r.arr[2] = arr[2]; r.arr[3] = arr[3];
return r;
}
} UnalignedVector4f;
//---------------------------------------------------------------------------------------------
typedef union _Vector2i {
struct { int x, y; };
int arr[2];
} Vector2i;
//---------------------------------------------------------------------------------------------
typedef union _Vector3i {
struct { int x, y, z; };
struct { int r, g, b; };
int arr[3];
} Vector3i, Color3i;
//---------------------------------------------------------------------------------------------
typedef union _Vector4i {
struct { int x, y, z, w; };
struct { int r, g, b, a; };
int arr[4];
} Vector4i, Color4i;
//---------------------------------------------------------------------------------------------
typedef union _Vector2b {
struct { BYTE x, y; };
BYTE arr[2];
} Vector2b;
//---------------------------------------------------------------------------------------------
typedef union _Vector3b {
struct { BYTE x, y, z; };
struct { BYTE r, g, b; };
BYTE arr[3];
} Vector3b, Color3b;
//---------------------------------------------------------------------------------------------
typedef union _Vector4b {
struct { BYTE x, y, z, w; };
struct { BYTE r, g, b, a; };
BYTE arr[4];
} Vector4b, Color4b;
//---------------------------------------------------------------------------------------------
typedef union _Matrix33f {
struct { float m1, m2, m3,
m4, m5, m6,
m7, m8, m9; };
float arr[9];
float m[3][3];
} Matrix33f;
//---------------------------------------------------------------------------------------------
typedef union _Matrix44f {
struct { float m1, m2, m3, m4,
m5, m6, m7, m8,
m9, m10, m11, m12,
m13, m14, m15, m16; };
struct { float rs[12]; Vector4f translation; };
float arr[16];
float m[4][4];
__m128 mm[4];
} Matrix44f;
//---------------------------------------------------------------------------------------------
typedef union _Rect4i {
struct { int left, right, top, bottom; };
int arr[4];
} Rect4i;
//---------------------------------------------------------------------------------------------
typedef union _Rect6i {
struct { int left, right, top, bottom,
width, height; };
int arr[6];
} Rect6i;
//---------------------------------------------------------------------------------------------
typedef union _Rect4f {
struct { float xmin, xmax, ymin, ymax; };
float arr[4];
} Rect4f;
//---------------------------------------------------------------------------------------------
typedef union _Rect6f {
struct { float xmin, xmax, ymin, ymax,
width, height; };
float arr[6];
} Rect6f;
//---------------------------------------------------------------------------------------------
typedef union _Range2f {
struct { float min, max; };
float arr[2];
bool cover( float _s );
} Range2f;
//---------------------------------------------------------------------------------------------
typedef union _Bound6f {
struct { float xmin, xmax, ymin, ymax, zmin, zmax; };
struct { Range2f xrange, yrange, zrange; };
float arr[6];
void normalize();
void operator += ( const _Bound6f & b ) {
if( xmin > b.xmin ) xmin = b.xmin;
if( xmax < b.xmax ) xmax = b.xmax;
if( ymin > b.ymin ) ymin = b.ymin;
if( ymax < b.ymax ) ymax = b.ymax;
if( zmin > b.zmin ) zmin = b.zmin;
if( zmax < b.zmax ) zmax = b.zmax;
}
void operator += ( const Vector3f & v ) {
if( xmin > v.x ) xmin = v.x;
if( xmax < v.x ) xmax = v.x;
if( ymin > v.y ) ymin = v.y;
if( ymax < v.y ) ymax = v.y;
if( zmin > v.z ) zmin = v.z;
if( zmax < v.z ) zmax = v.z;
}
void operator = ( const Vector3f & v ) {
xmax = xmin = v.x;
ymax = ymin = v.y;
zmax = zmin = v.z;
}
} Bound6f;
//---------------------------------------------------------------------------------------------
typedef union _Bound9f {
struct { float xmin, xmax, ymin, ymax, zmin, zmax,
width, height, length; };
struct { Bound6f box; float width, height, length; };
float arr[9];
void normalize();
} Bound9f;
//---------------------------------------------------------------------------------------------
typedef union _MemoryHeader {
unsigned int index;
_MemoryHeader *next;
} MemoryHeader;
//---------------------------------------------------------------------------------------------
# ifdef _DEBUG
# ifdef new
# undef new
# endif
# endif
class MemoryManager {
public:
MemoryManager();
virtual ~MemoryManager();
static void new_page( unsigned int size );
#ifndef _DEBUG
void *operator new( size_t );
void *operator new[]( size_t );
void operator delete( void * );
void operator delete[]( void * );
#else
void *operator new( size_t, const char *, int );
void *operator new[]( size_t, const char *, int );
void operator delete( void *, const char *, int );
void operator delete[]( void *, const char *, int );
#endif
void *operator new( size_t, void *p ) { return p; }
void operator delete( void *, void * ) {}
public:
static MemoryHeader *memoryChunks[ NUM_CHUNKS ];
static unsigned char *memoryPage;
static unsigned int memoryAvailable;
static unsigned int memoryUsage;
static MemoryHeader *memoryAllPages;
static int memoryManagerInited;
};
# ifdef _DEBUG
# ifndef new
# define new new( __FILE__, __LINE__ )
# endif
# endif
//---------------------------------------------------------------------------------------------
# include "templates.h"
//---------------------------------------------------------------------------------------------
class Entity : public MemoryManager {
public:
Entity() {
memset( name, 0, MAX_NAME_LEN );
}
virtual ~Entity() {
}
public:
Name name;
DECLARE_TYPE( EntityType )
};
//---------------------------------------------------------------------------------------------
class Reference : public Entity {
public:
Reference() {
}
virtual ~Reference() {
}
VIRTUAL void inherit( const Matrix44f & )/* = 0 */;
};
//---------------------------------------------------------------------------------------------
class Instance : public MemoryManager {
public:
Instance();
~Instance();
void instantiate( Scene *, const Matrix44f & );
public:
Object *object;
Light *light;
};
//---------------------------------------------------------------------------------------------
template <typename T>
class Property {
public:
Property() {
}
~Property() {
}
DLLPORT T &operator[]( int _i ) {
if( _i < 1 )
return k[0];
else
return k[1];
}
void init( float _s, float _begin, float _len ) {
k[1] -= k[0];
T d = k[1];
d *= _begin;
k[0] += d;
k[1] *= _len * _s;
}
void move() {
k[0] += k[1];
}
private:
T k[2];
};
//---------------------------------------------------------------------------------------------
# include "imagefile.h"
//---------------------------------------------------------------------------------------------
typedef enum _TextureFormat {
NONE, RAW, BMP, JPG, TIF,
} TextureFormat;
//---------------------------------------------------------------------------------------------
typedef enum _TextureType {
SRC, MIP_MAP,
} TextureType;
//---------------------------------------------------------------------------------------------
typedef struct _TextureInfo {
FileName name;
TextureType type;
int id;
} TextureInfo;
//---------------------------------------------------------------------------------------------
class TextureTile : public MemoryManager {
public:
TextureTile();
~TextureTile();
bool load( ImageFile *imgFile, int x1, int y1, int x2, int y2, TextureType t );
bool loaded();
int get_width() const;
int get_height() const;
void get_pixel( Vector3f & r, float x, float y, float dx, float dy );
void get_pixel( float & r, float x, float y, float dx, float dy );
private:
bool scale( int width, int height );
void normalize();
void get_color( Vector3b & r, BYTE *dibbits, int x, int y );
void get_color( BYTE & alpha, BYTE *dibbits, int x, int y );
void get_bilinear_color( Vector3f & r, BYTE *dibbits, float x, float y );
void get_bilinear_color( float & r, BYTE *dibbits, float x, float y );
void get_src_color( Vector3b & color, int x, int y );
void get_src_color( BYTE & alpha, int x, int y );
void get_src_bilinear_color( Vector3f & color, float x, float y );
void get_src_bilinear_color( float & alpha, float x, float y );
void blt( int x1, int y1, int x2, int y2,
int width, int height );
void bltB( int x1, int y1, int x2, int y2,
int width, int height );
void bltG( int x1, int y1, int x2, int y2,
int width, int height );
void bltR( int x1, int y1, int x2, int y2,
int width, int height );
void bltA( BYTE *begin, int width, int height );
bool make_mip_map();
float get_mm_bilinear( float x, float y );
float get_mm_bilinear( BYTE *begin, float x, float y, int width );
void get_pixel( Vector3f & clr, float x, float y, int d );
void get_pixel( float & clr, float x, float y, int d );
private:
BYTE *bits, *Abits, *mip_map, *Amip_map;
int m_width, m_height, m_width1, m_height1, m_area;
TextureType type;
};
//---------------------------------------------------------------------------------------------
class Texture : public Entity {
public:
Texture( TextureInfo * );
~Texture();
bool load();
int get_width() const;
int get_height() const;
void lookup( Vector3f & r, const Vector4f & uv );
void lookup( float & r, const Vector4f & uv );
private:
TextureInfo *info;
TextureFormat format;
Buffer<TextureTile> *tiles;
int m_width, m_height, m_width1, m_height1;
ImageFile *imgFile;
};
//---------------------------------------------------------------------------------------------
class TextureHandler {
public:
TextureHandler();
~TextureHandler();
bool load( int id );
void release();
DLLPORT void lookup( Vector3f & r, const Vector4f & uv );
DLLPORT void lookup( float & r, const Vector4f & uv );
private:
Texture *texture;
};
//---------------------------------------------------------------------------------------------
class Light : public Reference {
public:
Light();
virtual ~Light();
virtual void precompute();
DLLPORT VIRTUAL void illuminate( RayState * );
virtual void inherit( const Matrix44f & );
virtual void attrib_copy( const Light * );
public:
bool visible;
Vector3f i;
Vector3f pos;
bool shadowCast,
useAttenuation;
Vector3f attenuation;
float shadowOpacity;
// global illumination.
float giEnergy, giExponent;
UINT giPhotonNum;
// caustics.
float cstEnergy, cstExponent;
UINT cstPhotonNum;
LightShaderInstance *lightShader;
EmitterShaderInstance *emitterShader;
};
//---------------------------------------------------------------------------------------------
class PointLight : public Light {
public:
PointLight();
~PointLight();
void inherit( const Matrix44f & );
void precompute();
DLLPORT void illuminate( RayState * );
};
//---------------------------------------------------------------------------------------------
class DistantLight : public Light {
public:
DistantLight();
~DistantLight();
void inherit( const Matrix44f & );
void precompute();
DLLPORT void illuminate( RayState * );
void attrib_copy( const Light * );
public:
Vector3f dir;
float nearRadius, farRadius, outBright;
float inv_radius;
};
//---------------------------------------------------------------------------------------------
class SpotLight : public Light {
public:
SpotLight();
~SpotLight();
void inherit( const Matrix44f & );
void precompute();
DLLPORT void illuminate( RayState * );
void attrib_copy( const Light * );
public:
Vector3f dir;
float nearAngle, farAngle, outBright;
float near_ang, far_ang, inv_ang;
};
//---------------------------------------------------------------------------------------------
typedef SmartPtr<Light> LightPtr;
typedef SmartPtr<Group> GroupPtr;
typedef SmartPtr<Object> ObjectPtr;
typedef SmartPtr<RayObject> RayObjectPtr;
//---------------------------------------------------------------------------------------------
class Group : public Reference {
public:
Group();
~Group();
void translate( const Vector3f & );
void rotate( const Vector3f & );
void scale( const Vector3f & );
void inherit( const Matrix44f & );
void inherit( Scene *scene, const Matrix44f & );
void push_matrix( const Matrix44f & );
void apply_transform( Scene *scene );
bool find( const char *f_nam, Group* &grp );
bool erase( const char *f_nam );
void erase_dynamic_children();
public:
Vector3f pos;
Vector3f x_axis, y_axis, z_axis;
std::vector<GroupPtr> children;
Group *parent;
Instance *instance;
};
//---------------------------------------------------------------------------------------------
class Primitive : public MemoryManager {
public:
Primitive();
virtual ~Primitive();
public:
Object *object;
};
//---------------------------------------------------------------------------------------------
class NudeVertex {
public:
Vector3f pos;
Vector2f uv;
};
//---------------------------------------------------------------------------------------------
class AnimatableNudeVertex {
public:
Property<Vector3f> pos;
Property<Vector2f> uv;
};
//---------------------------------------------------------------------------------------------
typedef class Vertex : public AnimatableNudeVertex {
public:
Property<Vector3f> normal;
} *VertexPtr;
//---------------------------------------------------------------------------------------------
class RayVertex : public NudeVertex {
public:
Vector3f normal;
void operator = ( Vertex & vtx ) {
uv = vtx.uv[0];
}
};
//---------------------------------------------------------------------------------------------
class Triangle : public Primitive {
public:
Triangle();
~Triangle();
public:
UINT v[3];
Property<Vector3f> normal;
};
//---------------------------------------------------------------------------------------------
typedef union _HitType {
int type;
RayState *state;
} HitType;
//---------------------------------------------------------------------------------------------
class RayPrimitive : public MemoryManager {
public:
RayPrimitive();
virtual ~RayPrimitive();
VIRTUAL bool intersect( Ray *ray, RayState *state )/* = 0*/;
VIRTUAL void subdivide( RayObjectPtr & MObj, RayObjectPtr & LObj,
RayObjectPtr & RObj, bool *isExist,
UINT axis, float plane )/* = 0*/;
VIRTUAL void detail( RayState *state )/* = 0*/;
virtual void precompute() = 0;
virtual void bound() = 0;
virtual void boundDisplaced();
virtual float priority() = 0;
virtual void freeTemp();
void operator()( RayState *state );
public:
DECLARE_TYPE( RayPrimitiveType )
Primitive *primitive;
RayObject *object;
Bound6f box;
HitType hitType;
int hitHistory;
USHORT hitTime;
};
//---------------------------------------------------------------------------------------------
typedef struct _DisplacedTriangle {
RayVertex *v1, *v2, *v3;
} DisplacedTriangle;
//---------------------------------------------------------------------------------------------
typedef struct _DisplacedCell {
bool intersect( Ray *ray, RayState *state );
void precompute();
DisplacedTriangle tri[ DISPLACED_CELL_SIZE ];
__m128 lax, lay, laz, law,
lbx, lby, lbz, lbw,
lcx, lcy, lcz, lcw,
nx, ny, nz, nw;
Vector4f sphere;
} DisplacedCell;
//---------------------------------------------------------------------------------------------
class DisplacementData : public MemoryManager {
public:
DisplacementData();
~DisplacementData();
public:
RayVertex *dpVtx;
DisplacedCell *dpGrid;
float tolerance;
int noEdgeVtx, noDpVtx, noDpTri,
noDpCell, dpLevel, refCount;
};
//---------------------------------------------------------------------------------------------
class RayTriangle : public RayPrimitive {
public:
RayTriangle();
~RayTriangle();
bool intersect( Ray *ray, RayState *state );
void subdivide( RayObjectPtr & MObj, RayObjectPtr & LObj,
RayObjectPtr & RObj, bool *isExist,
UINT axis, float plane );
void detail( RayState *state );
void precompute();
void bound();
void boundDisplaced();
float priority();
void freeTemp();
private:
bool baseIntersect( Ray *ray, RayState *state );
bool intersectDisplaced( Ray *ray, RayState *state );
void tessellate();
void displace( DisplacementShaderInstance *shader, RayState *state );
bool ray_box_1d( float p1, float p2, float p3, float start, float end );
bool ray_box( const Vector3f & start, const Vector3f & end );
void getWrap( const Vector3f &, const Vector3f &, const Vector3f & );
void getWrap( const Vector3f &, const Vector3f &,
const Vector3f &, const Vector3f & );
void getWrap( const Vector3f &, const Vector3f &,
const Vector3f &, const Vector3f &,
const Vector3f &, const Vector3f & );
void edgeXplane( float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float & y3, float & z3 );
public:
UnalignedVector4f la, lb, lc, normal;
FakePtr<RayVertex> v1, v2, v3;
Vector3f uv_para1, uv_para2, uv_para3;
SafePtr<DisplacementData> dpd;
};
//---------------------------------------------------------------------------------------------
class Object : public Reference {
public:
Object();
virtual ~Object();
virtual void inherit( const Matrix44f & ) = 0;
virtual void initialize( const _Option & ) = 0;
virtual RayObject *newRayObject( const Matrix44f & ) = 0;
virtual void move() = 0;
virtual void clearDynamicData();
public:
Material *material;
bool visible;
bool rebuild_normals;
float shadingRate, maxDisplace;
int displacePeriod;
UINT mbSegment;
// global illumination.
bool giRecv, giCast;
// caustics.
bool cstRecv, cstCast;
// shadow.
bool shadowRecv, shadowCast;
};
//---------------------------------------------------------------------------------------------
class Polygon : public Object {
public:
Polygon();
~Polygon();
void inherit( const Matrix44f & );
void initialize( const _Option & );
RayObject *newRayObject( const Matrix44f & );
void move();
public:
std::vector<Vertex> vtxlist;
std::vector<Triangle> prilist;
};
//---------------------------------------------------------------------------------------------
class RayObject : public Entity {
public:
RayObject();
virtual ~RayObject();
VIRTUAL void illumIndirect( RayState *state );
VIRTUAL RayPrimitive *pri_at( int id )/* = 0*/;
VIRTUAL int pri_size()/* = 0*/;
VIRTUAL void pri_add( const RayPrimitive * )/* = 0*/;
VIRTUAL void pri_clear()/* = 0*/;
VIRTUAL void pri_copy( const RayObject * )/* = 0*/;
virtual void attrib_copy( const RayObject * ) = 0;
virtual void bound() = 0;
virtual void sort() = 0;
DLLPORT void operator()( RayState *state );
public:
Object *object;
Bound9f box;
PhotonMap *giMap, *cstMap;
Range2f time;
};
//---------------------------------------------------------------------------------------------
class RayPolygon : public RayObject {
public:
RayPolygon();
~RayPolygon();
void illumIndirect( RayState *state );
RayPrimitive *pri_at( int id );
int pri_size();
void pri_add( const RayPrimitive * );
void pri_clear();
void pri_copy( const RayObject * );
void attrib_copy( const RayObject * );
void bound();
void sort();
private:
std::vector<RayTriangle> prilist;
};
//---------------------------------------------------------------------------------------------
class BSPNode : public MemoryManager {
public:
BSPNode();
~BSPNode();
public:
BSPNode *LChild,
*RChild;
std::vector<RayObjectPtr> pObj;
UINT priCount;
std::vector<RayPrimitive*> refPri;
USHORT axis;
Bound6f box;
#ifdef _DEBUG
Vector3f color;
#endif
};
//---------------------------------------------------------------------------------------------
class Material : public Entity {
public:
Material();
~Material();
void precompute();
public:
SurfaceShaderInstance *surfaceShader;
ShadowShaderInstance *shadowShader;
DisplacementShaderInstance *displaceShader;
PhotonShaderInstance *photonShader;
bool twoFaced,
smooth;
};
//---------------------------------------------------------------------------------------------
class Camera : public Reference {
public:
Camera();
~Camera();
void inherit( const Matrix44f & );
void initialize( const _Option & );
void move();
void prepare( Vector3f &, Vector3f &, Vector3f &, Vector3f &,
Vector3f &, Vector3f &, int, int );
public:
Property<Vector3f> pos, tgt;
Vector3f dir, up;
Property<float> angle, roll;
Range2f clip;
UINT mbSegment;
float pixelSize;
};
//---------------------------------------------------------------------------------------------
class Photon {
public:
Vector3f pos, energy;
Light *light;
};
//---------------------------------------------------------------------------------------------
typedef enum _RayType {
RAY_EYE, // eye ray
RAY_TRANSPARENT, // transparency ray
RAY_REFLECT, // reflection ray
RAY_REFRACT, // refraction ray
RAY_LIGHT, // light ray
RAY_SHADOW, // shadow ray
RAY_ENVIRONMENT, // ray only into environment/volume
RAY_NONE, // other ray
PHOTON_ABSORB, // photon is absorbed (RIP)
PHOTON_LIGHT, // photon emitted from a light source
PHOTON_REFLECT_SPECULAR, // specular reflection of a photon
PHOTON_REFLECT_GLOSSY, // glossy reflection of a photon
PHOTON_REFLECT_DIFFUSE, // diffuse reflection of a photon
PHOTON_TRANSMIT_SPECULAR, // specular transmission of a photon
PHOTON_TRANSMIT_GLOSSY, // glossy transmission of a photon
PHOTON_TRANSMIT_DIFFUSE, // diffuse transmission of a photon
RAY_DISPLACE, // displacement during tesselation
RAY_OUTPUT, // output shader
PHOTON_SCATTER_VOLUME, // volume scattering of a photon
PHOTON_TRANSPARENT, // transparency photon
RAY_FINALGATHER, // final gather ray
RAY_LM_VERTEX, // light map vertex rendering
RAY_LM_MESH, // light map mesh rendering
PHOTON_EMIT_GI, // globillum photons (emitters only)
PHOTON_EMIT_CAUSTIC, // caustic photons (emitters only)
RAY_PROBE,
} RayType;
//---------------------------------------------------------------------------------------------
class Ray {
public:
Ray( RayType _t );
~Ray();
public:
Vector3f src, pos, dir, axis;
RayType type;
Photon *photon;
Ray *neighbors[4];
float displace_tol;
int id;
std::list<RayState> potentials;
};
//---------------------------------------------------------------------------------------------
# define QUERY_UV 0x4
# define QUERY_DU_DV 0x8
# define QUERY_NG 0x10
# define QUERY_N 0x20
# define QUERY_DPDU_DPDV 0x40
# define QUERY_BARY 0x80
# define QUERY_DOT_ND 0x100
# define QUERY_GI_LUM 0x200
# define QUERY_SHADOW_COLOR 0x400
# define QUERY_SHADING 0x800
//---------------------------------------------------------------------------------------------
typedef RayObject *Illuminator;
typedef RayPrimitive *GetDetails;
typedef Tracer *TextureLoader;
typedef Light* (*GetLight)( int id );
DLLPORT Light *getLightInstance( int id );
//---------------------------------------------------------------------------------------------
typedef union _CustomData {
float scalar;
int integer;
void *pointer;
} CustomData;
//---------------------------------------------------------------------------------------------
class RayState {
public:
RayState();
~RayState();
public:
// input :
Primitive *limitPri;
Object *limitObj;
BSPNode *limitBSP;
Vector2f rasterPos;
float time;
// output :
int query;
Primitive *hitPri;
Object *hitObj;
Illuminator illumIndirect;
GetDetails detail;
TextureLoader loadTex;
GetLight lightInst;
BSPNode *hitBSP;
float t, dtime;
Vector4f uv, shadowColor;
Vector3f P, Ng, N,
dPdu, dPdv,
bary, L;
float dotNd;
Vector3f I, giLum;
// state :
int depth;
float ior, prev_ior;
struct _Option *opt;
// custom :
CustomData cust_data[ MAX_CUSTOM_DATA_SIZE ];
};
//---------------------------------------------------------------------------------------------
class RayTracer : public MemoryManager {
public:
RayTracer();
~RayTracer();
DLLPORT static float distributed_trace( Vector4f & color, Ray *ray,
int depth, float time, Vector2f & rasterPos, int );
DLLPORT static float trace( Vector4f & color, Ray *ray,
int depth, float time, Vector2f & rasterPos, int );
DLLPORT static void trace( Vector4f & color, Ray *ray,
int depth, float time, Vector2f & rasterPos,
TextureLoader & );
static bool trace( Ray *ray, RayState *state );
static bool intersect( const Vector3f & pos, const Vector3f & dir,
const Bound6f & box, float & rt, Vector3f & hit );
static bool intersect( const Vector3f & pos, const Vector3f & dir,
const Bound6f & box, float & rt );
static bool intersect( const Vector3f & pos, const Vector3f & dir,
const Vector4f & sphere, float DDD );
static bool intersect( Ray *ray, RayState *state );
static bool near_far_clip( const Ray *ray, float t );
static void shade( Vector4f & c, Ray *ray, RayState *state );
static void prepare_intersect( const Vector3f & normal, const Vector3f & v1,
const Vector3f & v2, const Vector3f & v3,
UnalignedVector4f & plane );
static void prepare_uv_dir( const Vector2f & uv1, const Vector2f & uv2,
const Vector2f & uv3, Vector3f & para );
static void get_normal( const Vector3f & v1, const Vector3f & v2,
const Vector3f & v3, UnalignedVector4f & normal );
};
//---------------------------------------------------------------------------------------------
typedef enum _FilterType {
BOX, TRIANGLE, CATMULLROM, GAUSSIAN, SINC,
} FilterType;
//---------------------------------------------------------------------------------------------
typedef struct _Option {
// info
FileName fileName;
short noThread;
Camera view;
// sampling
UINT maxAntialias, // max depth of anti-aliasing
minAntialias; // min depth of anti-aliasing
Vector4f contrast,
timeContrast;
float filterSize;
FilterType filter,
timeFilter;
// parameters
USHORT coord;
UINT bucketSize;
Vector4f backgroundColor;
Vector3f ia;
float ior;
UINT traceDepth; // max depth of raytracing
UINT maxSubdivision;
bool memoryPinch,
diagnostic;
// bsp
USHORT bspMethod;
UINT bspSize;
UINT bspDepth;
// global illumination
bool useGI;
UINT giAccuracy;
float giRadius;
UINT giTraceDepth;
// caustics
bool useCst;
UINT cstAccuracy;
float cstRadius;
UINT cstTraceDepth;
// final gather
bool useFG;
UINT fgAccuracy;
UINT fgTraceDepth;
float fgMaxRadius,
fgMinRadius;
// motion blur
bool useMB;
Range2f mbShutter;
// depth of field
bool useDOF;
float fStop;
// rendering
UINT imageWidth, imageHeight;
} Option;
//---------------------------------------------------------------------------------------------
class Scene {
public:
Scene();
~Scene();
public:
Option opt;
// substance
Group root;
std::vector<ObjectPtr> objects;
std::vector<LightPtr> lights;
std::vector<LightPtr> lightInstances;
std::list<Material> materials;
std::list<PhotonMap> photonMaps;
std::vector<TextureInfo> texInfo;
BSPNode *bsp;
LensShaderInstance *lensShader;
ImagerShaderInstance *imagerShader;
// reserves
Bound9f box;
Vector3f origin[ MAX_MOTIONBLUR_SEGMENT ],
right_dir[ MAX_MOTIONBLUR_SEGMENT ],
down_dir[ MAX_MOTIONBLUR_SEGMENT ],
pixel_right_dir[ MAX_MOTIONBLUR_SEGMENT ],
pixel_down_dir[ MAX_MOTIONBLUR_SEGMENT ],
sdir[ MAX_MOTIONBLUR_SEGMENT ];
};
//---------------------------------------------------------------------------------------------
class PixelBuffer : public MemoryManager {
public:
PixelBuffer( int w, int h );
~PixelBuffer();
int getWidth() const;
int getHeight() const;
void set( int x, int y, const BYTE *clr );
void set( int x, int y, const Vector3f & clr );
void set( int x, int y, COLORREF clr );
void set4( int x, int y, const BYTE *clr );
void set4( int x, int y, const Vector4f & clr );
void add( int x, int y, const BYTE *clr );
void add( int x, int y, const Vector3f & clr );
void add4( int x, int y, const BYTE *clr );
void add4( int x, int y, const Vector4f & clr );
void mix4( int x, int y, const BYTE *clr, float t );
void mix4( int x, int y, const Vector4f & clr, float t );
void mul( int x, int y, float f );
void mul4( int x, int y, float f );
void get( BYTE *clr, int x, int y ) const;
void get( Vector3f & clr, int x, int y ) const;
void get4( BYTE *clr, int x, int y ) const;
void get4( Vector4f & clr, int x, int y ) const;
void gamma( BYTE *dst, const Vector3f & src ) const;
void gamma( BYTE *dst, const Vector4f & src ) const;
void fill( int x1, int x2, int y1, int y2, const Vector3f & clr );
bool filled( int x, int y );
bool saveBmp( const char *file_name, int frame_id, int w, int h ) const;
BYTE *getHandle() const;
private:
BYTE *bitmap, *Abitmap;
int m_width, m_height, m_width1, m_height1;
};
//---------------------------------------------------------------------------------------------
class Sampler : public MemoryManager {
public:
Sampler();
~Sampler();
void init( float pixel_size, FilterType filter );
void jitter();
void reg( int x, int y, float & sx, float & sy, float & time );
float getWeight( int x, int y );
int getFilterRadius();
private:
typedef struct _SamplePosition {
float x, y;
float jx, jy;
} SamplePosition;
SamplePosition table[9][9];
float **weights;
int filter_radius;
};
//---------------------------------------------------------------------------------------------
class Locker {
public:
Locker();
~Locker();
bool lock();
bool unlock();
static void time_out( DWORD _limit );
private:
bool entered;
TMutex mutex;
};
//---------------------------------------------------------------------------------------------
class Mender : public MemoryManager {
public:
Mender();
virtual ~Mender();
public:
static Locker g_locker, buckets_locker,
pBuffer_locker, dofBuffer_locker, scene_locker;
};
//---------------------------------------------------------------------------------------------
class Bucket : public Mender {
public:
Bucket();
~Bucket();
void init( int x, int y, PixelBuffer *b, Buffer<BYTE> *d, Rect6i *r );
void release();
void render();
void supersample( const Vector4f & avg,
const Vector4f & c1, const Vector4f & c2,
const Vector4f & c3, const Vector4f & c4,
const Rect4i & rct, USHORT depth );
BYTE sample( Vector4f & c, int x, int y, USHORT depth );
void timeSample( Vector4f & result, float & totalWeight, float & confusion,
const Vector4f & c1, const Vector4f & c2,
const Vector3f & org1, const Vector3f & org2,
const Vector3f & dir1, const Vector3f & dir2,
const Vector3f & axis1, const Vector3f & axis2,
float time1, float time2, float displace_tol,
Vector2f & rasterPos, USHORT depth );
bool compare( const Vector4f & c1, const Vector4f & c2 );
bool timeCompare( const Vector4f & c1, const Vector4f & c2 );
void average( Vector4f & avg,
const Vector4f & c1, const Vector4f & c2,
const Vector4f & c3, const Vector4f & c4 );
void contribute( int x, int y, int tx, int ty );
void setRasterPos( int x, int y );
public:
PixelBuffer *pBuffer, *gBuffer;
Buffer<BYTE> *dofBuffer;
Vector2i pos, l_rasterPos;
Vector2f rasterPos;
Rect6i rect;
bool caught;
Sampler sampler;
Scene *scene;
int thread;
};
//---------------------------------------------------------------------------------------------
class Tracer : public Mender {
public:
Tracer();
~Tracer();
void setID( int id );
int getID();
void walk();
void catchBucket();
void findBucket();
void trace();
void pause();
void resume();
void stop();
void releaseTextures();
static TThreadSpec proc( LPVOID lpthis );
TextureHandler *loadTexture( int id );
DLLPORT TextureHandler *operator()( int id ) {
return loadTexture( id );
}
public:
typedef enum _Step {
RIGHT, DOWN, LEFT, UP,
} Step;
Bucket *target;
Step lastStep;
TThread thread;
DWORD threadID;
bool stopped,
isRunning;
std::vector<TextureHandler> textureCache;
};
//---------------------------------------------------------------------------------------------
typedef enum _OSType {
Windows32s, WindowsNT3, Windows95, Windows98,
WindowsME, WindowsNT4, Windows2000, WindowsXP,
} OSType;
//---------------------------------------------------------------------------------------------
# include "random.h"
//---------------------------------------------------------------------------------------------
class Renderer : public Mender {
public:
DLLPORT Renderer();
DLLPORT virtual ~Renderer();
void begin();
void pause();
void resume();
void end();
void print( int msg_id, );
void registerPixels(int count);
BOOL post( UINT Msg, WPARAM wParam, LPARAM lParam );
void getSysInfo();
void getOSType( OSType & );
bool isRightOS();
void initScene();
void wrapScene();
void stuffBSP();
void buildBSP( BSPNode *curNode, USHORT method, UINT curDepth, UINT curAxis );
void initBSP( BSPNode *node );
void sortBSP( BSPNode *node );
void register_displaced_primitive( RayPrimitive *prim,
BSPNode *node, BSPNode *exclude );
void renderPhotonMap();
void buildKdtree();
void precompute();
void wait();
void release();
void clearDynamicData();
void dispatch( TThread & thread, int _id_ );
Bucket *getBucket( int x, int y );
public:
Bucket **buckets;
Tracer tracers[ MAX_THREAD_AMOUNT ];
TThread threads[ MAX_THREAD_AMOUNT ];
Scene *scene;
int noXBuckets, noYBuckets;
PixelBuffer *pBuffer;
float bias;
float div_100_render_area;
UINT pixel_count;
int last_percent;
UINT frameID;
int id_count;
char appPath[ MAX_PATH_LEN ];
// random generators
CSobol<4> traceGen, giGen, fgGen;
};
//---------------------------------------------------------------------------------------------
typedef enum _RenderState {
RENDERING, NOT_RENDERING, PAUSE,
} RenderState;
//---------------------------------------------------------------------------------------------
// _FLOAT :
# define FILTER_SIZE 100
# define REFRACTIVE_INDEX 101
# define GLOBALILLUM_RADIUS 102
# define CAUSTICS_RADIUS 103
# define FINALGATHER_MAX_RADIUS 104
# define FINALGATHER_MIN_RADIUS 105
# define F_STOP 106
# define CAMERA_ANGLE 107
# define CAMERA_ROLL 108
# define SHADING_RATE 109
# define MAX_DISPLACEMENT 110
# define GLOBALILLUM_ENERGY 111
# define GLOBALILLUM_DECAY 112
# define CAUSTICS_ENERGY 113
# define CAUSTICS_DECAY 114
# define SHADOW_OPACITY 115
# define OUTSIDE_BRIGHTNESS 116
# define RADIUS 117
// _INTEGER :
# define THREAD_NUMBER 200
# define MAX_ANTIALIASING 201
# define MIN_ANTIALIASING 202
# define BUCKET_SIZE 203
# define TRACE_DEPTH 204
# define MAX_SUBDIVISION 205
# define BSP_SIZE 206
# define BSP_DEPTH 207
# define GLOBALILLUM_ACCURACY 208
# define GLOBALILLUM_TRACE_DEPTH 209
# define CAUSTICS_ACCURACY 210
# define CAUSTICS_TRACE_DEPTH 211
# define FINALGATHER_ACCURACY 212
# define FINALGATHER_TRACE_DEPTH 213
# define MOTIONBLUR_SEGMENT 214
# define DISPLACEMENT_PERIOD 215
# define GLOBALILLUM_PHOTON_NUMBER 216
# define CAUSTICS_PHOTON_NUMBER 217
# define VERTEX_BUFFER_SIZE 218
# define TRIANGLE_BUFFER_SIZE 219
# define IMAGE_WIDTH 220
# define IMAGE_HEIGHT 221
# define SEGMENT 222
// _STRING :
# define NAME 300
# define FILE_NAME 301
# define FILTER 302
# define TIME_FILTER 303
# define BSP_METHOD 304
# define SURFACE_SHADER 305
# define LIGHT_SHADER 306
# define SHADOW_SHADER 307
# define DISPLACEMENT_SHADER 308
# define PHOTON_SHADER 309
# define EMITTER_SHADER 310
# define VOLUME_SHADER 311
# define COORDINATE_SYSTEM 312
# define MATERIAL 313
# define LENS_SHADER 314
# define IMAGER_SHADER 315
// _BOOLEAN :
# define MEMORY_PINCH 400
# define ENABLE_GLOBALILLUM 401
# define ENABLE_CAUSTICS 402
# define ENABLE_FINALGATHER 403
# define ENABLE_MOTIONBLUR 404
# define ENABLE_DEPTHOFFIELD 405
# define RECEIVE_GLOBALILLUM 406
# define CAST_GLOBALILLUM 407
# define RECEIVE_CAUSTICS 408
# define CAST_CAUSTICS 409
# define RECEIVE_SHADOW 410
# define CAST_SHADOW 411
# define VISIBLE 412
# define REBUILD_NORMALS 413
# define TWO_FACED 414
# define SMOOTH 415
# define ENABLE_ATTENUATION 416
# define DIAGNOSTIC_MODE 417
// _VECTOR2:
# define SHUTTER 500
# define CLIP 501
# define DISTANTLIGHT_RADIUS 502
# define SPOTLIGHT_ANGLE 503
// _VECTOR3 :
# define AMBIENT_COLOR 600
# define CAMERA_POSITION 601
# define CAMERA_TARGET 602
# define ILLUMINATION 603
# define ATTENUATION 604
// _VECTOR4 :
# define CONTRAST 700
# define TIME_CONTRAST 701
# define BACKGROUND_COLOR 702
//---------------------------------------------------------------------------------------------
typedef enum _PrimitiveType {
TRIANGLES,
} PrimitiveType;
//---------------------------------------------------------------------------------------------
class Interface : public Renderer {
friend class Parser;
public:
DLLPORT Interface();
DLLPORT ~Interface();
DLLPORT int read_scene( const char *name );
/* 1*/DLLPORT int world_begin();
/* 2*/DLLPORT int world_end();
/* 3*/DLLPORT int frame_begin();
/* 4*/DLLPORT int frame_end();
/* 5*/DLLPORT int group_begin( const char *name );
/* 6*/DLLPORT int group_end();
/* 7*/DLLPORT int init_instance( const char *name );
/* 8*/DLLPORT int object_begin( const char *name, const char *type );
/* 9*/DLLPORT int object_end();
/*10*/DLLPORT int motion_begin();
/*11*/DLLPORT int motion_end();
/*12*/DLLPORT int vertex( float x, float y, float z, float u, float v );
/*13*/DLLPORT int vertex( int id );
/*14*/DLLPORT int normal( float x, float y, float z );
/*15*/DLLPORT int primitive_begin( const char *type );
/*16*/DLLPORT int primitive_end();
/*17*/DLLPORT int texture( const char *name, const char *type );
/*18*/DLLPORT int translate( float x, float y, float z );
/*19*/DLLPORT int rotate( float x, float y, float z );
/*20*/DLLPORT int scale( float x, float y, float z );
/*21*/DLLPORT int material_begin( const char *name );
/*22*/DLLPORT int material_end();
/*23*/DLLPORT int light_begin( const char *name, const char *type );
/*24*/DLLPORT int light_end();
/*25*/DLLPORT int set_option( int name, float value );
/*26*/DLLPORT int set_parameter( const char *name, float value );
/*27*/DLLPORT int set_option( int name, int value );
/*28*/DLLPORT int set_parameter( const char *name, int value );
/*29*/DLLPORT int set_option( int name, const char *value );
/*30*/DLLPORT int set_parameter( const char *name, const char *str );
/*31*/DLLPORT int set_option( int name, bool value );
/*32*/DLLPORT int set_parameter( const char *name, bool value );
/*33*/DLLPORT int set_option( int name, float x, float y );
/*34*/DLLPORT int set_parameter( const char *name, float x, float y );
/*35*/DLLPORT int set_option( int name, float x, float y, float z );
/*36*/DLLPORT int set_parameter( const char *name, float x, float y, float z );
/*37*/DLLPORT int set_option( int name, float x, float y, float z, float w );
/*38*/DLLPORT int set_parameter( const char *name,
float x, float y, float z, float w );
/*39*/DLLPORT int select_group( const char *name );
/*40*/DLLPORT int delete_group( const char *name );
/*41*/DLLPORT int select_object( const char *name );
/*42*/DLLPORT int delete_object( const char *name );
/*43*/DLLPORT int select_material( const char *name );
/*44*/DLLPORT int delete_material( const char *name );
/*45*/DLLPORT int select_light( const char *name );
/*46*/DLLPORT int delete_light( const char *name );
/*47*/DLLPORT int select_end();
DLLPORT int render_pause();
DLLPORT int render_resume();
DLLPORT int render_end();
DLLPORT static void error( int id, char* & desc );
private:
static TThreadSpec loadProc( LPVOID );
private:
TThread mainThread;
bool worldBegin,
frameBegin,
groupBegin,
objectBegin,
motionBegin,
materialBegin,
lightBegin,
primitiveBegin;
RenderState renderState;
Material *pMtl;
Group *pGrp;
Object *pObj;
Light *pLight;
Vertex *pVtx;
Triangle *pTri;
int key, vtx_id;
PrimitiveType primType;
Triangle tri;
};
}
//---------------------------------------------------------------------------------------------
// Eclinse files :
# include "algebra.h"
# include "extern.h"
# include "shader.h"
# include "parser.h"
# include "opengl.h"
# include "window.h"
# include "geometry.h"
# include "photonmap.h"
# include "resource.h"
//---------------------------------------------------------------------------------------------
#endif