예제 #1
0
    /**
     * Builds a new GLTexture
     *
     * @param format The texture format
     * @param dim The texture size, or null for the default
     * @param mipmap Whether to mipmap or not
     */
    private GLTexture(Format format, ReadableDimension dim, boolean mipmap, int border)
        throws OpenGLException {
      this.format = format;
      size =
          dim != null
              ? new Dimension(
                  GLUtil.nextPowerOf2(dim.getWidth()), GLUtil.nextPowerOf2(dim.getHeight()))
              : new Dimension(textureDimension, textureDimension);
      this.mipmap = mipmap;

      packer = new RectanglePacker<Image>(size.getWidth(), size.getHeight(), border);

      recreate();
    }
예제 #2
0
    private void writeToTexture(RectanglePacker.Rectangle r, ByteBuffer data) {
      assert r.width * r.height * format.bytes == data.capacity()
          : r + " * " + format.bytes + " != " + data.capacity();

      GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);

      GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, format.bytes);

      GL11.glTexSubImage2D(
          GL11.GL_TEXTURE_2D,
          0,
          r.x,
          r.y,
          r.width,
          r.height,
          format.glFormat,
          GL11.GL_UNSIGNED_BYTE,
          data);

      GLUtil.checkGLError();

      if (mipmap) {
        mipmapDirty = true;
      }
    }
예제 #3
0
    private void recreate() {
      id = GL11.glGenTextures();

      GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);

      GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, format.bytes);

      ByteBuffer data =
          BufferUtils.createByteBuffer(size.getWidth() * size.getHeight() * format.bytes);

      if (mipmap) {
        GLU.gluBuild2DMipmaps(
            GL11.GL_TEXTURE_2D,
            format.glInternalFormat,
            size.getWidth(),
            size.getHeight(),
            format.glFormat,
            GL11.GL_UNSIGNED_BYTE,
            data);
      } else {
        GL11.glTexImage2D(
            GL11.GL_TEXTURE_2D,
            0,
            format.glInternalFormat,
            size.getWidth(),
            size.getHeight(),
            0,
            format.glFormat,
            GL11.GL_UNSIGNED_BYTE,
            data);
      }
      GLUtil.checkGLError();

      for (Texture t : residentTextures) {
        RectanglePacker.Rectangle rpr = packer.findRectangle(t.getSourceImage());

        if (rpr != null) {
          writeToTexture(rpr, t.getSourceImage().getData());
        }
      }

      regenerateMipmaps();
    }