LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Bingo90 on June 14, 2013, 11:34:55

Title: LWJGL slick texture problem
Post by: Bingo90 on June 14, 2013, 11:34:55
When I'm loading a texture, it automatically binds it. When I want to bind a texture manually, it won't work. When I want to bind a color manually it'll work. How to get textures to work?

My Texture loading code:

ex = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(System.getenv("APPDATA") + "\\Test\\Resources\\Textures\\image.png"));
        tex2 = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(System.getenv("APPDATA") + "\\Test\\Resources\\Textures\\image2.png"));


Drawing Code:


while (!Display.isCloseRequested())
        {



            GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_COLOR_BUFFER_BIT);
            glEnable(GL_BLEND);

            GL11.glEnable(GL11.GL_DEPTH_TEST);
            GL11.glBegin(GL11.GL_POLYGON);

            glEnable(GL_TEXTURE_2D);
            glColor3f(0f, 1f, 1f);

            Color.white.bind(); // This works
            ResourceManager.tex.bind(); //This not
            GL11.glTexCoord2f(1, 1);


            GL11.glVertex3f(0.0f, 0.0f, 0.0f);
            GL11.glVertex3f(0.0f, 1.0f, 0.0f);
            GL11.glVertex3f(1.0f, 1.0f, 0.0f);
            GL11.glVertex3f(2.25f, 0.6f, 0.0f);
            GL11.glVertex3f(2.75f, 0.25f, 0.0f);
            GL11.glVertex3f(2.5f, 0.0f, 0.0f);

            GL11.glEnd();

            //Color.white.bind();

            GL11.glBegin(GL11.GL_POLYGON);



            GL11.glVertex3f(2.5f, 0.0f, 0.0f);
            GL11.glVertex3f(2.75f, 0.25f, 0.0f);


            GL11.glVertex3f(2.75f, 0.25f, 1.0f);
            GL11.glVertex3f(2.5f, 0.0f, 1.0f);



            GL11.glEnd();

            GL11.glBegin(GL11.GL_POLYGON);

            GL11.glVertex3f(2.25f, 0.6f, 0.0f);
            GL11.glVertex3f(2.25f, 0.6f, 1.0f);

            GL11.glVertex3f(2.75f, 0.25f, 1.0f);


            GL11.glVertex3f(2.75f, 0.25f, 0.0f);

            GL11.glEnd();

            GL11.glBegin(GL11.GL_POLYGON);

            GL11.glVertex3f(0.0f, 0.0f, 1.0f);
            GL11.glVertex3f(0.0f, 1.0f, 1.0f);
            GL11.glVertex3f(1.0f, 1.0f, 1.0f);
            GL11.glVertex3f(2.25f, 0.6f, 1.0f);
            GL11.glVertex3f(2.75f, 0.25f, 1.0f);
            GL11.glVertex3f(2.5f, 0.0f, 1.0f);

            GL11.glEnd();

            GL11.glBegin(GL11.GL_POLYGON);


            GL11.glVertex3f(1.0f, 1.0f, 0.0f);
            GL11.glVertex3f(1.0f, 1.0f, 1.0f);
            GL11.glVertex3f(2.25f, 0.6f, 1.0f);
            GL11.glVertex3f(2.25f, 0.6f, 0.0f);

            GL11.glEnd();

            GL11.glBegin(GL11.GL_POLYGON);


            GL11.glVertex3f(0.0f, 0.0f, 0.0f);
            GL11.glVertex3f(0.0f, 0.0f, 1.0f);
            GL11.glVertex3f(2.5f, 0.0f, 1.0f);
            GL11.glVertex3f(2.5f, 0.0f, 0.0f);

            GL11.glEnd();

            GL11.glBegin(GL11.GL_POLYGON);


            GL11.glVertex3f(0.0f, 1.0f, 0.0f);
            GL11.glVertex3f(0.0f, 1.0f, 0.2f);
            GL11.glVertex3f(1.0f, 1.0f, 0.2f);
            GL11.glVertex3f(1.0f, 1.0f, 0.0f);

            GL11.glEnd();

            GL11.glBegin(GL11.GL_POLYGON);


            GL11.glVertex3f(0.0f, 1.0f, 0.8f);
            GL11.glVertex3f(0.0f, 1.0f, 1.0f);
            GL11.glVertex3f(1.0f, 1.0f, 1.0f);
            GL11.glVertex3f(1.0f, 1.0f, 0.8f);

            GL11.glEnd();

            GL11.glBegin(GL11.GL_POLYGON);


            GL11.glVertex3f(0.6f, 1.0f, 0.8f);
            GL11.glVertex3f(0.6f, 1.0f, 0.2f);
            GL11.glVertex3f(1.0f, 1.0f, 0.2f);
            GL11.glVertex3f(1.0f, 1.0f, 0.8f);

            GL11.glEnd();

            GL11.glBegin(GL11.GL_POLYGON);


            GL11.glVertex3f(0.0f, 1.0f, 0.8f);
            GL11.glVertex3f(0.0f, 1.0f, 1.0f);
            GL11.glVertex3f(0.0f, 0.0f, 1.0f);
            GL11.glVertex3f(0.0f, 0.0f, 0.8f);

            GL11.glEnd();

            GL11.glBegin(GL11.GL_POLYGON);

            //GL11.glColor3f(0.75f,0.25f,0.25f);

            GL11.glVertex3f(0.0f, 1.0f, 0.2f);
            GL11.glVertex3f(0.0f, 1.0f, 0.0f);
            GL11.glVertex3f(0.0f, 0.0f, 0.0f);
            GL11.glVertex3f(0.0f, 0.0f, 0.2f);



            GL11.glEnd();
            glDisable(GL_BLEND);

            Display.update();

            GL11.glLoadIdentity();

        }
Title: Re: LWJGL slick texture problem
Post by: Fool Running on June 14, 2013, 13:06:36
Your code only specifies one texture coordinate (that I saw). You need to specify the texture coordinate at each vertex (i.e. before each call to GL11.glVertex3f).
Title: Re: LWJGL slick texture problem
Post by: Bingo90 on June 15, 2013, 12:13:29
This doesn't work. The textures loaded are green (image.png) and blue(image2.png). Changing the texture coordinates doesn't work. I would see if texture loading works if the face would be green, but it's blue
Title: Re: LWJGL slick texture problem
Post by: Fool Running on June 17, 2013, 13:27:25
We need to see the rest of your code, then (at least the rest of your ResourceManager class).

P.S. Why are you using a texture for what you should be using glColor3f() for?
Title: Re: LWJGL slick texture problem
Post by: Bingo90 on June 17, 2013, 15:33:29
It's just for testing, I want to use better textures later. Here's the ResourceManager class:

package net.test.manage;

import java.io.IOException;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class ResourceManager
{
    public static Texture tex;
    public static Texture tex2;

    public static void LoadContent() throws IOException
    {
        tex = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(System.getenv("APPDATA") + "\\Test\\Resources\\Textures\\image.png"));
        tex2 = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(System.getenv("APPDATA") + "\\Test\\Resources\\Textures\\image2.png"));
    }
}
Title: Re: LWJGL slick texture problem
Post by: Fool Running on June 18, 2013, 12:50:04
Ah! I paid closer attention to your code this time.
You are attempting to bind the texture inside of a glBegin/glEnd. You can't do this. Basically, you can only call glColor, glTexCoord, and glVertex inside a begin/end block. Setting the color can be done inside the begin/end which is why it works.

P.S. GL_POLYGON is not the best way to draw. Use GL_TRIANGLES whenever possible.
Title: Re: LWJGL slick texture problem
Post by: Bingo90 on June 18, 2013, 13:03:24
Ah, I see. Thank you, this works well!   ;D