使用LWJGL和SlickUtil显示纹理的问题

时间:2017-07-11 14:12:06

标签: java textures lwjgl slick

我尝试了很多,但没有发现我的错误。继承我的代码:

public class Renderer {
public static final int WIDTH = 1080, HEIGHT = 720, FPS = 60;
public static void createDisplay()
{
    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);
}

public static void updateDisplay(){
    Display.update();
    Display.sync(FPS);
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(0.2f,0.2f,0.2f,1.0f);
}

public static void destroyDisplay()
{
    Display.destroy();
}

public static void renderDisplay(ArrayList<GameObject> render, Camera cam)
{

    Color.white.bind();

    for(GameObject obj: render)
    {

        obj.getTex().bind();
       if(obj instanceof Player)
        {
            glBegin(GL_QUADS);
            glTexCoord2f(0,0);
            glVertex2f(obj.getX()+WIDTH/2-cam.getX(), obj.getY()+HEIGHT/2-cam.getY());
            glTexCoord2f(obj.getTex().getWidth(),0);
            glVertex2f(obj.getX()+WIDTH/2+obj.getSizeX()-cam.getX(), obj.getY()+HEIGHT/2-cam.getY());
            glTexCoord2f(obj.getTex().getWidth(),obj.getTex().getHeight());
            glVertex2f(obj.getX()+WIDTH/2+obj.getSizeX()-cam.getX(), obj.getY()+HEIGHT/2+obj.getSizeY()-cam.getY());
            glTexCoord2f(0,obj.getTex().getHeight());
            glVertex2f(obj.getX()+WIDTH/2-cam.getX(), obj.getY()+HEIGHT/2+obj.getSizeY()-cam.getY());
            glEnd();
        }else{
            glBegin(GL_QUADS);
            glTexCoord2f(0,0);
            glVertex2f((obj.getX()-cam.getX()), (obj.getY()-cam.getY()));
            glTexCoord2f(1,0);
            glVertex2f((obj.getX()-cam.getX())+obj.getSizeX(), (obj.getY()-cam.getY()));
            glTexCoord2f(1,1);
            glVertex2f((obj.getX()-cam.getX())+obj.getSizeX(), (obj.getY()-cam.getY())+obj.getSizeY());
            glTexCoord2f(0,1);
            glVertex2f((obj.getX()-cam.getX()), (obj.getY()-cam.getY())+obj.getSizeY());
            glEnd();

        }
        obj.getTex().release(); 
    }

}

没有错误,纹理不会显示。我使用这种方法加载图像:

public static Texture getTex(String path)
{
    Texture texture = null;
    try {
        texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(path));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Display.destroy();
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
        Display.destroy();
        System.exit(1);
    }
    return texture;
}

这是我的主要游戏循环:

public static Player player = new Player(0, 0, 0, 0, 20, 20, 2, null);
public static Camera cam = new Camera(0, 0);
static ArrayList<GameObject> toRender = new ArrayList<GameObject>();

public static void main(String[] args) {
    Renderer.createDisplay();
    toRender.add(new Wall(300, 300, 200, 100, IO.getTex("res/Terrain/Grass.png")));
    toRender.add(player);   
    player.setTex(IO.getTex("res/Entities/Player/Player.png"));
    while(!Display.isCloseRequested())
    {
        Renderer.updateDisplay();
        Input.updateInput();
        player.update();
        Renderer.renderDisplay(toRender, cam);
    }
    Renderer.destroyDisplay();
    System.exit(0);
}

它只展示了一个白色四边形而不是纹理四边形。

1 个答案:

答案 0 :(得分:0)

要在LWJGL四边形上使用光滑渲染纹理加载,您需要在绘制四边形之前绑定纹理

试试

texture.bind(); //texture = your Texture name

glBegin(GL_QUADS);