gpt4 book ai didi

graphics - Slick-Util(在 lwjgl 中): Importing a texture and displaying it results in weird, 在纹理附近创建的额外像素(以线为单位)

转载 作者:行者123 更新时间:2023-12-04 04:57:54 25 4
gpt4 key购买 nike

代码:http://www.lwjgl.org/wiki/index.php?title=Slick-Util_Library_-_Part_1_-_Loading_Images_for_LWJGL

输出:

code output

(它应该只是一个矩形;但右侧、底部和右下角的像素出现了额外的线条。)

我也用 .jpg 对其进行了测试,似乎唯一的区别是底部的线条与要显示的矩形的宽度相同。

谁能帮我解释为什么/如何解决这个问题/如何正确导入图像?

import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class TextureExample {

/** The texture that will hold the image details */
private Texture texture;


/**
* Start the example
*/
public void start() {
initGL(800,600);
init();

while (true) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
render();

Display.update();
Display.sync(100);

if (Display.isCloseRequested()) {
Display.destroy();
System.exit(0);
}
}
}

/**
* Initialise the GL display
*
* @param width The width of the display
* @param height The height of the display
*/
private void initGL(int width, int height) {
try {
Display.setDisplayMode(new DisplayMode(width,height));
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}

GL11.glEnable(GL11.GL_TEXTURE_2D);

GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

// enable alpha blending
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

GL11.glViewport(0,0,width,height);
GL11.glMatrixMode(GL11.GL_MODELVIEW);

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}

/**
* Initialise resources
*/
public void init() {

try {
// load texture from PNG file
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/image.png"));

System.out.println("Texture loaded: "+texture);
System.out.println(">> Image width: "+texture.getImageWidth());
System.out.println(">> Image height: "+texture.getImageHeight());
System.out.println(">> Texture width: "+texture.getTextureWidth());
System.out.println(">> Texture height: "+texture.getTextureHeight());
System.out.println(">> Texture ID: "+texture.getTextureID());
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* draw a quad with the image on it
*/
public void render() {
Color.white.bind();
texture.bind(); // or GL11.glBind(texture.getTextureID());

GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(100,100);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(100+texture.getTextureWidth(),100);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(100+texture.getTextureWidth(),100+texture.getTextureHeight());
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(100,100+texture.getTextureHeight());
GL11.glEnd();
}

/**
* Main Class
*/
public static void main(String[] argv) {
TextureExample textureExample = new TextureExample();
textureExample.start();
}
}

最佳答案

纹理的分辨率需要是 2 的幂。一旦它们被初始化,它们就可以调整到任何需要的尺寸。

关于graphics - Slick-Util(在 lwjgl 中): Importing a texture and displaying it results in weird, 在纹理附近创建的额外像素(以线为单位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16533939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com