2
\$\begingroup\$

(Sorry if this has already been asked, I couldn't find any other question or source describing my problem)

Okay so basically Square textures load and display just fine. Picture of a Square tile with a Square texture

But for some reason, when trying to load in textures that aren't square (and/or resizing the model so the texture fits) isn't working right.

Picture of a Square tile with a 24x48 texture Picture of a Rectangle tile with a 24x48 texture

Here's the Player-bit:

float[] vertices = new float[] {
    -1, 1.5f, 0,  // Top Left     0
    1, 1.5f, 0,   // Top Right    1
    1, -1.5f, 0,  // Bottom Right 2
    -1, -1.5f, 0, // Bottom Left  3
};

float[] texture = new float[] {
    0,0,
    1,0,
    1,1.5f,
    0,1.5f,
};

int[] indices = new int[] {
    0,1,2,
    2,3,0
};

model = new Model(vertices, texture, indices);
this.texture = new Texture("animation/player/idle.png");

transform = new Transform();
transform.scale = new Vector3f(16,16,1);

and here my Texture Class code:

public Texture(String filename) {
    BufferedImage bi;
    try {
        URL path = getClass().getResource("/textures/" + filename);
        if(path == null) {
            throw new IllegalStateException("Texture not found");
        }

        URI file = path.toURI();
        bi = ImageIO.read(new File(file));
        width = bi.getWidth();
        height = bi.getHeight();
        System.out.println(filename + ": " + (int) width + "x" + (int) height);

        int[] rawPixels = new int[width * height * 4];
        rawPixels = bi.getRGB(0,0, width, height, null, 0, width);

        ByteBuffer pixels = BufferUtils.createByteBuffer(width*height*4);

        for(int x = 0; x < width; x++) {
            for(int y = 0; y < height; y++) {
                int pixel = rawPixels[x*width+y];
                // Red, Green, Blue, Alpha
                pixels.put((byte) ((pixel >> 16) & 0xFF));
                pixels.put((byte) ((pixel >> 8) & 0xFF));
                pixels.put((byte) (pixel & 0xFF));
                pixels.put((byte) ((pixel >> 24) & 0xFF));
            }
        }

        pixels.flip();
        id = glGenTextures();
        glBindTexture(GL_TEXTURE_2D,id);

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    } catch(IOException | URISyntaxException e) {
        e.printStackTrace();
    }
}

public void bind(int sampler) {
    if(sampler >= 0 && sampler <= 31) {
        glActiveTexture(GL_TEXTURE0 + sampler);
        glBindTexture(GL_TEXTURE_2D,id);
    }
}

Notes:

  • I am not using Slick or something like that, it's just plain LWJGL.
  • I don't know what code I should put in here, just ask me if you need more
\$\endgroup\$
7
  • \$\begingroup\$ Presumably you've tried with the simplest non square texture you can think of and stepped through your code? What does your debugger tell you? \$\endgroup\$
    – user35344
    Commented Nov 9, 2021 at 15:02
  • \$\begingroup\$ The Debugger doesn't tell me anything really worth mentioning, this is all I get \$\endgroup\$
    – DindinYT37
    Commented Nov 9, 2021 at 15:06
  • \$\begingroup\$ That's not the debugger. Have you checked that the variables contain the values you expect at all states? If not, at what point are they different from what you'd expect from the simple texture? \$\endgroup\$
    – user35344
    Commented Nov 9, 2021 at 15:07
  • \$\begingroup\$ I don't know about the debugger since that was the only kind of debugger I had. About the variables, they're all as they should be. \$\endgroup\$
    – DindinYT37
    Commented Nov 9, 2021 at 15:11
  • 1
    \$\begingroup\$ I don't know what's wrong, that's why I'm here. \$\endgroup\$
    – DindinYT37
    Commented Nov 9, 2021 at 15:12

1 Answer 1

4
\$\begingroup\$

It looks like your pixel lookup is backwards:

int pixel = rawPixels[x*width+y];

Should be

int pixel = rawPixels[x+width*y];
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .