简体   繁体   English

LWJGL和Slick纹理显示为黑色…?

[英]LWJGL and Slick textures display as black…?

I use eclipse. 我用日食。 so in my workspace, under my project, i create a new folder "res" with a subfolder "images" that have all my png's for use as textures. 所以在我的工作区中,在我的项目下,我创建了一个带有子文件夹“ images”的新文件夹“ res”,该文件夹包含我所有的png用作纹理。 so here is the method im using for loading the textures: 所以这是im用于加载纹理的方法:

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
{
public int count = 0;
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())
        {

            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.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    GL11.glDisable(GL11.GL_COLOR_MATERIAL);

    GL11.glLoadIdentity();
    GL11.glOrtho(0, width, height, 0, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

}
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);
    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
    System.out.println("working." + wood.getTextureID());

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, wood.getTextureID());
    GL11.glBegin(GL11.GL_QUADS);


    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());

    GL11.glEnd();
    GL11.glDisable(GL11.GL_BLEND);

    System.out.println(wood.getHeight()+ " " +wood.getWidth());


}
public static void main (String[] args)
{

    new TextureDemo();


}

} }

WHY WHY WHY are my textures black lol? 为什么为什么我的纹理为黑色大声笑? I really don't understand how my code did this. 我真的不明白我的代码是如何做到的。 Is it possible that my computer could have problems that are causing that? 我的计算机是否有可能引起此问题?

I'm sorry, however there are many problems with your code. 抱歉,您的代码有很多问题。 I would recommend taking a look at some opengl tutorials. 我建议您看一下一些opengl教程。 The arcsynthesis one is very in-depth. 反电弧合成非常深入。

Your problem is that the texture "wood" is never bound to opengl. 您的问题是纹理“ wood”永远不会绑定到opengl。

In order to bind your texture to the opengl context you must call 为了将纹理绑定到opengl上下文,您必须调用

glBindTexture(GL_TEXTURE_2D, id);

before glbegin() 在glbegin()之前

The id is the texture id generated by glGentextures or in your case the slick util method. 该id是glGentextures生成的纹理ID,或者是您使用的glGentextures util方法。 Also what is wood? 还有什么是木材? In the first code block it is the texture id but in the second block you are calling 'getHeght()' on type wood. 在第一个代码块中,它是纹理ID,但是在第二个代码块中,您在wood类型上调用“ getHeght()”。 I am confused as to what 'wood' is. 我对“木头”是什么感到困惑。

Either way, you need to bind the texture for opengl to use it and I again recommending looking at a few tutorials on the opengl basics. 无论哪种方式,您都需要为opengl绑定纹理才能使用它,我再次建议查看一些有关opengl基础的教程。

Asking the same question twice won't help. 两次问相同的问题无济于事。 However, you should make sure you first call glBindTexture , before you start glBegin . 但是,您应该确保在启动glBegin之前先调用glBindTexture

You haven't enabled textures! 您尚未启用纹理! In your initGL method call GL11.glEnable(GL11.GL_TEXTURE_2D). 在您的initGL方法中,调用GL11.glEnable(GL11.GL_TEXTURE_2D)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM