LWJGL - Made my first 3D world! Running into texture problems!

So I wrote my first 3D world in LWJGL (YEAH!), which I am very proud of, and I’ve just started adding textures to my lifeless world. I’ve had to rewrite this about 6 times now as i discover many new things/bugs about my problem. After a long analysis, it seems that my textures are binding themselves after being loaded.

try {
			textureFloor = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("img/floor.png"));
			textureWall = TextureLoader.getTexture("JPG", ResourceLoader.getResourceAsStream("img/tileWall.jpg"));
		} catch (IOException e) {
			e.printStackTrace();
		}

So when the floor is rendered, textureWall is already being drawn onto it! If I comment out textureWall, and load only textureFloor, then the floor texture is drawn onto the floor. This isn’t right at all! Nothing is happening to these texture objects, other than being loaded from the above code.

My Floor’s class is available here

I’ve got another problem aswell. When I add a box (and the textures still aren’t bound), it renders really weird, and makes the floor render weird aswell!

In this picture, we see the floor, and a box. As you can see, they have textures on them, that are extremely off.

Will add code if requested.

Thanks!!

		GL11.glVertex3f(width,0, length);
		GL11.glTexCoord2f(width, length);
		
		GL11.glVertex3f(-width,0, length);
		GL11.glTexCoord2f(-width, length);
		
		GL11.glVertex3f(-width,0,-length);
		GL11.glTexCoord2f(-width, -length);
		
		GL11.glVertex3f(width,0,-length);
		GL11.glTexCoord2f(width, -length);

texcoords are normalized, so you typically use the range 0.0 … 1.0

		GL11.glVertex3f(width,0, length);
		GL11.glTexCoord2f(1.0, 1.0);
		
		GL11.glVertex3f(-width,0, length);
		GL11.glTexCoord2f(0.0, 1.0);
		
		GL11.glVertex3f(-width,0,-length);
		GL11.glTexCoord2f(0.0, 0.0);
		
		GL11.glVertex3f(width,0,-length);
		GL11.glTexCoord2f(1.0, 0.0);

(This is all done without rendering the box)

Before:

After:

I don’t really know what normalized texcoords are, but from context I think it means they rangle from 0-1 (possibly -1 too).
What ever it means, it seems to just stretch the texture out over the whole quad, rather than repeating it. I think the later looks nicer.

Slick converts textures to a power-of-two (POT) size.

Ideally you should convert your assets to a power-of-two size.

Alternatively, you could use Texture.getWidth/getHeight to determine the normalized width/height, e.g.

glTextCoord2f(tex.getWidth(), tex.getHeight());

I like your thinking! Thanks for this.
I think I made it even better!

GL11.glTexCoord2f(World.textureFloor.getWidth()*repeats, World.textureFloor.getHeight()*repeats);

int repeats = 1;

int repeats = 8;

While all this is great, it still doesn’t solve my texture pre-binding themselves!

Call floorTex.bind() before rendering the floor geometry, and wallTex.bind() before rendering the wall geometry. etc.

Textures are right on each quad, but are still skewed!

Can you show any more code? I’d like to run it and have a stab at fixing

Oh please, stab away!

You’ll need LWJGL 2.8.3 (lwjgl.jar and lwjgl_util.jar) and slick-utils.
Thanks!

You have some problems with your code:
a) Don’t call glTranslate within glBegin/glEnd
b) If you translate to centerX/centerY then you don’t need to use centerX/centerY in glVertex! Right now you are translating twice.
c) Call glTexCoord2f before glVertex2f
d) Your texture coords for the box have some bugs. On once face, you forget to multiply the value by “repeats”. On another face, you use the coordinate (0, 0) twice. etc.
c) Avoid duplicate code and learn some basic design patterns. e.g. You might want to create a Quad class/utility so that you have a “single source of responsibility”. Right now you are repeating the same stuff over and over again, which is leading to typos and a more difficult program to debug.
d) Start reading up on OpenGL to learn more.

Cheers, good luck. :slight_smile: