forked from scottcgi/Mojoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite.c
475 lines (374 loc) · 14.2 KB
/
Sprite.c
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
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2013-4-20
* Update : 2019-1-19
* Author : scott.cgi
*/
#include <stdlib.h>
#include <memory.h>
#include "Engine/Toolkit/Platform/Log.h"
#include "Engine/Graphics/OpenGL/Sprite.h"
#include "Engine/Toolkit/HeaderUtils/Struct.h"
#include "Engine/Graphics/OpenGL/Shader/ShaderSprite.h"
#include "Engine/Graphics/Graphics.h"
#include "Engine/Graphics/OpenGL/GLTool.h"
static void Render(Drawable* drawable)
{
Sprite* sprite = AStruct_GetParent(drawable, Sprite);
AShaderSprite->Use(drawable->mvpMatrix, sprite->drawable->blendColor);
glBindTexture(GL_TEXTURE_2D, sprite->texture->id);
if (sprite->isDeformed)
{
sprite->isDeformed = false;
if (AGraphics->isUseVBO)
{
// load the vertex data
glBindBuffer(GL_ARRAY_BUFFER, sprite->vboIDs[Sprite_BufferVertex]);
// without vao state update sub data
if (AGraphics->isUseMapBuffer)
{
void* mappedPtr = glMapBufferRange
(
GL_ARRAY_BUFFER,
0,
sprite->vertexDataSize,
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT
);
memcpy(mappedPtr, sprite->vertexArr->data, (size_t) sprite->vertexDataSize);
glUnmapBuffer(GL_ARRAY_BUFFER);
}
else
{
glBufferSubData(GL_ARRAY_BUFFER, 0, sprite->vertexDataSize , sprite->vertexArr->data);
}
if (AGraphics->isUseVAO)
{
// clear VBO bind
glBindBuffer(GL_ARRAY_BUFFER, 0);
goto UseVAO;
}
goto UseVBO;
}
goto UseNormal;
}
if (AGraphics->isUseVAO)
{
UseVAO:
glBindVertexArray(sprite->vaoID);
glDrawElements(GL_TRIANGLES, sprite->indexCount, GL_UNSIGNED_SHORT, 0);
// clear VAO bind
glBindVertexArray(0);
}
else if (AGraphics->isUseVBO)
{
glBindBuffer(GL_ARRAY_BUFFER, sprite->vboIDs[Sprite_BufferVertex]);
UseVBO:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sprite->vboIDs[Sprite_BufferIndex]);
// load the position and texture coordinate
glVertexAttribPointer
(
(GLuint) AShaderSprite->attribPositionTexcoord,
Sprite_VertexNum,
GL_FLOAT,
false,
Sprite_VertexStride,
0
);
glDrawElements(GL_TRIANGLES, sprite->indexCount, GL_UNSIGNED_SHORT, 0);
// clear VBO bind
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
else
{
UseNormal:
// load the position and texture coordinate
glVertexAttribPointer
(
(GLuint) AShaderSprite->attribPositionTexcoord,
Sprite_VertexNum,
GL_FLOAT,
false,
Sprite_VertexStride,
sprite->vertexArr->data
);
glDrawElements(GL_TRIANGLES, sprite->indexCount, GL_UNSIGNED_SHORT, sprite->indexArr->data);
}
}
static void Release(Sprite* sprite)
{
free(sprite->vertexArr);
free(sprite->indexArr);
sprite->indexArr = NULL;
sprite->vertexArr = NULL;
sprite->texture = NULL;
if (AGraphics->isUseVBO)
{
glDeleteBuffers(Sprite_BufferNum, sprite->vboIDs);
sprite->vboIDs[Sprite_BufferVertex] = 0;
sprite->vboIDs[Sprite_BufferIndex] = 0;
if (AGraphics->isUseVAO)
{
glDeleteVertexArrays(1, &sprite->vaoID);
sprite->vaoID = 0;
}
}
}
static inline void InitSprite(Sprite* sprite, Texture* texture, Array(Quad)* quadArr)
{
Drawable* drawable = sprite->drawable;
ADrawable->Init(drawable);
// calculate and cache drawable mvp matrix
ADrawable_AddState(drawable, DrawableState_IsUpdateMVPMatrix);
AQuad->GetMaxSize(quadArr, &drawable->width, &drawable->height);
sprite->texture = texture;
sprite->uvWidth = AGLTool_ToUVWidth(drawable->width, texture->width);
sprite->uvHeight = AGLTool_ToUVWidth(drawable->height, texture->height);
sprite->vboIDs[Sprite_BufferVertex] = 0;
sprite->vboIDs[Sprite_BufferIndex] = 0;
sprite->vaoID = 0;
sprite->indexCount = quadArr->length * Quad_IndexNum;
sprite->vertexArr = AArray->Create(sizeof(float), quadArr->length * Quad_Position2UVNum);
sprite->indexArr = AArray->Create(sizeof(short), sprite->indexCount);
sprite->vertexDataSize = sprite->vertexArr->length * sizeof(float);
sprite->isDeformed = false;
drawable->Render = Render;
for (int i = 0; i < quadArr->length; ++i)
{
AQuad->GetPosition2UV
(
AArray_GetPtr(quadArr, i, Quad),
texture,
(float*) sprite->vertexArr->data + i * Quad_Position2UVNum
);
AQuad->GetIndex(i * Quad_VertexNum, (short*) sprite->indexArr->data + i * Quad_IndexNum);
}
if (AGraphics->isUseVBO)
{
if (sprite->vboIDs[Sprite_BufferVertex] == 0)
{
glGenBuffers(Sprite_BufferNum, sprite->vboIDs);
}
// vertex
glBindBuffer(GL_ARRAY_BUFFER, sprite->vboIDs[Sprite_BufferVertex]);
glBufferData
(
GL_ARRAY_BUFFER,
sprite->vertexDataSize,
sprite->vertexArr->data,
GL_STATIC_DRAW
);
// index
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sprite->vboIDs[Sprite_BufferIndex]);
glBufferData
(
GL_ELEMENT_ARRAY_BUFFER,
sprite->indexArr->length * sizeof(short),
sprite->indexArr->data,
GL_STATIC_DRAW
);
if (AGraphics->isUseVAO)
{
if (sprite->vaoID == 0)
{
glGenVertexArrays(1, &sprite->vaoID);
}
glBindVertexArray(sprite->vaoID);
// with vao has own state
glBindBuffer(GL_ARRAY_BUFFER, sprite->vboIDs[Sprite_BufferVertex]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sprite->vboIDs[Sprite_BufferIndex]);
glEnableVertexAttribArray((GLuint) AShaderSprite->attribPositionTexcoord);
// load the position and texture coordinate
glVertexAttribPointer
(
(GLuint) AShaderSprite->attribPositionTexcoord,
Sprite_VertexNum,
GL_FLOAT,
false,
Sprite_VertexStride,
0
);
// go back to normal state
glBindVertexArray(0);
}
}
}
#define CheckDeformLength(arr) \
ALog_A \
( \
(arr)->length == length, \
"ASprite Deform " #arr " length = %d must equals the half length = %d of sprite vertexArr", \
(arr)->length, \
sprite->vertexArr->length \
)
static void Deform(Sprite* sprite, Array(float)* positionDeformArr, Array(float)* uvDeformArr)
{
int length = sprite->vertexArr->length >> 1;
float* vertices = sprite->vertexArr->data;
if (positionDeformArr != NULL && uvDeformArr != NULL)
{
CheckDeformLength(positionDeformArr);
CheckDeformLength(uvDeformArr);
float* positions = positionDeformArr->data;
float* uvs = uvDeformArr->data;
for (int i = 0; i < positionDeformArr->length; i += Sprite_VertexPositionNum)
{
int i1 = i + 1;
int j = i << 1; // to vertexArr position index
vertices[j] += positions[i]; // x
vertices[j + 1] += positions[i1]; // y
vertices[j + 2] += uvs [i]; // u
vertices[j + 3] += uvs [i1]; // v
}
}
else if (positionDeformArr != NULL)
{
CheckDeformLength(positionDeformArr);
float* positions = positionDeformArr->data;
for (int i = 0; i < positionDeformArr->length; i += Sprite_VertexPositionNum)
{
int j = i << 1; // to vertexArr position index
vertices[j] += positions[i]; // x
vertices[j + 1] += positions[i + 1]; // y
}
}
else if (uvDeformArr != NULL)
{
CheckDeformLength(uvDeformArr);
float* uvs = uvDeformArr->data;
for (int i = 0; i < uvDeformArr->length; i += Sprite_VertexPositionNum)
{
int j = i << 1; // to vertexArr position index
vertices[j + 2] += uvs[i]; // x
vertices[j + 3] += uvs[i + 1]; // y
}
}
else
{
ALog_A(false, "ASprite Deform the positionDeformArr and uvDeformArr cannot both NULL");
}
sprite->isDeformed = true;
}
#define CheckDeformByIndex(tag, len) \
ALog_A \
( \
(len) <= length, \
"ASprite DeformByIndex the " tag " = %d must less than half length = %d of sprite vertexArr", \
(len), \
sprite->vertexArr->length \
)
static inline int GetPositionIndex(int index)
{
int offset = index % Sprite_VertexPositionNum; // (0 or 1), offset of one vertex data
int positionStartIndex = (index - offset) << 1; // to vertexArr position start index
// position index
return positionStartIndex + offset;
}
static void DeformByIndex(Sprite* sprite, Array(float)* positionDeformArr, Array(float)* uvDeformArr, Array(int)* indexArr)
{
int length = sprite->vertexArr->length >> 1;
CheckDeformByIndex("indexArr length", indexArr->length);
float* vertices = sprite->vertexArr->data;
int* indices = indexArr->data;
if (positionDeformArr != NULL && uvDeformArr != NULL)
{
CheckDeformByIndex("positionDeformArr length", positionDeformArr->length);
CheckDeformByIndex("uvDeformArr length", uvDeformArr->length);
float* positions = positionDeformArr->data;
float* uvs = uvDeformArr->data;
for (int i = 0; i < indexArr->length; ++i)
{
int index = GetPositionIndex(indices[i]);
vertices[index] += positions[i];
vertices[index + Sprite_VertexPositionNum] += uvs [i];
}
}
else if (positionDeformArr != NULL)
{
CheckDeformByIndex("positionDeformArr length", positionDeformArr->length);
float* positions = positionDeformArr->data;
for (int i = 0; i < indexArr->length; ++i)
{
vertices[GetPositionIndex(indices[i])] += positions[i];
}
}
else if (uvDeformArr != NULL)
{
CheckDeformByIndex("uvDeformArr length", uvDeformArr->length);
float* uvs = uvDeformArr->data;
for (int i = 0; i < indexArr->length; ++i)
{
vertices[GetPositionIndex(indices[i]) + Sprite_VertexPositionNum] += uvs[i];
}
}
else
{
ALog_A(false, "ASprite DeformByIndex the positionDeformArr and uvDeformArr cannot both NULL");
}
sprite->isDeformed = true;
}
#undef CheckDeformLength
#undef CheckDeformByIndex
static void Init(Texture* texture, Sprite* outSprite)
{
Quad quad[1];
AQuad->Init(texture->width, texture->height, quad);
InitSprite(outSprite, texture, (Array(Quad)[1]) {quad, 1}); // equals AArray_Make(Quad, 1, *quad)
}
static Sprite* Create(Texture* texture)
{
Sprite* sprite = malloc(sizeof(Sprite));
Init(texture, sprite);
return sprite;
}
static void InitWithQuad(Texture* texture, Quad* quad, Sprite* outSprite)
{
InitSprite(outSprite, texture, (Array(Quad)[1]) {quad, 1}); // equals AArray_Make(Quad, 1, *quad)
}
static Sprite* CreateWithQuad(Texture* texture, Quad* quad)
{
Sprite* sprite = malloc(sizeof(Sprite));
InitWithQuad(texture, quad, sprite);
return sprite;
}
static Sprite* CreateWithQuadArray(Texture* texture, Array(Quad)* quadArr)
{
Sprite* sprite = malloc(sizeof(Sprite));
InitSprite(sprite, texture, quadArr);
return sprite;
}
static void InitWithQuadArray(Texture* texture, Array(Quad)* quadArr, Sprite* outSprite)
{
InitSprite(outSprite, texture, quadArr);
}
static Sprite* CreateWithFile(const char* resourceFilePath)
{
return Create(ATexture->Get(resourceFilePath));
}
static void InitWithFile(const char* resourceFilePath, Sprite* outSprite)
{
Init(ATexture->Get(resourceFilePath), outSprite);
}
struct ASprite ASprite[1] =
{{
Create,
Init,
CreateWithFile,
InitWithFile,
CreateWithQuad,
InitWithQuad,
CreateWithQuadArray,
InitWithQuadArray,
Release,
Deform,
DeformByIndex,
Render,
}};