在OpenGL中在纹理上显示2D形状

时间:2011-11-11 21:24:57

标签: visual-c++ opengl opencv

我正在使用OpenCV捕获网络摄像头数据并将其显示为GL窗口的纹理,这样可以正常工作。

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, image->width, image->height, GL_RGB, GL_UNSIGNED_BYTE, image->imageData);

但我还希望覆盖一些基本形状,Md2模型,在背景和同一窗口本身保留纹理。

这是我传递给glutDisplayFunc()

的函数
void drawScene() {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);

    // Set Projection Matrix
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, VIEWPORT_WIDTH, VIEWPORT_HEIGHT, 0);

    // Setup the view  // Switch to Model View Matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glBegin(GL_QUADS); 
    // Trapezoid
    glVertex3f(-0.7f, -1.5f, -5.0f);
    glVertex3f(0.7f, -1.5f, -5.0f);
    glVertex3f(0.4f, -0.5f, -5.0f);
    glVertex3f(-0.4f, -0.5f, -5.0f);
    glEnd(); //End quadrilateral coordinates

    // start drawing the Background texture
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex2f(VIEWPORT_WIDTH, 0.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex2f(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
    glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, VIEWPORT_HEIGHT);
    glEnd();

    glFlush();
    glutSwapBuffers();
}

修改 新订单

glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex2f(VIEWPORT_WIDTH, 0.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex2f(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
    glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, VIEWPORT_HEIGHT);
    glEnd();

    glBegin(GL_QUADS); //Begin quadrilateral coordinates
    // Trapezoid
    glVertex3f(-0.7f, -1.5f, -5.0f);
    glVertex3f(0.7f, -1.5f, -5.0f);
    glVertex3f(0.4f, -0.5f, -5.0f);
    glVertex3f(-0.4f, -0.5f, -5.0f);
    glEnd(); //End quadrilateral coordinates

修改 新订单和坐标:

glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
// Trapezoid to be displayed on top of texture
glVertex3f(0.2f, 0.3f, -0.6f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.4f, -0.5f, -0.5f);
glVertex3f(-0.4f, -0.5f, -0.5f);
glEnd(); //End quadrilateral coordinates
////
glEnable(GL_TEXTURE_2D); // start drawing the background texture 
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(VIEWPORT_WIDTH, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, VIEWPORT_HEIGHT);
glEnd();

执行上述操作后的结果并禁用gluOrtho2D()

enter image description here

2 个答案:

答案 0 :(得分:1)

如果您正在绘制不应像您的梯形一样纹理化的内容,请务必停用纹理(glDisable(GL_TEXTURE_2D))。

否则,最后设置的纹理坐标将应用于所有新顶点,在一些奇怪的位置对纹理进行采样。

答案 1 :(得分:1)

查看gluOrtho2D()的文档:

  

gluOrtho2D建立二维正交观察区域。这相当于使用near = -1和far = 1调用glOrtho。

你将你的梯形放在Z = -5处,远离你的近剪裁平面:

glBegin(GL_QUADS);
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);
glEnd(); 

为Z坐标尝试-0.5,或至少在-1和0之间。

编辑:示例:

#include <GL/glut.h>

int win_w = 0;
int win_h = 0;
GLuint texObj = 0;

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // set up projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    double halfWidth = (win_w / 2.0);;
    double halfHeight = (win_h / 2.0);
    glOrtho( -halfWidth, halfWidth, -halfHeight, halfHeight, -10, 10 );

    // set up modelview
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // render textured quad
    glEnable(GL_TEXTURE_2D);
    glColor3ub(255,255,255);
    glPushMatrix();
        glScalef(halfWidth * 0.9, halfHeight * 0.9, 1.0);
        glBegin(GL_QUADS);
            glTexCoord2f(0,0);
            glVertex2f(-1,-1);
            glTexCoord2f(1,0);
            glVertex2f(1,-1);
            glTexCoord2f(1,1);
            glVertex2f(1,1);
            glTexCoord2f(0,1);
            glVertex2f(-1,1);
        glEnd();
    glPopMatrix();
    glDisable(GL_TEXTURE_2D);

    // draw quad on top
    glColor3ub(255,0,0);
    glPushMatrix();
        // move quad back a bit so it's above Z=0
        glTranslatef(0,0,-1);
        glScalef(50,50,50);
        glBegin(GL_QUADS);
            glVertex2f(-1,-1);
            glVertex2f(1,-1);
            glVertex2f(1,1);
            glVertex2f(-1,1);
        glEnd();
    glPopMatrix();

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    win_w = w;
    win_h = h;
    glViewport(0, 0, w, h);
}

GLuint getTexture()
{
    // 2x2 texture
    unsigned char bytes[] =
    {
          0,   0, 255,      255, 255, 255,
        255,   0,   0,      0, 255,   0,
    };

    GLuint texID;
    glGenTextures(1, &texID);
    glBindTexture(GL_TEXTURE_2D, texID);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, bytes);
    return texID;
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

    glutInitWindowSize(800,600);
    glutCreateWindow("Texture");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    texObj = getTexture();
    glutMainLoop();
    return 0;
}