PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Cube3D.h 2.3KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef CUBE_3D_H__
  2. #define CUBE_3D_H__
  3. #include <Arduino.h>
  4. #include "ILI9341_t3.h"
  5. #include "MathUtil.h"
  6. #include "BaseAnimation.h"
  7. const float CUBE_3D_ROTATE_SPEED = 0.02f;
  8. class Cube3D : public BaseAnimation {
  9. public:
  10. Cube3D() : BaseAnimation() {};
  11. void init( ILI9341_t3 tft );
  12. uint_fast16_t bgColor( void );
  13. String title();
  14. void perFrame( ILI9341_t3 tft, FrameParams frameParams );
  15. private:
  16. float _phase = 0;
  17. float _audio = 0;
  18. uint_fast16_t _bgColor;
  19. };
  20. void Cube3D::init( ILI9341_t3 tft ) {
  21. _bgColor = tft.color565( 0, 0, 0 );
  22. }
  23. uint_fast16_t Cube3D::bgColor(){
  24. return _bgColor;
  25. }
  26. String Cube3D::title() {
  27. return "Cube3D";
  28. }
  29. void Cube3D::perFrame( ILI9341_t3 tft, FrameParams frameParams ) {
  30. uint_fast16_t w = (uint_fast16_t)tft.width();
  31. uint_fast16_t h = (uint_fast16_t)tft.height();
  32. uint_fast16_t w_2 = (w>>1);
  33. uint_fast16_t h_2 = (h>>1);
  34. float oldPhase = _phase;
  35. float oldAudio = _audio;
  36. float oldCos = cos( oldPhase );
  37. float oldSin = sin( oldPhase );
  38. _phase += frameParams.timeMult * CUBE_3D_ROTATE_SPEED;
  39. _audio = frameParams.audioMean;
  40. float pCos = cos( _phase );
  41. float pSin = sin( _phase );
  42. //uint_fast16_t border = tft.color565( 0x08, 0, 0 );
  43. uint_fast16_t eraseColor = tft.color565( 0, 0, 0 );
  44. // Rotating cube yo
  45. for( float x=-1.0f; x<=1.0f; x+=0.333f ) {
  46. for( float y=-1.0f; y<=1.0f; y+=1.0f ) {
  47. for( float z=-1.0f; z<=1.0f; z+=1.0f ) {
  48. // Audio hash
  49. float hash = 3.7f*x + 5.1f*y + 9.8f*z;
  50. hash -= floor(hash);
  51. hash = (hash*2.0) - 1.0; // bipolar signal, -1..1
  52. // Erase the old point
  53. float x3d = oldCos*x - oldSin*z;
  54. float y3d = y + hash*oldAudio;
  55. float z3d = 3.0 + oldCos*z + oldSin*x;
  56. Point16 pt = xyz2screen( x3d, y3d, z3d, w_2, h_2 );
  57. //tft.drawCircle( pt.x, pt.y, 5, border );
  58. if( pt.y >= 0 ) { // sanity check
  59. tft.drawCircle( pt.x, pt.y, 7.0-z3d, eraseColor );
  60. }
  61. x3d = pCos*x - pSin*z;
  62. y3d = y + hash*_audio;
  63. z3d = 3.0 + pCos*z + pSin*x;
  64. pt = xyz2screen( x3d, y3d, z3d, w_2, h_2 );
  65. if( pt.y >= 0 ) { // sanity check
  66. float bright = ((3.0f+M_SQRT2)-z3d) * (1.0f/(2*M_SQRT2)); // range 0..1, higher number == closer
  67. uint_fast16_t fillColor = tft.color565( 0x44, lerp8(0x66,0xff,bright), 0x44 );
  68. tft.drawCircle( pt.x, pt.y, 7.0-z3d, fillColor );
  69. }
  70. }
  71. }
  72. }
  73. }
  74. #endif