summaryrefslogtreecommitdiff
path: root/lasertext/src/ofApp.h
blob: bd92da133d0315272d8dab1d926efe55a8446db8 (plain)
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
#pragma once

#include "ofMain.h"
#include "ofxSvg.h"
#include "ofxGui.h"
#include "ofxXmlSettings.h"

#include "ofxHelios.h"
#include "lineTransformer.h"
#include "colourPolyline.h"
#include "vectortext.h"

class scannableColourPolyline: public colourPolyline{
public:
	bool operator < (const scannableColourPolyline& line) const {
		return (operator[](0).x < line[0].x);
	}
};

class star{
public:
	star(ofVec2f p,ofVec2f v,float l){
		pos=p;
		vel=v;
		birthday=ofGetElapsedTimef();
		lifespan=l;
	}
	void update(vector<star>& stars){
		pos+=vel;
	}
	ofVec2f pos;
	ofVec2f vel;
	float birthday;
	float lifespan;
};

class starSystem{
public:
	vector<star> stars;
	float agevar;	
	float last;
	ofxPanel gui;
	ofParameter<float> x;
	ofParameter<float> y;
	ofParameter<float> rate;
    ofParameter<float> radius;
    ofParameter<float> speed;
    ofParameter<float> lifespan;
    ofParameter<bool> bScan;
	starSystem(){
	}
	void init(string name="",int _gx=5, int _gy=5, float _x=0, float _y=0, float _rad=100,float r=0.5,float s=5.0,float l=5.0,float v=1.0){
	
		agevar=v;

		gui.setup(name,"",_gx,_gy);
		gui.add(x.set("x", _x, -2000.0f, 2000.0f));
		gui.add(y.set("y", _y, -2000.0f, 2000.0f));
		gui.add(rate.set("rate", r, 0.01f, 3.0f));
    	gui.add(radius.set("radius", _rad, 10.0f, 500.0f));
    	gui.add(speed.set("speed", s, 0.0f, 10.0f));
    	gui.add(lifespan.set("life", l, 3.0f, 10.0f));
    	gui.add(bScan.set("scan", true));
	}
	void update(){
		float now=ofGetElapsedTimef();
		float segment=now-last;
		last=now;
		for(auto it = stars.begin(); it != stars.end();)
		{
		    if(now-it->birthday>it->lifespan){
		    	//ofLog()<<"erase star";
		        it = stars.erase(it);
		    }
		    else {
		    	//ofLog()<<"update star";
		    	it->update(stars);
		        ++it;
		    }
		}
		if (ofRandom(rate)<segment){
				float a=ofRandom(3.57f);
				float r=ofRandom(radius);
				ofVec2f offset=ofVec2f(cos(a)*r,sin(a)*r);

				//avoid having stars that don't move, for safety
				a=ofRandom(3.57f);
				r=(ofRandom(0.8)+0.2)*speed;
				ofVec2f speed=ofVec2f(cos(a)*r,sin(a)*r);
		
				stars.push_back(star(
					offset,
					speed,
					lifespan+ofRandom(agevar)-(agevar/2)
					));
				/*
				ofLog()<<"create star: "<<
				ofVec2f(ofRandom(ofGetWidth()),ofRandom(ofGetHeight()))<<", "<<
				ofVec2f(ofRandom(speed)-(speed/2),ofRandom(speed)-(speed/2))<<" lifespan: "<<
				lifespan+ofRandom(agevar)-(agevar/2);
				*/
					

		}
	}
	vector<colourPolyline> getPoints(){
		float now=ofGetElapsedTimef();
		vector<scannableColourPolyline> s;
		for(auto& star:stars){
			scannableColourPolyline l;
			//ofLog()<<"get star";
			l.addVertex(x+star.pos.x,y+star.pos.y,ofColor(1.0f-((now-star.birthday)/star.lifespan)*128.0f));
			l.addVertex(x+star.pos.x+1,y+star.pos.y+1,ofColor(1.0f-((now-star.birthday)/star.lifespan)*128.0f));
			s.push_back(l);
		}

		if (bScan){
			std::sort(s.begin(),s.end());
		}

		vector<colourPolyline> o;

		int numstars=0;
	    for (auto& shape: s) {
	    	shape.draw();
	    	o.push_back((colourPolyline)shape);
	    }

	    return o;
	}	
};



class ofApp : public ofBaseApp{

	public:
		void setup();
		void update();
		void draw();
		void exit();
		void keyPressed(ofKeyEventArgs &keyargs);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);

		void default_settings();
		void save_settings();

        ofxHelios laser;

        ofDirectory fonts;
        int currentFont;
        string displaytext;
        glyphbanner banner;

        ofxPanel textgui;
		ofParameter<float> laser_scale;
		ofParameter<float> laser_pos_x;
		ofParameter<float> laser_pos_y;
        ofParameter<float> text_speed;

        //======= laser gui

		ofxPanel lasergui;
		ofParameter<bool> laser_power;
        ofParameter<int> laser_intensity;
        ofParameter<int> laser_points;
        ofParameter<int> laser_subdivide;
        ofParameter<int> laser_blank_num;
        ofParameter<float> laser_max_angle;

        ofxXmlSettings XML;

        //vector<starSystem> stars;

        starSystem stars;

        glm::vec2 warpframe[4];
		int select_warpframe;
		bool bDrawFrame;	

		ofPoint outputWindowSize;

		ofPoint outputPosition;
		float outputScale;

};