看板 C_and_CPP 關於我們 聯絡資訊
開發平台(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://www.ptt.cc/bbs/C_and_CPP/M.1485152801.A.7F0.html
ggggggh: 為何有pyramid? 01/23 16:15
popen: 那裡有pyramid? 01/25 22:48
popen: 那個不用管,那是注解啊,那直接用copy來的 01/25 22:49
doom8199: vr 一般要跑在 90fps+ 才會順吧, 30太小了 01/26 20:15
doom8199: 再來你的 code 有幾個問題, 第一個是兩個 FBO 的 state 01/26 20:15
doom8199: 都設完了, 但沒人用它? 叫 GPU做事的指令應該是被埋在 01/26 20:16
doom8199: 'Submit' 裏頭, 可是也沒看到你把 FBO 傳進去 01/26 20:17
doom8199: 第二個是每個 swapbuffer 前都做了兩次 mat to texture 01/26 20:18
doom8199: 等於每個 frame 都做了兩次 memory copy 至 GPU 端 01/26 20:19
doom8199: 這種做法很沒效率; 一般應該是 GL要開 external texture 01/26 20:20
smartclever: windows上有eglimage可以用嗎? 01/27 12:50
popen: doom,請問能否提供範例,要怎麼修改才好 01/29 22:58