2
\$\begingroup\$

I am making a 3D game using LWJGL and I have a texture class with static variables so that I only need to load textures once, even if I need to use them more than once. I am using Slick Util for this. When I bind a texture it works fine, but then when I try to render something else after I have rendered the model with the texture, the texture is still being bound. How do I unbind the texture and set the rendermode to the one that was in use before any textures were bound? Some of my code is below. The problem I am having is the player texture is being used in the box drawn around the player after it the model has been rendered.

Model.java

public class Model {

public List<Vector3f> vertices = new ArrayList<Vector3f>();
public List<Vector3f> normals = new ArrayList<Vector3f>();
public ArrayList<Vector2f> textureCoords = new ArrayList<Vector2f>();
public List<Face> faces = new ArrayList<Face>();

public static Model TREE;
public static Model PLAYER;

public static void loadModels() {
    try {
        TREE = OBJLoader.loadModel(new File("assets/model/tree_pine_0.obj"));
        PLAYER = OBJLoader.loadModel(new File("assets/model/player.obj"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void render(Vector3f position, Vector3f scale, Vector3f rotation, Texture texture, float shinyness) {
    glPushMatrix();
    {
        texture.bind();

        glColor3f(1, 1, 1);
        glTranslatef(position.x, position.y, position.z);
        glScalef(scale.x, scale.y, scale.z);
        glRotatef(rotation.x, 1, 0, 0);
        glRotatef(rotation.y, 0, 1, 0);
        glRotatef(rotation.z, 0, 0, 1);

        glMaterialf(GL_FRONT, GL_SHININESS, shinyness);

        glBegin(GL_TRIANGLES);
        {
            for (Face face : faces) {
                Vector2f t1 = textureCoords.get((int) face.textureCoords.x - 1);
                glTexCoord2f(t1.x, t1.y);
                Vector3f n1 = normals.get((int) face.normal.x - 1);
                glNormal3f(n1.x, n1.y, n1.z);
                Vector3f v1 = vertices.get((int) face.vertex.x - 1);
                glVertex3f(v1.x, v1.y, v1.z);

                Vector2f t2 = textureCoords.get((int) face.textureCoords.y - 1);
                glTexCoord2f(t2.x, t2.y);
                Vector3f n2 = normals.get((int) face.normal.y - 1);
                glNormal3f(n2.x, n2.y, n2.z);
                Vector3f v2 = vertices.get((int) face.vertex.y - 1);
                glVertex3f(v2.x, v2.y, v2.z);

                Vector2f t3 = textureCoords.get((int) face.textureCoords.z - 1);
                glTexCoord2f(t3.x, t3.y);
                Vector3f n3 = normals.get((int) face.normal.z - 1);
                glNormal3f(n3.x, n3.y, n3.z);
                Vector3f v3 = vertices.get((int) face.vertex.z - 1);
                glVertex3f(v3.x, v3.y, v3.z);
            }
            texture.release();
        }
        glEnd();
    }
    glPopMatrix();
}

}

Textures.java

public class Textures {

public static Texture FLOOR;
public static Texture PLAYER;

public static Texture SKYBOX_TOP;
public static Texture SKYBOX_BOTTOM;
public static Texture SKYBOX_FRONT;
public static Texture SKYBOX_BACK;
public static Texture SKYBOX_LEFT;
public static Texture SKYBOX_RIGHT;

public static void loadTextures() {
    try {
        FLOOR = TextureLoader.getTexture("PNG", new FileInputStream(new File("assets/model/floor.png")));
        FLOOR.setTextureFilter(GL11.GL_NEAREST);
        PLAYER = TextureLoader.getTexture("PNG", new FileInputStream(new File("assets/model/tree_pine_0.png")));
        PLAYER.setTextureFilter(GL11.GL_NEAREST);

        SKYBOX_TOP = TextureLoader.getTexture("PNG", new FileInputStream(new File("assets/textures/skybox_top.png")));
        SKYBOX_TOP.setTextureFilter(GL11.GL_NEAREST);
        SKYBOX_BOTTOM = TextureLoader.getTexture("PNG", new FileInputStream(new File("assets/textures/skybox_bottom.png")));
        SKYBOX_BOTTOM.setTextureFilter(GL11.GL_NEAREST);
        SKYBOX_FRONT = TextureLoader.getTexture("PNG", new FileInputStream(new File("assets/textures/skybox_front.png")));
        SKYBOX_FRONT.setTextureFilter(GL11.GL_NEAREST);
        SKYBOX_BACK = TextureLoader.getTexture("PNG", new FileInputStream(new File("assets/textures/skybox_back.png")));
        SKYBOX_BACK.setTextureFilter(GL11.GL_NEAREST);
        SKYBOX_LEFT = TextureLoader.getTexture("PNG", new FileInputStream(new File("assets/textures/skybox_left.png")));
        SKYBOX_LEFT.setTextureFilter(GL11.GL_NEAREST);
        SKYBOX_RIGHT = TextureLoader.getTexture("PNG", new FileInputStream(new File("assets/textures/skybox_right.png")));
        SKYBOX_RIGHT.setTextureFilter(GL11.GL_NEAREST);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

Player.java

public class Player {

private Vector3f position;
private float yaw;
private float moveSpeed;

public Player(float x, float y, float z, float yaw, float moveSpeed) {
    this.position = new Vector3f(x, y, z);
    this.yaw = yaw;
    this.moveSpeed = moveSpeed;
}

public void update() {
    if (Keyboard.isKeyDown(Keyboard.KEY_W))
        walkForward(moveSpeed);
    if (Keyboard.isKeyDown(Keyboard.KEY_S))
        walkBackwards(moveSpeed);
    if (Keyboard.isKeyDown(Keyboard.KEY_A))
        strafeLeft(moveSpeed);
    if (Keyboard.isKeyDown(Keyboard.KEY_D))
        strafeRight(moveSpeed);

    if (Mouse.isButtonDown(0))
        yaw += Mouse.getDX();

    LowPolyRPG.getInstance().getCamera().setPosition(-position.x, -position.y, -position.z);
    LowPolyRPG.getInstance().getCamera().setYaw(yaw);
}

public void walkForward(float distance) {
    position.setX(position.getX() + distance * (float) Math.sin(Math.toRadians(yaw)));
    position.setZ(position.getZ() - distance * (float) Math.cos(Math.toRadians(yaw)));
}

public void walkBackwards(float distance) {
    position.setX(position.getX() - distance * (float) Math.sin(Math.toRadians(yaw)));
    position.setZ(position.getZ() + distance * (float) Math.cos(Math.toRadians(yaw)));
}

public void strafeLeft(float distance) {
    position.setX(position.getX() + distance * (float) Math.sin(Math.toRadians(yaw - 90)));
    position.setZ(position.getZ() - distance * (float) Math.cos(Math.toRadians(yaw - 90)));
}

public void strafeRight(float distance) {
    position.setX(position.getX() + distance * (float) Math.sin(Math.toRadians(yaw + 90)));
    position.setZ(position.getZ() - distance * (float) Math.cos(Math.toRadians(yaw + 90)));
}

public void render() {
    Model.PLAYER.render(new Vector3f(position.x, position.y + 12, position.z), new Vector3f(3, 3, 3), new Vector3f(0, -yaw + 90, 0), Textures.PLAYER, 128);

    GL11.glPushMatrix();
    GL11.glTranslatef(position.getX(), position.getY(), position.getZ());
    GL11.glRotatef(-yaw, 0, 1, 0);
    GL11.glScalef(5.8f, 21, 2.2f);

    GL11.glDisable(GL11.GL_LIGHTING);

    GL11.glLineWidth(3);
    GL11.glBegin(GL11.GL_LINE_STRIP);

    GL11.glColor3f(1, 1, 1);

    glVertex3f(1f, 0f, -1f);
    glVertex3f(-1f, 0f, -1f);
    glVertex3f(-1f, 1f, -1f);
    glVertex3f(1f, 1f, -1f);

    glVertex3f(-1f, 0f, 1f);
    glVertex3f(1f, 0f, 1f);
    glVertex3f(1f, 1f, 1f);
    glVertex3f(-1f, 1f, 1f);

    glVertex3f(1f, 1f, -1f);
    glVertex3f(-1f, 1f, -1f);
    glVertex3f(-1f, 1f, 1f);
    glVertex3f(1f, 1f, 1f);

    glVertex3f(1f, 0f, 1f);
    glVertex3f(-1f, 0f, 1f);
    glVertex3f(-1f, 0f, -1f);
    glVertex3f(1f, 0f, -1f);

    glVertex3f(1f, 0f, 1f);
    glVertex3f(1f, 0f, -1f);
    glVertex3f(1f, 1f, -1f);
    glVertex3f(1f, 1f, 1f);

    glVertex3f(-1f, 0f, -1f);
    glVertex3f(-1f, 0f, 1f);
    glVertex3f(-1f, 1f, 1f);
    glVertex3f(-1f, 1f, -1f);

    GL11.glEnd();

    GL11.glEnable(GL11.GL_LIGHTING);

    GL11.glPopMatrix();
}

public Vector3f getPosition() {
    return new Vector3f(-position.x, -position.y, -position.z);
}

public float getX() {
    return position.getX();
}

public float getY() {
    return position.getY();
}

public float getZ() {
    return position.getZ();
}

public void setPosition(Vector3f position) {
    this.position = position;
}

public void setPosition(float x, float y, float z) {
    this.position.setX(x);
    this.position.setY(y);
    this.position.setZ(z);
}
}

Thanks for the help.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The quick way is enabling/disabling textures using glEnable(GL_TEXTURE_2D) / glDisable(GL_TEXTURE_2D). Whether it would be best to enable textures before rendering the player and then disabling them after or disabling them before the box and enabling them after, depends on the how/if the rest of your program uses textures throughout.

\$\endgroup\$
1
  • \$\begingroup\$ Most of my rendering is done using a texture so I tried disabling GL_TEXTURE_2D before I rendered the box around the player, and enabling it after but the texture still ends up being bound to the box when it is drawn. \$\endgroup\$
    – user35753
    Commented Feb 20, 2014 at 18:56

You must log in to answer this question.