public void setTexture(int unit, Texture tex) {
    if (unit != 0 || tex.getType() != Texture.Type.TwoDimensional) {
      // throw new UnsupportedOperationException();
      return;
    }

    Image image = tex.getImage();
    if (image.isUpdateNeeded()
        || (image.isGeneratedMipmapsRequired() && !image.isMipmapsGenerated())) {
      updateTexImageData(image, tex.getType(), unit);
    }

    int texId = image.getId();
    assert texId != -1;

    Image[] textures = context.boundTextures;

    int type = convertTextureType(tex.getType());
    //        if (!context.textureIndexList.moveToNew(unit)) {
    //             if (context.boundTextureUnit != unit){
    //                gl.glActiveTexture(GL.GL_TEXTURE0 + unit);
    //                context.boundTextureUnit = unit;
    //             }
    //             gl.glEnable(type);
    //        }

    //        if (context.boundTextureUnit != unit) {
    //            gl.glActiveTexture(GL.GL_TEXTURE0 + unit);
    //            context.boundTextureUnit = unit;
    //        }

    if (textures[unit] != image) {
      GL gl = GLContext.getCurrentGL();
      gl.glEnable(type);
      gl.glBindTexture(type, texId);
      textures[unit] = image;

      statistics.onTextureUse(image, true);
    } else {
      statistics.onTextureUse(image, false);
    }

    setupTextureParams(tex);
  }
  public void renderMesh(Mesh mesh, int lod, int count) {
    if (mesh.getVertexCount() == 0) {
      return;
    }

    if (context.pointSize != mesh.getPointSize()) {
      glPointSize(mesh.getPointSize());
      context.pointSize = mesh.getPointSize();
    }
    if (context.lineWidth != mesh.getLineWidth()) {
      glLineWidth(mesh.getLineWidth());
      context.lineWidth = mesh.getLineWidth();
    }

    boolean dynamic = false;
    if (mesh.getBuffer(Type.InterleavedData) != null) {
      throw new UnsupportedOperationException("Interleaved meshes are not supported");
    }

    if (mesh.getNumLodLevels() == 0) {
      for (VertexBuffer vb : mesh.getBufferList().getArray()) {
        if (vb.getUsage() != VertexBuffer.Usage.Static) {
          dynamic = true;
          break;
        }
      }
    } else {
      dynamic = true;
    }

    statistics.onMeshDrawn(mesh, lod);

    //        if (!dynamic) {
    // dealing with a static object, generate display list
    //            renderMeshDisplayList(mesh);
    //        } else {
    renderMeshDefault(mesh, lod, count);
    //        }

  }
  public void updateTexImageData(Image img, Texture.Type type, int unit) {
    int texId = img.getId();
    if (texId == -1) {
      // create texture
      glGenTextures(ib1);
      texId = ib1.get(0);
      img.setId(texId);
      objManager.registerObject(img);

      statistics.onNewTexture();
    }

    // bind texture
    int target = convertTextureType(type);
    //        if (context.boundTextureUnit != unit) {
    //            glActiveTexture(GL_TEXTURE0 + unit);
    //            context.boundTextureUnit = unit;
    //        }
    if (context.boundTextures[unit] != img) {
      glEnable(target);
      glBindTexture(target, texId);
      context.boundTextures[unit] = img;

      statistics.onTextureUse(img, true);
    }

    // Check sizes if graphics card doesn't support NPOT
    if (!GLContext.getCapabilities().GL_ARB_texture_non_power_of_two) {
      if (img.getWidth() != 0 && img.getHeight() != 0) {
        if (!FastMath.isPowerOfTwo(img.getWidth()) || !FastMath.isPowerOfTwo(img.getHeight())) {

          // Resize texture to Power-of-2 size
          MipMapGenerator.resizeToPowerOf2(img);
        }
      }
    }

    if (!img.hasMipmaps() && img.isGeneratedMipmapsRequired()) {
      // No pregenerated mips available,
      // generate from base level if required

      // Check if hardware mips are supported
      if (GLContext.getCapabilities().OpenGL14) {
        glTexParameteri(target, GL14.GL_GENERATE_MIPMAP, GL_TRUE);
      } else {
        MipMapGenerator.generateMipMaps(img);
      }
      img.setMipmapsGenerated(true);
    } else {
    }

    if (img.getWidth() > maxTexSize || img.getHeight() > maxTexSize) {
      throw new RendererException(
          "Cannot upload texture "
              + img
              + ". The maximum supported texture resolution is "
              + maxTexSize);
    }

    /*
    if (target == GL_TEXTURE_CUBE_MAP) {
    List<ByteBuffer> data = img.getData();
    if (data.size() != 6) {
    logger.log(Level.WARNING, "Invalid texture: {0}\n"
    + "Cubemap textures must contain 6 data units.", img);
    return;
    }
    for (int i = 0; i < 6; i++) {
    TextureUtil.uploadTexture(img, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, i, 0, tdc);
    }
    } else if (target == EXTTextureArray.GL_TEXTURE_2D_ARRAY_EXT) {
    List<ByteBuffer> data = img.getData();
    // -1 index specifies prepare data for 2D Array
    TextureUtil.uploadTexture(img, target, -1, 0, tdc);
    for (int i = 0; i < data.size(); i++) {
    // upload each slice of 2D array in turn
    // this time with the appropriate index
    TextureUtil.uploadTexture(img, target, i, 0, tdc);
    }
    } else {*/
    TextureUtil.uploadTexture(img, target, 0, 0);
    // }

    img.clearUpdateNeeded();
  }
 public void cleanup() {
   logger.log(Level.FINE, "Deleting objects and invalidating state");
   objManager.deleteAllObjects(this);
   statistics.clearMemory();
   invalidateState();
 }
 public void resetGLObjects() {
   logger.log(Level.FINE, "Reseting objects and invalidating state");
   objManager.resetObjects();
   statistics.clearMemory();
   invalidateState();
 }