-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEffects.cpp
91 lines (81 loc) · 3.3 KB
/
Effects.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
#include "Effects.hpp"
GLuint Effects::genTexture()
{
if(!visited)
{
this->texture = matToTexture(effect, GL_NEAREST, GL_NEAREST, GL_CLAMP);
visited = true;
}
else
{
glDeleteTextures(1, &this->texture);
this->texture = matToTexture(effect, GL_NEAREST, GL_NEAREST, GL_CLAMP);
}
return this->texture;
}
GLuint Effects::matToTexture(cv::Mat &mat, GLenum minFilter, GLenum magFilter, GLenum wrapFilter)
{
// Generate a number for our textureID's unique handle
//static int visited = 0;
GLuint textureID;
glGenTextures(1, &textureID);
// Bind to our texture handle
glBindTexture(GL_TEXTURE_2D, textureID);
// Catch silly-mistake texture interpolation method for magnification
if (magFilter == GL_LINEAR_MIPMAP_LINEAR ||
magFilter == GL_LINEAR_MIPMAP_NEAREST ||
magFilter == GL_NEAREST_MIPMAP_LINEAR ||
magFilter == GL_NEAREST_MIPMAP_NEAREST)
{
std::cout << "You can't use MIPMAPs for magnification - setting filter to GL_LINEAR" << std::endl;
magFilter = GL_LINEAR;
}
// Set texture interpolation methods for minification and magnification
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
// Set texture clamping method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapFilter);
// Set incoming texture format to:
// GL_BGR for CV_CAP_OPENNI_BGR_IMAGE,
// GL_LUMINANCE for CV_CAP_OPENNI_DISPARITY_MAP,
// Work out other mappings as required ( there's a list in comments in main() )
GLenum inputColourFormat = GL_BGR;
if (mat.channels() == 1)
{
inputColourFormat = GL_LUMINANCE;
}
if (mat.channels() == 4)
{
inputColourFormat = GL_BGRA;
}
GLenum datatype = GL_UNSIGNED_BYTE;
if(mat.depth() == CV_32F) datatype = GL_FLOAT;
else if(mat.depth() == CV_64F) datatype = GL_DOUBLE;
else if(mat.depth() == CV_32S) datatype = GL_INT;
else if(mat.depth() == CV_16U) datatype = GL_UNSIGNED_SHORT;
else if(mat.depth() == CV_16S) datatype = GL_SHORT;
else if(mat.depth() == CV_8S) datatype = GL_BYTE;
// Create the texture
//if(!visited)
glTexImage2D(GL_TEXTURE_2D, // Type of texture
0, // Pyramid level (for mip-mapping) - 0 is the top level
GL_RGB, // Internal colour format to convert to
mat.cols, // Image width i.e. 640 for Kinect in standard mode
mat.rows, // Image height i.e. 480 for Kinect in standard mode
0, // Border width in pixels (can either be 1 or 0)
inputColourFormat, // Input image format (i.e. GL_RGB, GL_RGBA, GL_BGR etc.)
datatype, // Image data type
mat.ptr()); // The actual image data itself
//else glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mat.cols, mat.rows, GL_BGR, GL_UNSIGNED_BYTE, mat.ptr());
// If we're using mipmaps then generate them. Note: This requires OpenGL 3.0 or higher
if (minFilter == GL_LINEAR_MIPMAP_LINEAR ||
minFilter == GL_LINEAR_MIPMAP_NEAREST ||
minFilter == GL_NEAREST_MIPMAP_LINEAR ||
minFilter == GL_NEAREST_MIPMAP_NEAREST)
{
glGenerateMipmap(GL_TEXTURE_2D);
}
//visited = 1;
return textureID;
}