如何在OpenGL中使纹理透明? [英] How do I make textures transparent in OpenGL?

查看:565
本文介绍了如何在OpenGL中使纹理透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Google上进行研究,但我似乎没有任何连贯的简单答案。这是因为它不简单,还是因为我没有使用正确的关键字?



然而,这是我到目前为止的进步。


  1. 创建8个顶点以形成2个正方形。

  2. 创建了一个具有200位alpha值的纹理

  3. 注意到当我使用带有255 alpha的纹理时,


  4. init类似于以下内容:

      glClearColor(0.0,0.0,0.0,0.0); 
    glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);

    glEnable(GL_CULL_FACE);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

    glPixelStorei(GL_UNPACK_ALIGNMENT,1);
    glGenTextures(1,textureIds);
    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);

    int i,j;
    GLubyte pixel;
    for(i = 0; i {
    for(j = 0; j {
    pixel = ((((& 0x8)== 0)^((j& 0x8)== 0))* 255);
    texture [i] [j] [0] = pixel;
    texture [i] [j] [1] = pixel;
    texture [i] [j] [2] = pixel;
    texture [i] [j] [3] = 200;
    }
    }

    glBindTexture(GL_TEXTURE_2D,textureIds [0]);

    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

    glTexImage2D(
    GL_TEXTURE_2D,0,GL_RGBA,
    TEXTURE_WIDTH,TEXTURE_HEIGHT,
    0,GL_RGBA,GL_UNSIGNED_BYTE,texture);

    这有点类似于本书第417页的代码片段, ,并创建一个检查模式。



    然后,显示功能包含...

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

    //使用模型视图,以便旋转值是文字,不添加。
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();

    // ... translation,etc ...

    glBindTexture(GL_TEXTURE_2D,textureIds [0]);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0,0.0); glVertex3f(-1.0,+ 1.0,0.0); // top left
    glTexCoord2f(0.0,1.0); glVertex3f(-1.0,-1.0,0.0) // bottom left
    glTexCoord2f(1.0,1.0); glVertex3f(+1.0,-1.0,0.0); //右下角
    glTexCoord2f(1.0,0.0); glVertex3f(+1.0,+1.0,0.0); // top right
    glEnd();

    //不需要重复,只是好的做法
    glBindTexture(GL_TEXTURE_2D,textureIds [0]);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0,0.0); glVertex3f(-0.5,+1.0,-1.0); // top left
    glTexCoord2f(0.0,1.0); glVertex3f(-0.5,-1.0,-1.0); // bottom left
    glTexCoord2f(1.0,1.0); glVertex3f(+1.5,-1.0,-1.0); //右下角
    glTexCoord2f(1.0,0.0); glVertex3f(+1.5,+1.0,-1.0); //右上
    glEnd();

    glFlush();
    glDisable(GL_TEXTURE_2D);

    glPopMatrix();

    SwapBuffers();

    因此,这将在后台渲染一个第二个方块;我可以看到这一点,但它看起来像它们与背景混合(我假设这是因为他们是更暗的200位alpha比255位)而不是后面的纹理...





    如你所见,没有透明度...我如何解决这个问题?

    解决方案

    删除提到这一般,为了alpha混合正确工作,你需要在相机的坐标系中从远到近的对象排序。

    这就是为什么你的多边形与背景混合。您可以通过禁用深度测试来确认这确实是问题。没有深度测试,所有的片段都会显示出来,你可以看到alpha混合。



    此页


    I've tried to research this on Google but there doesn't appear to me to be any coherent simple answers. Is this because it's not simple, or because I'm not using the correct keywords?

    Nevertheless, this is the progress I've made so far.

    1. Created 8 vertices to form 2 squares.
    2. Created a texture with a 200 bit alpha value (so, about 80% transparent).
    3. Assigned the same texture to each square, which shows correctly.
    4. Noticed that when I use a texture with 255 alpha, it appears brighter.

    The init is something like the following:

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    glEnable(GL_DEPTH_TEST);
    
    glEnable(GL_CULL_FACE);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glGenTextures(1, textureIds);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    
    int i, j;
    GLubyte pixel;
    for (i = 0; i < TEXTURE_HEIGHT; i++)
    {
        for (j = 0; j < TEXTURE_WIDTH; j++)
        {
            pixel = ((((i & 0x8) == 0) ^ ((j & 0x8) == 0)) * 255);
            texture[i][j][0] = pixel;
            texture[i][j][1] = pixel;
            texture[i][j][2] = pixel;
            texture[i][j][3] = 200;
        }
    }
    
    glBindTexture(GL_TEXTURE_2D, textureIds[0]);
    
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    
    glTexImage2D(
        GL_TEXTURE_2D, 0, GL_RGBA,
        TEXTURE_WIDTH, TEXTURE_HEIGHT,
        0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
    

    This is somewhat similar to the code snippet from page 417 in the book, OpenGL Programming Guide, and creates a check pattern.

    And then, the display function contains...

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);
    
    // Use model view so that rotation value is literal, not added.
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    
    // ... translation, etc ...
    
    glBindTexture(GL_TEXTURE_2D, textureIds[0]);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, +1.0, 0.0); // top left
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, -1.0, 0.0); // bottom left
    glTexCoord2f(1.0, 1.0); glVertex3f(+1.0, -1.0, 0.0); // bottom right
    glTexCoord2f(1.0, 0.0); glVertex3f(+1.0, +1.0, 0.0); // top right
    glEnd();
    
        // not neccecary to repeat, just good practice
    glBindTexture(GL_TEXTURE_2D, textureIds[0]);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, +1.0, -1.0); // top left
    glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, -1.0, -1.0); // bottom left
    glTexCoord2f(1.0, 1.0); glVertex3f(+1.5, -1.0, -1.0); // bottom right
    glTexCoord2f(1.0, 0.0); glVertex3f(+1.5, +1.0, -1.0); // top right
    glEnd();
    
    glFlush();
    glDisable(GL_TEXTURE_2D);
    
    glPopMatrix();
    
    SwapBuffers();
    

    So, this renders a 2nd square in the background; I can see this but it looks like they're being blended with the background (I assume this because they are darker with 200 bit alpha than 255 bit) instead of the texture behind...

    As you can see, no transparency... How can I fix this?

    解决方案

    So the other answer which was here but was deleted mentioned this - Generally, for alpha blending to work correctly you need to sort the objects from far to near in the coordinate system of the camera.
    This is why your polygons are blended with the background. You can confirm that this is indeed the problem by disabling the depth test. Without depth test all the fragments are displayed and you'll be able to see the alpha blending.

    More on this in this page.

    这篇关于如何在OpenGL中使纹理透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆