使用OpenGL加载图像时出现问题

时间:2013-11-12 12:08:11

标签: java image opengl load lwjgl

我在加载图片时遇到问题。这就是我得到的:http://imgur.com/BZaubNz。黑色空间不应该在那里。我不知道我的图像或代码是否有问题。我尝试过其他图像,有些工作正常,有些没有。

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

public void init() {
    try {
        texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("sprites/playButton.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void render() {
    texture.bind();
    Color.white.bind();

    GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0,0);
        GL11.glVertex2f(400-getWidth()/2,250);
        GL11.glTexCoord2f(1,0);
        GL11.glVertex2f(400+getWidth()/2,250);
        GL11.glTexCoord2f(1,1);
        GL11.glVertex2f(400+getWidth()/2,250+getHeight());
        GL11.glTexCoord2f(0,1);
        GL11.glVertex2f(400-getWidth()/2,250+getHeight());
    GL11.glEnd();
}

这是主要的渲染功能。

public void start() {
    try {
        Display.setDisplayMode(new DisplayMode(800,600));
        Display.setInitialBackground(255, 255, 255);
        Display.create();
        Display.setVSyncEnabled(true);
    } catch (LWJGLException e) {
        e.printStackTrace();
        System.exit(0);
    }

    // init OpenGL here
    MainMenu mainMenu = new MainMenu();
    mainMenu.init();

    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glShadeModel(GL11.GL_SMOOTH);        
    GL11.glDisable(GL11.GL_DEPTH_TEST);
    GL11.glDisable(GL11.GL_LIGHTING);                    

    GL11.glClearColor(255.0f, 255.0f, 255.0f, 0.0f);                
    GL11.glClearDepth(1);                                       

    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

    GL11.glViewport(0,0,800,600);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, 800, 600, 0, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);

    while (!Display.isCloseRequested()) {
        // render OpenGL here
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

        mainMenu.render();

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

0 个答案:

没有答案