问题描述
我已经看到许多用于为 OpenGL 加载纹理的代码示例,其中许多代码的理解有些复杂,或者需要大量代码才能使用新功能.
I have seen many code samples for loading textures for OpenGL, many of them a bit complicated to understand or requiring new functions with a lot of code.
我当时以为 OpenCV 允许我们加载任何图像格式,这可能是将纹理加载到 OpenGL 的一种简单有效的方法,但是我缺少了一些东西.我在 c ++ 中有这段代码:
I was thinking that as OpenCV allows us to load any image format it can be a simple an efficient way to load textures to OpenGL, but I am missing something. I have this piece of code in c++:
cv::Mat texture_cv;
GLuint texture[1];
int Status=FALSE;
if( texture_cv = imread("stones.jpg")) {
Status=TRUE; // Set The Status To TRUE
glGenTextures(1, &texture[0]); // Create The Texture
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S , GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexImage2D(GL_TEXTURE_2D, 0, 3, texture_cv.cols, texture_cv.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_cv.data);
}
由于此错误,它无法编译:
And it is not compiling because of this error:
有什么建议吗?我应该如何将cv :: Mat转换为openGL纹理?
Any suggestions? How should I do the conversion from cv::Mat to openGL texture?
推荐答案
您的错误在那里出现了,对吧?
Your error appears there, right?
if( texture_cv = imread("stones.jpg")) {
因为在if(expr)
中expr必须为bool
或可以强制转换为bool
.但是无法将cv::Mat
隐式转换为布尔值.但是您可以像这样检查imread
的结果:
because in if(expr)
expr must be bool
or can be casted to bool
. But there is no way to convert cv::Mat
into boolean implicitly. But you can check the result of imread
like that:
texture_cv = imread("stones.jpg");
if (texture_cv.empty()) {
// handle was an error
} else {
// do right job
}
请参阅: cv :: Mat :: empty(), cv :: imread
希望对您有帮助.
这篇关于使用OpenCV为OpenGL加载纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!