Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include "cinder/app/App.h"
- #include "cinder/app/RendererGl.h"
- #include "cinder/gl/gl.h"
- #include "cinder/Rand.h"
- #include "cinder/ImageIo.h"
- #include "cinder/gl/Texture.h"
- using namespace ci;
- using namespace ci::app;
- using namespace std;
- // ======================================================================
- const int WINDOW_WIDTH { 1280 };
- const int WINDOW_HEIGHT { 720 };
- const int PRT_SPACING { 10 };
- const int PRT_RES_X { WINDOW_WIDTH / PRT_SPACING };
- const int PRT_RES_Y { WINDOW_HEIGHT / PRT_SPACING };
- const int PRT_COUNT { PRT_RES_X * PRT_RES_Y };
- // ======================================================================
- class Particle {
- vec2 origin;
- vec2 location;
- vec2 direction;
- float velocity { 0.0f };
- float radius { 5.0f };
- Color color;
- public:
- Particle( vec2 new_location, float new_radius, float new_velocity=0.0f ) {
- location = origin = new_location;
- direction = Rand::randVec2() * 5.0f;
- // velocity = Rand::randFloat() * 5.0f;
- velocity = new_velocity;
- set_radius( new_radius );
- color = Color( 1.0f, 1.0f, 1.0f );
- }
- void set_radius( const float new_radius ) {
- radius = new_radius;
- if ( radius < 0.1f ) {
- radius = 0.1f;
- }
- }
- void set_radius_random( const float new_radius ) {
- Rand::randSeed( static_cast<uint32_t>(location.x) + static_cast<uint32_t>(location.y) * PRT_RES_X );
- set_radius( Rand::randFloat() * new_radius );
- }
- void update( const float r, const Channel32f &chan ) {
- // I know this is highly unoptimized
- float w = static_cast<float>( getWindowWidth() );
- float h = static_cast<float>( getWindowHeight() );
- const vec2 travel = direction * velocity;
- const vec2 new_location = location + travel;
- if ( new_location.x < 0 || new_location.x > w ) {
- direction.x *= -1.0f;
- }
- if ( new_location.y < 0 || new_location.y > h ) {
- direction.y *= -1.0f;
- }
- location += direction * velocity;
- set_radius_random( r );
- float value = chan.getValue( location ) * 7.0f;
- color = Color( value, value, value );
- }
- const void draw() {
- gl::color( color );
- gl::drawSolidCircle( location, radius );
- }
- };
- // ======================================================================
- class proj01App : public App {
- gl::TextureRef image;
- Channel32f channel;
- std::vector<Particle> particles;
- float prt_radius = PRT_SPACING * 0.5f;
- public:
- void setup() override;
- void update() override;
- void draw() override;
- void mouseDown( MouseEvent event ) override;
- void mouseWheel( MouseEvent event ) override;
- void keyDown( KeyEvent event ) override;
- // bool
- };
- static void prepare_settings( App::Settings *settings ) {
- settings->setWindowSize( WINDOW_WIDTH, WINDOW_HEIGHT );
- settings->setFrameRate( 60.0f );
- }
- void proj01App::setup()
- {
- auto temp = loadImage( loadAsset( "tutorial_part1_01.jpg" ) );
- image = gl::Texture::create( temp );
- channel = Channel32f( temp );
- particles.reserve( PRT_COUNT );
- for ( int j = 0; j < PRT_RES_Y; j++ ) {
- for ( int i = 0; i < PRT_RES_X; i++ ) {
- // float x = Rand::randFloat() * getWindowWidth();
- // float y = Rand::randFloat() * getWindowHeight();
- float x = static_cast<float>(i + 0.5f ) * PRT_SPACING;
- float y = static_cast<float>(j + 0.5f ) * PRT_SPACING;
- float r = Rand::randFloat() * (prt_radius * 0.5f);
- particles.push_back( Particle(vec2(x,y), r) );
- }
- }
- }
- void proj01App::mouseDown( MouseEvent event )
- {
- for ( auto iterator = particles.begin(); iterator != particles.end(); iterator++ ) {
- float r = Rand::randFloat() * (static_cast<float>(PRT_SPACING) * 0.5f);
- iterator->set_radius( r );
- }
- event.setHandled( true );
- }
- void proj01App::keyDown( KeyEvent event )
- {
- const char value = event.getChar();
- if (value >= '1' && value <= '9') {
- char real_value = value - '1' + 1;
- for ( auto iterator = particles.begin(); iterator != particles.end(); iterator++ ) {
- float r = Rand::randFloat() * (static_cast<float>(PRT_SPACING) * 0.5f);
- iterator->set_radius( real_value );
- }
- console() << "+ Set size to " << value << endl;
- event.setHandled( true );
- }
- }
- void proj01App::mouseWheel( MouseEvent event ) {
- console() << "+ Wheel Down " << event.getWheelIncrement() << endl;
- prt_radius += event.getWheelIncrement();
- if ( prt_radius < 0.0 ) {
- prt_radius = 0.0;
- }
- else if ( prt_radius > PRT_SPACING ) {
- prt_radius = PRT_SPACING;
- }
- event.setHandled( true );
- }
- void proj01App::update()
- {
- for ( auto iterator = particles.begin(); iterator != particles.end(); iterator++ ) {
- iterator->update( prt_radius, channel );
- }
- }
- void proj01App::draw()
- {
- // float gray = static_cast<float>( sin( getElapsedSeconds() * 4.0 ) * 0.5 + 0.5 );
- // gl::clear( Color( gray, gray, gray ), true );
- // if (image) {
- // gl::draw( image, getWindowBounds() );
- // }
- for ( auto iterator = particles.begin(); iterator != particles.end(); iterator++ ) {
- iterator->draw();
- }
- }
- // ======================================================================
- CINDER_APP( proj01App, RendererGl, prepare_settings )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement