如何将纹理对象渲染为frambuffer纹理,通过OpenCL获取,转换为OpenCV

时间:2015-10-03 19:55:31

标签: opencv opengl opencl render-to-texture

所以我试图结合所有3个库的有用性,我用纹理加载一个对象:

// Load the model of the store, create a program with the shaders
GLint store = OpGL::initModel(MESH_PATH);
GLuint storeProgram = OpGL::initProgram(VS_GLSL_PATH, FS_GLSL_PATH);
glUseProgram (storeProgram);

// Find the location in the shader, for the texture image
GLuint TEX_ID = glGetUniformLocation(storeProgram, "tex_glsl");
GLuint TEX = OpGL::loadTexture(TEXTURE_IMAGE_PATH, 25);

// Bind texture in Texture Unit 0
glBindTexture(GL_TEXTURE_2D, TEX);

// Set
glUniform1i(TEX_ID, 0); // use texture 0

在GL中设置一个带纹理的帧缓冲:

GLuint g_fb = 0; // frame buffer
glGenFramebuffers (1, &g_fb);
glBindFramebuffer(GL_FRAMEBUFFER, g_fb);

GLuint g_fb_tex = 0;
glGenTextures (1, &g_fb_tex);
glBindTexture (GL_TEXTURE_2D, g_fb_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D ( GL_TEXTURE_2D,0, GL_RGBA,640,480,      0,GL_RGBA,GL_UNSIGNED_BYTE,NULL   );
glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, g_fb_tex, 0);

GLuint g_db = 0; // depth buffer
glGenRenderbuffers(1, &g_db);
glBindRenderbuffer(GL_RENDERBUFFER, g_db);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 640, 480);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, g_db);

/* tell the framebuffer to expect a colour output attachment*/
GLenum draw_bufs[1] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers (1, draw_bufs);

在CL中创建存储:

cl_mem CL_image; //location for the gl rendering to reside in CL
CL_image = clCreateFromGLTexture2D(context, CL_MEM_READ_WRITE, GL_TEXTURE_2D, 0, g_fb_tex, &err);

创建一个UMat:

cv::UMat Umat;

重新绑定原始纹理:

glBindTexture(GL_TEXTURE_2D, TEX);
glBindFramebuffer(GL_FRAMEBUFFER, g_fb);  // just as a precaution

渲染:

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)        
glUseProgram (storeProgram);
glBindVertexArray (store);
glDrawArrays (GL_TRIANGLES, 0, 79227);
glfwPollEvents ();
glFlush();

    // pass the images to CL
err = clEnqueueAcquireGLObjects(queue, 1, &CL_image, 0, NULL, NULL);
cl_event wait;

cv::ocl::convertFromImage(CL_image, Umat);
cv::flip(Umat,Umat,0);
cv::imshow("CVforCLimage", Umat);
cv::waitKey(1);

err = clEnqueueReleaseGLObjects(queue, 1, &out_toCL_image, 0, 0, 0);
err = clFinish(queue);

如果我将它发送到屏幕(glBindFramebuffer(GL_FRAMEBUFFER, 0);),一切都会好起来......但是当我将它呈现给CV时,我得到一个蓝色物体而不是橙色物体。几乎就像我加载的原始纹理没有进行渲染。 谢谢你的帮助!

0 个答案:

没有答案