OpenGL绘制不透明纹理透明

时间:2014-01-12 18:50:43

标签: opengl transparency

我想在具有透明度值的四边形上绘制不透明纹理(传统的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和随机图像数据以绘制到表面的事物的随机抽象...)

此外,编程语言完全不相关......

1 个答案:

答案 0 :(得分:2)

我已经根据你想要的状态修改你的功能......你可以自己找出启用/禁用这些状态的位置,但是 必须 在您尝试绘制此多边形之前出现。

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