-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTextureObject.cpp
More file actions
92 lines (78 loc) · 3.34 KB
/
Copy pathTextureObject.cpp
File metadata and controls
92 lines (78 loc) · 3.34 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
#include "TextureObject.h"
namespace BioTracker {
namespace Core {
TextureObject::TextureObject(QObject *parent)
: QObject(parent) {
// OpenCV's coordinate system originates in the upper left corner.
// OpenGL originates in the lower left. Thus the image has to be flipped vertically
for (int j = 0; j < 4; ++j) {
m_texCoords.append(QVector2D(j == 0 || j == 3, j == 2 || j == 3));
}
setImage(cv::Mat::zeros(1, 1, CV_8UC3));
}
void TextureObject::draw() const {
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_texture);
glVertexPointer(2, GL_FLOAT, 0, m_vertices.constData());
glTexCoordPointer(2, GL_FLOAT, 0, m_texCoords.constData());
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_POLYGON, 0, 4);
glDisable(GL_TEXTURE_2D);
}
void TextureObject::createVertices() {
glEnable(GL_DEBUG_OUTPUT);
//create a vertice as wide and long as the image has pixels
//for later being able to locate mouseclicks
const int corner1[2] = { m_img.cols, 0 };
const int corner2[2] = { 0, 0 };
const int corner3[2] = { 0, m_img.rows };
const int corner4[2] = { m_img.cols, m_img.rows };
// does not free the memory, so no segfault
m_vertices.remove(0, m_vertices.length());
m_vertices.append(QVector2D(static_cast<float>(corner1[0]),
static_cast<float>(corner1[1])));
m_vertices.append(QVector2D(static_cast<float>(corner2[0]),
static_cast<float>(corner2[1])));
m_vertices.append(QVector2D(static_cast<float>(corner3[0]),
static_cast<float>(corner3[1])));
m_vertices.append(QVector2D(static_cast<float>(corner4[0]),
static_cast<float>(corner4[1])));
}
void TextureObject::createTexture() {
glEnable(GL_DEBUG_OUTPUT);
// free memory
glDeleteTextures(1, &m_texture);
glVertexPointer(2, GL_FLOAT, 0, m_vertices.constData());
glTexCoordPointer(2, GL_FLOAT, 0, m_texCoords.constData());
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Non-mipmap way of mapping the texture (fast and clean): // Allocate the texture
glGenTextures(1, &m_texture);
// Select the texture.
glBindTexture(GL_TEXTURE_2D, m_texture);
// If texture area is larger then the image, upscale using no interpolation.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// If texture area is smaller than the image, downsample using no interpolation.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// Set Alignment for OpenGL for reading pixel data
// When the dimension of the image does not produce a 4-byte aligned structure
// we will get a crash here
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
// create Texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_img.cols, m_img.rows, 0, GL_BGR,
GL_UNSIGNED_BYTE, m_img.data);
}
void TextureObject::setImage(const cv::Mat &img) {
assert(!img.empty());
m_img = img;
createVertices();
createTexture();
draw();
}
} // namespace Core
} // namespace BioTracker