使用glTexImage2D和glGetTexImage的一些问题

时间:2016-07-26 16:45:57

标签: c++ opencv opengl

我遇到了有关glTexImage2D()glGetTexImage()的一些问题。

在我的代码中,我尝试使用OpenCV' imread()加载图像,将其转换为带有texture_id句柄的纹理格式,然后再将其转换回矩阵类型(OpenCV&#39) ; s Mat)。在某个地方,我的图像数据似乎丢失了,image.data返回NULL。

我怀疑问题出在matToTexture()

的实施中

我在Visual Studio 15中使用C ++编写并使用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();


    }
}

1 个答案:

答案 0 :(得分:0)

您不会创建OpenGL上下文。简单就是这样。如果没有创建OpenGL上下文并在当前线程上处于活动状态,则所有OpenGL函数调用都是无操作。