我正在尝试使用OpenGL / GLFW和OpenCV在两个屏幕上同时显示视频。当我在我的笔记本电脑上测试我的代码(2010年中期13“Macbook Pro)和外部屏幕时,程序工作得很好(减去视频以非常快的FPS播放的事实 - 任何能够解决这个问题的人都会非常有帮助。它也是颠倒的,但是OpenCV可以很好地翻转。)但是当我将代码移到2008年初的Mac Pro并运行代码时,图像似乎没有正确的纹理。图片如下:
由于某种原因,三种颜色记录被分成不同的列,并且框架不正确。这应该是它的样子:
图像的放置并不重要,这仅仅是因为屏幕尺寸的差异。我想知道是否有人之前已经看过这个问题,如果它只是一个问题我如何调用glTexImage2D?以下是我正在使用的代码。
#include <stdio.h>
#include <string.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <GLFW/glfw3.h>
#include <GLUT/glut.h>
#define VIEWPORT_WIDTH 1280
#define VIEWPORT_HEIGHT 800
#define KEY_ESCAPE 27
CvCapture* capture;
GLFWwindow* window1;
GLFWwindow* window2;
IplImage *image;
static GLuint texName;
void initTexture(IplImage* Image);
void applyTexture(int img_width, int img_height);
void loadImage(IplImage*, GLFWwindow* window);
int main(int argc, char* argv[])
{
if (!glfwInit())
exit(EXIT_FAILURE);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
int count;
GLFWmonitor** monitors = glfwGetMonitors(&count);
window1 = glfwCreateWindow(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, "Simple example1", monitors[0], NULL);
window2 = glfwCreateWindow(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, "Simple example2", monitors[1], NULL);
capture = cvCaptureFromAVI("../VideoTexture/movie.mov");
assert(capture);
// Initialize OpenGL
glfwMakeContextCurrent(window1);
while (!glfwWindowShouldClose(window1))
{
image = cvQueryFrame(capture);
if(!cvGrabFrame(capture)){ // capture a frame
printf("Could not grab a frame\n\7");
exit(0);
}
glfwMakeContextCurrent(window1);
loadImage(image, window1);
glfwMakeContextCurrent(window2);
loadImage(image, window2);
}
return 0;
}
void initTexture(IplImage *Image)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, Image->width, Image->height, 0, GL_BGR, GL_UNSIGNED_BYTE, Image->imageData);
}
void applyTexture(int img_width, int img_height)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, texName);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(VIEWPORT_WIDTH /2, VIEWPORT_HEIGHT/2, 0);
glTexCoord2f(0, 1); glVertex3f(VIEWPORT_WIDTH /2, VIEWPORT_HEIGHT/2+img_height, 0);
glTexCoord2f(1, 1); glVertex3f(VIEWPORT_WIDTH /2+img_width, VIEWPORT_HEIGHT/2+img_height, 0);
glTexCoord2f(1, 0); glVertex3f(VIEWPORT_WIDTH /2+img_width, VIEWPORT_HEIGHT/2, 0);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
}
void loadImage(IplImage *Image, GLFWwindow* window)
{
initTexture(Image);
glViewport(0, 0, VIEWPORT_WIDTH , VIEWPORT_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, VIEWPORT_WIDTH , 0, VIEWPORT_HEIGHT, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
applyTexture(Image->width,Image->height);
glfwSwapBuffers(window);
glfwPollEvents();
}
添加以下两行解决了这个问题。
glPixelStorei (GL_UNPACK_ALIGNMENT, Image->align);
glPixelStorei (GL_UNPACK_ROW_LENGTH, Image->widthStep / Image->nChannels);
答案 0 :(得分:4)
向GL发送/读取RGB图像时,需要将像素解包/包对齐设置为 1 。默认情况下,GL将读取您的图像数据,就好像每一行都与4字节边界对齐一样,并且显然是紧凑的(8位)RGB,而不是这种情况。
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
glTexImage2D (GL_TEXTURE_2D, 0, 3, Image->width, Image->height, 0, GL_BGR, GL_UNSIGNED_BYTE, Image->imageData);
<小时/>
IplImage
data structure提供了便携式执行此操作所需的所有字段:
glPixelStorei (GL_UNPACK_ALIGNMENT, Image->align);
glPixelStorei (GL_UNPACK_ROW_LENGTH, Image->widthStep / Image->nChannels);
glTexImage2D (GL_TEXTURE_2D, 0, 3, Image->width, Image->height, 0, GL_BGR, GL_UNSIGNED_BYTE, Image->imageData);
然而,根本问题是相同的。它归结为两台机器之间图像数据对齐的差异。
至于您的图片是颠倒的,请查看origin
结构的IplImage
字段。你需要自己补偿。不幸的是,GL无法在像素传输过程中翻转图像。