Opengl LWJGL和光滑-未显示纹理
我试图创建一个简单的类来加载精灵表并从中绘制2D纹理。但是,当我尝试渲染其中一个精灵时,它并没有正确地进行渲染。我要么什么都没有,要么只是一些粉红色的点()。有人能帮我找出哪里不对劲吗Opengl LWJGL和光滑-未显示纹理,opengl,textures,rendering,lwjgl,Opengl,Textures,Rendering,Lwjgl,我试图创建一个简单的类来加载精灵表并从中绘制2D纹理。但是,当我尝试渲染其中一个精灵时,它并没有正确地进行渲染。我要么什么都没有,要么只是一些粉红色的点()。有人能帮我找出哪里不对劲吗 public enum Art { GREEN("misc_1", 7, 5, 27, 10), BLUE("misc_1", 6, 37, 28, 5), MAGENTA("misc_1", 19, 68, 28, 6); private String spritesheet;
public enum Art
{
GREEN("misc_1", 7, 5, 27, 10),
BLUE("misc_1", 6, 37, 28, 5),
MAGENTA("misc_1", 19, 68, 28, 6);
private String spritesheet;
private int coordX;
private int coordY;
private int width;
private int height;
Art(String s, int x, int y, int w, int h)
{
this.spritesheet = s;
this.coordX = x;
this.coordY = y;
this.width = w;
this.height = h;
}
public String getSpritesheet()
{
return this.spritesheet;
}
public void render(int x, int y, int w, int h)
{
if (spritesheets.containsKey(this.getSpritesheet()))
{
Texture tex = spritesheets.get(this.getSpritesheet());
if (glGetInteger(GL_TEXTURE_BINDING_2D) != tex.getTextureID())
{
tex.bind();
}
float i = coordX / tex.getWidth();
float j = coordY / tex.getHeight();
float k = (coordX + width) / tex.getWidth();
float l = (coordY + height) / tex.getHeight();
int xx = x + w;
int yy = y + h;
glBegin(GL_QUADS);
glTexCoord2f(i, j);
glVertex2i(x, y);
glTexCoord2f(k, j);
glVertex2i(xx, y);
glTexCoord2f(k, l);
glVertex2i(xx, yy);
glTexCoord2f(i, l);
glVertex2i(x, yy);
glEnd();
}
}
private static boolean isLoaded = false;
public static HashMap<String, Texture> spritesheets = new HashMap<String, Texture>();
public static void loadTextures()
{
if (!isLoaded)
{
try
{
Texture misc_1 = TextureLoader.getTexture("PNG", new FileInputStream("res/misc_1.png"));
spritesheets.put("misc_1", misc_1);
} catch (IOException e)
{
e.printStackTrace();
}
isLoaded = true;
}
}
}
编辑:我只需将getWidth()和getHeight()更改为getImageWidth()和getImageHeight(),这就解决了问题。您没有显示
纹理的定义。但我认为getWidth()
和getHeight()
返回int
结果。如果是这种情况,您在这里有一个问题:
float i = coordX / tex.getWidth();
float j = coordY / tex.getHeight();
coordX
和tex.getWidth()
都是int
类型的值,因此除法将作为整数除法执行。这意味着,例如,如果coordX
小于tex.getWidth()
,结果将始终为零
要获得浮点除法,需要将其中一个值强制转换为浮点
:
float i = (float)coordX / tex.getWidth();
float j = (float)coordY / tex.getHeight();
有关更多详细信息,请参见此问题的示例:。它实际上是Slick2D的(slick util)纹理类,返回的值是一个浮点值。但是多亏了你,我才注意到我必须使用getImageHeight()和getImageWidth()而不是getWidth()和getHeight()。这就解决了!现在我只需要找出如何阻止纹理变得模糊。再次感谢!:D
float i = (float)coordX / tex.getWidth();
float j = (float)coordY / tex.getHeight();