-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgl_imshow.cpp
More file actions
96 lines (82 loc) · 3.09 KB
/
gl_imshow.cpp
File metadata and controls
96 lines (82 loc) · 3.09 KB
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
#include "gl_imshow.hpp"
#include <iostream>
GL2dImagePanel::GL2dImagePanel(size_t img_width, size_t img_height)
: img_width(img_width), img_height(img_height) {}
void GL2dImagePanel::init() {
program.compileShader("myshader.vert", GLSLShaderType::VERTEX);
program.compileShader("myshader.frag", GLSLShaderType::FRAGMENT);
program.link();
// ------------------------------------------------------------
static const GLfloat vert_data[] = {
-1.0f, 1.0f, 0.0f, // top left
1.0f, 1.0f, 0.0f, // top right
-1.0f, -1.0f, 0.0f, // bottom left
1.0f, -1.0f, 0.0f, // bottom right
};
static const GLfloat texcoord_data[] = {
0.0f, 0.0f, // top left
1.0f, 0.0f, // top right
0.0f, 1.0f, // bottom left
1.0f, 1.0f, // bottom right
};
static const GLuint indexes_data[] = {0, 1, 2, 1, 2, 3};
// Vertex Array Object
GLuint vert_buf;
GLuint texcoord_buf;
{
glGenBuffers(1, &vert_buf);
glBindBuffer(GL_ARRAY_BUFFER, vert_buf);
glBufferData(GL_ARRAY_BUFFER, 3 * 4 * sizeof(GLfloat), vert_data, GL_STATIC_DRAW);
glGenBuffers(1, &texcoord_buf);
glBindBuffer(GL_ARRAY_BUFFER, texcoord_buf);
glBufferData(GL_ARRAY_BUFFER, 2 * 4 * sizeof(GLfloat), texcoord_data, GL_STATIC_DRAW);
}
{
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLint pos_loc = program.getAttribLocation("VertexPosition");
glEnableVertexAttribArray(pos_loc);
glBindBuffer(GL_ARRAY_BUFFER, vert_buf);
glVertexAttribPointer(pos_loc, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte *)NULL);
GLint texcoord_loc = program.getAttribLocation("TextureCoordinate");
glEnableVertexAttribArray(texcoord_loc);
glBindBuffer(GL_ARRAY_BUFFER, texcoord_buf);
glVertexAttribPointer(texcoord_loc, 2, GL_FLOAT, GL_FALSE, 0, (GLubyte *)NULL);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
{
glGenBuffers(1, &eao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eao);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * 2 * sizeof(GLuint), indexes_data, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
{
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &tbo);
glBindTexture(GL_TEXTURE_2D, tbo);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
void GL2dImagePanel::updateImage(const cv::Mat &img) {
if (img.cols != img_width || img.rows != img_height) {
std::cerr << "Invalid image size!" << std::endl;
return;
}
glBindTexture(GL_TEXTURE_2D, tbo);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img_width, img_height, 0, GL_RGB, GL_UNSIGNED_BYTE,
img.data);
}
void GL2dImagePanel::draw() {
program.enable();
GLuint tex_id = 0;
program.setUniform("tex", tex_id);
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eao);
glDrawElements(GL_TRIANGLES, 3 * 2, GL_UNSIGNED_INT, 0);
}