Last active
October 11, 2022 20:52
-
-
Save zhangzhensong/03f67947c22acb5ee922 to your computer and use it in GitHub Desktop.
Converting OpenCV Mat to OpenGL texture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// don't forget to include related head files | |
void BindCVMat2GLTexture(cv::Mat& image, GLuint& imageTexture) | |
{ | |
if(image.empty()){ | |
std::cout << "image empty" << std::endl; | |
}else{ | |
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); | |
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); | |
glGenTextures(1, &imageTexture1); | |
glBindTexture(GL_TEXTURE_2D, imageTexture1); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
// Set texture clamping method | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); | |
cv::cvtColor(image, image, CV_RGB2BGR); | |
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 | |
image.cols, // Image width i.e. 640 for Kinect in standard mode | |
image.rows, // Image height i.e. 480 for Kinect in standard mode | |
0, // Border width in pixels (can either be 1 or 0) | |
GL_RGB, // Input image format (i.e. GL_RGB, GL_RGBA, GL_BGR etc.) | |
GL_UNSIGNED_BYTE, // Image data type | |
image.ptr()); // The actual image data itself | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HI,
how to use this function?