OpenGL 透明度不起作用
OpenGL transparency not working
我正在尝试使用 LWJGL 将我的字体纹理图集绘制到屏幕上,但 OpenGL 却绘制了一个纯白色方块。
使用我的绘图代码的工作示例:
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
是透明白色上的白色。这是用于测试目的的默认 Minecraft 字体。
您的问题可以通过添加纹理坐标来解决:
{
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
可以被发现 here
为了获得实际的透明度,您还需要更改代码中的这一行: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
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;
}
}
我正在尝试使用 LWJGL 将我的字体纹理图集绘制到屏幕上,但 OpenGL 却绘制了一个纯白色方块。
使用我的绘图代码的工作示例:
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
是透明白色上的白色。这是用于测试目的的默认 Minecraft 字体。
您的问题可以通过添加纹理坐标来解决:
{
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
可以被发现 here
为了获得实际的透明度,您还需要更改代码中的这一行: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
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;
}
}