如何在OpenGL场景中绘制透明橡皮筋

发布于 2024-10-31 14:06:32 字数 1750 浏览 3 评论 0原文

我正在使用 OpenGL 来显示从摄像机抓取的帧和纹理映射。完全没有问题,但我必须选择抓取框架的某些区域,然后我必须缩放它,首先,我无法绘制橡胶矩形,当我选择抓取框架的某些区域时,它必须是透明的要透明地查看纹理帧,您可以给我一些代码或线索吗?顺便说一句,我正在显示抓取的帧,任何建议都对我有好处...

void GLWidget::initializeGL()
{
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &m_pTextureId);
    glBindTexture(GL_TEXTURE_2D, m_pTextureId);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
void GLWidget::resizeGL(int w, int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1,1,-1,1,-1,1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void GLWidget::setTextureData(BYTE *data, int width, int height)
{
    makeCurrent();
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if (m_isTextured)
        glTexSubImage2D(GL_TEXTURE_2D,0,0,0,width,height,GL_BGRA_EXT,GL_UNSIGNED_BYTE,data);
    else
        glTexImage2D(GL_TEXTURE_2D,0,3,width,height,0,GL_BGRA_EXT,GL_UNSIGNED_BYTE,0);
    glBindTexture(GL_TEXTURE_2D,m_pTextureId);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0,0.0);glVertex2f(-1.0,-1.0);
    glTexCoord2f(0.0,1.0);glVertex2f(-1.0,1.0);
    glTexCoord2f(1.0,1.0);glVertex2f(1.0,1.0);
    glTexCoord2f(1.0,0.0);glVertex2f(1.0,-1.0);
    glEnd();
    swapBuffers();
}

我将捕获的帧数据传递给 setTextureData 它正在工作,但我不知道这样做好方法...


感谢回复@genpfault,

我使用了x1=0、y1=0、x2=1、y2=1,但它正在向所有纹理区域绘制矩形。并再绘制 1 个矩形到坐标 x1,y1... 。我该如何修复它。顺便说一句,我怎样才能删除那个矩形?,如果我绘制一个矩形然后再次纹理矩形变得不可见,还有一个问题......

i'm using OpenGL for showing grabbed frames from video camera with texture mapping. There is no problem at all but i have to select some region of the grabbed frame then i have to zoom it, first of all i can't draw a Rubber rectangle it must be transparent when i select the some region of grabbed frame i have to see textured frame transparently could you gimme some piece of code or clue, btw i'm showing grabbed frames like that any suggestion will be good for me...

void GLWidget::initializeGL()
{
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &m_pTextureId);
    glBindTexture(GL_TEXTURE_2D, m_pTextureId);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
void GLWidget::resizeGL(int w, int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1,1,-1,1,-1,1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void GLWidget::setTextureData(BYTE *data, int width, int height)
{
    makeCurrent();
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if (m_isTextured)
        glTexSubImage2D(GL_TEXTURE_2D,0,0,0,width,height,GL_BGRA_EXT,GL_UNSIGNED_BYTE,data);
    else
        glTexImage2D(GL_TEXTURE_2D,0,3,width,height,0,GL_BGRA_EXT,GL_UNSIGNED_BYTE,0);
    glBindTexture(GL_TEXTURE_2D,m_pTextureId);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0,0.0);glVertex2f(-1.0,-1.0);
    glTexCoord2f(0.0,1.0);glVertex2f(-1.0,1.0);
    glTexCoord2f(1.0,1.0);glVertex2f(1.0,1.0);
    glTexCoord2f(1.0,0.0);glVertex2f(1.0,-1.0);
    glEnd();
    swapBuffers();
}

I'm passing captured frame data to setTextureData it's working but i dunno does it good way ...


Thanks for reply @genpfault,

i used x1=0, y1=0, x2=1, y2=1 but it's drawing rectangle to all texture area. and draws 1 more rectangle to coordinates x1,y1... . How can i fix it. Btw how can i remove that rectangle ?, There is one more issue if i draw a rectangle then texture again rectangle going invisible ...

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

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

发布评论

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

评论(1

め可乐爱微笑 2024-11-07 14:06:32

只需绘制一个半透明(alpha < 1.0)矩形

glEnable(GL_BLEND); 
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4ub(255, 255, 255, 128);
glRectf(x1, y1, x2, y2);
glDisable(GL_BLEND);

Just draw a translucent (alpha < 1.0) rectangle:

glEnable(GL_BLEND); 
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4ub(255, 255, 255, 128);
glRectf(x1, y1, x2, y2);
glDisable(GL_BLEND);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文