1 #pragma once
2 
3 #ifndef Y_SCENE_H
4 #define Y_SCENE_H
5 
6 #include <yafray_constants.h>
7 #include <utilities/threadUtils.h>
8 
9 #define USER_DATA_SIZE 1024
10 
11 // Object flags
12 
13 // Lower order byte indicates type
14 #define TRIM		0x0000
15 #define VTRIM		0x0001
16 #define MTRIM		0x0002
17 
18 // Higher order byte indicates options
19 #define INVISIBLEM	0x0100
20 #define BASEMESH	0x0200
21 
22 __BEGIN_YAFRAY
23 class scene_t;
24 class camera_t;
25 class material_t;
26 class object3d_t;
27 class triangleObject_t;
28 class meshObject_t;
29 class surfacePoint_t;
30 class ray_t;
31 class diffRay_t;
32 class primitive_t;
33 class triKdTree_t;
34 template<class T> class kdTree_t;
35 class triangle_t;
36 class background_t;
37 class light_t;
38 class surfaceIntegrator_t;
39 class volumeIntegrator_t;
40 class imageFilm_t;
41 class random_t;
42 class renderEnvironment_t;
43 class VolumeRegion;
44 class renderPasses_t;
45 class matrix4x4_t;
46 class color_t;
47 enum intPassTypes_t : int;
48 
49 typedef unsigned int objID_t;
50 
51 /*!
52 	\var renderState_t::wavelength
53 		the range is defined going from 400nm (0.0) to 700nm (1.0)
54 		although the widest range humans can perceive is ofteb given 380-780nm.
55 */
56 struct YAFRAYCORE_EXPORT renderState_t
57 {
renderState_trenderState_t58 	renderState_t():raylevel(0), currentPass(0), pixelSample(0), rayDivision(1), rayOffset(0), dc1(0), dc2(0),
59 		traveled(0.0), chromatic(true), includeLights(false), userdata(nullptr), lightdata(nullptr), prng(nullptr) {};
renderState_trenderState_t60 	renderState_t(random_t *rand):raylevel(0), currentPass(0), pixelSample(0), rayDivision(1), rayOffset(0), dc1(0), dc2(0),
61 		traveled(0.0), chromatic(true), includeLights(false), userdata(nullptr), lightdata(nullptr), prng(rand) {};
~renderState_trenderState_t62 	~renderState_t(){};
63 
64 	int raylevel;
65 	float depth;
66 	float contribution; //?
67 	const void *skipelement;
68 	int currentPass;
69 	int pixelSample; //!< number of samples inside this pixels so far
70 	int rayDivision; //!< keep track of trajectory splitting
71 	int rayOffset; //!< keep track of trajectory splitting
72 	float dc1, dc2; //!< used to decorrelate samples from trajectory splitting
73 	float traveled;
74 	int pixelNumber;
75 	int threadID; //!< identify the current render thread; shall range from 0 to scene_t::getNumThreads() - 1
76 	unsigned int samplingOffs; //!< a "noise-like" pixel offset you may use to decorelate sampling of adjacent pixel.
77 	//point3d_t screenpos; //!< the image coordinates of the pixel being computed currently
78 	const camera_t *cam;
79 	bool chromatic; //!< indicates wether the full spectrum is calculated (true) or only a single wavelength (false).
80 	bool includeLights; //!< indicate that emission of materials assiciated to lights shall be included, for correctly visible lights etc.
81 	float wavelength; //!< the (normalized) wavelength being used when chromatic is false.
82 	float time; //!< the current (normalized) frame time
83 	mutable void *userdata; //!< a fixed amount of memory where materials may keep data to avoid recalculations...really need better memory management :(
84 	void *lightdata; //!< reserved; non-dirac lights may do some surface-point dependant initializations in the future to reduce redundancy...
85 	random_t *const prng; //!< a pseudorandom number generator
86 
87 	//! set some initial values that are always the same before integrating a primary ray
setDefaultsrenderState_t88 	void setDefaults()
89 	{
90 		raylevel = 0;
91 		chromatic = true;
92 		rayDivision = 1;
93 		rayOffset = 0;
94 		dc1 = dc2 = 0.f;
95 		traveled = 0;
96 	}
97 
98 //	protected:
renderState_trenderState_t99 	explicit renderState_t(const renderState_t &r):prng(r.prng) {}//forbiden
100 };
101 
102 __END_YAFRAY
103 
104 #include "bound.h"
105 #include <vector>
106 #include <map>
107 #include <list>
108 
109 #define Y_SIG_ABORT 1
110 #define Y_SIG_PAUSE 1<<1
111 #define Y_SIG_STOP  1<<2
112 
113 __BEGIN_YAFRAY
114 
115 /*! describes an instance of a scene, including all data and functionality to
116 	create and render a whole scene on the lowest "layer".
117 	Allocating, configuring and deallocating scene elements etc. however has
118 	to be performed on the next layer, scene_t only knows the base classes.
119 	Exception are triangle meshes, which are created by scene_t.
120 	This implementation currently only supports triangle meshes as geometry,
121 	for general geometric primitives there will most likely be a separate class
122 	to keep this one as optimized as possible;
123 */
124 struct objData_t
125 {
126 	triangleObject_t *obj;
127 	meshObject_t *mobj;
128 	int type;
129 	size_t lastVertId;
130 };
131 
132 struct sceneGeometryState_t
133 {
134 	std::list<unsigned int> stack;
135 	unsigned int changes;
136 	objID_t nextFreeID;
137 	objData_t *curObj;
138 	triangle_t *curTri;
139 	bool orco;
140 	float smooth_angle;
141 };
142 
143 class YAFRAYCORE_EXPORT scene_t
144 {
145 	public:
146 		scene_t(const renderEnvironment_t *render_environment);
147 		~scene_t();
148 		explicit scene_t(const scene_t &s);
149 		bool render();
150 		void abort();
151 		bool startGeometry();
152 		bool endGeometry();
153 		bool startTriMesh(objID_t id, int vertices, int triangles, bool hasOrco, bool hasUV=false, int type=0, int object_pass_index=0);
154 		bool endTriMesh();
155 		bool startCurveMesh(objID_t id, int vertices, int object_pass_index=0);
156 		bool endCurveMesh(const material_t *mat, float strandStart, float strandEnd, float strandShape);
157 		int  addVertex(const point3d_t &p);
158 		int  addVertex(const point3d_t &p, const point3d_t &orco);
159 		void addNormal(const normal_t &n);
160 		bool addTriangle(int a, int b, int c, const material_t *mat);
161 		bool addTriangle(int a, int b, int c, int uv_a, int uv_b, int uv_c, const material_t *mat);
162 		int  addUV(float u, float v);
163 		bool startVmap(int id, int type, int dimensions);
164 		bool endVmap();
165 		bool addVmapValues(float *val);
166 		bool smoothMesh(objID_t id, float angle);
167 		bool update();
168 
169 		bool addLight(light_t *l);
170 		bool addMaterial(material_t *m, const char* name);
171 		objID_t getNextFreeID();
172 		bool addObject(object3d_t *obj, objID_t &id);
173         bool addInstance(objID_t baseObjectId, const matrix4x4_t &objToWorld);
addVolumeRegion(VolumeRegion * vr)174 		void addVolumeRegion(VolumeRegion* vr) { volumes.push_back(vr); }
175 		void setCamera(camera_t *cam);
176 		void setImageFilm(imageFilm_t *film);
177 		void setBackground(background_t *bg);
178 		void setSurfIntegrator(surfaceIntegrator_t *s);
getSurfIntegrator()179 		surfaceIntegrator_t* getSurfIntegrator() const { return surfIntegrator; }
180 		void setVolIntegrator(volumeIntegrator_t *v);
181 		void setAntialiasing(int numSamples, int numPasses, int incSamples, double threshold, float resampled_floor, float sample_multiplier_factor, float light_sample_multiplier_factor, float indirect_sample_multiplier_factor, bool detect_color_noise, int dark_detection_type, float dark_threshold_factor, int variance_edge_size, int variance_pixels, float clamp_samples, float clamp_indirect);
182 		void setNumThreads(int threads);
183 		void setNumThreadsPhotons(int threads_photons);
setMode(int m)184 		void setMode(int m){ mode = m; }
185 		background_t* getBackground() const;
186 		triangleObject_t* getMesh(objID_t id) const;
187 		object3d_t* getObject(objID_t id) const;
getVolumes()188 		std::vector<VolumeRegion*> getVolumes() const { return volumes; }
getCamera()189 		const camera_t* getCamera() const { return camera; }
getImageFilm()190 		imageFilm_t* getImageFilm() const { return imageFilm; }
191 		bound_t getSceneBound() const;
getNumThreads()192 		int getNumThreads() const { return nthreads; }
getNumThreadsPhotons()193 		int getNumThreadsPhotons() const { return nthreads_photons; }
194 		int getSignals() const;
195 		//! only for backward compatibility!
196 		void getAAParameters(int &samples, int &passes, int &inc_samples, float &threshold, float &resampled_floor, float &sample_multiplier_factor, float &light_sample_multiplier_factor, float &indirect_sample_multiplier_factor, bool &detect_color_noise, int &dark_detection_type, float &dark_threshold_factor, int &variance_edge_size, int &variance_pixels, float &clamp_samples, float &clamp_indirect) const;
197 		bool intersect(const ray_t &ray, surfacePoint_t &sp) const;
198 		bool intersect(const diffRay_t &ray, surfacePoint_t &sp) const;
199 		bool isShadowed(renderState_t &state, const ray_t &ray, float &obj_index, float &mat_index) const;
200 		bool isShadowed(renderState_t &state, const ray_t &ray, int maxDepth, color_t &filt, float &obj_index, float &mat_index) const;
201 		const renderPasses_t* getRenderPasses() const;
202 		bool pass_enabled(intPassTypes_t intPassType) const;
203 
204 		enum sceneState { READY, GEOMETRY, OBJECT, VMAP };
205 		enum changeFlags { C_NONE=0, C_GEOM=1, C_LIGHT= 1<<1, C_OTHER=1<<2,
206 							C_ALL=C_GEOM|C_LIGHT|C_OTHER };
207 
208 		std::vector<light_t *> lights;
209 		volumeIntegrator_t *volIntegrator;
210 
211 		float shadowBias;  //shadow bias to apply to shadows to avoid self-shadow artifacts
212 		bool shadowBiasAuto;  //enable automatic shadow bias calculation
213 
214 		float rayMinDist;  //ray minimum distance
215 		bool rayMinDistAuto;  //enable automatic ray minimum distance calculation
216 
217 	protected:
218 
219 		sceneGeometryState_t state;
220 		std::map<objID_t, object3d_t *> objects;
221 		std::map<objID_t, objData_t> meshes;
222         std::map< std::string, material_t * > materials;
223 		std::vector<VolumeRegion *> volumes;
224         camera_t *camera;
225 		imageFilm_t *imageFilm;
226 		triKdTree_t *tree; //!< kdTree for triangle-only mode
227 		kdTree_t<primitive_t> *vtree; //!< kdTree for universal mode
228 		background_t *background;
229 		surfaceIntegrator_t *surfIntegrator;
230 		bound_t sceneBound; //!< bounding box of all (finite) scene geometry
231 
232 		int AA_samples, AA_passes;
233 		int AA_inc_samples; //!< sample count for additional passes
234 		float AA_threshold;
235 		float AA_resampled_floor; //!< minimum amount of resampled pixels (% of the total pixels) below which we will automatically decrease the AA_threshold value for the next pass
236 		float AA_sample_multiplier_factor;
237 		float AA_light_sample_multiplier_factor;
238 		float AA_indirect_sample_multiplier_factor;
239 		bool AA_detect_color_noise;
240 		int AA_dark_detection_type;
241 		float AA_dark_threshold_factor;
242 		int AA_variance_edge_size;
243 		int AA_variance_pixels;
244 		float AA_clamp_samples;
245 		float AA_clamp_indirect;
246 		int nthreads;
247 		int nthreads_photons;
248 		int mode; //!< sets the scene mode (triangle-only, virtual primitives)
249 		int signals;
250 		const renderEnvironment_t *env;	//!< reference to the environment to which this scene belongs to
251 		mutable std::mutex sig_mutex;
252 };
253 
254 __END_YAFRAY
255 
256 #endif // Y_SCENE_H
257