forked from danielbui78/yaluxplug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.cpp
1262 lines (1060 loc) · 43.3 KB
/
renderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// renderer.cpp
// yaluxplug
//
// Created by Daniel Bui on 4/23/15.
//
//
/*****************************
Include files
*****************************/
#include <QtGui/QMessageBox>
#include <QtCore/QDateTime>
#include <QtCore/QFile>
#include <QtGui/QColor>
#include <QtCore/QTimer>
#include <QtCore/QProcess>
#include <QtGui/QFrame>
#include <QtGuI/QLayout>
#include <QtGui/QBoxLayout>
#include <QtGUI/QTextEdit>
#include <QtCore/QByteArray>
#include <QtGui/QWidget>
#include <QtGui/QButtonGroup>
#include "dzapp.h"
#include "dzscene.h"
#include "dzrenderoptions.h"
#include "dztimerange.h"
#include "dzcamera.h"
#include "dzmatrix4.h"
#include "dzlight.h"
#include "dzdistantlight.h"
#include "dzrenderhandler.h"
#include "dzrendersettings.h"
#include "dzimagemgr.h"
#include "dzfileio.h"
#include "dztexture.h"
#include "dzproperty.h"
#include "dzobject.h"
#include "dzshape.h"
#include "dzgeometry.h"
#include "dzmaterial.h"
#include "dzdefaultmaterial.h"
#include "dztarray.h"
#include "dzstringproperty.h"
#include "dzcolorproperty.h"
#include "dzimageproperty.h"
#include "dzfloatproperty.h"
#include "dzintproperty.h"
#include "dznodeproperty.h"
#include "dznumericproperty.h"
#include "dzvertexmesh.h"
#include "dzfacetmesh.h"
#include "dzfacegroup.h"
#include "dzviewrenderhandler.h"
#include "dzrenderdata.h"
#include "dzmainwindow.h"
#include "optionsframe.h"
#include "dazToPLY.h"
#include "renderer.h"
#include "plugin.h"
///////////////////////////////////////////////////////////////////////
// yaluxplug - YaLuxRender class
///////////////////////////////////////////////////////////////////////
/**
**/
YaLuxRender::YaLuxRender()
{
// DEBUG
// DEFAULT LuxPath
YaLuxGlobal.LuxExecPath = "/Applications/LuxRender1.3.1/LuxRender.app/Contents/MacOS/luxconsole";
// dzApp->log("yaluxplug: initializing options");
YaLuxGlobal.activeFrame = -1;
YaLuxGlobal.frame_counter = 0;
YaLuxGlobal.endFrame = -1;
YaLuxGlobal.totalFrames = 0;
YaLuxGlobal.tempCounter = 0;
YaLuxGlobal.inProgress = false;
YaLuxGlobal.currentNode = DI_NULL;
YaLuxGlobal.cachePath = dzApp->getTempPath() + "/yaluxCache/";
// create cache working directory
DzFileIO::pathExists(YaLuxGlobal.cachePath,true);
dzApp->log("yaluxplug: Initialized.");
///////////
// Create Log window
////////////
YaLuxGlobal.logWindow = new QFrame();
YaLuxGlobal.logWindow->setParent( (QWidget*) dzApp->getInterface() );
YaLuxGlobal.logWindow->setWindowTitle("LogWindow");
YaLuxGlobal.logWindow->setMinimumSize(800, 100);
QVBoxLayout *layout = new QVBoxLayout(YaLuxGlobal.logWindow);
YaLuxGlobal.logText = new QTextEdit(YaLuxGlobal.logWindow);
layout->addWidget(YaLuxGlobal.logText);
QHBoxLayout *buttonBar = new QHBoxLayout();
QPushButton *showLXS = new QPushButton("&Show Scenefile (.LXS)", YaLuxGlobal.logWindow);
buttonBar->addWidget(showLXS);
QPushButton *previewCurrentFrame = new QPushButton("Pre&view current frame", YaLuxGlobal.logWindow);
buttonBar->addWidget(previewCurrentFrame);
QPushButton *stopRenderButton = new QPushButton("&Stop all rendering", YaLuxGlobal.logWindow);
buttonBar->addWidget(stopRenderButton);
QPushButton *nextFrameButton = new QPushButton("&Next frame", YaLuxGlobal.logWindow);
buttonBar->addWidget(nextFrameButton);
layout->addLayout(buttonBar);
connect(stopRenderButton, SIGNAL(clicked()),
this, SLOT(handleStopRender()) );
connect(nextFrameButton, SIGNAL(clicked()),
this, SLOT(handleNextFrame()) );
connect(previewCurrentFrame, SIGNAL(clicked()),
this, SLOT(handlePreviewCurrentFrame()) );
connect(showLXS, SIGNAL( clicked()),
this, SLOT(handleShowLXS()) );
return;
}
///////////////////////////////////////////////////////////////////////
// public
///////////////////////////////////////////////////////////////////////
void YaLuxRender::handleNextFrame()
{
if (YaLuxGlobal.luxRenderProc->state() == QProcess::Running)
killRender();
}
void YaLuxRender::handleStopRender()
{
// kill current render and cancel all rendering
if (YaLuxGlobal.luxRenderProc->state() == QProcess::Running)
{
killRender();
YaLuxGlobal.RenderProgress->cancel(); // this does not work
YaLuxGlobal.bIsCancelled = true;
}
}
void YaLuxRender::handlePreviewCurrentFrame()
{
QString file = YaLuxGlobal.workingRenderFilename;
YaLuxGlobal.logText->setTextColor( QColor(255,255,255));
YaLuxGlobal.logText->append( QString("Opening file: [%1]").arg( QUrl::fromLocalFile(file).toString() ) );
QDesktopServices::openUrl( QUrl::fromLocalFile(file) );
}
void YaLuxRender::handleShowLXS()
{
QString file =YaLuxGlobal.tempPath + "/" + YaLuxGlobal.tempFilenameBase + ".lxs" ;
YaLuxGlobal.logText->setTextColor( QColor(255,255,255));
YaLuxGlobal.logText->append( QString("Opening file: [%1]").arg( QUrl::fromLocalFile(file).toString() ) );
QDesktopServices::openUrl( QUrl::fromLocalFile(file) );
}
QString YaLuxRender::getLuxExecPath() const
{
return YaLuxGlobal.LuxExecPath;
}
void YaLuxRender::setLuxExecPath(const QString &execPath)
{
YaLuxGlobal.LuxExecPath = execPath;
}
bool YaLuxRender::render(DzRenderHandler *old_handler, DzCamera *camera, const DzRenderOptions &opt)
{
QSize renderImageSize;
QString mesg;
QString fullPathFileNameLXS;
QString fullPathTempFileNameNoExt;
QString tempPath;
int steps = 100;
bool bIsAnimation=false;
DzTimeRange timeRenderingRange;
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("\nyaluxplug: render() called.");
old_handler->dumpObjectInfo();
old_handler->dumpObjectTree();
DzViewRenderHandler *handler = new DzViewRenderHandler(old_handler->getSize(), old_handler->getStartingTime(), QString(""), true);
// DzImageRenderHandler *handler = new DzImageRenderHandler(old_handler->getSize(), old_handler->getStartingTime(), old_handler->getNumFrames(), QString(""), true);
YaLuxGlobal.inProgress = true;
YaLuxGlobal.bIsCancelled = false;
YaLuxGlobal.optFrame->applyChanges();
YaLuxGlobal.RenderProgress = new DzProgress("yaluxplug Render Started", steps, true, true);
YaLuxGlobal.RenderProgress->setUseCloseCheckbox(true);
YaLuxGlobal.RenderProgress->setCloseOnFinish(true);
fullPathTempFileNameNoExt = dzApp->getTempFilename();
YaLuxGlobal.workingRenderFilename = fullPathTempFileNameNoExt + ".png";
YaLuxGlobal.tempPath = DzFileIO::getFilePath(fullPathTempFileNameNoExt);
YaLuxGlobal.tempFilenameBase = DzFileIO::getBaseFileName( fullPathTempFileNameNoExt );
fullPathFileNameLXS = fullPathTempFileNameNoExt + ".lxs";
// DEBUG
mesg = "Writing to LXS file = " + fullPathFileNameLXS;
if (YaLuxGlobal.debugLevel >= 2) // debugging data
dzApp->log( QString("yaluxplug: pathTempName=[%1], workingRenderFilename=[%2], fileNameLXS=[%3]").arg(YaLuxGlobal.tempPath).arg(YaLuxGlobal.workingRenderFilename).arg(fullPathFileNameLXS) );
// Get Render Settings
// time range
YaLuxGlobal.options.copyFrom(&opt);
YaLuxGlobal.frame_counter = 0;
if ( !opt.isCurrentFrameRender() )
{
timeRenderingRange.setEnds(opt.getStartTime(),opt.getEndTime());
dzScene->setTime(timeRenderingRange.getStart());
YaLuxGlobal.activeFrame = dzScene->getFrame();
DzTime timeDuration = timeRenderingRange.getDuration();
int nFramesToRender = (timeDuration / dzScene->getTimeStep() )+1;
// DEBUG
if (YaLuxGlobal.debugLevel >=1) // user data
dzApp->log( QString("number of frames to render is %1").arg(nFramesToRender));
YaLuxGlobal.endFrame = YaLuxGlobal.activeFrame + nFramesToRender;
YaLuxGlobal.totalFrames = nFramesToRender;
if (nFramesToRender > 1)
bIsAnimation = true;
mesg = QString("Preparing to render: %1 to %2 (%3 frames)\n").arg(timeRenderingRange.getStart()).arg(timeRenderingRange.getEnd()).arg(nFramesToRender);
YaLuxGlobal.RenderProgress->setCurrentInfo(mesg);
} else {
YaLuxGlobal.activeFrame = dzScene->getFrame();
YaLuxGlobal.endFrame = YaLuxGlobal.activeFrame;
YaLuxGlobal.totalFrames = 1;
}
YaLuxGlobal.RenderProgress->step();
///////////////////////////////
emit aboutToRender(this);
// connect(this, SIGNAL(updateData( DzRenderData &)),
// handler, SLOT(passData( DzRenderData &)) );
connect(this, SIGNAL(beginningFrame(int)),
handler, SLOT(beginFrame(int)) );
connect(this, SIGNAL(frameFinished() ),
handler, SLOT(finishFrame()) );
connect(this, SIGNAL(beginningRender() ),
handler, SLOT(beginRender()) );
connect(this, SIGNAL(renderFinished()),
handler, SLOT(finishRender()) );
connect(handler, SIGNAL(killRender()),
this, SLOT(killRender()) );
YaLuxGlobal.handler = handler;
//////////////////////////
// Set up external process
///////////////////////////
QProcess *process = new QProcess(this);
YaLuxGlobal.luxRenderProc = process;
QString logFileName = QString("%1/yaluxplug.log").arg(YaLuxGlobal.tempPath);
// process->setStandardErrorFile(logFile, QIODevice::Append);
process->setReadChannel(QProcess::StandardError);
connect(process, SIGNAL( finished(int, QProcess::ExitStatus) ),
this, SLOT( handleRenderProcessComplete(int, QProcess::ExitStatus) ) );
connect(process, SIGNAL( stateChanged( QProcess::ProcessState) ),
this, SLOT( handleRenderProcessStateChange( QProcess::ProcessState)) );
QString file;
if (bIsAnimation)
{
file = YaLuxGlobal.LuxExecPath;
}
else
{
if (YaLuxGlobal.bShowLuxRenderWindow)
{
#if defined( Q_OS_WIN )
file = DzFileIO::getFilePath(YaLuxGlobal.LuxExecPath) + "/luxrender.exe";
#elif defined( Q_WS_MAC )
file = DzFileIO::getFilePath(YaLuxGlobal.LuxExecPath) + "/luxrender";
#endif
}
else
{
file = YaLuxGlobal.LuxExecPath;
}
}
QStringList userargs = YaLuxGlobal.CmdLineArgs.split(" ", QString::SkipEmptyParts);
if (YaLuxGlobal.bNetworkRenderOn)
{
for (int i=0; i<YaLuxGlobal.slaveNodeList.count(); i++)
{
userargs << QString("-u%1").arg(YaLuxGlobal.slaveNodeList[i]);
}
}
QStringList args = QStringList() << "-l" << userargs << fullPathFileNameLXS;
//DEBUG
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log( QString("yaluxplug: DEBUG: render process argument list = [%1]").arg(args.join(",")) );
// args << "-uphenom-ubuntu";
// process->start(file, args);
// dzApp->log( QString("yaluxplug: SPAWNING: %1 %2").arg(file).arg(args.join(" ")) );
// calling this will open up a Daz render window
// handler->beginRender();
// emit beginningFrame(YaLuxGlobal.activeFrame);
// emit beginningRender();
// DEBUG
// Start a render loop...
// one time through loop per frame
// QImage qimg;
// DzRenderData *data;
int TimeOut = 500;
// QTimer tmr;
// connect(&tmr, SIGNAL(timeout()),
// this, SLOT(updateData()) );
handler->beginRender();
QFile logFile(logFileName);
logFile.open(QIODevice::WriteOnly);
/////////////////////////////////////////
//
// Begin Animation Rendering loop.
// Single frame renders only go through once.
// A scene file for the current frame time is first composed and then a QProcess
// is prepared for execution by the frame rendering loop.
//
/////////////////////////////////////////
while (YaLuxGlobal.frame_counter < YaLuxGlobal.totalFrames)
{
//DEBUG
mesg = "Preparing new frame:";
if (YaLuxGlobal.debugLevel >=1) // user info
dzApp->log( mesg );
YaLuxGlobal.RenderProgress->setCurrentInfo(mesg);
////////////////////////
// manage PLYgarbageCollectionList:
// If this is a single frame render, then keep the PLYs in the temp
// directory so that the LXS file can be run manually if desired.
// If this is a multiframe render, then delete the PLYs in the temp
// directory so that we don't run out of harddisk space if rendering
// infinite frames or something.
////////////////////////
while ( YaLuxGlobal.PLYgarbageCollectionList.count() > 0)
{
QTemporaryFile *file = YaLuxGlobal.PLYgarbageCollectionList.last();
file->setAutoRemove(bIsAnimation);
delete file;
YaLuxGlobal.PLYgarbageCollectionList.removeLast();
}
// DEBUG
// can't do spotrenders yet (code incomplete) so abort if this is one
if (YaLuxGlobal.bIsSpotRender == true)
{
YaLuxGlobal.RenderProgress->finish();
YaLuxGlobal.FrameProgress->finish();
YaLuxGlobal.inProgress = false;
return false;
}
////////////////////////////
//
// Generate Lux Scene File
//
/////////////////////////////
dzScene->setFrame(YaLuxGlobal.activeFrame);
LuxMakeSceneFile(fullPathFileNameLXS, this, camera, opt);
// Set up progress bar for the current frame
YaLuxGlobal.FrameProgress = new DzProgress("Current Frame Progress", 100);
//////////////////////////////
//
// Start the luxrender/luxconsole process
//
//////////////////////////////
process->start(file, args);
process->waitForStarted();
// Update the progress window
mesg = QString("Rendering frame #%1 (%2/%3)...").arg(YaLuxGlobal.activeFrame).arg(YaLuxGlobal.frame_counter+1).arg(YaLuxGlobal.totalFrames);
YaLuxGlobal.RenderProgress->setCurrentInfo(mesg);
float progressFraction = ((float)YaLuxGlobal.frame_counter+1)/((float)YaLuxGlobal.totalFrames);
int updateProgress = 9 + (progressFraction)*90;
YaLuxGlobal.RenderProgress->update( updateProgress );
YaLuxGlobal.FrameProgress->step();
// tmr.start(1000);
// Notify the render handler of the newly started frame -- it will respond by
// opening a render preview window if this is a single frame render job.
if (YaLuxGlobal.bShowLuxRenderWindow == false || (YaLuxGlobal.totalFrames > 1))
{
handler->beginFrame(YaLuxGlobal.frame_counter);
YaLuxGlobal.logWindow->show();
YaLuxGlobal.logWindow->activateWindow();
}
connect(dzApp->getInterface(), SIGNAL(aboutToClose()),
YaLuxGlobal.logWindow, SLOT(close()) );
// reset bFrameisFinished before we start the frame render loop
YaLuxGlobal.bFrameisFinished = false;
//////////////////////////////////////////////
//
// Begin frame rendering loop
// This loop contains starting the render process and monitoring its output.
// While the loop runs, it will also sleep 50ms and processEvents each cycle.
// When the process terminates or render is cancelled by the UI, the loop breaks.
//
//////////////////////////////////////////////
while (YaLuxGlobal.bFrameisFinished == false)
{
// DEBUG
//process->waitForFinished();
// double check process
// DEBUG
int processState = -1;
processState = process->state();
if ( (processState == QProcess::NotRunning) || (YaLuxGlobal.RenderProgress->isCancelled() == true) )
// if ( (YaLuxGlobal.RenderProgress->isCancelled() == true) )
{
if (YaLuxGlobal.debugLevel >= 2) // debugging data
dzApp->log("yaluxplug: Rendering, progress exited or cancelled.");
if (YaLuxGlobal.luxRenderProc->state() == QProcess::Running)
YaLuxGlobal.luxRenderProc->terminate();
// if network rendering, also send terminate signal to render servers
if (YaLuxGlobal.bNetworkRenderOn)
resetRenderServers();
// Set bFrameisFinished to true to indicate stopping the frame render loop
YaLuxGlobal.bFrameisFinished = true;
break;
} else
// Update the log window
// process->waitForFinished();
// process->waitForReadyRead(5000);
processRenderLog(process, logFile, true);
QCoreApplication::processEvents(QEventLoop::AllEvents);
int timeout2 = 50;
#ifdef Q_OS_WIN
Sleep(uint(timeout2));
#else
struct timespec ts = { timeout2 / 1000, (timeout2 % 1000) * 1000 * 1000 };
nanosleep(&ts, NULL);
#endif
}
//////////////////////////////////////////////
//
// End of frame rendering loop
//
/////////////////////////////////////////////
// tmr.stop();
// disconnect(&tmr, SIGNAL(timeout()),
// this, SLOT(updateData()) );
// Read the remainder of the stdoutput to the logfile
// but don't update the image since this was already done when process sent the finish() signal.
processRenderLog(process, logFile, false);
// If Renderprogress is cancelled of bIsCancelled is true, then stop the animation rendering
if ( (YaLuxGlobal.RenderProgress->isCancelled() == true) || (YaLuxGlobal.bIsCancelled == true) )
{
YaLuxGlobal.luxRenderProc->deleteLater();
YaLuxGlobal.inProgress = false;
YaLuxGlobal.RenderProgress->finish();
YaLuxGlobal.FrameProgress->finish();
logFile.close();
return false;
}
// if not cancelled, then update frame counts, progress and continue
YaLuxGlobal.frame_counter++;
YaLuxGlobal.activeFrame++;
YaLuxGlobal.FrameProgress->finish();
mesg = "Frame completed.";
YaLuxGlobal.RenderProgress->setCurrentInfo(mesg);
progressFraction = ((float)YaLuxGlobal.frame_counter)/((float)YaLuxGlobal.totalFrames);
updateProgress = 10 + (progressFraction)*90;
YaLuxGlobal.RenderProgress->update( updateProgress );
}
//////////////////////
//
// End of animation rendering loop
//
//////////////////////////
// disconnect(process, SIGNAL( finished(int, QProcess::ExitStatus) ),
// this, SLOT( handleRenderProcessComplete(int, QProcess::ExitStatus) ) );
YaLuxGlobal.luxRenderProc->deleteLater();
YaLuxGlobal.inProgress = false;
handler->finishRender();
YaLuxGlobal.RenderProgress->finish();
YaLuxGlobal.FrameProgress->finish();
logFile.close();
return true;
}
void YaLuxRender::resetRenderServers()
{
QProcess cmdProc;
QStringList terminateCommand;
for (int i=0; i<YaLuxGlobal.slaveNodeList.count(); i++)
{
// terminateCommand << QString("--resetserver %1").arg(YaLuxGlobal.slaveNodeList[i]);
terminateCommand << QString("--resetserver") << YaLuxGlobal.slaveNodeList[i];
}
// use YaLuxGlobal.LuxExecPath, since this will be set to luxconsole
cmdProc.start(YaLuxGlobal.LuxExecPath, terminateCommand);
cmdProc.waitForFinished();
QByteArray qa = cmdProc.readAllStandardError();
YaLuxGlobal.logText->append( QString(qa) );
// YaLuxGlobal.RenderProgress->setInfo( QString(qa) );
}
void YaLuxRender::processRenderLog(QProcess *process, QFile &logFile, bool bUpdateRender)
{
// Read the remainder of the stdoutput to the logfile
while (process->canReadLine() )
{
// NOTE: we don't need to process the "writing tonemapped PNG" because a
// final loading of the file was done when the process called the finish() signal.
QByteArray qa = process->readLine();
if (qa.contains("Writing Tonemapped"))
{
if (bUpdateRender)
updateData();
} else if (qa.contains("ERROR"))
{
logToWindow( QString(qa.data()), QColor(255,0,0), true);
/*
QString newInfo = QString( qa.data() );
newInfo = newInfo.replace("\n", "");
YaLuxGlobal.logText->setBold(true);
YaLuxGlobal.logText->setTextColor( QColor(255,0,0) );
YaLuxGlobal.logText->append( newInfo );
YaLuxGlobal.logText->setBold(false);
*/
} else if (qa.contains("Lux version"))
{
logToWindow( QString(qa.data()), QColor(0,255,0), true);
/*
QString newInfo = QString( qa.data() );
newInfo = newInfo.replace("\n", "");
YaLuxGlobal.logText->setBold(true);
YaLuxGlobal.logText->setTextColor( QColor(0,255,0) );
YaLuxGlobal.logText->append( newInfo );
YaLuxGlobal.logText->setBold(false);
*/
} else if (qa.contains("100% rendering done"))
{
logToWindow( QString(qa.data()), QColor(0,255,0), true);
/*
QString newInfo = QString( qa.data() );
newInfo = newInfo.replace("\n", "");
YaLuxGlobal.logText->setBold(true);
YaLuxGlobal.logText->setTextColor( QColor(0,255,0) );
YaLuxGlobal.logText->append( newInfo );
YaLuxGlobal.logText->setBold(false);
*/
} else if ( qa.contains("server"))
{
logToWindow( QString(qa.data()), QColor(100,200,255), true);
/*
QString newInfo = QString( qa.data() );
newInfo = newInfo.replace("\n", "");
YaLuxGlobal.logText->setBold(true);
YaLuxGlobal.logText->setTextColor( QColor(100,200,255) );
YaLuxGlobal.logText->append( newInfo );
YaLuxGlobal.logText->setBold(false);
*/
} else if ( qa.contains("Tessellating"))
{
logToWindow( QString(qa.data()) );
/*
QString newInfo = QString( qa.data() );
newInfo = newInfo.replace("\n", "");
YaLuxGlobal.logText->setTextColor( QColor(255,255,255) );
YaLuxGlobal.logText->append( newInfo );
*/
} else if ( (qa.contains("% T)") || qa.contains("% Thld)") ) )
{
if (YaLuxGlobal.debugLevel >= 1)
{
logToWindow( QString(qa.data()), QColor(100,200,255) );
/*
QString newInfo = QString( qa.data() );
newInfo = newInfo.replace("\n", "");
YaLuxGlobal.logText->setTextColor( QColor(100,200,255) );
YaLuxGlobal.logText->append( newInfo );
*/
}
QRegExp regexp("\\(([\\d]*)% T\\)");
if ( regexp.indexIn( QString(qa) ) != -1 )
{
QString percentString = regexp.cap(1);
// YaLuxGlobal.FrameProgress->setInfo( QString("Frame render: %1\% completed").arg(percentString));
YaLuxGlobal.FrameProgress->update(percentString.toInt());
}
} else if (qa.contains("INFO") && YaLuxGlobal.debugLevel >= 2)
{
QString newInfo = QString( qa.data() );
newInfo = newInfo.replace("\n", "");
YaLuxGlobal.logText->setTextColor( QColor(255,255,255) );
YaLuxGlobal.logText->append( newInfo );
}
logFile.write(qa);
}
logFile.flush();
}
void YaLuxRender::logToWindow( QString data, QColor textcolor, bool bIsBold )
{
QString formated = data.replace("\n", "");
// if (bIsBold) YaLuxGlobal.logText->setBold(true);
if (bIsBold) YaLuxGlobal.logText->setFontWeight(QFont::Bold);
YaLuxGlobal.logText->setTextColor( textcolor );
YaLuxGlobal.logText->append( formated );
YaLuxGlobal.logText->setTextColor( QColor(255,255,255) );
// if (bIsBold) YaLuxGlobal.logText->setBold(false);
if (bIsBold) YaLuxGlobal.logText->setFontWeight(QFont::Normal);
}
void YaLuxRender::updateData()
{
int timeout = 1000;
#ifdef Q_OS_WIN
Sleep(uint(timeout));
#else
struct timespec ts = { timeout / 1000, (timeout % 1000) * 1000 * 1000 };
nanosleep(&ts, NULL);
#endif
QImage *qimg = new QImage();
DzRenderData *data;
QFile imgFile(YaLuxGlobal.workingRenderFilename);
while (imgFile.open(QIODevice::ReadOnly) == false)
{
#ifdef Q_OS_WIN
Sleep(uint(timeout));
#else
nanosleep(&ts, NULL);
#endif
}
QByteArray qa;
qa = imgFile.readAll();
for (int i=0; ( qimg->loadFromData(qa) == false) && i <= 2; i++)
// if ( qimg->loadFromData(qa) == false)
{
if ( i==2 )
{
QString mesg = "yaluxplug: ERROR: Unable to update Daz with rendered image from luxrender.";
dzApp->log(mesg);
YaLuxGlobal.logText->setTextColor( QColor(255,0,0));
YaLuxGlobal.logText->append(mesg);
return;
}
#ifdef Q_OS_WIN
Sleep(uint(timeout));
#else
nanosleep(&ts, NULL);
#endif
imgFile.close();
if (imgFile.open(QIODevice::ReadOnly) == true)
qa = imgFile.readAll();
}
data = new DzRenderData(YaLuxGlobal.cropWindow.top(), YaLuxGlobal.cropWindow.left(), qimg->convertToFormat(QImage::Format_ARGB32));
YaLuxGlobal.handler->passData( (*data) );
delete qimg;
// delete data;
}
void YaLuxRender::handleRenderProcessStateChange( QProcess::ProcessState newstate )
{
if (newstate == QProcess::NotRunning)
YaLuxGlobal.bFrameisFinished = true;
}
void YaLuxRender::handleRenderProcessComplete( int exitCode, QProcess::ExitStatus status )
{
QImage *qimg = new QImage();
DzRenderData *data;
if (status == QProcess::CrashExit)
{
QString error = QString("yaluxplug: ERROR: luxrender process stopped unexpectedly: exitCode=%1").arg(exitCode);
logToWindow( QString(error), QColor(255,0,0), true);
/*
YaLuxGlobal.logText->setTextColor( QColor(255, 0, 0) );
YaLuxGlobal.logText->setBold(true);
YaLuxGlobal.logText->append( error );
YaLuxGlobal.logText->setBold(false);
*/
dzApp->log(error);
}
if (YaLuxGlobal.inProgress == false)
return;
if (qimg->load(YaLuxGlobal.workingRenderFilename) == true)
{
data = new DzRenderData(YaLuxGlobal.cropWindow.top(), YaLuxGlobal.cropWindow.left(), qimg->convertToFormat(QImage::Format_ARGB32));
YaLuxGlobal.handler->passData( (*data) );
QString tempRenderName = dzApp->getTempRenderFilename() + ".png";
qimg->save(tempRenderName);
delete qimg;
}
if (YaLuxGlobal.bShowLuxRenderWindow == true && (YaLuxGlobal.totalFrames == 1) )
{
YaLuxGlobal.handler->beginFrame(YaLuxGlobal.frame_counter);
}
emit frameFinished();
YaLuxGlobal.bFrameisFinished = true;
/*
if (YaLuxGlobal.activeFrame <= YaLuxGlobal.endFrame)
{
emit frameFinished();
}
else
{
emit frameFinished();
emit renderFinished(this);
// YaLuxGlobal.inProgress = false;
// YaLuxGlobal.luxRenderProc->deleteLater();
dzApp->log( QString("yaluxplug: RENDER PROCESS exited with %1 ").arg(exitCode) );
}
*/
}
bool YaLuxRender::customRender(DzRenderHandler *handler, DzCamera *camera, DzLightList &lights, DzNodeList &nodes, const DzRenderOptions &opt)
{
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: unimplemented call customRender()");
return true;
}
DzOptionsFrame* YaLuxRender::getOptionsFrame() const
{
YaLuxGlobal.optFrame = new YaLuxOptionsFrame();
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: creating options frame");
return YaLuxGlobal.optFrame;
}
DtFilterFunc YaLuxRender::getFilterFunction(DzRenderOptions::PixelFilter filterType) const
{
DtFilterFunc fpResult = DI_NULL;
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: unimplemented call getFilterFunction()");
return fpResult;
}
///////////////////////////////////////////////////////////////////////
// public slots
///////////////////////////////////////////////////////////////////////
//
// MANIPULATORS (public slot)
//
bool YaLuxRender::render(DzRenderHandler *handler, DzCamera *camera, const DzRenderOptions *opt)
{
// This does not get caleld by clicking render button.... callback for message-passing backend
return render(handler, camera, (DzRenderOptions &)opt);
}
bool YaLuxRender::customRender( DzRenderHandler *handler, DzCamera *camera, QObjectList lights, QObjectList nodes, const DzRenderOptions *opt )
{
// This does not get caleld by clicking render button.... callback for message-passing backend
return customRender(handler, camera, (DzLightList& ) lights, (DzNodeList& ) nodes, (DzRenderOptions &)opt);
}
void YaLuxRender::prepareImage(const DzTexture *img, const QString &filename)
{
QSize imgSize;
QImage qimg;
int WidthResize = YaLuxGlobal.maxTextureSize;
imgSize = img->getOriginalImageSize();
if ( (YaLuxGlobal.maxTextureSize == -1) || (imgSize.width() <= WidthResize) )
{
// this size is good, just keep it
emit imagePrepared(img, filename);
return;
}
// otherwise, spawn a thread to do it after loading completed
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: prepareImage( " + filename + " ) - Starting New Thread" );
// QThread *thread = new QThread;
// Update the BackgroundProgress
if (YaLuxGlobal.backgroundProgress == NULL)
{
YaLuxGlobal.backgroundProgress = new DzBackgroundProgress("Optimizing images...", 100, false);
}
// recalculate the progress bar
YaLuxGlobal.currentBackgroundProgress = (YaLuxGlobal.currentBackgroundProgress * YaLuxGlobal.numBackgroundThreads) / (YaLuxGlobal.numBackgroundThreads+1);
// ** make sure this is the only place that increments the numBackgroundThreads **
YaLuxGlobal.numBackgroundThreads++;
YaLuxGlobal.backgroundProgress->update( YaLuxGlobal.currentBackgroundProgress * 100);
WorkerPrepareImage *worker = new WorkerPrepareImage(img, filename);
worker->myThread = new QThread;
worker->moveToThread(worker->myThread);
connect(worker->myThread, SIGNAL(started()),
worker, SLOT(doPrepareImage()) );
connect(worker, SIGNAL(prepareImageComplete(WorkerPrepareImage *, const DzTexture *, const QString &) ),
this, SLOT(handlePrepareImageComplete(WorkerPrepareImage *, const DzTexture *, const QString &) ) );
connect(worker, SIGNAL(finished()), worker->myThread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(worker->myThread, SIGNAL(finished()), worker->myThread, SLOT(deleteLater()));
worker->myThread->start();
return;
};
void YaLuxRender::handlePrepareImageComplete( WorkerPrepareImage *worker, const DzTexture *img, const QString &filename)
{
emit imagePrepared(img, filename);
YaLuxGlobal.currentBackgroundProgress += 1/YaLuxGlobal.numBackgroundThreads;
YaLuxGlobal.backgroundProgress->update( YaLuxGlobal.currentBackgroundProgress * 100 );
// ** make sure this is the only place that decrements the numBackgroundThreads **
YaLuxGlobal.numBackgroundThreads--;
if (YaLuxGlobal.numBackgroundThreads == 0)
{
YaLuxGlobal.backgroundProgress->finish();
delete YaLuxGlobal.backgroundProgress;
YaLuxGlobal.backgroundProgress = NULL;
}
};
QString YaLuxRender::compileShader(const QString &shaderPath)
{
QString sResult = shaderPath;
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: DEBUG: unimplemented call: compileShader (" + shaderPath + ").");
return sResult;
};
QString YaLuxRender::compileShader(const QString &shaderPath, QString &output)
{
QString sResult = shaderPath;
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: DEBUG: compileshader (" + shaderPath + "," + output +").");
output = "yaluxplug: compiling shader for " + shaderPath;
return sResult;
}
DzShaderDescription* YaLuxRender::getShaderInfo(const QString &shaderPath)
{
DzShaderDescription* oResult = NULL;
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: DEBUG: getShaderInfo called (" + shaderPath + ").");
return oResult;
}
void YaLuxRender::killRender()
{
// stop rendering now
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: killRender was called.");
// Kill Render process
YaLuxGlobal.luxRenderProc->kill();
return;
};
bool YaLuxRender::bake( DzRenderHandler *handler, DzCamera *camera, DzLightListIterator &lights, DzNodeListIterator &nodes, const DzBakerOptions &opt )
{
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: unimplemented call bake().");
return false;
}
bool YaLuxRender::autoBake( DzRenderHandler *handler, DzCamera *camera, DzLightListIterator &lights, DzNodeListIterator &nodes, const DzBakerOptions &opt )
{
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: unimplemented call autoBake();");
return false;
}
void YaLuxRender::stopBaking()
{
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: unimplemented call stopBaking();");
return;
}
void YaLuxRender::saveBakeImage( const DzBakerOptions &opt, bool wait )
{
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: unimplemented call saveBakeImage()");
return;
}
bool YaLuxRender::textureConvert( DzRenderHandler *handler, DzCamera *camera, const DzTextureConvertorOptions &opt )
{
// convert a texture for rendering?
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: unimplemented call textureConvert()");
return false;
}
//
// ACCESSORS (public slot)
//
QString YaLuxRender::getShaderCompilerPath()
{
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: unimplemented call getShaderCompilerPath()");
QString sResult = "";
return sResult;
}
QString YaLuxRender::getTextureUtilityPath()
{
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: unimplemented call getTextureUtilityPath()");
QString sResult = "";
return sResult;
};
QStringList YaLuxRender::getShaderSearchPaths() const
{
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: unimplemented call getShaderSearchPaths()");
QStringList oResult = QStringList() << "";
return oResult;
};
QString YaLuxRender::processShaderName( const QString &shaderName ) const
{
if (YaLuxGlobal.debugLevel >=2) // debugging data
dzApp->log("yaluxplug: unimplemented call processShaderName()");
QString sResult = NULL;
return sResult;
};