-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCreatureRenderer.cpp
333 lines (284 loc) · 11.6 KB
/
CreatureRenderer.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
/******************************************************************************
* Creature Runtimes License
*
* Copyright (c) 2015, Kestrel Moon Studios
* All rights reserved.
*
* Preamble: This Agreement governs the relationship between Licensee and Kestrel Moon Studios(Hereinafter: Licensor).
* This Agreement sets the terms, rights, restrictions and obligations on using [Creature Runtimes] (hereinafter: The Software) created and owned by Licensor,
* as detailed herein:
* License Grant: Licensor hereby grants Licensee a Sublicensable, Non-assignable & non-transferable, Commercial, Royalty free,
* Including the rights to create but not distribute derivative works, Non-exclusive license, all with accordance with the terms set forth and
* other legal restrictions set forth in 3rd party software used while running Software.
* Limited: Licensee may use Software for the purpose of:
* Running Software on Licensee’s Website[s] and Server[s];
* Allowing 3rd Parties to run Software on Licensee’s Website[s] and Server[s];
* Publishing Software’s output to Licensee and 3rd Parties;
* Distribute verbatim copies of Software’s output (including compiled binaries);
* Modify Software to suit Licensee’s needs and specifications.
* Binary Restricted: Licensee may sublicense Software as a part of a larger work containing more than Software,
* distributed solely in Object or Binary form under a personal, non-sublicensable, limited license. Such redistribution shall be limited to unlimited codebases.
* Non Assignable & Non-Transferable: Licensee may not assign or transfer his rights and duties under this license.
* Commercial, Royalty Free: Licensee may use Software for any purpose, including paid-services, without any royalties
* Including the Right to Create Derivative Works: Licensee may create derivative works based on Software,
* including amending Software’s source code, modifying it, integrating it into a larger work or removing portions of Software,
* as long as no distribution of the derivative works is made
*
* THE RUNTIMES IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE RUNTIMES OR THE USE OR OTHER DEALINGS IN THE
* RUNTIMES.
*****************************************************************************/
#include "CreatureRenderer.h"
namespace CreatureRenderer {
Renderer *
Renderer::create(CreatureModule::CreatureManager * manager_in,
cocos2d::Texture2D * texture_in)
{
Renderer * new_node = new Renderer(manager_in, texture_in);
new_node->autorelease();
return new_node;
}
Renderer::Renderer(CreatureModule::CreatureManager * manager_in,
cocos2d::Texture2D * texture_in)
: manager(manager_in), texture(nullptr), debug_draw(false)
{
setGLProgram(cocos2d::GLProgramCache::getInstance()->getGLProgram(cocos2d::GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setTexture(texture_in);
scheduleUpdate();
}
Renderer::~Renderer()
{
if (texture)
{
CC_SAFE_RELEASE_NULL(texture);
}
}
void
Renderer::setBlendFunc (const cocos2d::BlendFunc& blendFunc)
{
_blendFunc = blendFunc;
}
const cocos2d::BlendFunc&
Renderer::getBlendFunc () const
{
return _blendFunc;
}
void
Renderer::update(float delta)
{
manager->Update(delta);
auto compute_bounds = GetCharacterBounds();
character_bounds = cocos2d::Rect(compute_bounds.first.x, compute_bounds.first.y,
compute_bounds.second.x - compute_bounds.first.x,
compute_bounds.second.y - compute_bounds.first.y);
if (metadata) {
if (useSkinSwap())
{
processSkinswap();
}
else if (useLayerOrder())
{
processLayerorder(static_cast<int>(manager->getActualRunTime()));
}
}
}
void
Renderer::draw(cocos2d::Renderer* renderer,
const cocos2d::Mat4& transform,
uint32_t transformFlags)
{
_drawCommand.init(_globalZOrder);
_drawCommand.func = CC_CALLBACK_0(Renderer::doDraw, this, transform, transformFlags);
renderer->addCommand(&_drawCommand);
}
std::pair<glm::vec2, glm::vec2>
Renderer::GetCharacterBounds()
{
auto cur_pts = manager->GetCreature()->GetRenderPts();
glm::vec2 min_pt, max_pt;
if( manager->GetCreature()->GetTotalNumPoints() > 0)
{
min_pt.x = cur_pts[0];
min_pt.y = cur_pts[1];
max_pt = min_pt;
}
for(int i = 0; i < manager->GetCreature()->GetTotalNumPoints() * 3; i+= 3)
{
glm::float32 cur_x = cur_pts[i];
glm::float32 cur_y = cur_pts[i + 1];
if(cur_x < min_pt.x)
{
min_pt.x = cur_x;
}
if(cur_y < min_pt.y)
{
min_pt.y = cur_y;
}
if(cur_x > max_pt.x)
{
max_pt.x = cur_x;
}
if(cur_y > max_pt.y)
{
max_pt.y = cur_y;
}
}
return std::make_pair(min_pt, max_pt);
}
void Renderer::loadMetaData(const std::string & filename)
{
std::ifstream read_file;
read_file.open(filename.c_str());
std::stringstream str_stream;
str_stream << read_file.rdbuf();
read_file.close();
metadata = std::make_unique<CreatureModule::CreatureMetaData>(str_stream.str());
meta_indices.resize(manager->GetCreature()->GetTotalNumIndices());
}
void Renderer::setTexture(cocos2d::Texture2D* texture_in)
{
if (texture != texture_in) {
CC_SAFE_RELEASE(texture);
CC_SAFE_RETAIN(texture_in);
texture = texture_in;
}
}
void
Renderer::SetDebugDraw(bool flag_in)
{
debug_draw = flag_in;
}
void
Renderer::doDraw(const cocos2d::Mat4& transform, uint32_t transformFlags)
{
cocos2d::Director* director = cocos2d::Director::getInstance();
cocos2d::Size frameSize = director->getOpenGLView()->getFrameSize();
cocos2d::Rect curScreenRect(0, 0, frameSize.width, frameSize.height);
cocos2d::Vec3 character_min_pt(character_bounds.getMinX(), character_bounds.getMinY(), 0);
cocos2d::Vec3 character_max_pt(character_bounds.getMaxX(), character_bounds.getMaxY(), 0);
//cocos2d::Vec3 character_min_world_pt, character_max_world_pt;
transform.transformPoint(&character_min_pt);
transform.transformPoint(&character_max_pt);
cocos2d::Rect character_world_rect(character_min_pt.x, character_min_pt.y,
character_max_pt.x - character_min_pt.x,
character_max_pt.y - character_min_pt.y);
if(curScreenRect.intersectsRect(character_world_rect) == false)
{
// not on screen, skip drawing
return;
}
getGLProgramState()->apply(transform);
cocos2d::GL::bindTexture2D(texture->getName());
cocos2d::Color3B nodeColor = getColor();
manager->GetCreature()->FillRenderColours(nodeColor.r,
nodeColor.g,
nodeColor.b,
getDisplayedOpacity());
glEnableVertexAttribArray(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION);
glEnableVertexAttribArray(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR);
glEnableVertexAttribArray(cocos2d::GLProgram::VERTEX_ATTRIB_TEX_COORDS);
glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_POSITION,
3,
GL_FLOAT,
GL_FALSE,
0,
manager->GetCreature()->GetRenderPts());
glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_COLOR,
4,
GL_UNSIGNED_BYTE,
GL_TRUE,
0,
manager->GetCreature()->GetRenderColours());
glVertexAttribPointer(cocos2d::GLProgram::VERTEX_ATTRIB_TEX_COORDS,
2,
GL_FLOAT,
GL_FALSE,
0,
manager->GetCreature()->GetGlobalUvs());
bool use_meta_indices = (metadata
&& (useSkinSwap() || use_layerorder)
&& (real_meta_indices.empty() == false));
glDrawElements(
GL_TRIANGLES,
use_meta_indices ? (int)real_meta_indices.size() : manager->GetCreature()->GetTotalNumIndices(),
GL_UNSIGNED_INT,
use_meta_indices ? real_meta_indices.data() : manager->GetCreature()->GetGlobalIndices());
#ifdef _CREATURE_DEBUG_DRAW
if(debug_draw)
{
director->pushMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);
cocos2d::DrawPrimitives::setPointSize(5.0);
glLineWidth(5.0f);
cocos2d::DrawPrimitives::setDrawColor4F(1, 1, 1, 1);
drawDebugBones(manager->GetCreature()->GetRenderComposition()->getRootBone());
director->popMatrix(cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}
#endif
}
#ifdef _CREATURE_DEBUG_DRAW
void
Renderer::drawDebugBones(meshBone * draw_bone)
{
glm::vec4 pt1 = draw_bone->getWorldStartPt();
glm::vec4 pt2 = draw_bone->getWorldEndPt();
cocos2d::DrawPrimitives::drawLine(cocos2d::Vec2(pt1.x, pt1.y),
cocos2d::Vec2(pt2.x, pt2.y));
const std::vector<meshBone *>& bone_children = draw_bone->getChildren();
for(auto& cur_child : bone_children)
{
drawDebugBones(cur_child);
}
}
#endif
void Renderer::processSkinswap()
{
int real_indices_size = 0;
auto render_composition = manager->GetCreature()->GetRenderComposition();
auto retval = metadata->buildSkinSwapIndices(
skinswap_name,
render_composition,
[&](int idx, int value)
{
meta_indices[idx] = value;
},
real_indices_size
);
if (retval)
{
if (real_meta_indices.size() != (int)real_indices_size)
{
real_meta_indices.resize(real_indices_size);
}
for (int i = 0; i < real_indices_size; i++)
{
real_meta_indices[i] = meta_indices[i];
}
}
}
void Renderer::processLayerorder(int time_in)
{
metadata->updateIndicesAndPoints(
manager->GetCreature()->GetGlobalIndices(),
[&](int idx, int value)
{
meta_indices[idx] = value;
},
manager->GetCreature()->GetTotalNumIndices(),
manager->GetActiveAnimationName(),
time_in
);
if (real_meta_indices.size() != meta_indices.size())
{
real_meta_indices.resize(meta_indices.size());
}
for (int i = 0; i < (int)real_meta_indices.size(); i++)
{
real_meta_indices[i] = meta_indices[i];
}
}
}