纹理不解除绑定

时间:2014-05-24 14:52:58

标签: java opengl lwjgl

我正在尝试使用不同的纹理渲染多个顶点,但输出是这样的:http://i.stack.imgur.com/952p8.png如果我删除左边,它会使用前面的纹理,如果我删除前面它使用后面的纹理。

这是渲染代码。

package org.pcbuilder.renderer;

import java.util.HashMap;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureImpl;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
import org.pcbuilder.components.Case;

public class RenderUtils
{
    public static boolean caseInitialized = false;
    public static HashMap<String, Texture> textures;

    public static void renderCase(Case c, float scale, float x, float y)
    {
        if(!caseInitialized)
        {
            textures =  new HashMap<String, Texture>();
            for(Case ca : Case.types)
            {
                String location = "./images/case/" + ca.id;

                try
                {
                    String back = location + "/back.png";
                    String front = location + "/front.png";
                    String left = location + "/left.png";
                    String right = location + "/right.png";

                    String[] strings = new String[] {back, front, left, right};

                    for(String str : strings)
                    {
                        System.out.println(ca.id + "-" + str.replace(location, "").replace("/", "").replace(".png", ""));
                        System.out.println(str);
                        textures.put(ca.id + "-" + str.replace(location, "").replace("/", "").replace(".png", ""), TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(str)));
                    }
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }

            caseInitialized = true;
        }

        renderBack(c, scale, x + c.fbw, y - c.fbh / 3);
        renderLeft(c, scale, x, y, x + c.fbw, y - c.fbh / 3);
        renderFront(c, scale, x, y);
    }

    public static void renderLeft(Case c, float scale, float x, float y, float endX, float endY)
    {
        GL11.glBegin(GL11.GL_QUADS);

        GL11.glColor3f(1f, 1f, 1f);

        Texture t = textures.get(c.id + "-" + "left");
        t.bind();

        GL11.glTexCoord2f(0, 0);
        GL11.glVertex2f(0 + x, y);

        GL11.glTexCoord2f(1, 0);
        GL11.glVertex2f(t.getImageWidth() + x, endY);

        GL11.glTexCoord2f(1, 1);
        GL11.glVertex2f(t.getImageWidth() + x, t.getImageHeight() + y);

        GL11.glTexCoord2f(0, 1);
        GL11.glVertex2f(0 + endX, t.getImageHeight() + endY);

        GL11.glEnd();   

        TextureImpl.unbind();
    }

    public static void renderRight(Case c, float scale, float x, float y)
    {

    }

    public static void renderFront(Case c, float scale, float x, float y)
    {
        GL11.glBegin(GL11.GL_QUADS);

        GL11.glColor3f(1f, 1f, 1f);

        Texture t = textures.get(c.id + "-" + "front");
        t.bind();

        GL11.glTexCoord2f(0, 0);
        GL11.glVertex2f(0 + x, 0 + y);

        GL11.glTexCoord2f(1, 0);
        GL11.glVertex2f(t.getImageWidth() + x, 0 + y);

        GL11.glTexCoord2f(1, 1);
        GL11.glVertex2f(t.getImageWidth() + x, t.getImageHeight() + y);

        GL11.glTexCoord2f(0, 1);
        GL11.glVertex2f(0 + x, t.getImageHeight() + y);

        GL11.glEnd();   

        TextureImpl.unbind();
    }

    public static void renderBack(Case c, float scale, float x, float y)
    {
        GL11.glBegin(GL11.GL_QUADS);

        GL11.glColor3f(1f, 1f, 1f);

        Texture t = textures.get(c.id + "-" + "back");
        t.bind();

        GL11.glTexCoord2f(0, 0);
        GL11.glVertex2f(0 + x, 0 + y);

        GL11.glTexCoord2f(1, 0);
        GL11.glVertex2f(t.getImageWidth() + x, 0 + y);

        GL11.glTexCoord2f(1, 1);
        GL11.glVertex2f(t.getImageWidth() + x, t.getImageHeight() + y);

        GL11.glTexCoord2f(0, 1);
        GL11.glVertex2f(0 + x, t.getImageHeight() + y);

        GL11.glEnd();   

        TextureImpl.unbind();
    }
}

1 个答案:

答案 0 :(得分:3)

在Begin / End块中,只允许GL命令的一个非常有限的子集 - 只有操纵顶点当前属性的命令,而glVertex调用自己最终发出一个顶点。

这尤其意味着您尝试发出的glBindTexture()来电无效,并且只会导致生成GL错误。