作者popen (Penpineapple-applepen)
看板C_and_CPP
标题[问题] 请高手帮忙OpenGL framebuffer
时间Mon Jan 23 14:26:37 2017
开发平台(Platform): (Ex: Win10, Linux, ...)
win 7
编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
VC++ 2013
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
OpenCV, OpenGL, OpenVR
问题(Question):
我用2台IP CAM抓即时影像丢入Htc vive的双眼虚拟实境里,每秒30张,
但在VR里的影像有一点不连续,能否帮我看是不是openGL的frame buffer有写错
因为我对OpenGL不太熟,程式也是从网路抓下来修修改改测试的
程式码(Code):(请善用置底文网页, 记得排版)
int framebufferWidth = 704, framebufferHeight = 480;
int numEyes = 2; //左右眼
GLuint framebuffer[numEyes];
glGenFramebuffers(numEyes, framebuffer);
//主要while回圈,在VR里显示影像
while (!glfwWindowShouldClose(window)) {
for (int eye = 0; eye < numEyes; ++eye) {
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer[eye]);
glViewport(0, 0, framebufferWidth, framebufferHeight);
glBindFramebuffer(GL_FRAMEBUFFER, 0); //(这行是不是不需要)
glClearColor(0.1f, 0.2f, 0.3f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLuint colorRenderTarget[numEyes];
if (eye == 0) //将左眼的OpenCV mat转成OpenGL texture格式
colorRenderTarget[eye] = matToTexture(left_image, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP);
else if (eye == 1) //将右眼的OpenCV mat转成OpenGL texture格式
colorRenderTarget[eye] = matToTexture(right_image, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, colorRenderTarget[eye]);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer[eye]);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorRenderTarget[eye], 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
//将双眼影像丢入Htc vive里
const vr::Texture_t tex = { reinterpret_cast<void*>(intptr_t(colorRenderTarget[eye])), vr::API_OpenGL, vr::ColorSpace_Linear };
vr::VRCompositor()->Submit(vr::EVREye(eye), &tex);
glDeleteTextures(1, &colorRenderTarget[numEyes]);
glDisable(GL_TEXTURE_2D);
}
}
glfwSwapBuffers(window);
glfwPollEvents();
下面是matToTexture函式(从网路抓来修改的):
GLuint matToTexture(cv::Mat &mat, GLenum minFilter, GLenum magFilter, GLenum
wrapFilter)
{
// Generate a number for our textureID's unique handle
GLuint textureID;
glGenTextures(1, &textureID);
// Bind to our texture handle
glBindTexture(GL_TEXTURE_2D, textureID);
// Catch silly-mistake texture interpolation method for magnification
if (magFilter == GL_LINEAR_MIPMAP_LINEAR ||
magFilter == GL_LINEAR_MIPMAP_NEAREST ||
magFilter == GL_NEAREST_MIPMAP_LINEAR ||
magFilter == GL_NEAREST_MIPMAP_NEAREST)
{
cout << "You can't use MIPMAPs for magnification - setting filter to GL_LINEAR" << endl;
magFilter = GL_LINEAR;
}
// Set texture interpolation methods for minification and magnification
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
// Set texture clamping method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapFilter);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set incoming texture format to:
// GL_BGR for CV_CAP_OPENNI_BGR_IMAGE,
// GL_LUMINANCE for CV_CAP_OPENNI_DISPARITY_MAP,
// Work out other mappings as required ( there's a list in comments in
main() )
GLenum inputColourFormat = GL_BGR;
if (mat.channels() == 1)
{
inputColourFormat = GL_LUMINANCE;
}
// Create the texture
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
mat.cols, // Image width i.e. 640 for Kinect in standard mode
mat.rows, // Image height i.e. 480 for Kinect in standard mode
0, // Border width in pixels (can either be 1 or 0)
inputColourFormat, // Input image format (i.e. GL_RGB, GL_RGBA, GL_BGR
etc.)
GL_UNSIGNED_BYTE, // Image data type
mat.ptr()); // The actual image data itself
// If we're using mipmaps then generate them. Note: This requires OpenGL
3.0 or higher
if (minFilter == GL_LINEAR_MIPMAP_LINEAR ||
minFilter == GL_LINEAR_MIPMAP_NEAREST ||
minFilter == GL_NEAREST_MIPMAP_LINEAR ||
minFilter == GL_NEAREST_MIPMAP_NEAREST)
{
glGenerateMipmap(GL_TEXTURE_2D);
}
return textureID;
}
补充说明(Supplement):
matToTexture函式应该还好, 能帮我检查主要的while回圈吗?
--
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.96.33.181
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1485152801.A.7F0.html
1F:推 ggggggh: 为何有pyramid? 01/23 16:15
2F:→ popen: 那里有pyramid? 01/25 22:48
3F:→ popen: 那个不用管,那是注解啊,那直接用copy来的 01/25 22:49
4F:推 doom8199: vr 一般要跑在 90fps+ 才会顺吧, 30太小了 01/26 20:15
5F:→ doom8199: 再来你的 code 有几个问题, 第一个是两个 FBO 的 state 01/26 20:15
6F:→ doom8199: 都设完了, 但没人用它? 叫 GPU做事的指令应该是被埋在 01/26 20:16
7F:→ doom8199: 'Submit' 里头, 可是也没看到你把 FBO 传进去 01/26 20:17
8F:→ doom8199: 第二个是每个 swapbuffer 前都做了两次 mat to texture 01/26 20:18
9F:→ doom8199: 等於每个 frame 都做了两次 memory copy 至 GPU 端 01/26 20:19
10F:→ doom8199: 这种做法很没效率; 一般应该是 GL要开 external texture 01/26 20:20
11F:推 smartclever: windows上有eglimage可以用吗? 01/27 12:50
12F:→ popen: doom,请问能否提供范例,要怎麽修改才好 01/29 22:58