1 
2 #include <interface/yafrayinterface.h>
3 #include <yafray_config.h>
4 #include <core_api/logging.h>
5 #include <core_api/session.h>
6 #include <core_api/environment.h>
7 #include <core_api/scene.h>
8 #include <core_api/imagefilm.h>
9 #include <core_api/params.h>
10 #include <signal.h>
11 
12 #ifdef WIN32
13 	#include <windows.h>
14 #endif
15 
16 __BEGIN_YAFRAY
17 
18 scene_t *globalScene = nullptr;
19 
20 #ifdef WIN32
ctrl_c_handler(DWORD signal)21 BOOL WINAPI ctrl_c_handler(DWORD signal) {
22 	if(globalScene)
23 	{
24 		globalScene->abort();
25 		session.setStatusRenderAborted();
26 		Y_WARNING << "Interface: Render aborted by user." << yendl;
27 	}
28 	else
29 	{
30 		session.setStatusRenderAborted();
31 		Y_WARNING << "Interface: Render aborted by user." << yendl;
32 		exit(1);
33 	}
34     return TRUE;
35 }
36 #else
ctrl_c_handler(int signal)37 void ctrl_c_handler(int signal)
38 {
39 	if(globalScene)
40 	{
41 		globalScene->abort();
42 		session.setStatusRenderAborted();
43 		Y_WARNING << "Interface: Render aborted by user." << yendl;
44 	}
45 	else
46 	{
47 		session.setStatusRenderAborted();
48 		Y_WARNING << "Interface: Render aborted by user." << yendl;
49 		exit(1);
50 	}
51 }
52 #endif
53 
yafrayInterface_t()54 yafrayInterface_t::yafrayInterface_t(): scene(nullptr), film(nullptr), inputGamma(1.f), inputColorSpace(RAW_MANUAL_GAMMA)
55 {
56 	//handle CTRL+C events
57 #ifdef WIN32
58 	SetConsoleCtrlHandler(ctrl_c_handler, true);
59 #else
60 	struct sigaction signalHandler;
61 	signalHandler.sa_handler = ctrl_c_handler;
62 	sigemptyset(&signalHandler.sa_mask);
63 	signalHandler.sa_flags = 0;
64 	sigaction(SIGINT, &signalHandler, nullptr);
65 #endif
66 
67 	env = new renderEnvironment_t();
68 	params = new paraMap_t;
69 	eparams = new std::list<paraMap_t>;
70 	cparams = params;
71 }
72 
~yafrayInterface_t()73 yafrayInterface_t::~yafrayInterface_t()
74 {
75 	Y_VERBOSE << "Interface: Deleting scene..." << yendl;
76 	if(scene) delete scene;
77 	Y_VERBOSE << "Interface: Deleting environment..." << yendl;
78 	if(env) delete env;
79 	Y_INFO << "Interface: Done." << yendl;
80 	if(film) delete film;
81 	delete params;
82 	delete eparams;
83 	yafLog.clearAll();
84 }
85 
loadPlugins(const char * path)86 void yafrayInterface_t::loadPlugins(const char *path)
87 {
88 	if(path != nullptr)
89 	{
90 		std::string plugPath(path);
91 		if(plugPath.empty()) env->getPluginPath(plugPath);
92 		env->loadPlugins(plugPath);
93 	}
94 	else
95 	{
96 		std::string plugPath;
97 		if( env->getPluginPath(plugPath) )
98 		{
99 			env->loadPlugins(plugPath);
100 		}
101 	}
102 }
103 
104 
clearAll()105 void yafrayInterface_t::clearAll()
106 {
107 	Y_VERBOSE << "Interface: Cleaning environment..." << yendl;
108 	env->clearAll();
109 	Y_VERBOSE << "Interface: Deleting scene..." << yendl;
110 	if(scene) delete scene;
111 	Y_VERBOSE << "Interface: Clearing film and parameter maps scene..." << yendl;
112 	scene = nullptr;//new scene_t();
113 	if(film) delete film;
114 	film = nullptr;
115 	params->clear();
116 	eparams->clear();
117 	cparams = params;
118 	Y_VERBOSE << "Interface: Cleanup done." << yendl;
119 }
120 
startScene(int type)121 bool yafrayInterface_t::startScene(int type)
122 {
123 	if(scene) delete scene;
124 	scene = new scene_t(env);
125 	globalScene = scene;	//for the CTRL+C handler
126 	scene->setMode(type);
127 	env->setScene(scene);
128 	return true;
129 }
130 
setLoggingAndBadgeSettings()131 bool yafrayInterface_t::setLoggingAndBadgeSettings()
132 {
133 	env->setupLoggingAndBadge(*params);
134 	return true;
135 }
136 
setupRenderPasses()137 bool yafrayInterface_t::setupRenderPasses()
138 {
139 	env->setupRenderPasses(*params);
140 	return true;
141 }
142 
setInteractive(bool interactive)143 bool yafrayInterface_t::setInteractive(bool interactive)
144 {
145 	session.setInteractive(interactive);
146 	return true;
147 }
148 
startGeometry()149 bool yafrayInterface_t::startGeometry() { return scene->startGeometry(); }
150 
endGeometry()151 bool yafrayInterface_t::endGeometry() { return scene->endGeometry(); }
152 
getNextFreeID()153 unsigned int yafrayInterface_t::getNextFreeID() {
154 	objID_t id;
155 	id = scene->getNextFreeID();
156 	return id;
157 }
158 
startTriMesh(unsigned int id,int vertices,int triangles,bool hasOrco,bool hasUV,int type,int obj_pass_index)159 bool yafrayInterface_t::startTriMesh(unsigned int id, int vertices, int triangles, bool hasOrco, bool hasUV, int type, int obj_pass_index)
160 {
161 	bool success = scene->startTriMesh(id, vertices, triangles, hasOrco, hasUV, type, obj_pass_index);
162 	return success;
163 }
164 
startCurveMesh(unsigned int id,int vertices,int obj_pass_index)165 bool yafrayInterface_t::startCurveMesh(unsigned int id, int vertices, int obj_pass_index)
166 {
167         bool success = scene->startCurveMesh(id, vertices, obj_pass_index);
168         return success;
169 }
170 
171 
startTriMeshPtr(unsigned int * id,int vertices,int triangles,bool hasOrco,bool hasUV,int type,int obj_pass_index)172 bool yafrayInterface_t::startTriMeshPtr(unsigned int *id, int vertices, int triangles, bool hasOrco, bool hasUV, int type, int obj_pass_index)
173 {
174 	Y_WARNING << "Interface: This method is going to be removed, please use getNextFreeID() and startTriMesh() for trimesh generation" << yendl;
175 	objID_t _id;
176 	_id = scene->getNextFreeID();
177 	if ( _id > 0 )
178 	{
179 		bool success = scene->startTriMesh(_id, vertices, triangles, hasOrco, hasUV, type, obj_pass_index);
180 		*id = _id;
181 		return success;
182 	}
183 	else
184 	{
185 		return false;
186 	}
187 }
188 
endTriMesh()189 bool yafrayInterface_t::endTriMesh() { return scene->endTriMesh(); }
endCurveMesh(const material_t * mat,float strandStart,float strandEnd,float strandShape)190 bool yafrayInterface_t::endCurveMesh(const material_t *mat, float strandStart, float strandEnd, float strandShape) { return scene->endCurveMesh(mat, strandStart, strandEnd, strandShape); }
191 
addVertex(double x,double y,double z)192 int  yafrayInterface_t::addVertex(double x, double y, double z) { return scene->addVertex( point3d_t(x,y,z) ); }
193 
addVertex(double x,double y,double z,double ox,double oy,double oz)194 int  yafrayInterface_t::addVertex(double x, double y, double z, double ox, double oy, double oz)
195 {
196 	return scene->addVertex(point3d_t(x,y,z), point3d_t(ox,oy,oz));
197 }
198 
addNormal(double x,double y,double z)199 void yafrayInterface_t::addNormal(double x, double y, double z)
200 {
201 	scene->addNormal( normal_t(x,y,z) );
202 }
203 
addTriangle(int a,int b,int c,const material_t * mat)204 bool yafrayInterface_t::addTriangle(int a, int b, int c, const material_t *mat) { return scene->addTriangle(a, b, c, mat); }
205 
addTriangle(int a,int b,int c,int uv_a,int uv_b,int uv_c,const material_t * mat)206 bool yafrayInterface_t::addTriangle(int a, int b, int c, int uv_a, int uv_b, int uv_c, const material_t *mat)
207 {
208 	return scene->addTriangle(a, b, c, uv_a, uv_b, uv_c, mat);
209 }
210 
addUV(float u,float v)211 int yafrayInterface_t::addUV(float u, float v) { return scene->addUV(u, v); }
212 
smoothMesh(unsigned int id,double angle)213 bool yafrayInterface_t::smoothMesh(unsigned int id, double angle) { return scene->smoothMesh(id, angle); }
214 
addInstance(unsigned int baseObjectId,matrix4x4_t objToWorld)215 bool yafrayInterface_t::addInstance(unsigned int baseObjectId, matrix4x4_t objToWorld)
216 {
217 	return scene->addInstance(baseObjectId, objToWorld);
218 }
219 // paraMap_t related functions:
paramsSetPoint(const char * name,double x,double y,double z)220 void yafrayInterface_t::paramsSetPoint(const char* name, double x, double y, double z)
221 {
222 	(*cparams)[std::string(name)] = parameter_t(point3d_t(x,y,z));
223 }
224 
paramsSetString(const char * name,const char * s)225 void yafrayInterface_t::paramsSetString(const char* name, const char* s)
226 {
227 	(*cparams)[std::string(name)] = parameter_t(std::string(s));
228 }
229 
paramsSetBool(const char * name,bool b)230 void yafrayInterface_t::paramsSetBool(const char* name, bool b)
231 {
232 	(*cparams)[std::string(name)] = parameter_t(b);
233 }
234 
paramsSetInt(const char * name,int i)235 void yafrayInterface_t::paramsSetInt(const char* name, int i)
236 {
237 	(*cparams)[std::string(name)] = parameter_t(i);
238 }
239 
paramsSetFloat(const char * name,double f)240 void yafrayInterface_t::paramsSetFloat(const char* name, double f)
241 {
242 	(*cparams)[std::string(name)] = parameter_t(f);
243 }
244 
paramsSetColor(const char * name,float r,float g,float b,float a)245 void yafrayInterface_t::paramsSetColor(const char* name, float r, float g, float b, float a)
246 {
247 	colorA_t col(r,g,b,a);
248 	col.linearRGB_from_ColorSpace(inputColorSpace, inputGamma);
249 	(*cparams)[std::string(name)] = parameter_t(col);
250 }
251 
paramsSetColor(const char * name,float * rgb,bool with_alpha)252 void yafrayInterface_t::paramsSetColor(const char* name, float *rgb, bool with_alpha)
253 {
254 	colorA_t col(rgb[0],rgb[1],rgb[2], (with_alpha ? rgb[3] : 1.f));
255 	col.linearRGB_from_ColorSpace(inputColorSpace, inputGamma);
256 	(*cparams)[std::string(name)] = parameter_t(col);
257 }
258 
paramsSetMatrix(const char * name,float m[4][4],bool transpose)259 void yafrayInterface_t::paramsSetMatrix(const char* name, float m[4][4], bool transpose)
260 {
261 	if(transpose)	cparams->setMatrix(std::string(name), matrix4x4_t(m).transpose());
262 	else		cparams->setMatrix(std::string(name), matrix4x4_t(m));
263 }
264 
paramsSetMatrix(const char * name,double m[4][4],bool transpose)265 void yafrayInterface_t::paramsSetMatrix(const char* name, double m[4][4], bool transpose)
266 {
267 	if(transpose)	cparams->setMatrix(std::string(name), matrix4x4_t(m).transpose());
268 	else		cparams->setMatrix(std::string(name), matrix4x4_t(m));
269 }
270 
paramsSetMemMatrix(const char * name,float * matrix,bool transpose)271 void yafrayInterface_t::paramsSetMemMatrix(const char* name, float* matrix, bool transpose)
272 {
273 	float mat[4][4];
274 	int i,j;
275 	for(i= 0; i < 4; i++)
276 		for(j= 0; j < 4; j++)
277 			mat[i][j] = *(matrix+i*4+j);
278 	paramsSetMatrix(name, mat, transpose);
279 }
280 
paramsSetMemMatrix(const char * name,double * matrix,bool transpose)281 void yafrayInterface_t::paramsSetMemMatrix(const char* name, double* matrix, bool transpose)
282 {
283 	double mat[4][4];
284 	int i,j;
285 	for(i= 0; i < 4; i++)
286 		for(j= 0; j < 4; j++)
287 			mat[i][j] = *(matrix+i*4+j);
288 	paramsSetMatrix(name, mat, transpose);
289 }
290 
setInputColorSpace(std::string color_space_string,float gammaVal)291 void yafrayInterface_t::setInputColorSpace(std::string color_space_string, float gammaVal)
292 {
293 	if(color_space_string == "sRGB") inputColorSpace = SRGB;
294 	else if(color_space_string == "XYZ") inputColorSpace = XYZ_D65;
295 	else if(color_space_string == "LinearRGB") inputColorSpace = LINEAR_RGB;
296 	else if(color_space_string == "Raw_Manual_Gamma") inputColorSpace = RAW_MANUAL_GAMMA;
297 	else inputColorSpace = SRGB;
298 
299 	inputGamma = gammaVal;
300 }
301 
paramsClearAll()302 void yafrayInterface_t::paramsClearAll()
303 {
304 	params->clear();
305 	eparams->clear();
306 	cparams = params;
307 }
308 
paramsStartList()309 void yafrayInterface_t::paramsStartList()
310 {
311 	if(!eparams->empty()) eparams->push_back(paraMap_t());
312 	else Y_WARNING << "Interface: Appending to existing list!" << yendl;
313 	cparams = &eparams->back();
314 }
paramsPushList()315 void yafrayInterface_t::paramsPushList()
316 {
317 	eparams->push_back(paraMap_t());
318 	cparams = &eparams->back();
319 }
320 
paramsEndList()321 void yafrayInterface_t::paramsEndList()
322 {
323 	cparams = params;
324 }
325 
createLight(const char * name)326 light_t* yafrayInterface_t::createLight(const char* name)
327 {
328 	light_t* light = env->createLight(name, *params);
329 	if(light) scene->addLight(light);
330 	return light;
331 }
332 
createTexture(const char * name)333 texture_t* 		yafrayInterface_t::createTexture(const char* name) { return env->createTexture(name, *params); }
createMaterial(const char * name)334 material_t* 	yafrayInterface_t::createMaterial(const char* name) { return env->createMaterial(name, *params, *eparams); }
createCamera(const char * name)335 camera_t* 		yafrayInterface_t::createCamera(const char* name)
336 {
337 	camera_t *camera = env->createCamera(name, *params);
338 	return camera;
339 }
createBackground(const char * name)340 background_t* 	yafrayInterface_t::createBackground(const char* name) { return env->createBackground(name, *params); }
createIntegrator(const char * name)341 integrator_t* 	yafrayInterface_t::createIntegrator(const char* name) { return env->createIntegrator(name, *params); }
createImageHandler(const char * name,bool addToTable)342 imageHandler_t* yafrayInterface_t::createImageHandler(const char* name, bool addToTable) { return env->createImageHandler(name, *params, addToTable); }
343 
createVolumeRegion(const char * name)344 VolumeRegion* 	yafrayInterface_t::createVolumeRegion(const char* name)
345 {
346 	VolumeRegion* vr = env->createVolumeRegion(name, *params);
347 	if (!vr) return nullptr;
348 	scene->addVolumeRegion(vr);
349 	return nullptr;
350 }
351 
createObject(const char * name)352 unsigned int yafrayInterface_t::createObject(const char* name)
353 {
354 	object3d_t *object = env->createObject(name, *params);
355 	if(!object) return 0;
356 	objID_t id;
357 	if( scene->addObject(object, id) ) return id;
358 	return 0;
359 }
360 
abort()361 void yafrayInterface_t::abort()
362 {
363 	if(scene) scene->abort();
364 	session.setStatusRenderAborted();
365 	Y_WARNING << "Interface: Render aborted by user." << yendl;
366 }
367 
getRenderedImage(int numView,colorOutput_t & output)368 bool yafrayInterface_t::getRenderedImage(int numView, colorOutput_t &output)
369 {
370 	if(!film) return false;
371 	film->flush(numView, IF_ALL, &output);
372 	return true;
373 }
374 
listImageHandlers()375 std::vector<std::string> yafrayInterface_t::listImageHandlers()
376 {
377 	return env->listImageHandlers();
378 }
379 
listImageHandlersFullName()380 std::vector<std::string> yafrayInterface_t::listImageHandlersFullName()
381 {
382 	return env->listImageHandlersFullName();
383 }
384 
getImageFormatFromFullName(const std::string & fullname)385 std::string yafrayInterface_t::getImageFormatFromFullName(const std::string &fullname)
386 {
387 	return env->getImageFormatFromFullName(fullname);
388 }
389 
getImageFullNameFromFormat(const std::string & format)390 std::string yafrayInterface_t::getImageFullNameFromFormat(const std::string &format)
391 {
392 	return env->getImageFullNameFromFormat(format);
393 }
394 
getVersion() const395 std::string yafrayInterface_t::getVersion() const
396 {
397 	return YAFARAY_BUILD_VERSION;
398 }
399 
printDebug(const std::string & msg)400 void yafrayInterface_t::printDebug(const std::string &msg)
401 {
402 	Y_DEBUG << msg << yendl;
403 }
404 
printVerbose(const std::string & msg)405 void yafrayInterface_t::printVerbose(const std::string &msg)
406 {
407 	Y_VERBOSE << msg << yendl;
408 }
409 
printInfo(const std::string & msg)410 void yafrayInterface_t::printInfo(const std::string &msg)
411 {
412 	Y_INFO << msg << yendl;
413 }
414 
printParams(const std::string & msg)415 void yafrayInterface_t::printParams(const std::string &msg)
416 {
417 	Y_PARAMS << msg << yendl;
418 }
419 
printWarning(const std::string & msg)420 void yafrayInterface_t::printWarning(const std::string &msg)
421 {
422 	Y_WARNING << msg << yendl;
423 }
424 
printError(const std::string & msg)425 void yafrayInterface_t::printError(const std::string &msg)
426 {
427 	Y_ERROR << msg << yendl;
428 }
429 
render(colorOutput_t & output,progressBar_t * pb)430 void yafrayInterface_t::render(colorOutput_t &output, progressBar_t *pb)
431 {
432 	if(! env->setupScene(*scene, *params, output, pb) ) return;
433 	session.setStatusRenderStarted();
434 	scene->render();
435 	film = scene->getImageFilm();
436 	//delete film;
437 }
438 
setParamsBadgePosition(const std::string & badgePosition)439 void yafrayInterface_t::setParamsBadgePosition(const std::string &badgePosition)
440 {
441 	yafLog.setParamsBadgePosition(badgePosition);
442 }
443 
getDrawParams()444 bool yafrayInterface_t::getDrawParams()
445 {
446 	bool dp = false;
447 
448 	dp = yafLog.getUseParamsBadge();
449 
450 	return dp;
451 }
452 
setOutput2(colorOutput_t * out2)453 void yafrayInterface_t::setOutput2(colorOutput_t *out2)
454 {
455 	if(env) env->setOutput2(out2);
456 }
457 
setConsoleVerbosityLevel(const std::string & strVLevel)458 void yafrayInterface_t::setConsoleVerbosityLevel(const std::string &strVLevel)
459 {
460 	yafLog.setConsoleMasterVerbosity(strVLevel);
461 }
462 
setLogVerbosityLevel(const std::string & strVLevel)463 void yafrayInterface_t::setLogVerbosityLevel(const std::string &strVLevel)
464 {
465 	yafLog.setLogMasterVerbosity(strVLevel);
466 }
467 
468 // export "factory"...
469 
470 extern "C"
471 {
getYafray()472 	YAFRAYPLUGIN_EXPORT yafrayInterface_t* getYafray()
473 	{
474 		return new yafrayInterface_t();
475 	}
476 }
477 
478 __END_YAFRAY
479 
480