我遇到了一些關於glTexImage2D()
和glGetTexImage()
的問題。使用glTexImage2D和glGetTexImage的一些問題
在我的代碼中,我嘗試使用OpenCV的imread()
加載圖像,將其轉換爲具有texture_id句柄的紋理格式,然後再次將其轉換回矩陣類型(OpenCV的Mat
)。一路上我的圖像數據似乎丟失,image.data
返回NULL。
我懷疑問題出在我執行matToTexture()
的地方。
我在C++中使用Visual Studio 15編寫,並使用OpenCV 3.1和OpenGL 4.4。
Texture.cpp:
#include "Texture.h"
Mat textureToMat(GLuint textureID);
GLuint matToTexture(Mat image);
GLuint matToTexture(Mat image) {
GLuint texture_id;
if (image.empty()) {
cout << "Image empty." << endl;
}
else {
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
image.cols,
image.rows,
0,
GL_BGR,
GL_UNSIGNED_BYTE,
image.ptr());
}
return texture_id;
}
Mat textureToMat(GLuint texture_id) {
glBindTexture(GL_TEXTURE_2D, texture_id);
GLenum texture_width, texture_height, texture_format;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, (GLint*)&texture_width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, (GLint*)&texture_height);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, (GLint*)&texture_format);
unsigned char* texture_bytes = (unsigned char*)malloc(sizeof(unsigned char)*texture_width*texture_height * 3);
glGetTexImage(GL_TEXTURE_2D, 0, GL_BGR, GL_UNSIGNED_BYTE, texture_bytes);
Mat out(texture_height, texture_width, CV_8UC3, texture_bytes);
free(texture_bytes);
return out;
}
和標題Texture.h:
#ifndef TEXTURE_H
#define TEXTURE_H
#include "glew.h"
#include "glfw3.h"
#include <string>
#include <iostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <opencv\highgui.h>
#include <opencv\cv.h>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
Mat textureToMat(GLuint textureID);
GLuint matToTexture(Mat image);
#endif /*!TEXTURE_H*/
和呼叫:
#include "Texture.h"
int main(){
glfwInit();
glewExperimental = GL_TRUE;
glewInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* offscreen_context = glfwCreateWindow(800, 600, "OpenGL", nullptr, nullptr);
glfwMakeContextCurrent(offscreen_context);
Mat dummy = imread("mini.jpg", CV_LOAD_IMAGE_COLOR);
GLuint tex = matToTexture(dummy);
glBindTexture(GL_TEXTURE_2D, tex);
Mat some = textureToMat(tex);
while (!glfwWindowShouldClose(offscreen_context)) {
glfwSwapBuffers(offscreen_context);
glfwPollEvents();
if (glfwGetKey(offscreen_context, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(offscreen_context, GL_TRUE);
}
}
glfwTerminate();
}
}
我懷疑'free(texture_bytes)'是「NULL'ness」的來源:D。嘗試評論它。 – sarasvati
@sarasvati是正確的,'Mat out(texture_height,texture_width,CV_8UC3,texture_bytes)'只提供數據的淺拷貝,Mat內部指向你提供的'texture_bytes',如果你刪除了內存,它就消失了。你可以做'Mat out = Mat(texture_height,texture_width,CV_8UC3,texture_bytes).clone()'來解決這個問題,更好的方法是創建一個大小正確的Mat,然後在Mat上調用'glGetTexImage'。避免複製。 – cxyzs7
你在哪裏創建OpenGL上下文?沒有當前GL上下文,那些GL紋理操作都不會起作用。在[mcve]中編輯。 – genpfault