Select one of the symbols to view example projects that use it.
 
Outline
#include <gui/animated_graphics_screen/BumpMapImage.hpp>
#include <touchgfx/EasingEquations.hpp>
BumpMapImage::BumpMapImage(BitmapId, const unsigned int *, const unsigned char *)
BumpMapImage::~BumpMapImage()
BumpMapImage::handleTickEvent()
BumpMapImage::handleClickEvent(const ClickEvent &)
BumpMapImage::draw(const touchgfx::Rect &) const
BumpMapImage::drawOptimized(const touchgfx::Rect &) const
BumpMapImage::drawNormal(const touchgfx::Rect &) const
BumpMapImage::invalidateAccordingToCurrentLightSetting()
BumpMapImage::setLightPosition(uint16_t, uint16_t)
BumpMapImage::setDragFinish(bool)
BumpMapImage::height(uint16_t) const
BumpMapImage::getSolidRect() const
#include <iostream>
#include <fstream>
Files
loading...
CodeScopeSTM32 Libraries and SamplesTouchGFXGui/gui/src/animated_graphics_screen/BumpMapImage.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * This file is part of the TouchGFX 4.10.0 distribution. * * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ #include <gui/animated_graphics_screen/BumpMapImage.hpp> #include <touchgfx/EasingEquations.hpp> BumpMapImage::BumpMapImage(BitmapId image_, const unsigned int* bump_map_, const unsigned char* light_source_) : currentState(ANIMATE_AUTO), image(image_), //bump(BITMAP_LIGHT_EFFECT_IMAGE_ID), bump(0), bump_map(bump_map_), light_source(light_source_), currentTime(0), catchTime(25), lightPositionX(1000), // Set position outside the visible area lightPositionY(1000), tempPositionX(1000), tempPositionY(1000) { Application::getInstance()->registerTimerWidget(this); // Use these methods to convert and print a bitmap or light table //printBumpMap(); //printLightTable(); }{ ... } BumpMapImage::~BumpMapImage() { Application::getInstance()->unregisterTimerWidget(this); }{ ... } void BumpMapImage::handleTickEvent() { currentTime += 2; tempPositionX = (int)(128.0f * cos((double)currentTime / 64)) - 20 + 160; tempPositionY = (int)(128.0f * sin((double) - currentTime / 45)) + 20 + 110; invalidateAccordingToCurrentLightSetting(); if (currentState == ANIMATE_AUTO) { lightPositionX = tempPositionX; lightPositionY = tempPositionY; }if (currentState == ANIMATE_AUTO) { ... } else if (currentState == ANIMATE_CATCH) { if (catchTime > 0) { lightPositionX = lightPositionX + ((tempPositionX - lightPositionX) / catchTime); lightPositionY = lightPositionY + ((tempPositionY - lightPositionY) / catchTime); catchTime--; }if (catchTime > 0) { ... } else { catchTime = 25; currentState = ANIMATE_AUTO; }else { ... } }else if (currentState == ANIMATE_CATCH) { ... } invalidateAccordingToCurrentLightSetting(); }{ ... } void BumpMapImage::handleClickEvent(const ClickEvent& evt) { if (evt.getType() == ClickEvent::PRESSED) { setDragFinish(false); }if (evt.getType() == ClickEvent::PRESSED) { ... } else { setDragFinish(true); }else { ... } }{ ... } void BumpMapImage::draw(const touchgfx::Rect& r) const { drawOptimized(r); //drawNormal(r); ...} void BumpMapImage::drawOptimized(const touchgfx::Rect& r) const { // invalidatedArea is relative to enclosing container Rect absoluteRect = r; translateRectToAbsolute(absoluteRect); // In case of an odd r.x draw() will also redraw r.x-1 // This is due to the fact that an uint32_t pointer is // used for the imageData/FB pointers. This is chosen // because of optimized performance. // This issue does not affect this demo screen. /* assert(absoluteRect.x % 2 == 0); assert(absoluteRect.width % 2 == 0); assert(HAL::DISPLAY_WIDTH % 2 == 0); *//* ... */ // Bump map slope information int16_t nx; int16_t ny; int16_t lightCalculationPositionX; int16_t lightCalculationPositionY; uint32_t* fb = (uint32_t*)HAL::getInstance()->lockFrameBuffer(); const uint32_t* imageData = (const uint32_t*)(image.getData()); int offset; uint16_t invalidatedY = absoluteRect.y; uint16_t invalidatedX = absoluteRect.x; uint32_t newValue = 0; uint16_t imageDataValue = 0; uint8_t lightDiff = 0; uint8_t red = 0; uint8_t green = 0; uint8_t blue = 0; uint32_t colorResult = 0; int relativeX; int relativeY; int bumpMapInfo; uint32_t pixelColorValues; // Go through each invalidated pixel. For optimization reasons the pointers used are 32 bits, // so each read yield values for two pixels. for (int absoluteY = invalidatedY; absoluteY < absoluteRect.bottom(); absoluteY++) { // Set offset to point to the next line in the image bitmap and bump_map offset = (absoluteY * image.getWidth() / 2) + (invalidatedX - getX()) / 2; for (int absoluteX = invalidatedX; absoluteX < absoluteRect.right(); absoluteX += 2, offset += 1) { newValue = 0; relativeX = absoluteX - getX(); relativeY = absoluteY - getY(); bumpMapInfo = bump_map[offset]; pixelColorValues = *(imageData + offset); // Read the bump map value for the current pixel // The bump map info is packed in a 32 bit format and need a bit of shifting nx = (int8_t)(bumpMapInfo >> 24); ny = (int8_t)((bumpMapInfo >> 16) & 0xFF); // Calculate the pixels position in the light lightCalculationPositionX = (relativeX - lightPositionX) + (lightSourceWidth / 2) + nx; lightCalculationPositionY = (relativeY - lightPositionY) + (lightSourceHeight / 2) + ny; //lightDiff = light(lightCalculationPositionX, lightCalculationPositionY); // Get the light value that should be applied to the pixel if (lightCalculationPositionX < 0 || lightCalculationPositionY < 0 || lightCalculationPositionX >= lightSourceWidth || lightCalculationPositionY >= lightSourceHeight) { lightDiff = 0; }if (lightCalculationPositionX < 0 || lightCalculationPositionY < 0 || lightCalculationPositionX >= lightSourceWidth || lightCalculationPositionY >= lightSourceHeight) { ... } else { // Value can be found in the precalculated light_source lightDiff = light_source[lightCalculationPositionY * lightSourceWidth + lightCalculationPositionX]; }else { ... } // Get the actual color value from the 32 bit value (which contains two pixels) from the image imageDataValue = pixelColorValues & 0xFFFF; red = (imageDataValue & 0xF800) >> 11; green = (imageDataValue & 0x07E0) >> 5; blue = imageDataValue & 0x001F; // Apply the light effect if (lightDiff < lightMax / 2) { // Darken pixels that have no or a little extra light red = red / 4 + (red - red / 4) * lightDiff / (lightMax / 2); green = green / 4 + (green - green / 4) * lightDiff / (lightMax / 2); blue = blue / 4 + (blue - blue / 4) * lightDiff / (lightMax / 2); }if (lightDiff < lightMax / 2) { ... } else { red = red + (31 - red) * (lightDiff - (lightMax / 2)) / (lightMax / 2); green = green + (63 - green) * (lightDiff - (lightMax / 2)) / (lightMax / 2); blue = blue + (31 - blue) * (lightDiff - (lightMax / 2)) / (lightMax / 2); }else { ... } // Combine the colors and add them to the new color value for the pixel (packed together in a 32 bit value) colorResult = (red << 11) | (green << 5) | blue; newValue |= colorResult; // Do the calculation for the second pixel in the 32 bit read // Read the bump map value for the current pixel nx = (int8_t)((bumpMapInfo >> 8) & 0xFF); ny = (int8_t)(bumpMapInfo & 0xFF); lightCalculationPositionX = (relativeX + 1 - lightPositionX) + (lightSourceWidth / 2) + nx; lightCalculationPositionY = (relativeY + 1 - lightPositionY) + (lightSourceHeight / 2) + ny; //lightDiff = light(lightCalculationPositionX, lightCalculationPositionY); if (lightCalculationPositionX < 0 || lightCalculationPositionY < 0 || lightCalculationPositionX >= lightSourceWidth || lightCalculationPositionY >= lightSourceHeight) { lightDiff = 0; }if (lightCalculationPositionX < 0 || lightCalculationPositionY < 0 || lightCalculationPositionX >= lightSourceWidth || lightCalculationPositionY >= lightSourceHeight) { ... } else { lightDiff = light_source[lightCalculationPositionY * lightSourceWidth + lightCalculationPositionX]; }else { ... } imageDataValue = pixelColorValues >> 16; red = (imageDataValue & 0xF800) >> 11; green = (imageDataValue & 0x07E0) >> 5; blue = imageDataValue & 0x001F; if (lightDiff < lightMax / 2) { // Darken pixels that have no or a little extra light red = red / 4 + (red - red / 4) * lightDiff / (lightMax / 2); green = green / 4 + (green - green / 4) * lightDiff / (lightMax / 2); blue = blue / 4 + (blue - blue / 4) * lightDiff / (lightMax / 2); }if (lightDiff < lightMax / 2) { ... } else { red = red + (31 - red) * (lightDiff - (lightMax / 2)) / (lightMax / 2); green = green + (63 - green) * (lightDiff - (lightMax / 2)) / (lightMax / 2); blue = blue + (31 - blue) * (lightDiff - (lightMax / 2)) / (lightMax / 2); }else { ... } colorResult = (red << 11) | (green << 5) | blue; newValue |= (colorResult << 16); // Update framebuffer *(fb + (absoluteX / 2) + (HAL::DISPLAY_WIDTH / 2 * absoluteY)) = newValue; }for (int absoluteX = invalidatedX; absoluteX < absoluteRect.right(); absoluteX += 2, offset += 1) { ... } }for (int absoluteY = invalidatedY; absoluteY < absoluteRect.bottom(); absoluteY++) { ... } HAL::getInstance()->unlockFrameBuffer(); ...} void BumpMapImage::drawNormal(const touchgfx::Rect& r) const { // invalidatedArea is relative to enclosing container Rect absoluteRect = r; translateRectToAbsolute(absoluteRect); int8_t nx; int8_t ny; int16_t lightCalculationPositionX; int16_t lightCalculationPositionY; uint16_t* fb = HAL::getInstance()->lockFrameBuffer(); const uint16_t* imageData = (const uint16_t*) image.getData(); const uint16_t* bumpData = (const uint16_t*) bump.getData(); // we skip the first line since there are no pixels above to calculate the slope with int offset = absoluteRect.x; if (absoluteRect.y == 0) { while (offset < absoluteRect.width) { fb[offset++] = 0x0000; }while (offset < absoluteRect.width) { ... } offset += (HAL::DISPLAY_WIDTH - absoluteRect.width); }if (absoluteRect.y == 0) { ... } uint16_t invalidatedY = absoluteRect.y; uint16_t invalidatedX = absoluteRect.x; if (invalidatedX == 0) { invalidatedX = 1; }if (invalidatedX == 0) { ... } if (invalidatedY == 0) { invalidatedY = 1; }if (invalidatedY == 0) { ... } int relativeX; int relativeY; uint8_t lightDiff; for (int absoluteY = invalidatedY; absoluteY < absoluteRect.bottom(); absoluteY++) { offset = (absoluteY * image.getWidth()) + invalidatedX - getX(); // likewise, skip first pixel since there are no pixels on the left if (absoluteRect.x == 0) { fb[offset++] = 0; }if (absoluteRect.x == 0) { ... } for (int absoluteX = invalidatedX; absoluteX < absoluteRect.right(); absoluteX++, offset++) { relativeX = absoluteX - getX(); relativeY = absoluteY - getY(); /* if (offset%2==0) { nx = (int8_t)(bump_map[offset/2] >> 24); ny = (int8_t)((bump_map[offset/2] >> 16) & 0xFF); } else { nx = (int8_t)((bump_map[offset/2] >> 8) & 0xFF); ny = (int8_t)(bump_map[offset/2] & 0xFF); }*//* ... */ nx = height(bumpData[offset - 1]) - height(bumpData[offset]); ny = height(bumpData[offset - image.getWidth()]) - height(bumpData[offset]); lightCalculationPositionX = (relativeX - lightPositionX) + (lightSourceWidth / 2) + nx; lightCalculationPositionY = (relativeY - lightPositionY) + (lightSourceHeight / 2) + ny; //lightDiff = light(lightCalculationPositionX, lightCalculationPositionY); if (lightCalculationPositionX < 0 || lightCalculationPositionY < 0 || lightCalculationPositionX >= lightSourceWidth || lightCalculationPositionY >= lightSourceHeight) { lightDiff = 0; }if (lightCalculationPositionX < 0 || lightCalculationPositionY < 0 || lightCalculationPositionX >= lightSourceWidth || lightCalculationPositionY >= lightSourceHeight) { ... } else { lightDiff = light_source[lightCalculationPositionY * lightSourceWidth + lightCalculationPositionX]; }else { ... } uint8_t red = (*(imageData + offset) & 0xF800) >> 11; uint8_t green = (*(imageData + offset) & 0x07E0) >> 5; uint8_t blue = *(imageData + offset) & 0x001F; if (lightDiff < lightMax / 2) { red = red / 4 + (red - red / 4) * lightDiff / (lightMax / 2); green = green / 4 + (green - green / 4) * lightDiff / (lightMax / 2); blue = blue / 4 + (blue - blue / 4) * lightDiff / (lightMax / 2); }if (lightDiff < lightMax / 2) { ... } else { red = red + (31 - red) * (lightDiff - (lightMax / 2)) / (lightMax / 2); green = green + (63 - green) * (lightDiff - (lightMax / 2)) / (lightMax / 2); blue = blue + (31 - blue) * (lightDiff - (lightMax / 2)) / (lightMax / 2); }else { ... } // Update framebuffer *(fb + absoluteX + (HAL::DISPLAY_WIDTH * absoluteY)) = (uint16_t)((red << 11) | (green << 5) | blue); }for (int absoluteX = invalidatedX; absoluteX < absoluteRect.right(); absoluteX++, offset++) { ... } }for (int absoluteY = invalidatedY; absoluteY < absoluteRect.bottom(); absoluteY++) { ... } HAL::getInstance()->unlockFrameBuffer(); ...} void BumpMapImage::invalidateAccordingToCurrentLightSetting() { // Include an area around the light cone since these can also be illuminated uint8_t margin = 66; int16_t x = lightPositionX - (lightSourceWidth / 2) - margin; if (x < 0) { x = 0; }if (x < 0) { ... } // Place x on an even address if (x % 2 == 1) { x--; }if (x % 2 == 1) { ... } int16_t y = lightPositionY - (lightSourceHeight / 2) - margin; if (y < 0) { y = 0; }if (y < 0) { ... } uint16_t width = lightSourceWidth + 2 * margin; uint16_t height = lightSourceHeight + 2 * margin; // Ensure that the width is even if (width % 2 == 1) { width++; }if (width % 2 == 1) { ... } Rect newRect = Rect(x, y, width, height); invalidateRect(newRect); }{ ... } void BumpMapImage::setLightPosition(uint16_t x, uint16_t y) { lightPositionX = x; lightPositionY = y; invalidateAccordingToCurrentLightSetting(); }{ ... } void BumpMapImage::setDragFinish(bool finish) { if (finish) { currentState = ANIMATE_CATCH; }if (finish) { ... } else { currentState = DRAGGING; }else { ... } }{ ... } int BumpMapImage::height(uint16_t value) const { uint8_t red = (value & 0xF800) >> 11; uint8_t green = (value & 0x07E0) >> 5; uint8_t blue = value & 0x001F; return red + green + blue; ...} Rect BumpMapImage::getSolidRect() const { return Rect(0, 0, getWidth(), getHeight()); ...} #ifdef SIMULATOR #include <iostream> #include <fstream> void BumpMapImage::printLightTable() { std::ofstream outputFile; outputFile.open("lightTableTemp.txt"); uint32_t result; outputFile << "const unsigned short _light_source[] = {\n "; for (int y = 0; y < lightSourceHeight; y++) { for (int x = 0; x < lightSourceWidth; x++) { result = light(x, y); outputFile << std::hex << "0x" << result; if (!((x == lightSourceWidth - 1) && (y == lightSourceHeight - 1))) { outputFile << ", "; }if (!((x == lightSourceWidth - 1) && (y == lightSourceHeight - 1))) { ... } }for (int x = 0; x < lightSourceWidth; x++) { ... } }for (int y = 0; y < lightSourceHeight; y++) { ... } outputFile << "\n};"; outputFile.close(); }printLightTable () { ... } void BumpMapImage::printBumpMap() { std::ofstream outputFile; outputFile.open("bumpMapTemp.txt"); int8_t nx; int8_t ny; //uint16_t *fb = HAL::getInstance()->lockFrameBuffer(); //const uint16_t *imageData = image.getData(); const uint16_t* bumpData = (const uint16_t*) bump.getData(); // we skip the first line since there are no pixels above to calculate the slope with int offset = 0; while (offset < image.getWidth() / 2) { outputFile << std::hex << "0x" << 0 << ", "; offset++; }while (offset < image.getWidth() / 2) { ... } offset = image.getWidth(); uint32_t printValue = 0; for (int y = 1; y < image.getHeight(); y++) { // likewise, skip first pixel since there are no pixels on the left offset++; for (int x = 1; x < image.getWidth(); x++, offset++) { nx = height(bumpData[offset - 1]) - height(bumpData[offset]); ny = height(bumpData[offset - image.getWidth()]) - height(bumpData[offset]); if (x % 2 == 0) { printValue |= (uint32_t)((((uint8_t)(nx) << 8) | (uint8_t)(ny)) << 16); }if (x % 2 == 0) { ... } else { printValue |= ((uint8_t)(nx) << 8) | (uint8_t)(ny); outputFile << std::hex << "0x" << printValue << ", "; printValue = 0; }else { ... } }for (int x = 1; x < image.getWidth(); x++, offset++) { ... } }for (int y = 1; y < image.getHeight(); y++) { ... } HAL::getInstance()->unlockFrameBuffer(); outputFile.close(); }printBumpMap () { ... } uint8_t BumpMapImage::light(int16_t x, int16_t y) const { if (x < 0 || y < 0 || x > lightSourceWidth || y > lightSourceHeight) { return 0; }if (x < 0 || y < 0 || x > lightSourceWidth || y > lightSourceHeight) { ... } uint16_t distToCenter = (uint16_t)(sqrt((float)((x - lightSourceWidth / 2) * (x - lightSourceWidth / 2) + (y - lightSourceHeight / 2) * (y - lightSourceHeight / 2)))); //uint16_t distToCenter = x - lightSourceWidth/2; int16_t result = lightMax - distToCenter; result = (result > 0) ? result : 0; return (uint8_t) result; ...} /* ... */ #endif // SIMULATOR