本文介绍了OpenGL纹理反转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将网络摄像头的输入映射到OpenGL中的平面.我正在使用OpenCV从网络摄像头获取图像.

I'm trying to map the input from my webcam to a plane in OpenGL. I'm using OpenCV to get the images from the webcam.

我的问题是纹理垂直反转,如果我的纹理是"v",则当前结果是"^".

The problem I have is that the texture is vertically inverted, if my texture is "v", the current result is "^".

我想将从网络摄像头拍摄的图像适合我的飞机(2x2).它的左下角是-1,-1,右上角是1,1.

I want to fit the image taken from the webcam to my plane (2x2). Its lower left corner is -1, -1 and the upper right corner is 1,1.

代码是:

const int VIEWPORT_WIDTH = 640;
const int VIEWPORT_HEIGHT = 480;
const int KEY_ESCAPE = 27;

CvCapture* g_Capture;
IplImage* image;
GLint g_hWindow;

GLvoid InitGL();
GLvoid OnDisplay();
GLvoid OnReshape(GLint w, GLint h);
GLvoid OnKeyPress (unsigned char key, GLint x, GLint y);

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);

    g_hWindow = glutCreateWindow("Image");

    image = cvLoadImage("average.jpg", 1);
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image->width, image->height, GL_RGB, GL_UNSIGNED_BYTE, image->imageData);

    InitGL();

    glutMainLoop();

    return 0;
}

GLvoid InitGL()
{
    glClearColor (0.0, 0.0, 0.0, 0.0);

    glutDisplayFunc(OnDisplay);
    glutReshapeFunc(OnReshape);
    glutKeyboardFunc(OnKeyPress);
}

GLvoid OnDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);

    glPushMatrix();
        glTranslatef(0.0f, 0.0f, -2.5f);

        glBegin(GL_QUADS);

            glTexCoord2f(0.0f, 0.0f);
            glVertex3f(-1.0f,-1.0f, 1.0f);

            glTexCoord2f(1.0f, 0.0f);
            glVertex3f( 1.0f,-1.0f, 1.0f);

            glTexCoord2f(1.0f, 1.0f);
            glVertex3f( 1.0f, 1.0f, 1.0f);

            glTexCoord2f(0.0f, 1.0f);
            glVertex3f(-1.0f, 1.0f, 1.0f);

        glEnd();
    glPopMatrix();

    glutSwapBuffers();
}

GLvoid OnReshape(GLint width, GLint height)
{
    if (height==0)
        height=1;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glViewport(0, 0, width, height);
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height, 1.0f, 10.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt( 0.0, 0.0, 0.0, 0.0, 0.0, -6.0, 0.0f, 1.0f, 0.0f);
}

GLvoid OnKeyPress(unsigned char key, int x, int y)
{
    switch (key) {
        case KEY_ESCAPE:
            cvReleaseImage(&image);
            glutDestroyWindow(g_hWindow);
            exit(0);
            break;
    }
    glutPostRedisplay();
}

顺便说一句.在这段代码中,我正在加载图像,而不是从网络摄像头获取图像.

BTW. In this code I'm loading an image instead of getting from the webcam.

有什么建议吗?

推荐答案

在要渲染的四边形上翻转纹理坐标. OpenGL确实以大多数人认为的那样颠倒"地存储纹理,因此处理该纹理的最简单方法是使用OGL.

Flip the texture coordinates on the quad you're rendering. OpenGL does store texture "upside-down" from how most people assume they'd be, so the simplest way to handle it is just work with OGL.

        glTexCoord2f(0.0f, 1.0f);
        glVertex3f(-1.0f,-1.0f, 1.0f);

        glTexCoord2f(1.0f, 1.0f);
        glVertex3f( 1.0f,-1.0f, 1.0f);

        glTexCoord2f(1.0f, 0.0f);
        glVertex3f( 1.0f, 1.0f, 1.0f);

        glTexCoord2f(0.0f, 0.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);

我认为应该这样做.

这篇关于OpenGL纹理反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:19