-
Notifications
You must be signed in to change notification settings - Fork 0
/
emberscene.cpp
253 lines (201 loc) · 6.13 KB
/
emberscene.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
#include "emberscene.h"
#include <EmberToXml.h>
#include <XmlToEmber.h>
#include <QFile>
QMutex EmberScene::s_renderMutex;
EmberCLns::RendererCL<EMBER_PRECISION> *EmberScene::s_renderer = 0;
EmberNs::SheepTools<EMBER_PRECISION, EMBER_PRECISION> *EmberScene::s_tools = 0;
const unsigned int EmberScene::MAX_XFORMS = 5;
const std::vector<EmberNs::eVariationId> &EmberScene::vars()
{
static std::vector<EmberNs::eVariationId> vars;
if ( vars.size() == 0 )
{
std::vector<EmberNs::eVariationId> noVars;
// nicked from emberGenome -- don't use these vars
EmberNs::VariationList<EMBER_PRECISION> varList;
noVars.push_back(VAR_NOISE);
noVars.push_back(VAR_BLUR);
noVars.push_back(VAR_GAUSSIAN_BLUR);
noVars.push_back(VAR_RADIAL_BLUR);
noVars.push_back(VAR_NGON);
noVars.push_back(VAR_SQUARE);
noVars.push_back(VAR_RAYS);
noVars.push_back(VAR_CROSS);
noVars.push_back(VAR_PRE_BLUR);
noVars.push_back(VAR_SEPARATION);
noVars.push_back(VAR_SPLIT);
noVars.push_back(VAR_SPLITS);
//Loop over the novars and set ivars to the complement.
for (int i = 0; i < varList.Size(); i++)
{
int j;
for (j = 0; j < noVars.size(); j++)
{
if (noVars[j] == varList.GetVariation(i)->VariationId())
break;
}
if (j == noVars.size())
vars.push_back(varList.GetVariation(i)->VariationId());
}
}
return vars;
}
EmberScene::EmberScene( int width, int height )
:AbstractScene()
{
m_width = width;
m_height = height;
m_ember.m_OrigFinalRasW = width;
m_ember.m_OrigFinalRasH = height;
m_ember.m_TemporalSamples = 1;
m_ember.m_Quality = 50;
m_ember.m_OrigPixPerUnit = 20;
randomise();
}
EmberScene::EmberScene( const EmberScene &other )
:AbstractScene( other ), m_ember( other.m_ember )
{
m_width = other.m_width;
m_height = other.m_height;
}
AbstractScene *EmberScene::clone() const
{
return new EmberScene( *this );
}
EmberScene::~EmberScene()
{
}
void EmberScene::randomise()
{
if ( s_tools )
{
do {
s_tools->Random( m_ember );
} while ( m_ember.XformCount() > MAX_XFORMS );
}
else
throw EmberRendererNotInitialisedException();
m_ember.m_FinalRasW = m_width;
m_ember.m_FinalRasH = m_height;
m_ember.m_PixelsPerUnit = 240;
m_ember.m_Quality = 50;
}
QPair< AbstractScene*, AbstractScene* > EmberScene::breed( AbstractScene *other, int mutationStrength )
{
// create two new scenes from this and one other, merging data from the two and
// mutating some parameters
EmberScene *left = new EmberScene( m_width, m_height );
EmberScene *right = new EmberScene( m_width, m_height );
EmberScene *emberOther = dynamic_cast< EmberScene* > ( other );
do {
s_tools->Cross( this->m_ember, emberOther->m_ember, left->m_ember, CROSS_NOT_SPECIFIED );
} while ( left->m_ember.XformCount() > MAX_XFORMS );
do {
s_tools->Cross( emberOther->m_ember, this->m_ember, right->m_ember, CROSS_NOT_SPECIFIED );
} while ( right->m_ember.XformCount() > MAX_XFORMS );
left->mutate( mutationStrength );
right->mutate( mutationStrength );
return qMakePair< AbstractScene*, AbstractScene* > ( left, right );
}
void EmberScene::mutateOnce()
{
Ember<EMBER_PRECISION> mutated( m_ember );
std::vector<EmberNs::eVariationId> vars(this->vars());
if ( s_tools )
s_tools->Mutate( mutated, MUTATE_NOT_SPECIFIED, vars, 0, 0.1 );
else
throw EmberRendererNotInitialisedException();
if ( mutated.XformCount() > MAX_XFORMS )
mutateOnce();
else
m_ember = mutated;
}
void EmberScene::saveToFile( const QString &fn )
{
QFile f( fn );
if ( f.open( QFile::WriteOnly | QFile::Truncate ) )
{
EmberNs::EmberToXml<EMBER_PRECISION> serializer;
serializer.Save( fn.toStdString(), m_ember, 0, false, false, false );
// std::string s;
// s = serializer.ToString( m_ember, 0, false, false, true );
// QString os;
// if ( s.length() )
// os = QString::fromStdString( s );
// f.write( os.toUtf8() );
// f.close();
}
}
void EmberScene::saveToStream( QDataStream &stream )
{
// I'm worried that this might be too slow :/
EmberNs::EmberToXml<EMBER_PRECISION> serializer;
stream << m_width;
stream << m_height;
std::string s( serializer.ToString( m_ember, 0, false, false, true ) );
QString os = QString::fromStdString( s );
stream << os;
}
void EmberScene::loadFromStream( QDataStream &stream )
{
// I'm worried that this might be too slow :/
QString s;
std::vector<EmberNs::Ember<EMBER_PRECISION>> embers;
stream >> m_width;
stream >> m_height;
stream >> s;
QByteArray bs( s.toUtf8() );
bs.append( static_cast< char > ( 0 ) );
EmberNs::XmlToEmber<EMBER_PRECISION> serializer;
serializer.Parse( reinterpret_cast< byte * > ( bs.data() ), "", embers );
m_ember = embers[0];
}
bool EmberScene::renderTo( QImage &image )
{
bool failed = true;
if ( s_renderer )
{
s_renderMutex.lock();
std::vector<byte> imgBytes( m_width * m_height * 4, 0xff0000ff );
m_ember.m_Quality = 50;
s_renderer->SetEmber( m_ember );
if ( s_renderer->Run( imgBytes ) == RENDER_OK )
{
// I don't know if this is async or not, but treating it as such
while ( s_renderer->ProcessState() != ACCUM_DONE )
usleep(50);
failed = false;
}
s_renderMutex.unlock();
if ( failed )
{
image = QImage( m_width, m_height, QImage::Format_ARGB32 );
image.fill( Qt::black );
qDebug( "Ember rendering failed" );
} else {
qDebug( "Ember rendering success" );
image = QImage( imgBytes.data(), m_width, m_height, QImage::Format_ARGB32 );
}
} else {
throw EmberRendererNotInitialisedException();
}
return !failed;
}
void EmberScene::destroyRenderer()
{
// deleting tools deletes the renderer
if ( s_tools )
delete s_tools;
else
delete s_renderer;
s_renderer = 0;
s_tools = 0;
}
bool EmberScene::initialiseRenderer( const QString &palettePath, int platform, int device )
{
destroyRenderer();
s_renderer = new EmberCLns::RendererCL<EMBER_PRECISION>( platform, device );
s_tools = new EmberNs::SheepTools<EMBER_PRECISION, EMBER_PRECISION>( palettePath.toStdString(), s_renderer );
return true;
}