LWJGL Forum

Programming => OpenGL => Topic started by: overlisted on October 25, 2019, 21:45:04

Title: Access violation exception by glTexImage2D
Post by: overlisted on October 25, 2019, 21:45:04
I'm trying to make a font renderer (using java.awt) but getting error uploading texture. The texture constructor for fonts:

public Texture(ByteBuffer image, int width, int height) {
    this.id = glGenTextures();

    glBindTexture(GL_TEXTURE_2D, this.id);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_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, image);
    glGenerateMipmap(GL_TEXTURE_2D);
  }


Font renderer code (method of my Font class):

  public IObject getStringAsObject(String string, Vector3f position) {
    Rectangle2D rect = this.fontAWT.getStringBounds(string, context);

    this.gfx.dispose();

    this.image = new BufferedImage(
      (int) Math.ceil(rect.getWidth()),
      (int) Math.ceil(rect.getHeight()),
      BufferedImage.TYPE_4BYTE_ABGR
    );

    this.gfx = image.getGraphics();

    this.gfx.setColor(this.color);
    this.gfx.setFont(this.fontAWT);
    this.gfx.drawString(string, 0, this.gfx.getFontMetrics().getAscent());
    this.gfx.dispose();

    byte[] pixelData = ((DataBufferByte) this.image.getRaster().getDataBuffer()).getData();
    byte[] pixelDataRGBA = pixelData;

    assert pixelData.length % 4 != 0;

    for(int i = 0; i < pixelData.length; i += 4) {
      pixelDataRGBA[i] = pixelData[i + 3]; // r1 / r4
      pixelDataRGBA[i + 1] = pixelData[i + 2]; // g2 / g3
      pixelDataRGBA[i + 2] = pixelData[i + 1]; // b3 / b2
      pixelDataRGBA[i + 3] = pixelData[i]; // a4 / a1
    }

    System.out.println(Arrays.toString(pixelDataRGBA));
    System.out.println(pixelDataRGBA.length == image.getWidth() * image.getHeight() * 4); // true so opengl doesn't have to break

    SimpleObject object = new SimpleObject(
      position,
      new Texture(ByteBuffer.wrap(pixelDataRGBA), image.getWidth(), image.getHeight()),
      new Vertex[] {
        new Vertex(0, 0, 0, 0, 1), // 1 triangle
        new Vertex(0, (float) rect.getHeight(), 0, 0, 0),
        new Vertex((float) rect.getWidth(), (float) rect.getHeight(), 0, 1, 0),
        new Vertex(0, 0, 0, 0, 1), // 2 triangle
        new Vertex((float) rect.getWidth(), 0, 0, 1, 1),
        new Vertex((float) rect.getWidth(), (float) rect.getHeight(), 0, 1, 0)
      }
    );

    object.model.scale(0.01f);

    return object;
  }

I should flip ABGR to RGBA to get colors that OpenGL can read.
And also I have this part of code: System.out.println(pixelDataRGBA.length == image.getWidth() * image.getHeight() * 4); It prints true.
IObject and SimpleObject are made by me.

Can anyone help me? EXCEPTION_ACCESS_VIOLATION (0xc0000005) at org.lwjgl.opengl.GL11C.nglTexImage2D(IIIIIIIIJ)V+0