Java lwjgl 和 slick 不显示我的纹理

标签 java opengl textures lwjgl slick2d

我尝试了很多不同的方法,但到目前为止没有任何效果。起初,问题是程序运行时只有一个黑屏。我终于让它显示一个空白的白色方 block 。我的图像是 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();
}


}

我只是想能够看到我在程序中拥有的 png。你知道为什么我会得到白色图像吗? initGL 方法的顺序有多重要?

最佳答案

不确定,但我认为您必须使用这种旧的已弃用的 OpenGL 样式glEnable(GL_TEXTURE_2D);。 (而不是 GL_TEXTURE)

更新:我真的认为你应该这样做glEnable(GL_TEXTURE_2D);。尝试使用灰色透明颜色。也许它们只是变成了黑色方 block 而不是白色。 Slick 有一个绑定(bind)纹理的函数(我不确定 getTextureID 返回什么)。

更新:确保先glBindTexture,然后glBegin

关于Java lwjgl 和 slick 不显示我的纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18499531/

相关文章:

c++ - OpenGL 模糊 RAW 文件

java - JNI : convert Primitive type to jobject Or SetObjectArrayElement of Object type after casting

java - CloseableHttpResponse.close() 和 httpPost.releaseConnection() 有什么区别?

java - 二维数组不是深度克隆,更改会影响两个数组

java - Selenium WebDriver 中的 MouseHover 和 Click 事件

multithreading - 带有 OpenMP 的 OpenGL 总是出现段错误

ios - 如何将 bgra8Unorm iOS-Metal 纹理转换为 rgba8Unorm 纹理?

c++ - OGRE 异常(7:InternalErrorException):在 FreeImageCodec::decode 中解码图像时出错

java - 使用 LWJGL 创建隐藏 Canvas

c++ - 离屏渲染(使用 FBO 和 RenderBuffer)以及颜色、深度、模板的像素传输