<commit_before>/*
 * Copyright (c) 2003-2004, jMonkeyEngine - Mojo Monkey Coding All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the Mojo Monkey Coding, jME, jMonkey Engine, nor the
 * names of its contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */
package com.jme.scene.state.lwjgl;

import java.nio.IntBuffer;
import java.util.Stack;
import java.util.logging.Level;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.opengl.glu.GLU;

import com.jme.image.Image;
import com.jme.image.Texture;
import com.jme.scene.Spatial;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.util.LoggingSystem;
import org.lwjgl.opengl.EXTTextureFilterAnisotropic;
import java.nio.FloatBuffer;
import java.io.IOException;

/**
 * <code>LWJGLTextureState</code> subclasses the TextureState object using the
 * LWJGL API to access OpenGL for texture processing.
 *
 * @author <NAME>
 * @version $Id: LWJGLTextureState.java,v 1.29 2004-07-10 19:51:55 renanse Exp $
 */
public class LWJGLTextureState extends TextureState {

  private static Texture[] currentTexture;

  //OpenGL texture attributes.
  private int[] textureCorrection = {GL11.GL_FASTEST, GL11.GL_NICEST};

  private int[] textureApply = {GL11.GL_REPLACE, GL11.GL_DECAL,
      GL11.GL_MODULATE, GL11.GL_BLEND, GL13.GL_COMBINE};

  private int[] textureFilter = {GL11.GL_NEAREST, GL11.GL_LINEAR};

  private int[] textureMipmap = {GL11.GL_NEAREST, // MM_NONE (no mipmap)
      GL11.GL_NEAREST, GL11.GL_LINEAR, GL11.GL_NEAREST_MIPMAP_NEAREST,
      GL11.GL_NEAREST_MIPMAP_LINEAR, GL11.GL_LINEAR_MIPMAP_NEAREST,
      GL11.GL_LINEAR_MIPMAP_LINEAR};

  private int[] textureCombineFunc = {GL11.GL_REPLACE, GL11.GL_MODULATE,
      GL11.GL_ADD, GL13.GL_ADD_SIGNED, GL13.GL_SUBTRACT,
      GL13.GL_INTERPOLATE, GL13.GL_DOT3_RGB,
      GL13.GL_DOT3_RGBA};

  private int[] textureCombineSrc = {GL11.GL_TEXTURE, GL13.GL_PRIMARY_COLOR,
      GL13.GL_CONSTANT, GL13.GL_PREVIOUS};

  private int[] textureCombineOpRgb = {GL11.GL_SRC_COLOR,
      GL11.GL_ONE_MINUS_SRC_COLOR, GL11.GL_SRC_ALPHA,
      GL11.GL_ONE_MINUS_SRC_ALPHA};

// first two entries are mostly dummy (except for def = 0) since our alpha's
// only use 3 and 4
  private int[] textureCombineOpAlpha = {GL11.GL_SRC_ALPHA,
      GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_SRC_ALPHA,
      GL11.GL_ONE_MINUS_SRC_ALPHA};

  private int[] imageComponents = {GL11.GL_RGBA4, GL11.GL_RGB8,
      GL11.GL_RGB5_A1, GL11.GL_RGBA8, GL11.GL_LUMINANCE8_ALPHA8};

  private int[] imageFormats = {GL11.GL_RGBA, GL11.GL_RGB, GL11.GL_RGBA,
      GL11.GL_RGBA, GL11.GL_LUMINANCE_ALPHA};

  private transient IntBuffer id = BufferUtils.createIntBuffer(1);

  /**
   * Constructor instantiates a new <code>LWJGLTextureState</code> object.
   * The number of textures that can be combined is determined during
   * construction. This equates the number of texture units supported by the
   * graphics card.
   *
   */
  public LWJGLTextureState() {
    super();
    supportsMultiTexture = (GLContext.GL_ARB_multitexture && GLContext.OpenGL13);
    if (numTexUnits == 0) {
      if (supportsMultiTexture) {
        IntBuffer buf = BufferUtils.createIntBuffer(16); //ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()).asIntBuffer();
        GL11.glGetInteger(GL13.GL_MAX_TEXTURE_UNITS, buf);
        numTexUnits = buf.get(0);
      } else {
        numTexUnits = 1;
      }
    }

    if (currentTexture == null)
      currentTexture = new Texture[numTexUnits];

    if (maxAnisotropic == -1.0) {
      // Due to LWJGL buffer check, you can't use smaller sized buffers (min_size = 16 for glGetFloat()).
      FloatBuffer max_a = BufferUtils.createFloatBuffer(16);
      max_a.rewind();

      // Grab the maximum anisotropic filter.
      GL11.glGetFloat(EXTTextureFilterAnisotropic.
                      GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT,
                      max_a);

      // set max.
      maxAnisotropic = max_a.get(0);
    }
    texture = new Texture[numTexUnits];
  }

  /**
   * <code>apply</code> manages the textures being described by the state. If
   * the texture has not been loaded yet, it is generated and loaded using
   * OpenGL11. This means the initial pass to set will be longer than
   * subsequent calls. The multitexture extension is used to define the
   * multiple texture states, with the number of units being determined at
   * construction time.
   *
   * @see com.jme.scene.state.RenderState#apply()
   */
  public void apply() {

    if (isEnabled()) {
      int index;
      Texture texture;
      for (int i = 0; i < numTexUnits; i++) {
        index = GL13.GL_TEXTURE0 + i;
        texture = getTexture(i);
        if (texture == currentTexture[i]) continue;
        currentTexture[i] = texture;

        if (supportsMultiTexture && i != 0) {
          GL13.glActiveTexture(index);
        }

        if (texture == null) {
          GL11.glDisable(GL11.GL_TEXTURE_2D);<commit_msg>bug, had i!=0 still in there.<commit_after>/*
 * Copyright (c) 2003-2004, jMonkeyEngine - Mojo Monkey Coding All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the Mojo Monkey Coding, jME, jMonkey Engine, nor the
 * names of its contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */
package com.jme.scene.state.lwjgl;

import java.nio.IntBuffer;
import java.util.Stack;
import java.util.logging.Level;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.opengl.glu.GLU;

import com.jme.image.Image;
import com.jme.image.Texture;
import com.jme.scene.Spatial;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.util.LoggingSystem;
import org.lwjgl.opengl.EXTTextureFilterAnisotropic;
import java.nio.FloatBuffer;
import java.io.IOException;

/**
 * <code>LWJGLTextureState</code> subclasses the TextureState object using the
 * LWJGL API to access OpenGL for texture processing.
 *
 * @author <NAME>
 * @version $Id: LWJGLTextureState.java,v 1.30 2004-07-12 15:10:30 renanse Exp $
 */
public class LWJGLTextureState extends TextureState {

  private static Texture[] currentTexture;

  //OpenGL texture attributes.
  private int[] textureCorrection = {GL11.GL_FASTEST, GL11.GL_NICEST};

  private int[] textureApply = {GL11.GL_REPLACE, GL11.GL_DECAL,
      GL11.GL_MODULATE, GL11.GL_BLEND, GL13.GL_COMBINE};

  private int[] textureFilter = {GL11.GL_NEAREST, GL11.GL_LINEAR};

  private int[] textureMipmap = {GL11.GL_NEAREST, // MM_NONE (no mipmap)
      GL11.GL_NEAREST, GL11.GL_LINEAR, GL11.GL_NEAREST_MIPMAP_NEAREST,
      GL11.GL_NEAREST_MIPMAP_LINEAR, GL11.GL_LINEAR_MIPMAP_NEAREST,
      GL11.GL_LINEAR_MIPMAP_LINEAR};

  private int[] textureCombineFunc = {GL11.GL_REPLACE, GL11.GL_MODULATE,
      GL11.GL_ADD, GL13.GL_ADD_SIGNED, GL13.GL_SUBTRACT,
      GL13.GL_INTERPOLATE, GL13.GL_DOT3_RGB,
      GL13.GL_DOT3_RGBA};

  private int[] textureCombineSrc = {GL11.GL_TEXTURE, GL13.GL_PRIMARY_COLOR,
      GL13.GL_CONSTANT, GL13.GL_PREVIOUS};

  private int[] textureCombineOpRgb = {GL11.GL_SRC_COLOR,
      GL11.GL_ONE_MINUS_SRC_COLOR, GL11.GL_SRC_ALPHA,
      GL11.GL_ONE_MINUS_SRC_ALPHA};

// first two entries are mostly dummy (except for def = 0) since our alpha's
// only use 3 and 4
  private int[] textureCombineOpAlpha = {GL11.GL_SRC_ALPHA,
      GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_SRC_ALPHA,
      GL11.GL_ONE_MINUS_SRC_ALPHA};

  private int[] imageComponents = {GL11.GL_RGBA4, GL11.GL_RGB8,
      GL11.GL_RGB5_A1, GL11.GL_RGBA8, GL11.GL_LUMINANCE8_ALPHA8};

  private int[] imageFormats = {GL11.GL_RGBA, GL11.GL_RGB, GL11.GL_RGBA,
      GL11.GL_RGBA, GL11.GL_LUMINANCE_ALPHA};

  private transient IntBuffer id = BufferUtils.createIntBuffer(1);

  /**
   * Constructor instantiates a new <code>LWJGLTextureState</code> object.
   * The number of textures that can be combined is determined during
   * construction. This equates the number of texture units supported by the
   * graphics card.
   *
   */
  public LWJGLTextureState() {
    super();
    supportsMultiTexture = (GLContext.GL_ARB_multitexture && GLContext.OpenGL13);
    if (numTexUnits == 0) {
      if (supportsMultiTexture) {
        IntBuffer buf = BufferUtils.createIntBuffer(16); //ByteBuffer.allocateDirect(64).order(ByteOrder.nativeOrder()).asIntBuffer();
        GL11.glGetInteger(GL13.GL_MAX_TEXTURE_UNITS, buf);
        numTexUnits = buf.get(0);
      } else {
        numTexUnits = 1;
      }
    }

    if (currentTexture == null)
      currentTexture = new Texture[numTexUnits];

    if (maxAnisotropic == -1.0) {
      // Due to LWJGL buffer check, you can't use smaller sized buffers (min_size = 16 for glGetFloat()).
      FloatBuffer max_a = BufferUtils.createFloatBuffer(16);
      max_a.rewind();

      // Grab the maximum anisotropic filter.
      GL11.glGetFloat(EXTTextureFilterAnisotropic.
                      GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT,
                      max_a);

      // set max.
      maxAnisotropic = max_a.get(0);
    }
    texture = new Texture[numTexUnits];
  }

  /**
   * <code>apply</code> manages the textures being described by the state. If
   * the texture has not been loaded yet, it is generated and loaded using
   * OpenGL11. This means the initial pass to set will be longer than
   * subsequent calls. The multitexture extension is used to define the
   * multiple texture states, with the number of units being determined at
   * construction time.
   *
   * @see com.jme.scene.state.RenderState#apply()
   */
  public void apply() {

    if (isEnabled()) {
      int index;
      Texture texture;
      for (int i = 0; i < numTexUnits; i++) {
        texture = getTexture(i);
        if (texture == currentTexture[i]) continue;
        currentTexture[i] = texture;

        index = GL13.GL_TEXTURE0 + i;

        if (supportsMultiTexture) {
          GL13.glActiveTexture(index);
        }

        if (texture == null) {
          GL11.glDisable(GL11.GL_TEXTURE_2D);
{'fraction_non_alphanumeric': 0.08950593345226092, 'fraction_numerical': 0.026681144807259752, 'mean_word_length': 3.6414686825053995, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '410928', 'n_tokens_mistral': 4513, 'n_tokens_neox': 4134, 'n_words': 1194}