-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGP_AddModule.cpp
178 lines (146 loc) · 4.89 KB
/
GP_AddModule.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
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
#include "GP_AddModule.hpp"
#include <Graphics/Graphics.hpp>
#include <Vector/VectorGL.hpp>
#include <Graphics/GLExtensions.hpp>
#include <Graphics/GLSLShader.hpp>
#undef ERROR
#define ERROR( X ) std::cerr << X << std::endl
#define WARNING( X ) std::cout << X << std::endl
#define INFO( X ) std::cout << X << std::endl
#include <sstream>
using std::istringstream;
namespace {
Module *create_module(const std::string ¶ms) {
return new AddModule();
}
class Fred {
public:
Fred() {
register_module("add", create_module);
}
} fred;
}
REQUIRE_GL_EXTENSION( GL_EXT_framebuffer_object )
REQUIRE_GL_EXTENSION( GL_ARB_texture_rectangle )
REQUIRE_GL_EXTENSION( GL_ARB_texture_float )
REQUEST_GL_EXTENSION( GL_ARB_color_buffer_float )
REQUIRE_GL_EXTENSION( GL_ARB_shader_objects )
REQUIRE_GL_EXTENSION( GL_ARB_multitexture )
#include "gp_gl_helpers.hpp"
namespace {
Graphics::ProgramObjectRef interpolate_add_shader = NULL;
}
AddModule::AddModule() {
in_ports.push_back(&guess_in);
in_ports.push_back(&correction_in);
out_ports.push_back(&guess_out);
guess_in.name = "guess in";
correction_in.name = "correction";
guess_out.name = "guess out";
correction_in.position = make_vector(-0.2f, 0.0f);
guess_in.position = make_vector(0.0f,-0.2f);
guess_out.position = make_vector(0.2f, 0.0f);
do_add = false;
if (!interpolate_add_shader.ref) {
Graphics::ShaderObjectRef frag = Graphics::get_shader_object("gp_shaders/vc_interpolate_add.glsl", GL_FRAGMENT_SHADER_ARB);
assert(frag.ref);
interpolate_add_shader = Graphics::get_program_object(frag);
assert(interpolate_add_shader.ref);
glUseProgramObjectARB(interpolate_add_shader->handle);
glUniform1iARB(glGetUniformLocationARB(interpolate_add_shader->handle, "small"), 0);
glUniform1iARB(glGetUniformLocationARB(interpolate_add_shader->handle, "add"), 1);
glUseProgramObjectARB(0);
}
}
Vector2f AddModule::size() {
return make_vector(0.4f, 0.4f);
}
void AddModule::draw(Box2f viewport, Box2f screen_viewport, float scale, unsigned int recurse) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glBoxToBox(viewport, screen_viewport);
Vector2f s = size();
glBegin(GL_QUADS);
glColor3f(0.5f, 0.5f, 0.5f);
glVertex2f(-0.5f*s.x, -0.5f*s.y);
glVertex2f( 0.5f*s.x, -0.5f*s.y);
glColor3f(0.5f, 0.5f, 0.7f);
glVertex2f( 0.5f*s.x, 0.5f*s.y);
glVertex2f(-0.5f*s.x, 0.5f*s.y);
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(-0.5f*s.x, -0.5f*s.y);
glVertex2f( 0.5f*s.x, -0.5f*s.y);
glVertex2f( 0.5f*s.x, 0.5f*s.y);
glVertex2f(-0.5f*s.x, 0.5f*s.y);
glEnd();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
Graphics::gl_errors("Add::draw");
}
void AddModule::update(float elapsed_time) {
guess_out().new_flag = false;
if (correction_in().width != guess_out().width || correction_in().height != guess_out().height) {
guess_out().dealloc();
guess_out().alloc(correction_in().width, correction_in().height);
guess_out().new_flag = true;
}
if (do_add) {
do_add = false;
if (guess_in().width != correction_in().width || guess_in().height != correction_in().height) {
WARNING("WARNING: have bad guess_in!");
}
//run iteration:
glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0,0,guess_out().width, guess_out().height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, guess_out().fb);
glUseProgramObjectARB(interpolate_add_shader->handle);
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, guess_in().tex);
set_nearest();
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, correction_in().tex);
set_clamp_to_edge(); set_nearest();
glDisable(GL_BLEND);
glBegin(GL_QUADS);
unsigned int w = correction_in().width;
unsigned int h = correction_in().height;
unsigned int w2 = guess_in().width;
unsigned int h2 = guess_in().height;
glColor3f(1.0f, 1.0f, 1.0f);
glMultiTexCoord2i(GL_TEXTURE1, 0, 0);
glTexCoord2i(0, 0); glVertex2f(-1.0f, -1.0f);
glMultiTexCoord2i(GL_TEXTURE1, w2, 0);
glTexCoord2i(w, 0); glVertex2f( 1.0f, -1.0f);
glMultiTexCoord2i(GL_TEXTURE1, w2, h2);
glTexCoord2i(w, h); glVertex2f( 1.0f, 1.0f);
glMultiTexCoord2i(GL_TEXTURE1, 0, h2);
glTexCoord2i(0, h); glVertex2f(-1.0f, 1.0f);
glEnd();
glEnable(GL_BLEND);
glActiveTexture(GL_TEXTURE1);
glDisable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
glActiveTexture(GL_TEXTURE0);
glDisable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
glUseProgramObjectARB(0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glPopAttrib();
guess_out().new_flag = true;
}
}
bool AddModule::handle_event(SDL_Event const &event, Vector2f local_mouse) {
if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
do_add = true;
return true;
}
return false;
}