[英]Java lwjgl and slick are not displaying my textures
I have tried a lot of different things, but nothing worked so far. 我尝试了很多不同的方法,但到目前为止没有任何效果。 at first, the problem was that there was just a blank screen when the program was run. 起初,问题在于程序运行时只有一个空白屏幕。 I finally got it to display a blank white square. 我终于得到它显示一个空白的白色正方形。 My images are 128x128 and the other is 512x512. 我的图片是128x128,另一个是512x512。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
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;
import org.lwjgl.input.Mouse;
import java.util.Random;
public class TextureDemo
{
private static Texture wood;
Random random = new Random();
public TextureDemo()
{
initGL(640, 480, "SLICK TEXTURES");
loadTexture("mozilla");
int x = 100, y = 100, count = 0, width = 0, height = 0, counter = 10;
while(true)
{
count++;
if(count == counter)
{
x--; y--; width++; height++; counter += random.nextInt(50) + 1;
}
render(x, y, width, height);
Display.update();
Display.sync(60);
if(Display.isCloseRequested())
{
wood.release();
Display.destroy();
System.exit(0);
}
}
}
private void initGL(int width, int height, String title)
{
try
{
Display.setDisplayMode(new DisplayMode(width, height));
Display.setTitle(title);
Display.create();
}
catch(LWJGLException e)
{
e.printStackTrace();
Display.destroy();
System.exit(1);
}
GL11.glEnable(GL11.GL_TEXTURE);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, 0, height, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public void loadTexture(String key)
{
try
{
wood = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("./res/images/"+key+".png"));
System.out.println("working." + wood.getTextureID());
}
catch(FileNotFoundException e)
{
e.printStackTrace();
Display.destroy();
System.exit(1);
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void render(int x, int y, int width, int height)
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
System.out.println("working." + wood.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, wood.getTextureID());
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(x, y);
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(x + wood.getImageWidth(), y);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(x + wood.getImageWidth(), y + wood.getImageHeight());
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(x, y + wood.getImageHeight());
GL11.glEnd();
System.out.println(wood.getHeight()+ " " +wood.getWidth());
}
public static void main (String[] args)
{
new TextureDemo();
}
}
I just want to be able to see the png's i have in the program. 我只希望能够看到程序中的png。 Soany ideas why i'm getting white images? Soany想法为什么我会得到白色图像? how important is the order of the initGL method? initGL方法的顺序有多重要?
Not sure about it, but I think you had to glEnable(GL_TEXTURE_2D);
不确定,但是我认为您必须glEnable(GL_TEXTURE_2D);
in this old deprecated OpenGL style. 以这种不推荐使用的旧OpenGL样式。 (Instead of GL_TEXTURE) (而不是GL_TEXTURE)
Update : I really think you should do glEnable(GL_TEXTURE_2D);
更新 :我真的认为您应该做glEnable(GL_TEXTURE_2D);
. 。 Try using a gray clear color. 尝试使用灰色透明色。 Maybe they just turned into black squares instead of whites. 也许他们只是变成了黑色正方形而不是白色正方形。 Slick has a function to bind the texture (I'm not sure what getTextureID returns). Slick具有绑定纹理的功能(我不确定getTextureID返回什么)。
Update: Make sure you first glBindTexture
and then glBegin
. 更新:确保首先使用glBindTexture
,然后再使用glBegin
。
I think your texturecoords are wrong. 我认为您的texturecoords是错误的。 Try replacing with this: 尝试替换为:
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(x, y);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(x + wood.getImageWidth(), y);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(x + wood.getImageWidth(), y + wood.getImageHeight());
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(x, y + wood.getImageHeight());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.