【问题标题】:OpenGL draw opaque texture transparentOpenGL绘制不透明纹理透明
【发布时间】:2014-01-12 18:50:42
【问题描述】:

我想在具有透明度值的四边形上绘制不透明纹理(旧版 openGL - 所以没有着色器),以便它与背景混合到一定程度,使四边形透明。

有什么诀窍?搜索具有透明度的绘图内容通常会导致通过启用和设置混合功能来获得已经透明的纹理的教程...

编辑:考虑到评论,似乎就是这样:

drawTranparentSprite(TextureInformation t, ImageInformation i){
    glBindTexture(t.target, t.textureID);
    glBegin(GL_QUAD);
    {
        glColor4f(1,1,1,i.alpha)
        glTexCoord2d(t.x, t.y);
        glVertex3d(i.x, i.y, 0);
        glTexCoord2d(t.x, t.y + t.h);
        glVertex3d(i.x, i.y + i.h, 0);
        glTexCoord2d(t.x + t.w, t.y + t.h);
        glVertex3d(i.x + i.w, i.y + i.h, 0);
        glTexCoord2d(t.x + t.w, t.y);
        glVertex3d(i.x + i.w, i.y, 0);
    }
    glEnd();
}

尽管欢迎您纠正我...(TextureInformation 和 ImageInformation 是您需要处理 glTexture 和随机图像数据以绘制到表面的事物的随机抽象...)

编程语言在这里也完全不相关......

【问题讨论】:

  • 混合是诀窍。默认情况下,OpenGL 只是用任何新内容覆盖帧缓冲区的内容。但是如果你使用像 glBlendFunc (GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA) 这样的混合函数,它会将新的(源颜色)与旧的(目标颜色)混合——由新的(源 alpha)调制。
  • 问题是,源 alpha 值和目标 alpha 值都是 1 ... 那么如何设置每个顶点的 alpha 值?
  • @salberia:您可以设置每个顶点的 alpha 值以使用不透明纹理进行调制,以在固定功能管道中生成 alpha 分量。 glColor4f (1.0f, 1.0f, 1.0f, 0.5f),例如。虽然这可能需要您启用GL_COLOR_MATERIAL。您也可能正在使用顶点数组,从问题中不清楚 - 但您想要的是材料或每个顶点的 alpha。
  • 不,我正在按四边形绘制(绘制精灵) - 我想将“绘制透明图像”集成到我的库中

标签: opengl transparency


【解决方案1】:

我已经用必要的状态修改了你的函数来做你想做的事......你可以自己弄清楚在哪里启用/禁用这些状态,但它们必须 在您尝试绘制此多边形之前出现。

drawTranparentSprite(TextureInformation t, ImageInformation i){
  /**
   * Additions: Setup state necessary for blending and per-vertex color.
   *
  **/
  glBlendFunc  (GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
  glEnable     (GL_BLEND);
  glEnable     (GL_COLOR_MATERIAL);

  // Moved this out of the begin/end block to make it more obvious that it is constant...
  glColor4f    (1.0f, 1.0f, 1.0f, i.alpha);

  glBindTexture(t.target, t.textureID);
  glBegin(GL_QUAD);
  {
    glTexCoord2d(t.x, t.y);
    glVertex3d(i.x, i.y, 0);
    glTexCoord2d(t.x, t.y + t.h);
    glVertex3d(i.x, i.y + i.h, 0);
    glTexCoord2d(t.x + t.w, t.y + t.h);
    glVertex3d(i.x + i.w, i.y + i.h, 0);
    glTexCoord2d(t.x + t.w, t.y);
    glVertex3d(i.x + i.w, i.y, 0);
  }
  glEnd();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 2011-04-03
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多