BliepMonster
BliepMonster

Reputation: 1

Distorted pixel display in LWJGL textures

I was making a simple game and my pixels appear distorted (image).It's not perfect I don't have any experience with this library, and I followed two tutorials for texture loading and screen setup. And now I have a problem with this. It does work for zoomed in textures, but with small spaces it does not work. Texture drawing method: (below) this is where I think the problem is, but I don't know where. I draw a square with a texture based on aspect ratio and parameters.

public static void image(DisplayScreen screen, Texture texture, float x1, float y1, float width, float height) {
        GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
        texture.bind();
        int screenWidth = screen.getWidth();
        int screenHeight = screen.getHeight();
        glEnable(GL_TEXTURE);
        glEnable(GL_TEXTURE_2D);
        glBegin(GL_QUADS);
        float extendedX = (x1 * screenHeight) / screenWidth + (width * screenHeight) / screenWidth;
        glTexCoord2f(0.0f, .0f);
        GL11.glVertex2f((x1 * screenHeight) / screenWidth, y1+height);
        GL11.glTexCoord2f(1.0f, 0.0f);
        GL11.glVertex2f(extendedX, y1+height);
        GL11.glTexCoord2f(1.0f, 1.0f);
        GL11.glVertex2f(extendedX, y1);
        GL11.glTexCoord2f(0.0f, 1.0f);
        GL11.glVertex2f((x1 * screenHeight) / screenWidth, y1);
        GL11.glEnd();
    }

I got this from a tutorial:

public Texture(String filename) {
        BufferedImage bi;
        try {
            bi = ImageIO.read(new File(filename));
            width = bi.getWidth();
            height = bi.getHeight();

            int[] pixels_raw;
            pixels_raw = bi.getRGB(0, 0, width, height, null, 0, width);
            ByteBuffer pixels = BufferUtils.createByteBuffer(width*height*4);
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    int pixel = pixels_raw[i * height + j];
                    pixels.put((byte) ((pixel >> 16) & 0xFF)); //RED
                    pixels.put((byte) ((pixel >> 8) & 0xFF));  //GREEN
                    pixels.put((byte) (pixel & 0xFF));         //BLUE
                    pixels.put((byte) ((pixel >> 24) & 0xFF)); //ALPHA
                }
            }
            glEnable(GL_BLEND);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            pixels.flip();
            id = glGenTextures();
            bind();
            glOrtho(2, 2, 2, 2, 2, 2);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexParameteri(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(Exception e) {
            e.printStackTrace();
        }

I added this because I thought it would do something: (it didn't do much, and the issue is still there) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_UNPACK_ALIGNMENT, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

Upvotes: 0

Views: 35

Answers (0)

Related Questions