0

I am creating a 2D game using Java and LWJGL. In the game I have a Quad that represents the player. I have an Image specified as a BufferedImage Currently I am using this code (below) to get and show the image but it doesnt seem to be working. location is the variable I have that specifies the relative location to what is specified (just ignore it) win is the long field that represents the window in GLFW

public void render() {
        glfwMakeContextCurrent(win);

        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, loadTexture());
        glBegin(GL_QUADS);
        glVertex2f(-0.5f + ]location, 0.5f);
        glVertex2f(0.5f + location, 0.5f);
        glVertex2f(0.5f + location, -0.5f);
        glVertex2f(-0.5f + location, -0.5f);
        glEnd();
        glDisable(GL_TEXTURE_2D);
    }

and

public int loadTexture() {
        glfwMakeContextCurrent(win);

        int[] pixels = null;
        int width = 0;
        int height = 0;
        try {
            InputStream resourceBuff = getResourceAsStream(texture);
            BufferedImage image = ImageIO.read(resourceBuff);
            width = image.getWidth();
            height = image.getHeight();
            pixels = new int[width * height];
            image.getRGB(0, 0, width, height, pixels, 0, width);
        } catch (IOException e) {
            Dreadmare.err.createCrashReport(e);
        }
        int[] data = new int[width * height];
        for (int i = 0; i < width * height; i++) {
            int a = pixels[i] >> 24 & 255;
            int r = pixels[i] >> 16 & 255;
            int g = pixels[i] >> 8 & 255;
            int b = pixels[i] & 255;

            data[i] = a << 24 | b << 16 | g << 8 | r;
        }

        ByteBuffer dataBuff = BufferUtils.createByteBuffer((width * height) + width);

        int result = glGenTextures();
        glBindTexture(GL_TEXTURE_2D, result);
        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,
                data);
        glBindTexture(GL_TEXTURE_2D, 0);
        return result;
    }

When I run the code, I just get a black rectangle

Pluto7073
  • 39
  • 3
  • You are encoding your data into ABGR format but when creating your texture you are specifying RGBA format. Also after you create your dataBuf object your are not calling any put() method to load your data into it. Make sure you call flip() after you do put(data). You shouldn't pass your int array directly to the texImage2d() since you have specified unigned_byte as your data type but instead pass the dataBuf object. The size of your dataBuf object must be 4*width*height – Sync it Jan 31 '21 at 05:28
  • Also you haven't specified [tex coords](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTexCoord.xml) for your quad – Sync it Jan 31 '21 at 05:32
  • You have to specify the texture coordinates with [`glTexCoord2f`](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTexCoord.xml). See [How do opengl texture coordinates work?](https://stackoverflow.com/questions/5532595/how-do-opengl-texture-coordinates-work) – Rabbid76 Jan 31 '21 at 07:42

0 Answers0