1 Replies - 437 Views - Last Post: 01 March 2013 - 11:27 AM Rate Topic: -----

#1 RedChu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 12-December 08

OpenGL/LWJGL Texturing Mapping

Posted 25 February 2013 - 10:43 PM

I've recently started using LWJGL and have been in the process of writing my own 2D graphics rendering engine with it. So far, I've been able to render textured quads using a single image for each texture, but now I want to get it working with multiple textures on a single image. This is what I'm having problems with.

Currently, the quads are an isometric shape (64x32), as are the textures I'm trying to use. The image I'm using is 512x512 and holds 8 tile textures on a single row with the capability of holding 16 rows, so I've tried changing the texture coordinates to fractions but they just do not show the full texture (or any texture at all).

public void draw(int x, int y, int tex_x, int tex_y) {
    GL11.glBegin(GL11.GL_QUADS);
        GL11.glTexCoord2f(0, 31/32); //GL11.glTexCoord2f(0, 0.5f);
        GL11.glVertex2f(x, y);

        GL11.glTexCoord2f(1/16, 1); //GL11.glTexCoord2f(0.5f, 1);
        GL11.glVertex2f(x+32, y+16);

        GL11.glTexCoord2f(1/8, 31/32); //GL11.glTexCoord2f(1, 0.5f);
        GL11.glVertex2f(x+64, y);

        GL11.glTexCoord2f(1/16, 15/16); //GL11.glTexCoord2f(0.5f, 0);
        GL11.glVertex2f(x+32, y-16);
    GL11.glEnd();
}

The code above is what I'm currently using, though I've tried a bunch of other ways (the commented coordinates work when using a single image). It doesn't even show the textures on the screen. The code is attempting to get the first tile on the top row of the image.

Other coordinates I've tried have gotten only a very tiny amount of the texture onto the quad.

This is the image I'm using:
Posted Image

I'm fairly new to OpenGL and texturing has been the biggest pain for me so far, so any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: OpenGL/LWJGL Texturing Mapping

#2 RedChu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 12-December 08

Re: OpenGL/LWJGL Texturing Mapping

Posted 01 March 2013 - 11:27 AM

I have figured out what I was doing wrong... kind of. The top answer of this question solved my problem. Apparently there's more to it than simply getting the coordinates by doing something like 32.0f/512.0f.

Anyway, this is my implementation of the code prior to me beginning a rewrite of the engine:
public void draw(int x, int y, int spriteIndex) {
        int u = spriteIndex % 8;
        int v = spriteIndex / 16;
        
        float xOffset = 1f / 8;
        float yOffset = 1f / 16;

        float uOffset = (u * xOffset);
        float vOffset = (v * yOffset);
        
        GL11.glBegin(GL11.GL_QUADS);
            GL11.glTexCoord2f(uOffset, vOffset+(yOffset/2));//GL11.glTexCoord2f(0, 0.5f);
            GL11.glVertex2f(x, y);

            GL11.glTexCoord2f(uOffset+(xOffset/2), vOffset);//GL11.glTexCoord2f(0.5f, 1);
            GL11.glVertex2f(x+32, y+16);

            GL11.glTexCoord2f(uOffset+xOffset, vOffset+(yOffset/2));//GL11.glTexCoord2f(1, 0.5f);
            GL11.glVertex2f(x+64, y);

            GL11.glTexCoord2f(uOffset+(xOffset/2), vOffset+yOffset);//GL11.glTexCoord2f(0.5f, 0);
            GL11.glVertex2f(x+32, y-16);
        GL11.glEnd();
    }

I just shoved it in there to make sure it worked, and it did.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1