-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglwindow.cpp
327 lines (275 loc) · 8.33 KB
/
glwindow.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <GL/glew.h>
#include "glwindow.hpp"
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
static const GLfloat vertex_data[] = {
0.f,
0.5f,
0.f,
1.f,
0.5f,
-0.366f,
0.f,
1.f,
-0.5f,
-0.366f,
0.f,
1.f,
};
const char *vertexShaderSource = "#version 400 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char *fragmentShaderSource = "#version 400 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";
glviewwindow::glviewwindow()
{
// m_GLArea.set_expand(true);
buildUI();
}
void glviewwindow::buildUI()
{
set_title("GL Area");
set_default_size(400, 600);
// m_GLArea = Gtk::GLArea::GLArea();
// add(m_VBox);
// m_GLArea.set_expand(true);
// m_GLArea.set_size_request(100, 200);
// m_GLArea.set_auto_render(true);
m_GLArea.set_required_version(4, 0); //your desired gl version
// m_GLArea.set_auto_render(true);
// std::cout << "set gl area"<< std::endl;
// Connect gl area signals
m_GLArea.signal_realize().connect(sigc::mem_fun(*this, &glviewwindow::realize));
// // Important that the unrealize signal calls our handler to clean up
// // GL resources _before_ the default unrealize handler is called (the "false")
m_GLArea.signal_unrealize().connect(sigc::mem_fun(*this, &glviewwindow::unrealize), false);
m_GLArea.signal_render().connect(sigc::mem_fun(*this, &glviewwindow::render), false);
add(m_GLArea);
show_all_children(); // must call this in order to make above connect() work.
} // glviewwindow::buildUI
// openGL
void glviewwindow::init_buffers()
{
glGenVertexArrays(1, &m_Vao);
glBindVertexArray(m_Vao);
glGenBuffers(1, &m_Buffer);
glBindBuffer(GL_ARRAY_BUFFER, m_Buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
static GLuint create_shader(int type, const char *src)
{
auto shader = glCreateShader(type);
glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader);
int success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
int log_len;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len);
std::string log_space(log_len + 1, ' ');
glGetShaderInfoLog(shader, log_len, nullptr, (GLchar *)log_space.c_str());
std::cerr << "Compile failure in " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader: " << log_space << std::endl;
glDeleteShader(shader);
return 0;
}
return shader;
}
void glviewwindow::init_shaders(const std::string &vertex_path, const std::string &fragment_path)
{
// auto vshader_bytes = Gio::Resource::lookup_data_global(vertex_path);
// if (!vshader_bytes)
// {
// std::cerr << "Failed fetching vertex shader resource" << std::endl;
// m_Program = 0;
// return;
// }
// gsize vshader_size{vshader_bytes->get_size()};
// auto vertex = create_shader(GL_VERTEX_SHADER,
// (const char *)vshader_bytes->get_data(vshader_size));
auto vertex = create_shader(GL_VERTEX_SHADER, vertexShaderSource);
if (vertex == 0)
{
m_Program = 0;
return;
}
// auto fshader_bytes = Gio::Resource::lookup_data_global(fragment_path);
// if (!fshader_bytes)
// {
// std::cerr << "Failed fetching fragment shader resource" << std::endl;
// glDeleteShader(vertex);
// m_Program = 0;
// return;
// }
// gsize fshader_size{fshader_bytes->get_size()};
// auto fragment = create_shader(GL_FRAGMENT_SHADER,
// (const char *)fshader_bytes->get_data(fshader_size));
auto fragment = create_shader(GL_FRAGMENT_SHADER,fragmentShaderSource);
if (fragment == 0)
{
glDeleteShader(vertex);
m_Program = 0;
return;
}
m_Program = glCreateProgram();
glAttachShader(m_Program, vertex);
glAttachShader(m_Program, fragment);
glLinkProgram(m_Program);
int status;
glGetProgramiv(m_Program, GL_LINK_STATUS, &status);
if (status == GL_FALSE)
{
int log_len;
glGetProgramiv(m_Program, GL_INFO_LOG_LENGTH, &log_len);
string log_space(log_len + 1, ' ');
glGetProgramInfoLog(m_Program, log_len, nullptr, (GLchar *)log_space.c_str());
std::cerr << "Linking failure: " << log_space << std::endl;
glDeleteProgram(m_Program);
m_Program = 0;
}
else
{
/* Get the location of the "mvp" uniform */
// m_Mvp = glGetUniformLocation(m_Program, "mvp");
// glDetachShader(m_Program, vertex);
// glDetachShader(m_Program, fragment);
}
glDeleteShader(vertex);
glDeleteShader(fragment);
}
static void compute_mvp(float *res,
float phi,
float theta,
float psi)
{
float x{phi * ((float)G_PI / 180.f)};
float y{theta * ((float)G_PI / 180.f)};
float z{psi * ((float)G_PI / 180.f)};
float c1{cosf(x)};
float s1{sinf(x)};
float c2{cosf(y)};
float s2{sinf(y)};
float c3{cosf(z)};
float s3{sinf(z)};
float c3c2{c3 * c2};
float s3c1{s3 * c1};
float c3s2s1{c3 * s2 * s1};
float s3s1{s3 * s1};
float c3s2c1{c3 * s2 * c1};
float s3c2{s3 * c2};
float c3c1{c3 * c1};
float s3s2s1{s3 * s2 * s1};
float c3s1{c3 * s1};
float s3s2c1{s3 * s2 * c1};
float c2s1{c2 * s1};
float c2c1{c2 * c1};
/* apply all three rotations using the three matrices:
*
* ⎡ c3 s3 0 ⎤ ⎡ c2 0 -s2 ⎤ ⎡ 1 0 0 ⎤
* ⎢ -s3 c3 0 ⎥ ⎢ 0 1 0 ⎥ ⎢ 0 c1 s1 ⎥
* ⎣ 0 0 1 ⎦ ⎣ s2 0 c2 ⎦ ⎣ 0 -s1 c1 ⎦
*/
res[0] = c3c2;
res[4] = s3c1 + c3s2s1;
res[8] = s3s1 - c3s2c1;
res[12] = 0.f;
res[1] = -s3c2;
res[5] = c3c1 - s3s2s1;
res[9] = c3s1 + s3s2c1;
res[13] = 0.f;
res[2] = s2;
res[6] = -c2s1;
res[10] = c2c1;
res[14] = 0.f;
res[3] = 0.f;
res[7] = 0.f;
res[11] = 0.f;
res[15] = 1.f;
}
void glviewwindow::realize()
{
m_GLArea.make_current();
glewExperimental=true; //GLArea only support Core profile. must call this one otherwise we get segmenation fault error.
if(glewInit() != GLEW_OK) {
std::cout << "cannot init" << std::endl;
}
try
{
m_GLArea.throw_if_error();
// create buffer, glbindbuffer, glbufferdata
init_buffers();
// const bool use_es = m_GLArea.get_context()->get_use_es();
// const bool use_es = false;
const std::string vertex_path = "/home/fan/Documents/RGBD/gtkmm/glarea-gl.vs.glsl";
const std::string fragment_path = "/home/fan/Documents/RGBD/gtkmm/glarea-gl.fs.glsl";
init_shaders(vertex_path, fragment_path);
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
catch (const Gdk::GLError &gle)
{
std::cerr << "An error occured making the context current during realize:" << std::endl;
std::cerr << gle.domain() << "-" << gle.code() << "-" << gle.what() << std::endl;
}
}
void glviewwindow::unrealize()
{
m_GLArea.make_current();
try
{
m_GLArea.throw_if_error();
// Delete buffers and program
glDeleteBuffers(1, &m_Buffer);
glDeleteProgram(m_Program);
}
catch (const Gdk::GLError &gle)
{
std::cerr << "An error occured making the context current during unrealize" << std::endl;
std::cerr << gle.domain() << "-" << gle.code() << "-" << gle.what() << std::endl;
}
}
bool glviewwindow::render(const Glib::RefPtr<Gdk::GLContext> & /* context */)
{
try
{
m_GLArea.throw_if_error();
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
draw_triangle();
glFlush();
return true;
}
catch (const Gdk::GLError &gle)
{
std::cerr << "An error occurred in the render callback of the GLArea" << std::endl;
std::cerr << gle.domain() << "-" << gle.code() << "-" << gle.what() << std::endl;
return false;
}
}
void glviewwindow::draw_triangle()
{
float mvp[16];
// compute_mvp(mvp,
// m_RotationAngles[X_AXIS],
// m_RotationAngles[Y_AXIS],
// m_RotationAngles[Z_AXIS]);
glUseProgram(m_Program);
// glUniformMatrix4fv(m_Mvp, 1, GL_FALSE, &mvp[0]);
glBindBuffer(GL_ARRAY_BUFFER, m_Buffer);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glUseProgram(0);
}