在 OpenGL 中在纹理顶部显示 2D 形状

发布于 2024-12-15 05:29:44 字数 2711 浏览 0 评论 0原文

我正在使用 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();
}

EDIT 新订单

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() 后的结果

在此处输入图像描述

I'm capturing webcam data using OpenCV and display them as the Texture for a GL Window, This works fine.

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

But I want also to overlay some basic shapes, Md2 models on top of this, retaining the texture in the background and in the same window itself.

This is my function that is passed into 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();
}

EDIT
new order

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

EDIT:
New order and 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();

The result after doing above and Disabling gluOrtho2D()

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吖咩 2024-12-22 05:29:44

检查文档中的 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;
}

Check the docs for gluOrtho2D():

gluOrtho2D sets up a two-dimensional orthographic viewing region. This is equivalent to calling glOrtho with near = -1 and far = 1.

You're placing your trapezoid at Z = -5, way behind your near clipping plane:

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(); 

Try -0.5 for your Z coordinates, or at least something between -1 and 0.

EDIT: Example:

#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;
}
物价感观 2024-12-22 05:29:44

如果您正在绘制不应该像梯形那样具有纹理的东西,请确保禁用纹理 (glDisable(GL_TEXTURE_2D))。

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

Make sure to disable texturing (glDisable(GL_TEXTURE_2D)) if you're drawing things that shouldn't be textured like your trapezoid.

Otherwise the last set texture coordinate will apply to all your new vertices, sampling your texture in some strange spot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文