Advertisement
Guest User

Untitled

a guest
Aug 28th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. #include "cinder/app/App.h"
  5. #include "cinder/app/RendererGl.h"
  6. #include "cinder/gl/gl.h"
  7. #include "cinder/Rand.h"
  8.  
  9. #include "cinder/ImageIo.h"
  10. #include "cinder/gl/Texture.h"
  11.  
  12. using namespace ci;
  13. using namespace ci::app;
  14. using namespace std;
  15.  
  16. // ======================================================================
  17. const int WINDOW_WIDTH  { 1280 };
  18. const int WINDOW_HEIGHT { 720 };
  19. const int PRT_SPACING   { 10 };
  20. const int PRT_RES_X     { WINDOW_WIDTH / PRT_SPACING };
  21. const int PRT_RES_Y     { WINDOW_HEIGHT / PRT_SPACING };
  22. const int PRT_COUNT     { PRT_RES_X * PRT_RES_Y };
  23.  
  24.  
  25. // ======================================================================
  26. class Particle {
  27.     vec2 origin;
  28.     vec2 location;
  29.     vec2 direction;
  30.     float velocity { 0.0f };
  31.     float radius { 5.0f };
  32.     Color color;
  33.    
  34. public:
  35.     Particle( vec2 new_location, float new_radius, float new_velocity=0.0f ) {
  36.         location  = origin = new_location;
  37.         direction = Rand::randVec2() * 5.0f;
  38.         // velocity  = Rand::randFloat() * 5.0f;
  39.         velocity  = new_velocity;
  40.         set_radius( new_radius );
  41.         color = Color( 1.0f, 1.0f, 1.0f );
  42.     }
  43.  
  44.     void set_radius( const float new_radius ) {
  45.         radius = new_radius;
  46.         if ( radius < 0.1f ) {
  47.             radius = 0.1f;
  48.         }
  49.     }
  50.  
  51.     void set_radius_random( const float new_radius ) {
  52.         Rand::randSeed( static_cast<uint32_t>(location.x) + static_cast<uint32_t>(location.y) * PRT_RES_X );
  53.         set_radius( Rand::randFloat() * new_radius );
  54.     }
  55.  
  56.     void update( const float r, const Channel32f &chan ) {
  57.         // I know this is highly unoptimized
  58.         float w = static_cast<float>( getWindowWidth() );
  59.         float h = static_cast<float>( getWindowHeight() );
  60.  
  61.         const vec2 travel = direction * velocity;
  62.         const vec2 new_location = location + travel;
  63.        
  64.         if ( new_location.x < 0 || new_location.x > w ) {
  65.             direction.x *= -1.0f;
  66.         }
  67.  
  68.         if ( new_location.y < 0 || new_location.y > h ) {
  69.             direction.y *= -1.0f;
  70.         }
  71.  
  72.         location += direction * velocity;
  73.  
  74.         set_radius_random( r );
  75.  
  76.         float value = chan.getValue( location ) * 7.0f;
  77.         color = Color( value, value, value );
  78.     }
  79.    
  80.     const void draw() {
  81.         gl::color( color );
  82.         gl::drawSolidCircle( location, radius );
  83.     }
  84. };
  85.  
  86.  
  87. // ======================================================================
  88. class proj01App : public App {
  89.     gl::TextureRef image;
  90.     Channel32f channel;
  91.     std::vector<Particle> particles;
  92.  
  93.     float prt_radius = PRT_SPACING * 0.5f;
  94.  
  95. public:
  96.     void setup() override;
  97.     void update() override;
  98.     void draw() override;
  99.  
  100.     void mouseDown( MouseEvent event )  override;
  101.     void mouseWheel( MouseEvent event ) override;
  102.     void keyDown( KeyEvent event ) override;
  103.  
  104.     // bool
  105. };
  106.  
  107. static void prepare_settings( App::Settings *settings ) {
  108.     settings->setWindowSize( WINDOW_WIDTH, WINDOW_HEIGHT );
  109.     settings->setFrameRate( 60.0f );
  110. }
  111.  
  112. void proj01App::setup()
  113. {
  114.     auto temp = loadImage( loadAsset( "tutorial_part1_01.jpg" ) );
  115.     image = gl::Texture::create( temp );
  116.     channel = Channel32f( temp );
  117.  
  118.     particles.reserve( PRT_COUNT );
  119.  
  120.     for ( int j = 0; j < PRT_RES_Y; j++ ) {
  121.         for ( int i = 0; i < PRT_RES_X; i++ ) {
  122.             // float x = Rand::randFloat() * getWindowWidth();
  123.             // float y = Rand::randFloat() * getWindowHeight();
  124.             float x = static_cast<float>(i + 0.5f ) * PRT_SPACING;
  125.             float y = static_cast<float>(j + 0.5f ) * PRT_SPACING;
  126.             float r = Rand::randFloat() * (prt_radius * 0.5f);
  127.             particles.push_back( Particle(vec2(x,y), r) );
  128.         }
  129.     }
  130. }
  131.  
  132. void proj01App::mouseDown( MouseEvent event )
  133. {
  134.     for ( auto iterator = particles.begin(); iterator != particles.end(); iterator++ ) {
  135.         float r = Rand::randFloat() * (static_cast<float>(PRT_SPACING) * 0.5f);
  136.         iterator->set_radius( r );
  137.     }
  138.  
  139.     event.setHandled( true );
  140. }
  141.  
  142. void proj01App::keyDown( KeyEvent event )
  143. {
  144.     const char value = event.getChar();
  145.     if (value >= '1' && value <= '9') {
  146.         char real_value = value - '1' + 1;
  147.         for ( auto iterator = particles.begin(); iterator != particles.end(); iterator++ ) {
  148.             float r = Rand::randFloat() * (static_cast<float>(PRT_SPACING) * 0.5f);
  149.             iterator->set_radius( real_value );
  150.         }
  151.         console() << "+ Set size to " << value << endl;
  152.         event.setHandled( true );
  153.     }
  154.  
  155. }
  156.  
  157. void proj01App::mouseWheel( MouseEvent event ) {
  158.     console() << "+ Wheel Down " << event.getWheelIncrement() << endl;
  159.  
  160.     prt_radius += event.getWheelIncrement();
  161.     if ( prt_radius < 0.0 ) {
  162.         prt_radius = 0.0;
  163.     }
  164.     else if ( prt_radius > PRT_SPACING ) {
  165.         prt_radius = PRT_SPACING;
  166.     }
  167.  
  168.     event.setHandled( true );
  169. }
  170.  
  171. void proj01App::update()
  172. {
  173.     for ( auto iterator = particles.begin(); iterator != particles.end(); iterator++ ) {
  174.         iterator->update( prt_radius, channel );
  175.     }
  176. }
  177.  
  178. void proj01App::draw()
  179. {
  180.     // float gray = static_cast<float>( sin( getElapsedSeconds() * 4.0 ) * 0.5 + 0.5 );
  181.     // gl::clear( Color( gray, gray, gray ), true );
  182.    
  183.     // if (image) {
  184.     //  gl::draw( image, getWindowBounds() );
  185.     // }
  186.    
  187.     for ( auto iterator = particles.begin(); iterator != particles.end(); iterator++ ) {
  188.         iterator->draw();
  189.     }
  190. }
  191.  
  192.  
  193. // ======================================================================
  194. CINDER_APP( proj01App, RendererGl, prepare_settings )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement