為什麼這篇glTexParameteri鄉民發文收入到精華區:因為在glTexParameteri這個討論話題中,有許多相關的文章在討論,這篇最有參考價值!作者popen (Penpineapple-applepen)看板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