首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >OpenGL透明度不起作用

OpenGL透明度不起作用
EN

Stack Overflow用户
提问于 2015-02-20 23:17:11
回答 1查看 239关注 0票数 0

我试图用LWJGL将我的字体纹理地图集绘制到屏幕上,但是OpenGL却画了一个坚实的白色方块。

使用我的绘图代码的工作示例:

代码语言:javascript
运行
复制
import java.awt.image.*;
import java.io.*;
import java.nio.*;
import javax.imageio.*;
import org.lwjgl.*;
import org.lwjgl.opengl.*;

public class OpenGLImageTest
{
    private static int textureID;

    public static void main(String[] args) throws Exception
    {
        Display.setTitle("OpenGL Image Test");
        Display.setDisplayMode(new DisplayMode(640, 480));
        Display.create();

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 640, 0, 480, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        textureID = bindTextureFile("textures/font.png");

        while(!Display.isCloseRequested())
        {
            Display.sync(60);

            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
            GL11.glClearColor(0, 0, 0, 1);

            GL11.glColor4f(1, 0, 0, 1);
            drawFontAtlas(0, 0);

            Display.update();
        }

        Display.destroy();
    }

    private static void drawFontAtlas(int x, int y)
    {
        GL11.glPushMatrix();
            GL11.glTranslatef(x, y, 0);
            GL11.glDisable(GL11.GL_DEPTH_TEST);
            GL11.glEnable(GL11.GL_TEXTURE_2D);
            GL11.glEnable(GL11.GL_BLEND);
                GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
                GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
                GL11.glBegin(GL11.GL_QUADS);
                    GL11.glVertex2i(0, 0);
                    GL11.glVertex2i(0, 256);
                    GL11.glVertex2i(256, 256);
                    GL11.glVertex2i(256, 0);
                GL11.glEnd();
            GL11.glDisable(GL11.GL_BLEND);
            GL11.glDisable(GL11.GL_TEXTURE_2D);
            GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glPopMatrix();
    }

    private static int bindTextureFile(String file)
    {
        try
        {
            BufferedImage image = ImageIO.read(new FileInputStream(file));

            int[] pixels = new int[image.getWidth() * image.getHeight()];

            image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());

            ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);

            for(int y = 0; y < image.getWidth(); y++)
            {
                for(int x = 0; x < image.getHeight(); x++)
                {
                    int pixel = pixels[y * image.getWidth() + x];

                    buffer.put((byte)((pixel >> 16) & 0xFF));
                    buffer.put((byte)((pixel >> 8) & 0xFF));
                    buffer.put((byte)(pixel & 0xFF));
                    buffer.put((byte)((pixel >> 24) & 0xFF));
                }
            }

            buffer.flip();

            int textureID = GL11.glGenTextures();

            GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);

            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);

            GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB8, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);

            return textureID;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return -1;
    }
}

有人能告诉我我做错了什么以及如何解决吗?

编辑: font.png在透明白色上是白色的。这是默认的“我的世界”字体,用于测试。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-21 08:56:15

您的问题可以通过添加纹理坐标来解决:

代码语言:javascript
运行
复制
    {
        GL11.glTexCoord2f(0, 1); // added texture coordinate
        GL11.glVertex2i(0, 0);
        GL11.glTexCoord2f(0, 0); // added texture coordinate
        GL11.glVertex2i(0, 256);
        GL11.glTexCoord2f(1, 0); // added texture coordinate
        GL11.glVertex2i(256, 256);
        GL11.glTexCoord2f(1, 1); // added texture coordinate
        GL11.glVertex2i(256, 0);
    }

我实际上使用slick-util.jar来加载您的纹理,但是它将与您的代码一样工作.你可以找到我的代码的一个工作例子。slick-util.jar可以找到这里

为了获得实际的透明性,您还需要在代码中更改这一行:GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB8, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);,需要更改的是将GL11.GL_RGB8改为GL11.GL_RGBA

代码语言:javascript
运行
复制
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class OpenGLImageTest {

    private static Texture tex; //private static int textureID;

    public static void main(String[] args) throws Exception {
        Display.setTitle("OpenGL Image Test");
        Display.setDisplayMode(new DisplayMode(640, 480));
        Display.create();

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 640, 0, 480, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        tex = loadTexture("textures/font.png"); //textureID = bindTextureFile("textures/font.png");

        while (!Display.isCloseRequested()) {
            Display.sync(60);

            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
            GL11.glClearColor(0, 0, 0, 1);

            GL11.glColor4f(1, 1, 1, 1);
            drawFontAtlas(0, 0);

            Display.update();
        }

        Display.destroy();
    }

    private static void drawFontAtlas(int x, int y) {
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        GL11.glPushMatrix();
        GL11.glTranslatef(x, y, 0);
        GL11.glDisable(GL11.GL_DEPTH_TEST);
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glEnable(GL11.GL_BLEND);

        tex.bind(); //GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
        GL11.glBegin(GL11.GL_QUADS);
        {
            GL11.glTexCoord2f(0, 1); // added texture coordinate
            GL11.glVertex2i(0, 0);
            GL11.glTexCoord2f(0, 0); // added texture coordinate
            GL11.glVertex2i(0, 256);
            GL11.glTexCoord2f(1, 0); // added texture coordinate
            GL11.glVertex2i(256, 256);
            GL11.glTexCoord2f(1, 1); // added texture coordinate
            GL11.glVertex2i(256, 0);
        }
        GL11.glEnd();
        GL11.glDisable(GL11.GL_BLEND);
        GL11.glDisable(GL11.GL_TEXTURE_2D);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glPopMatrix();
    }

    public static Texture loadTexture(String texturePath) {
        try {
            return TextureLoader.getTexture("png", new FileInputStream(new File(texturePath)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28639880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档