Java的LWJGL /油滑的Util texture.bind()结合了相同的纹理每次 [英] Java LWJGL / Slick-Util texture.bind() binds the same texture everytime

查看:285
本文介绍了Java的LWJGL /油滑的Util texture.bind()结合了相同的纹理每次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编程现在了几年,想尝试的Java LWJGL,人们用油滑的Util库,所以我将所有的它,等推荐的,我已经工作几个项目,从来没有遇到此问题。不过,我遇到过一个纹理绑定OpenGL的时候,同样的质感是不正确的约束,而不是每次都出现问题。我和一帮班等项目就遇到了这个问题,第一,而选择了尝试凝结问题成一个jar文件。

I've been programming for a few years now and wanted to try Java LWJGL, and people recommended using the Slick-Util library so I set all of it up, etc. and I've been working on several projects, never encountering this problem. However, I've encountered a problem where when binding a texture to openGL, the same incorrect texture is instead bound every time. I ran into this problem first with a project with a bunch of classes etc, and chose to try to condense the problem into one jar file.

所以我才显示code,我想解释一下示例文件是如何工作的。基本上,有我的项目在四PNG的。它们包含在res /纹理/字符/ texturesHere。的PNG的名称由为:char_d.png,char_u.png,char_l.png,char_r.png。我已经检查了所有四个四个有人物的方向不同的纹理,因为它应该是。这是为了证明该项目的资源都没有问题。这里是浓缩成一个主类文件中的整个问题。

So before I show the code, I wanna explain how the example file works. Basically, there are four PNG's within my project. They are contained in res/textures/character/texturesHere. The names of the PNG's consist as: char_d.png, char_u.png, char_l.png, char_r.png. I've checked all four and all four have different textures of the character's direction, as it should be. This is to prove that the resources of the project are NOT the problem. Here is the entire problem condensed into a "Main" class file.

package main_p;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

import static org.lwjgl.opengl.GL11.*;

public class Main
{

    public final int srcWidth = 1280;
    public final int srcHeight = 720;

    public float x = 150;
    public float y = 150;
    public float w = 40;
    public float h = 40;

    public Texture char_down;
    public Texture char_right;
    public Texture char_left;
    public Texture char_up;

    public Main()
    {
        try
        {
            Display.setTitle("Escape Alpha v0.0.1");
            Display.setDisplayMode(new DisplayMode(srcWidth, srcHeight));
            Display.create();
        }
        catch (LWJGLException e)
        {
            e.printStackTrace();
        }

        initGL();
        initTextures();
        loop();
    }

    public void loop()
    {
        glClear(GL_COLOR_BUFFER_BIT);

        while (!Display.isCloseRequested())
        {
            glBegin(GL_QUADS);

                char_left.bind();
                glTexCoord2f(0, 0);
                glVertex2f(x, y);
                glTexCoord2f(1, 0);
                glVertex2f(x + w, y);
                glTexCoord2f(1, 1);
                glVertex2f(x + w, y + h);
                glTexCoord2f(0, 1);
                glVertex2f(x, y + h);

            glEnd();

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

        Display.destroy();
    }

    public void initGL()
    {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, srcWidth, srcHeight, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    }

    public void initTextures()
    {
        char_down = loadTextureFrom("character/char_d.png");
        char_up = loadTextureFrom("character/char_u.png");
        char_left = loadTextureFrom("character/char_l.png");
        char_right = loadTextureFrom("character/char_r.png");
    }

    public Texture loadTextureFrom(String path)
    {
        try
        {
            return TextureLoader.getTexture("PNG", new FileInputStream(new File("res/textures/" + path)));
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        return null;
    }

    public static void write(Object ob)
    {
        System.out.println(ob);
    }

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

有一件事我注意到的是,initTextures方法中(),如果我改变了最后加载的质感,我绑定任何纹理将最终渲染initTextures加载的最后质地()

One thing I've noticed is that within the initTextures() method, if I change the LAST loaded texture, ANY texture I bind will end up rendering the last texture loaded in initTextures()

在这个例子中我绑定纹理char_left,而是它使用char_right,因为这是我initTextures()方法加载的最后一个。如果我改变任何纹理最后加载在我initTextures我可以,因为证明这一点()方法,它最终呈现一个代替,再次无视纹理我告诉它绑定。就像我说的,我检查了我所有的PNG的多次,他们都好看,并显示朝向正确的方向的字符。我loadTextureFrom(字符串路径)方法是任何其它质地装载机。什么特别的地方真的。其实,我用我的Java LWJGL /油滑的Util 2D项目的90%,这个确切的方法,我是最近才开始运行到这个问题。我在失去了言语,以前从来没有碰到了这个问题。任何帮助是极大的AP preciated。 :)

In this example I bind Texture char_left, but instead it uses char_right, because it was the last one loaded in my initTextures() method. I can prove this because if I change whichever texture is LAST loaded in my initTextures() method, it ends up rendering that one instead, again disregarding the texture I tell it to bind. Like I said, I've checked all of my PNG's multiple times, and they all look good, and show the character facing the correct direction. My loadTextureFrom(String path) method is as any other texture loader. Nothing special about it really. I actually am using this exact method in 90% of my 2D projects for Java LWJGL / Slick-Util, and I've only recently started running into this problem. I'm at a loss for words, and have never ran into this problem before. Any help is greatly appreciated. :)

编辑1:

我只是做了一个重要的考验。我检查了所有四个纹理的哈希code,经过initTextures内加载它们()方法。

I just did an important test. I checked the HashCode of all four textures, AFTER loading them within the initTextures() method.

public void initTextures()
{
    char_left = loadTextureFrom("character/char_l.png");
    char_right = loadTextureFrom("character/char_r.png");
    char_down = loadTextureFrom("character/char_d.png");
    char_up = loadTextureFrom("character/char_u.png");

    write(System.identityHashCode(char_left));
    write(System.identityHashCode(char_right));
    write(System.identityHashCode(char_down));
    write(System.identityHashCode(char_up));
}

据返回此:

1364345882
1878339755
1246651385
1619367563

我觉得这确实证明了initTextures()方法是没有问题的。再次,任何帮助AP preciated!这种100%持有我回来从做任何项目的任何更多的工作:/谢谢! :)

I think this truly proves that the initTextures() method isn't the problem. Once again, any help is appreciated! This kind of 100% holds me back from doing any more work on any projects :/ Thanks! :)

推荐答案

我知道这是一个老的文章,但我不希望任何人谁看到这个去answerless。基本上,纹理绑定呼叫必须outsde了在glBegin。这就是问题的所在。您不能在glBegin调用里面绑定纹理。如果你不这样做,GL将只结合过去的质感加载。

I know this is an old post but I don't want anyone who sees this to go answerless. Basically, the texture bind call has to be outsde the "glBegin". That's all the problem is. You cannot bind textures inside of the glBegin call. If you don't do this, gl will just bind the last texture loaded.

这篇关于Java的LWJGL /油滑的Util texture.bind()结合了相同的纹理每次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆