-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfb.cpp
126 lines (100 loc) · 2.5 KB
/
tfb.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
#include <glbinding/Binding.h>
#include <glog/logging.h>
#include <chrono>
#include "gl_shared.hpp"
#include "glfw_app.hpp"
#include "shader.hpp"
#include "particle_buffer.h"
#include "billboard.hpp"
#include "fbo.h"
#include <cstdlib> // atoi?
shader* particle_fb = nullptr;
particle_buffer* particle = nullptr;
int h = 0, w = 0;
bool clear = false;
std::string shader_fn;
fbo* accum = nullptr;
billboard* bb = nullptr;
shader* slab_pass = nullptr;
void load_shaders() {
particle_fb = new shader(shader_fn, shader_fn, {"outPosition", "outVelocity"});
slab_pass = new shader("img/passthru");
}
int main(int argc, char **argv) {
shader::setdir("/Users/jrsa/code/gl/glsl/");
auto t_prev = std::chrono::high_resolution_clock::now();
auto setup_proc = [&] {
glbinding::Binding::initialize(false);
w = 640;
h = 480;
glViewport(0, 0, 640, 480);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
int n = 0;
if (argc >= 2) {
n = atoi(argv[1]);
} else {
n = 100;
}
if (argc >= 3) {
shader_fn = argv[2];
}
load_shaders();
bb = new billboard();
accum = new fbo(h, w);
particle = new particle_buffer(n);
};
auto draw_proc = [&] {
auto t_now = std::chrono::high_resolution_clock::now();
float time = std::chrono::duration_cast<std::chrono::duration<float>>(t_now - t_prev).count();
t_prev = t_now;
glClear(GL_COLOR_BUFFER_BIT);
// draw particles into fbo
accum->bind();
// glViewport(0, 0, w, h);
if (clear) {
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
}
particle_fb->use();
particle_fb->u1f("time", time);
particle->draw();
fbo::unbind_all();
// glViewport(0, 0, w, h);
accum->bind_tex();
slab_pass->use();
bb->draw();
};
glfw_app gltest(draw_proc, setup_proc);
gltest.set_key_proc([](int k, int, int a, int) {
if(a == GLFW_PRESS) {
switch (k) {
case 'C': {
clear = !clear;
break;
}
case 'R': {
load_shaders();
break;
}
case 'P': {
particle->seed();
break;
}
default: {
break;
}
}
}
});
gltest.set_cursor_proc([](double x, double y){
particle_fb->u2f("mousePos", glm::vec2(1/x, 1/y));
});
gltest.set_fbsize_proc([](int width, int height) {
w = width;
h = height;
glViewport(0, 0, w, h);
accum = new fbo(h, w);
});
gltest.run();
return 0;
}