- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
知道为什么我无法使用 LWJGL 创建透明纹理吗?我使用 .gif
图像,因为 .PNG
图像会导致纹理出现奇怪的定位问题。我尝试了一些其他答案,但它没有解决我遇到的问题。我并不是最擅长 OpenGL,但我花了很多天试图弄清楚它。
纹理.java
public class Sprite {
/** The texture that stores the image for this sprite */
private Texture texture;
/** The width in pixels of this sprite */
private int width;
/** The height in pixels of this sprite */
private int height;
/**
* Create a new sprite from a specified image.
*
* @param loader the texture loader to use
* @param ref A reference to the image on which this sprite should be based
*/
public Sprite(TextureLoader loader, String ref) {
try {
texture = loader.getTexture(ref);
width = texture.getImageWidth();
height = texture.getImageHeight();
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(-1);
}
}
/**
* Get the width of this sprite in pixels
*
* @return The width of this sprite in pixels
*/
public int getWidth() {
return texture.getImageWidth();
}
/**
* Get the height of this sprite in pixels
*
* @return The height of this sprite in pixels
*/
public int getHeight() {
return texture.getImageHeight();
}
/**
* Draw the sprite at the specified location
*
* @param x The x location at which to draw this sprite
* @param y The y location at which to draw this sprite
*/
public void draw(int x, int y) {
// store the current model matrix
glPushMatrix();
// bind to the appropriate texture for this sprite
texture.bind();
// translate to the right location and prepare to draw
glTranslatef(x, y, 0);
// draw a quad textured to match the sprite
glBegin(GL_QUADS);
{
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, texture.getHeight());
glVertex2f(0, height);
glTexCoord2f(texture.getWidth(), texture.getHeight());
glVertex2f(width, height);
glTexCoord2f(texture.getWidth(), 0);
glVertex2f(width, 0);
}
glEnd();
// restore the model view matrix to prevent contamination
glPopMatrix();
}
public void draw(int x, int y,int rotate) {
// store the current model matrix
glPushMatrix();
// bind to the appropriate texture for this sprite
texture.bind();
// translate to the right location and prepare to draw
//glTranslatef(x, y, 0);
glTranslatef(x+getWidth()/2, y+getHeight()/2, 0); // M1 - 2nd translation
glRotatef(rotate, 0.0f, 0.0f, 1.0f); // M2
glTranslatef( -getWidth()/2, -getHeight()/2, 0); // M3 - 1st translation
// draw a quad textured to match the sprite
glBegin(GL_QUADS);
{
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, texture.getHeight());
glVertex2f(0, height);
glTexCoord2f(texture.getWidth(), texture.getHeight());
glVertex2f(width, height);
glTexCoord2f(texture.getWidth(), 0);
glVertex2f(width, 0);
}
glEnd();
// restore the model view matrix to prevent contamination
glPopMatrix();
}
<小时/>
纹理.java
public class Texture {
/** The GL target type */
private int target;
/** The GL texture ID */
private int textureID;
/** The height of the image */
private int height;
/** The width of the image */
private int width;
/** The width of the texture */
private int texWidth;
/** The height of the texture */
private int texHeight;
/** The ratio of the width of the image to the texture */
private float widthRatio;
/** The ratio of the height of the image to the texture */
private float heightRatio;
/**
* Create a new texture
*
* @param target The GL target
* @param textureID The GL texture ID
*/
public Texture(int target, int textureID) {
this.target = target;
this.textureID = textureID;
}
/**
* Bind the specified GL context to a texture
*/
public void bind() {
glBindTexture(target, textureID);
}
/**
* Set the height of the image
*
* @param height The height of the image
*/
public void setHeight(int height) {
this.height = height;
setHeight();
}
/**
* Set the width of the image
*
* @param width The width of the image
*/
public void setWidth(int width) {
this.width = width;
setWidth();
}
/**
* Get the height of the original image
*
* @return The height of the original image
*/
public int getImageHeight() {
return height;
}
/**
* Get the width of the original image
*
* @return The width of the original image
*/
public int getImageWidth() {
return width;
}
/**
* Get the height of the physical texture
*
* @return The height of physical texture
*/
public float getHeight() {
return heightRatio;
}
/**
* Get the width of the physical texture
*
* @return The width of physical texture
*/
public float getWidth() {
return widthRatio;
}
/**
* Set the height of this texture
*
* @param texHeight The height of the texture
*/
public void setTextureHeight(int texHeight) {
this.texHeight = texHeight;
setHeight();
}
/**
* Set the width of this texture
*
* @param texWidth The width of the texture
*/
public void setTextureWidth(int texWidth) {
this.texWidth = texWidth;
setWidth();
}
/**
* Set the height of the texture. This will update the
* ratio also.
*/
private void setHeight() {
if (texHeight != 0) {
heightRatio = ((float) height) / texHeight;
}
}
/**
* Set the width of the texture. This will update the
* ratio also.
*/
private void setWidth() {
if (texWidth != 0) {
widthRatio = ((float) width) / texWidth;
}
}
<小时/>
textureLoader.java
public class TextureLoader {
/** The table of textures that have been loaded in this loader */
private HashMap<String, Texture> table = new HashMap<String, Texture>();
/** The colour model including alpha for the GL image */
private ColorModel glAlphaColorModel;
/** The colour model for the GL image */
private ColorModel glColorModel;
/** Scratch buffer for texture ID's */
private IntBuffer textureIDBuffer = BufferUtils.createIntBuffer(1);
/**
* Create a new texture loader based on the game panel
*/
public TextureLoader() {
glAlphaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {8,8,8,8},
true,
false,
ComponentColorModel.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {8,8,8,0},
false,
false,
ComponentColorModel.OPAQUE,
DataBuffer.TYPE_BYTE);
}
/**
* Create a new texture ID
*
* @return A new texture ID
*/
private int createTextureID() {
glGenTextures(textureIDBuffer);
return textureIDBuffer.get(0);
}
/**
* Load a texture
*
* @param resourceName The location of the resource to load
* @return The loaded texture
* @throws IOException Indicates a failure to access the resource
*/
public Texture getTexture(String resourceName) throws IOException {
Texture tex = table.get(resourceName);
if (tex != null) {
return tex;
}
tex = getTexture(resourceName,
GL_TEXTURE_2D, // target
GL_RGBA, // dst pixel format
GL_LINEAR, // min filter (unused)
GL_LINEAR);
table.put(resourceName,tex);
return tex;
}
/**
* Load a texture into OpenGL from a image reference on
* disk.
*
* @param resourceName The location of the resource to load
* @param target The GL target to load the texture against
* @param dstPixelFormat The pixel format of the screen
* @param minFilter The minimising filter
* @param magFilter The magnification filter
* @return The loaded texture
* @throws IOException Indicates a failure to access the resource
*/
public Texture getTexture(String resourceName,
int target,
int dstPixelFormat,
int minFilter,
int magFilter) throws IOException {
int srcPixelFormat;
// create the texture ID for this texture
int textureID = createTextureID();
Texture texture = new Texture(target,textureID);
// bind this texture
glBindTexture(target, textureID);
BufferedImage bufferedImage = loadImage(resourceName);
texture.setWidth(bufferedImage.getWidth());
texture.setHeight(bufferedImage.getHeight());
if (bufferedImage.getColorModel().hasAlpha()) {
srcPixelFormat = GL_RGBA;
} else {
srcPixelFormat = GL_RGB;
}
// convert that image into a byte buffer of texture data
ByteBuffer textureBuffer = convertImageData(bufferedImage,texture);
if (target == GL_TEXTURE_2D) {
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilter);
}
// produce a texture from the byte buffer
glTexImage2D(target,
0,
dstPixelFormat,
get2Fold(bufferedImage.getWidth()),
get2Fold(bufferedImage.getHeight()),
0,
srcPixelFormat,
GL_UNSIGNED_BYTE,
textureBuffer );
return texture;
}
/**
* Get the closest greater power of 2 to the fold number
*
* @param fold The target number
* @return The power of 2
*/
private static int get2Fold(int fold) {
int ret = 2;
while (ret < fold) {
ret *= 2;
}
return ret;
}
/**
* Convert the buffered image to a texture
*
* @param bufferedImage The image to convert to a texture
* @param texture The texture to store the data into
* @return A buffer containing the data
*/
private ByteBuffer convertImageData(BufferedImage bufferedImage,Texture texture) {
ByteBuffer imageBuffer;
WritableRaster raster;
BufferedImage texImage;
int texWidth = 2;
int texHeight = 2;
// find the closest power of 2 for the width and height
// of the produced texture
while (texWidth < bufferedImage.getWidth()) {
texWidth *= 2;
}
while (texHeight < bufferedImage.getHeight()) {
texHeight *= 2;
}
texture.setTextureHeight(texHeight);
texture.setTextureWidth(texWidth);
// create a raster that can be used by OpenGL as a source
// for a texture
if (bufferedImage.getColorModel().hasAlpha()) {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,4,null);
texImage = new BufferedImage(glAlphaColorModel,raster,false,new Hashtable());
} else {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,3,null);
texImage = new BufferedImage(glColorModel,raster,false,new Hashtable());
}
// copy the source image into the produced image
Graphics g = texImage.getGraphics();
g.setColor(new Color(0f,0f,0f,0f));
g.fillRect(0,0,texWidth,texHeight);
g.drawImage(bufferedImage,0,0,null);
// build a byte buffer from the temporary image
// that be used by OpenGL to produce a texture.
byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();
imageBuffer = ByteBuffer.allocateDirect(data.length);
imageBuffer.order(ByteOrder.nativeOrder());
imageBuffer.put(data, 0, data.length);
imageBuffer.flip();
return imageBuffer;
}
/**
* Load a given resource as a buffered image
*
* @param ref The location of the resource to load
* @return The loaded buffered image
* @throws IOException Indicates a failure to find a resource
*/
private BufferedImage loadImage(String ref) throws IOException {
URL url = TextureLoader.class.getClassLoader().getResource(ref);
if (url == null) {
throw new IOException("Cannot find: " + ref);
}
// due to an issue with ImageIO and mixed signed code
// we are now using good oldfashioned ImageIcon to load
// images and the paint it on top of a new BufferedImage
Image img = new ImageIcon(url).getImage();
BufferedImage bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bufferedImage.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return bufferedImage;
}
}
最佳答案
为了便于阅读,我将其作为答案
初始化 openGL 调用后
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
这将启用标准类型的混合
您不必在每次渲染调用时使用它,只需在开始时使用一次
关于java - LWJGL 透明度不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20687407/
如何以编程方式创建最大化的 lwjgl 窗口或使已创建的显示最大化? 注意:我不是问如何为显示器设置全屏模式。 最佳答案 Display.setResizable(true) 这将启用最大化按钮。 关
我对使用外部 Java 库非常陌生,我从基于 LWJGL 构建的 Slick2D 开始。我编写了一个简单的 StateBasedGame,它只在屏幕上打印三个选项。它给了我一个关于某些(jar?)文件
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.libr ary.path at
我正在 Itellij 中制作一个依赖 org.lwjgl.input 代码的 Minecraft mod。该库已从 LWJGL 3 中删除,因此我必须使用 LWJGL 2。我将正确的 LWJGL 2
我安装了 lwjgl 库和 natives 并将其放入/lib 文件夹。在 Eclipse Java 构建路径上设置库位置和 native 库位置。当我尝试在 Eclipse 上运行它时,它可以工作,
所以我已经使用 LWJGL 开发一个程序很长一段时间了,现在我希望能够轻松地向其他人展示它。但是,当我尝试将其导出为可运行的 jar 文件并在控制台中运行时,出现上述错误。 我还尝试将其导出为普通的
我尝试使用 LWJGL 编译 ( NetBeans ) Java 项目。我单击了项目 -> 属性,然后单击库 -> 编译。我添加了 Jars 位置、源文件位置和 javadoc 位置。 当我尝试构建项
在 NEHE 第 06 课 (http://nehe.gamedev.net/tutorial/texture_mapping/12038/) 中,您可以加载图像并用它来纹理对象。我目前使用的是LWJ
我正在尝试向我的程序添加全屏功能,但我无法让它工作。我想 Display.setFullscreen(true); 我尝试将其位置更改为创建显示的位置或设置显示模式的位置上方,但仍然无法正常工作。有什
我刚刚开始使用 LWJGL 和 OpenGL,但遇到了问题。我正在尝试弄清楚如何使用我找到的游戏引擎为我正在制作的游戏绘制一条简单的线条。 这是我正在使用的引擎的链接:https://github.c
我正在尝试在 java lwjgl 2.9 中编译着色器,如果我运行我的游戏,它无法编译着色器。问题是为什么着色器不重新识别 const 关键字,如果我删除 const 那么它会抛出错误 float
我在使用 Java 和 lwjgl 导入时遇到问题: import org.lwjgl.input.Keyboard; 行不通。我已经添加了 jar : lwjgl-glfw.jar lwjgl-op
我在 OpenGL 上下文中移动实体时遇到问题:当我尝试放置一个实体时,位置似乎是正确的,但是当实体开始移动时,一切都会出错,并且碰撞不起作用。我是 OpenGL 新手,我怀疑我的世界矩阵或模型矩阵是
我正在做一项家庭作业,使用 LightWeight Java 游戏库修改教授给出的代码。问题是我无法完全加载测试代码来开始测试修改。我已经按照修改中的说明链接了 jar 文件,但仍然存在一个挥之不去的
我是 Java 新手,我想将基本的 LWJGL 小程序上传到我的网站并在浏览器中播放。我不知道该怎么做,我想确保在继续开发游戏之前我知道如何让它工作。请帮助我,因为我不知道如何将 JWJGL 小程序或
我正在尝试旋转屏幕上的两个对象之一。我该怎么做?我只能让它旋转屏幕上的所有对象。代码: glColor3f(1, 0, 0); glBegin(GL_QUADS
我目前正在渲染一个天空盒,如下所示: //Front Face GL11.glBindTexture(GL11.GL_TEXTURE_2D, skybox[0].getText
我正在编写类似 Minecraft 的游戏。为了加载纹理和字体(目前没有声音),我使用 slick。 Slick 不再开发,maven 存储库不可用。是否有其他库(LibGDX 除外)可用于加载 lw
我正在尝试使用opengl在lwjgl显示屏上显示具有自定义字体的文本。目前,我正在使用自定义位图字体类从png文件加载字体,并以与tileet相同的方式显示它们。它可以正常工作,但是当我将文本绘制到
我正在使用 VBO 来渲染游戏中的所有立方体(因为会有很多立方体),并且我希望它们都带有纹理。当我运行我的代码时,立方体没有纹理(相反,它看起来有点红色,因为我的纹理是砖墙),并且只有当我非常接近立方
我是一名优秀的程序员,十分优秀!