Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Riven (posted 2014-07-14 11:51:35, viewed 39 times)

1   2   3   4   5   6   7   8   9   10   11   12  
#version 330

smooth out vec2 texCoord;
flat out int  texSlice;
flat out int  texUnit;

void main(){
    gl_Position = gl_ModelViewProjectionMatrix * vec4(gl_Vertex.xy, 0, 1);
    texCoord = gl_MultiTexCoord0.xy;
    texSlice = int(gl_MultiTexCoord0.z);
    texUnit  = int(gl_MultiTexCoord0.w);
}


1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192  
#version 330

smooth in vec2 texCoord;
flat in int  texSlice;
flat in int  texUnit;

uniform sampler2DArray samplerSet[32];

float[] muls = float[256](
   1,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
   0,1,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
   0,0,1,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
   0,0,0,1, 0,0,0,0, 0,0,0,0, 0,0,0,0,
   
   0,0,0,0, 1,0,0,0, 0,0,0,0, 0,0,0,0,
   0,0,0,0, 0,1,0,0, 0,0,0,0, 0,0,0,0,
   0,0,0,0, 0,0,1,0, 0,0,0,0, 0,0,0,0,
   0,0,0,0, 0,0,0,1, 0,0,0,0, 0,0,0,0,
   
   0,0,0,0, 0,0,0,0, 1,0,0,0, 0,0,0,0,
   0,0,0,0, 0,0,0,0, 0,1,0,0, 0,0,0,0,
   0,0,0,0, 0,0,0,0, 0,0,1,0, 0,0,0,0,
   0,0,0,0, 0,0,0,0, 0,0,0,1, 0,0,0,0,
   
   0,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,0,0,
   0,0,0,0, 0,0,0,0, 0,0,0,0, 0,1,0,0,
   0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,1,0,
   0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,1
);

vec4 fetch0(vec3 uvs, int texUnit) {
   return texture(samplerSet[texUnit], uvs);
}

vec4 fetch1(vec3 uvs, int texUnit) {
    if(texUnit == 0x0) return texture(samplerSet[0x0], uvs);
    if(texUnit == 0x1) return texture(samplerSet[0x1], uvs);
    if(texUnit == 0x2) return texture(samplerSet[0x2], uvs);
    if(texUnit == 0x3) return texture(samplerSet[0x3], uvs);
    if(texUnit == 0x4) return texture(samplerSet[0x4], uvs);
    if(texUnit == 0x5) return texture(samplerSet[0x5], uvs);
    if(texUnit == 0x6) return texture(samplerSet[0x6], uvs);
    if(texUnit == 0x7) return texture(samplerSet[0x7], uvs);
    if(texUnit == 0x8) return texture(samplerSet[0x8], uvs);
    if(texUnit == 0x9) return texture(samplerSet[0x9], uvs);
    if(texUnit == 0xA) return texture(samplerSet[0xA], uvs);
    if(texUnit == 0xB) return texture(samplerSet[0xB], uvs);
    if(texUnit == 0xC) return texture(samplerSet[0xC], uvs);
    if(texUnit == 0xD) return texture(samplerSet[0xD], uvs);
    if(texUnit == 0xE) return texture(samplerSet[0xE], uvs);
    if(texUnit == 0xF) return texture(samplerSet[0xF], uvs);
                       return vec4(1.0, 1.0, 1.0, 1.0);
}

vec4 fetch2(vec3 uvs, int texUnit) {
   vec4 texel;   
        if(texUnit == 0x0) texel = texture(samplerSet[0x0], uvs);
   else if(texUnit == 0x1) texel = texture(samplerSet[0x1], uvs);
   else if(texUnit == 0x2) texel = texture(samplerSet[0x2], uvs);
   else if(texUnit == 0x3) texel = texture(samplerSet[0x3], uvs);
   else if(texUnit == 0x4) texel = texture(samplerSet[0x4], uvs);
   else if(texUnit == 0x5) texel = texture(samplerSet[0x5], uvs);
   else if(texUnit == 0x6) texel = texture(samplerSet[0x6], uvs);
   else if(texUnit == 0x7) texel = texture(samplerSet[0x7], uvs);
   else if(texUnit == 0x8) texel = texture(samplerSet[0x8], uvs);
   else if(texUnit == 0x9) texel = texture(samplerSet[0x9], uvs);
   else if(texUnit == 0xA) texel = texture(samplerSet[0xA], uvs);
   else if(texUnit == 0xB) texel = texture(samplerSet[0xB], uvs);
   else if(texUnit == 0xC) texel = texture(samplerSet[0xC], uvs);
   else if(texUnit == 0xD) texel = texture(samplerSet[0xD], uvs);
   else if(texUnit == 0xE) texel = texture(samplerSet[0xE], uvs);
   else if(texUnit == 0xF) texel = texture(samplerSet[0xF], uvs);
   else                    texel = vec4(1.0, 1.0, 1.0, 1.0);
   return texel;
}

vec4 fetch3(vec3 uvs, int texUnit) {
   
   vec4 texel;
   
   switch(texUnit) {
      case 0x0: texel = texture(samplerSet[0x0], uvs); break;
      case 0x1: texel = texture(samplerSet[0x1], uvs); break;
      case 0x2: texel = texture(samplerSet[0x2], uvs); break;
      case 0x3: texel = texture(samplerSet[0x3], uvs); break;
      case 0x4: texel = texture(samplerSet[0x4], uvs); break;
      case 0x5: texel = texture(samplerSet[0x5], uvs); break;
      case 0x6: texel = texture(samplerSet[0x6], uvs); break;
      case 0x7: texel = texture(samplerSet[0x7], uvs); break;
      case 0x8: texel = texture(samplerSet[0x8], uvs); break;
      case 0x9: texel = texture(samplerSet[0x9], uvs); break;
      case 0xA: texel = texture(samplerSet[0xA], uvs); break;
      case 0xB: texel = texture(samplerSet[0xB], uvs); break;
      case 0xC: texel = texture(samplerSet[0xC], uvs); break;
      case 0xD: texel = texture(samplerSet[0xD], uvs); break;
      case 0xE: texel = texture(samplerSet[0xE], uvs); break;
      case 0xF: texel = texture(samplerSet[0xF], uvs); break;
      default:  texel = vec4(1.0, 1.0, 1.0, 1.0);
   }
      
   return texel;
}

vec4 fetch4(vec3 uvs, int texUnit) {
   
   vec4 texel = vec4(0.0, 0.0, 0.0, 1.0);
   
   texel += texture(samplerSet[0x0], uvs) * (texUnit==0x0 ? 1.0 : 0.0);
   texel += texture(samplerSet[0x1], uvs) * (texUnit==0x1 ? 1.0 : 0.0);
   texel += texture(samplerSet[0x2], uvs) * (texUnit==0x2 ? 1.0 : 0.0);
   texel += texture(samplerSet[0x3], uvs) * (texUnit==0x3 ? 1.0 : 0.0);
   texel += texture(samplerSet[0x4], uvs) * (texUnit==0x4 ? 1.0 : 0.0);
   texel += texture(samplerSet[0x5], uvs) * (texUnit==0x5 ? 1.0 : 0.0);
   texel += texture(samplerSet[0x6], uvs) * (texUnit==0x6 ? 1.0 : 0.0);
   texel += texture(samplerSet[0x7], uvs) * (texUnit==0x7 ? 1.0 : 0.0);
   texel += texture(samplerSet[0x8], uvs) * (texUnit==0x8 ? 1.0 : 0.0);
   texel += texture(samplerSet[0x9], uvs) * (texUnit==0x9 ? 1.0 : 0.0);
   texel += texture(samplerSet[0xA], uvs) * (texUnit==0xA ? 1.0 : 0.0);
   texel += texture(samplerSet[0xB], uvs) * (texUnit==0xB ? 1.0 : 0.0);
   texel += texture(samplerSet[0xC], uvs) * (texUnit==0xC ? 1.0 : 0.0);
   texel += texture(samplerSet[0xD], uvs) * (texUnit==0xD ? 1.0 : 0.0);
   texel += texture(samplerSet[0xE], uvs) * (texUnit==0xE ? 1.0 : 0.0);
   texel += texture(samplerSet[0xF], uvs) * (texUnit==0xF ? 1.0 : 0.0);

   return texel;
}

vec4 fetch5(vec3 uvs, int texUnit) {    
    float fTexUnit1 = float(texUnit);
    float fTexUnit2 = float(-texUnit);
   
   vec4 texel = vec4(0.0, 0.0, 0.0, 1.0);
   
   texel += texture(samplerSet[0x0], uvs) * step(0.0, fTexUnit1) * step(-0.0, fTexUnit2);
   texel += texture(samplerSet[0x1], uvs) * step(1.0, fTexUnit1) * step(-1.0, fTexUnit2);
   texel += texture(samplerSet[0x2], uvs) * step(2.0, fTexUnit1) * step(-2.0, fTexUnit2);
   texel += texture(samplerSet[0x3], uvs) * step(3.0, fTexUnit1) * step(-3.0, fTexUnit2);
   texel += texture(samplerSet[0x4], uvs) * step(4.0, fTexUnit1) * step(-4.0, fTexUnit2);
   texel += texture(samplerSet[0x5], uvs) * step(5.0, fTexUnit1) * step(-5.0, fTexUnit2);
   texel += texture(samplerSet[0x6], uvs) * step(6.0, fTexUnit1) * step(-6.0, fTexUnit2);
   texel += texture(samplerSet[0x7], uvs) * step(7.0, fTexUnit1) * step(-7.0, fTexUnit2);
   texel += texture(samplerSet[0x8], uvs) * step(8.0, fTexUnit1) * step(-8.0, fTexUnit2);
   texel += texture(samplerSet[0x9], uvs) * step(9.0, fTexUnit1) * step(-9.0, fTexUnit2);
   texel += texture(samplerSet[0xA], uvs) * step(10.0, fTexUnit1) * step(-10.0, fTexUnit2);
   texel += texture(samplerSet[0xB], uvs) * step(11.0, fTexUnit1) * step(-11.0, fTexUnit2);
   texel += texture(samplerSet[0xC], uvs) * step(12.0, fTexUnit1) * step(-12.0, fTexUnit2);
   texel += texture(samplerSet[0xD], uvs) * step(13.0, fTexUnit1) * step(-13.0, fTexUnit2);
   texel += texture(samplerSet[0xE], uvs) * step(14.0, fTexUnit1) * step(-14.0, fTexUnit2);
   texel += texture(samplerSet[0xF], uvs) * step(15.0, fTexUnit1) * step(-15.0, fTexUnit2);

   return texel;
}

vec4 fetch6(vec3 uvs, int texUnit) {

    int offset = texUnit * 16;
    
    float fTexUnit1 = float(texUnit);
    float fTexUnit2 = float(-texUnit);
   
   vec4 texel = vec4(0.0, 0.0, 0.0, 1.0);
   
   texel += texture(samplerSet[0x0], uvs) * muls[offset + 0x0];
   texel += texture(samplerSet[0x1], uvs) * muls[offset + 0x1];
   texel += texture(samplerSet[0x2], uvs) * muls[offset + 0x2];
   texel += texture(samplerSet[0x3], uvs) * muls[offset + 0x3];
   texel += texture(samplerSet[0x4], uvs) * muls[offset + 0x4];
   texel += texture(samplerSet[0x5], uvs) * muls[offset + 0x5];
   texel += texture(samplerSet[0x6], uvs) * muls[offset + 0x6];
   texel += texture(samplerSet[0x7], uvs) * muls[offset + 0x7];
   texel += texture(samplerSet[0x8], uvs) * muls[offset + 0x8];
   texel += texture(samplerSet[0x9], uvs) * muls[offset + 0x9];
   texel += texture(samplerSet[0xA], uvs) * muls[offset + 0xA];
   texel += texture(samplerSet[0xB], uvs) * muls[offset + 0xB];
   texel += texture(samplerSet[0xC], uvs) * muls[offset + 0xC];
   texel += texture(samplerSet[0xD], uvs) * muls[offset + 0xD];
   texel += texture(samplerSet[0xE], uvs) * muls[offset + 0xE];
   texel += texture(samplerSet[0xF], uvs) * muls[offset + 0xF];

   return texel;
}

void main() {
   vec3 texcoord = vec3(texCoord, float(texSlice));
   
   //vec4 texel1 = texture(samplerSet[texUnit], texcoord);
   //vec4 texel2 = texture(samplerSet[texUnit], texcoord);
   
   vec4 texel = texelFetch(samplerSet[texUnit], ivec3(texcoord), 0);
   
   gl_FragColor = texel;//mix(texel1, texel2, 0.5);
}


1   2   3   4   5   6   7   8   9   10   11   12   13   14  
public class TextureArraySliceHandle {
   public final TextureArray texArray;
   public final int slice;

   public TextureArraySliceHandle(TextureArray texArray, int slice) {
      this.texArray = texArray;
      this.slice = slice;
   }

   @Override
   public String toString() {
      return "handle[texId=" + texArray.texId + ", slice=" + slice + ", texUnit=" + texArray.currTexUnit + "]";
   }
}


1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145  
import static org.lwjgl.opengl.GL11.glBindTexture;
import static org.lwjgl.opengl.GL11.glGetInteger;
import static org.lwjgl.opengl.GL13.GL_TEXTURE0;
import static org.lwjgl.opengl.GL13.glActiveTexture;
import static org.lwjgl.opengl.GL20.GL_MAX_TEXTURE_IMAGE_UNITS;
import static org.lwjgl.opengl.GL20.glGetActiveUniformSize;
import static org.lwjgl.opengl.GL20.glGetUniformLocation;
import static org.lwjgl.opengl.GL20.glUniform1;
import static org.lwjgl.opengl.GL30.GL_TEXTURE_2D_ARRAY;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import org.lwjgl.BufferUtils;

public abstract class TextureArraySet {
   private final int texFormat, bytesPerPixel;
   private final int texUnitOffset, texUnitCount, samplerArrayUniform;
   private final Map<Integer, TextureArray> sizeId2texArray;
   private final List<TextureArray> texArrays;
   private final Map<String, TextureArraySliceHandle> name2handle;
   private final IntBuffer samplerArrayBindings;

   public TextureArraySet(int texFormat, int bytesPerPixel, int texUnitOffset, int program) {
      if(bytesPerPixel < 1)
         throw new IllegalStateException();

      if(texUnitOffset < 0)
         throw new IllegalStateException();
      if(program <= 0)
         throw new IllegalStateException();

      this.samplerArrayUniform = glGetUniformLocation(program, "samplerSet");
      if(this.samplerArrayUniform < 0)
         throw new IllegalStateException("uniform not found: 'samplerSet'");

      int texUnitCount = glGetActiveUniformSize(program, this.samplerArrayUniform);
      if(texUnitCount < 4)
         throw new IllegalStateException("texUnitCount=" + texUnitCount);
      texUnitCount = Math.min(texUnitCount, glGetInteger(GL_MAX_TEXTURE_IMAGE_UNITS));
      if(texUnitCount < 4)
         throw new IllegalStateException("texUnitCount=" + texUnitCount);
      this.texUnitCount = texUnitCount;

      this.texFormat = texFormat;
      this.bytesPerPixel = bytesPerPixel;
      this.texUnitOffset = texUnitOffset;
      this.samplerArrayBindings = BufferUtils.createIntBuffer(texUnitCount);
      this.sizeId2texArray = new HashMap<>();
      this.texArrays = new ArrayList<>();
      this.name2handle = new HashMap<>();

      samplerArrayBindings.clear();
      while (samplerArrayBindings.hasRemaining())
         samplerArrayBindings.put(texUnitOffset + samplerArrayBindings.position());
      samplerArrayBindings.flip();
   }

   public int getTexUnitCount() {
      return texUnitCount;
   }

   protected abstract void onCreatedTextureArray(int texId, int width, int height);

   protected abstract void onDeletedTextureArray(int texId, int width, int height);

   public TextureArraySliceHandle putTexture(String name, int width, int height, ByteBuffer pixels) {
      int sizeId = sizeToID(width, height);

      TextureArray texArray = sizeId2texArray.get(Integer.valueOf(sizeId));

      if(name2handle.containsKey(name) && name2handle.get(name).texArray != texArray) {
         this.removeTexture(name);
      }

      if(texArray == null) {
         if(texArrays.size() == texUnitCount)
            throw new IllegalStateException("reached max texture-units: " + texUnitCount);
         texArray = new TextureArray(texFormat, bytesPerPixel, width, height);
         sizeId2texArray.put(Integer.valueOf(sizeId), texArray);
         texArrays.add(texArray);

         this.onCreatedTextureArray(texArray.texId, texArray.width, texArray.height);
      }

      glBindTexture(GL_TEXTURE_2D_ARRAY, texArray.texId);
      int slice = texArray.putSlice(name, pixels);
      TextureArraySliceHandle handle = new TextureArraySliceHandle(texArray, slice);
      name2handle.put(name, handle);
      this.reconfigureTextureUnits();
      return handle;
   }

   public boolean hasTexture(String name) {
      return name2handle.containsKey(name);
   }

   public void removeTexture(String name) {
      TextureArraySliceHandle handle = name2handle.remove(name);
      if(handle == null)
         throw new NoSuchElementException("handle '" + name + "'");

      if(handle.texArray.clearSlice(handle.slice) == 0) {
         int sizeId = sizeToID(handle.texArray.width, handle.texArray.height);
         if(sizeId2texArray.remove(Integer.valueOf(sizeId)) != handle.texArray)
            throw new IllegalStateException();
         if(!texArrays.remove(handle.texArray))
            throw new IllegalStateException();
         handle.texArray.delete();

         this.onDeletedTextureArray(handle.texArray.texId, handle.texArray.width, handle.texArray.height);
         this.reconfigureTextureUnits();
      }
   }

   public TextureArraySliceHandle getHandle(String name) {
      TextureArraySliceHandle handle = name2handle.get(name);
      if(handle == null)
         throw new NoSuchElementException("handle '" + name + "'");
      return handle;
   }

   private void reconfigureTextureUnits() {
      for(int i = texArrays.size() - 1; i >= 0; i--) {
         TextureArray texArray = texArrays.get(i);
         texArray.currTexUnit = texUnitOffset + i;

         glActiveTexture(GL_TEXTURE0 + texArray.currTexUnit);
         glBindTexture(GL_TEXTURE_2D_ARRAY, texArray.texId);
      }

      glUniform1(samplerArrayUniform, samplerArrayBindings);
   }

   private static int sizeToID(int w, int h) {
      if(w <= 0 || h <= 0)
         throw new IllegalArgumentException();
      return (h << 16) | w;
   }
}


1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147  
import static org.lwjgl.opengl.ARBTextureStorage.glTexStorage3D;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
import static org.lwjgl.opengl.GL11.glDeleteTextures;
import static org.lwjgl.opengl.GL11.glGenTextures;
import static org.lwjgl.opengl.GL12.glTexSubImage3D;
import static org.lwjgl.opengl.GL30.GL_TEXTURE_2D_ARRAY;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.lwjgl.BufferUtils;

class TextureArray {
   public final int texId, texFormat, bytesPerPixel;
   public final int width, height;
   private int capacity;
   public int currTexUnit;
   private final Map<String, Integer> name2slice;
   private final List<ByteBuffer> slices;

   public TextureArray(int texFormat, int bytesPerPixel, int width, int height) {
      this.texId = glGenTextures();
      this.texFormat = texFormat;
      this.bytesPerPixel = bytesPerPixel;
      this.width = width;
      this.height = height;
      this.capacity = 0;
      this.currTexUnit = -1;
      this.slices = new ArrayList<>();
      this.name2slice = new HashMap<>();
   }

   private void log(String msg) {
      if(false)
         System.out.println(TextureArray.class.getSimpleName() + "[id=" + texId + ", " + width + "x" + height + ", unit=" + currTexUnit + "] " + msg);
   }

   void delete() {
      glDeleteTextures(texId);
   }

   private void checkBoundTexture() {
      //if(glGetInteger(GL_TEXTURE_BINDING_2D_ARRAY) != this.texId)
      //   throw new IllegalStateException();
   }

   private void rebuild() {
      int minCap = ((width * height) < 1024 * 1024) ? 4 : 2;
      int newCap = Math.max(minCap, capacity * 2);
      this.log("allocate " + newCap + " slices (was " + capacity + ")");
      capacity = newCap;

      int levels = 1;
      int depth = capacity;

      this.checkBoundTexture();
      glTexStorage3D(GL_TEXTURE_2D_ARRAY, levels, texFormat, width, height, depth);

      for(int i = 0; i < slices.size(); i++) {
         this.uploadSlice(i);
      }
   }

   public int putSlice(String name, ByteBuffer pixels) {
      int byteCount = pixels.remaining();
      if(width * height * bytesPerPixel != byteCount)
         throw new IllegalArgumentException("expected " + (width * height * bytesPerPixel) + " bytes, found " + byteCount);

      ByteBuffer copy = BufferUtils.createByteBuffer(byteCount);
      copy.put(pixels.slice()).flip();

      int slice;
      Integer sliceObj = name2slice.get(name);
      if(sliceObj == null) {
         slice = -1;

         for(int i = 0; i < slices.size(); i++) {
            if(slices.get(i) == null) {
               this.log("found unused slice: " + i);
               // put in unused slot
               slice = i;
               name2slice.put(name, slice);
               slices.set(slice, copy);
               this.uploadSlice(slice);
               return slice;
            }
         }

         // append
         this.log("append slice");
         slice = slices.size();
         name2slice.put(name, slice);
         slices.add(copy);

         if(slices.size() >= capacity) {
            this.rebuild();
         }
         else {
            this.uploadSlice(slice);
         }
      }
      else {
         slice = sliceObj.intValue();
         slices.set(slice, copy);
         this.uploadSlice(slice);
      }
      return slice;
   }

   int clearSlice(int slice) {
      this.log("clear slice: " + slice);
      slices.set(slice, null);

      for(Entry<String, Integer> entry : name2slice.entrySet()) {
         if(entry.getValue().intValue() == slice) {
            name2slice.remove(entry.getKey());
            break;
         }
      }

      return name2slice.size();
   }

   private void uploadSlice(int slice) {
      this.log("update slice: " + slice);

      ByteBuffer pixels = slices.get(slice);

      int level = 0;

      int xoffset = 0;
      int yoffset = 0;
      int zoffset = slice;

      int depth = 1;

      this.checkBoundTexture();
      glTexSubImage3D(GL_TEXTURE_2D_ARRAY, level, //
            xoffset, yoffset, zoffset, //
            width, height, depth, //
            texFormat, GL_UNSIGNED_BYTE, pixels);
   }
}


1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300  
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.util.Random;

import org.lwjgl.BufferUtils;

import static org.lwjgl.opengl.ARBShaderObjects.*;

import org.lwjgl.opengl.ARBFragmentShader;
import org.lwjgl.opengl.ARBVertexShader;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
import static org.lwjgl.opengl.GL30.*;

public class Demo {
   public static void main(String[] args) throws Exception {
      String title = "TextureArray Set";
      int wWindow = 800;
      int hWindow = 600;
      Display.setDisplayMode(new DisplayMode(wWindow, hWindow));
      Display.setTitle(title);
      Display.create();

      Random rndm = new Random(12345L);
      int m = 64;

      int vertShader = createShader("shaders/tex-array-set.vert", ARBVertexShader.GL_VERTEX_SHADER_ARB);
      int fragShader = createShader("shaders/tex-array-set.frag", ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
      int program = createAttachLinkProgram(vertShader, fragShader);
      glUseProgramObjectARB(program);
      validateProgram(program);
      glUseProgramObjectARB(program);

      TextureArraySet texArraySet = new TextureArraySet(GL_RGB, 3, 0, program) {
         @Override
         protected void onCreatedTextureArray(int texId, int width, int height) {
            System.out.println("onCreatedTextureArray[" + texId + "]");

            glBindTexture(GL_TEXTURE_2D_ARRAY, texId);

            glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
            glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
         }

         @Override
         protected void onDeletedTextureArray(int texId, int width, int height) {
            System.out.println("onDeletedTextureArray[" + texId + "]");
         }
      };

      System.out.println(texArraySet.getTexUnitCount());

      {
         ByteBuffer bbYellow = BufferUtils.createByteBuffer(16 * 16 * 3);
         while (bbYellow.hasRemaining())
            bbYellow.put((byte) (0xff - rndm.nextInt(m))).put((byte) (0xff - rndm.nextInt(m))).put((byte) (0x00 + rndm.nextInt(m)));
         bbYellow.flip();
         texArraySet.putTexture("yellow", 16, 16, bbYellow);
      }

      {
         ByteBuffer bbRed = BufferUtils.createByteBuffer(64 * 64 * 3);
         while (bbRed.hasRemaining())
            bbRed.put((byte) (0xff - rndm.nextInt(m))).put((byte) (0x00 + rndm.nextInt(m))).put((byte) (0x00 + rndm.nextInt(m)));
         bbRed.flip();
         texArraySet.putTexture("red", 64, 64, bbRed);
      }

      {
         ByteBuffer bbGreen = BufferUtils.createByteBuffer(16 * 16 * 3);
         while (bbGreen.hasRemaining())
            bbGreen.put((byte) (0x00 + rndm.nextInt(m))).put((byte) (0xff - rndm.nextInt(m))).put((byte) (0x00 + rndm.nextInt(m)));
         bbGreen.flip();
         texArraySet.putTexture("green", 16, 16, bbGreen);
      }

      {
         ByteBuffer bbBlue = BufferUtils.createByteBuffer(16 * 16 * 3);
         while (bbBlue.hasRemaining())
            bbBlue.put((byte) (0x00 + rndm.nextInt(m))).put((byte) (0x00 + rndm.nextInt(m))).put((byte) (0xff - rndm.nextInt(m)));
         bbBlue.flip();
         texArraySet.putTexture("blue", 16, 16, bbBlue);
      }

      {
         ByteBuffer bbCyan = BufferUtils.createByteBuffer(8 * 8 * 3);
         while (bbCyan.hasRemaining())
            bbCyan.put((byte) (0x00 + rndm.nextInt(m))).put((byte) (0xff - rndm.nextInt(m))).put((byte) (0xff - rndm.nextInt(m)));
         bbCyan.flip();
         texArraySet.putTexture("cyan", 8, 8, bbCyan);
      }

      {
         ByteBuffer bbPurple = BufferUtils.createByteBuffer(8 * 32 * 3);
         while (bbPurple.hasRemaining())
            bbPurple.put((byte) (0xff - rndm.nextInt(m))).put((byte) (0x00 + rndm.nextInt(m))).put((byte) (0xff - rndm.nextInt(m)));
         bbPurple.flip();
         texArraySet.putTexture("purple", 8, 32, bbPurple);
      }
      {
         ByteBuffer bb = BufferUtils.createByteBuffer(4 * 4 * 3);
         while (bb.hasRemaining())
            bb.put((byte) (0xff - rndm.nextInt(m))).put((byte) (0xff - rndm.nextInt(m))).put((byte) (0xff - rndm.nextInt(m)));
         bb.flip();
         texArraySet.putTexture("empty2", 4, 4, bb);
         texArraySet.putTexture("empty1", 4, 4, bb);
      }

      int frameCounter = 0;
      int lastFrame = 0;
      long lastSec = System.currentTimeMillis();

      while (!Display.isCloseRequested()) {

         glViewport(0, 0, wWindow, hWindow);
         glMatrixMode(GL_PROJECTION);
         glLoadIdentity();
         glOrtho(0, wWindow, 0, hWindow, -1, +1);

         glMatrixMode(GL_MODELVIEW);
         glLoadIdentity();

         glClearColor(0, 0, 0, 1);
         glClear(GL_COLOR_BUFFER_BIT);

         {
            glBegin(GL_QUADS);

            for(int k = 0; k < 1024; k++) {
               int i = 0;
               for(String color : new String[] { "blue", "red", "yellow", "green", "cyan", "purple" }) {
                  TextureArraySliceHandle handle = texArraySet.getHandle(color);

                  int dim = 256;
                  int xoff = (i % 3) * 256;
                  int yoff = (i / 3) * 256;

                  glTexCoord4f(0.0f * handle.texArray.width, 0.0f * handle.texArray.height, handle.slice, handle.texArray.currTexUnit);
                  glVertex2f(0 * dim + xoff, 0 * dim + yoff);

                  glTexCoord4f(1.0f * handle.texArray.width, 0.0f * handle.texArray.height, handle.slice, handle.texArray.currTexUnit);
                  glVertex2f(1 * dim + xoff, 0 * dim + yoff);

                  glTexCoord4f(1.0f * handle.texArray.width, 1.0f * handle.texArray.height, handle.slice, handle.texArray.currTexUnit);
                  glVertex2f(1 * dim + xoff, 1 * dim + yoff);

                  glTexCoord4f(0.0f * handle.texArray.width, 1.0f * handle.texArray.height, handle.slice, handle.texArray.currTexUnit);
                  glVertex2f(0 * dim + xoff, 1 * dim + yoff);

                  i++;
               }
            }

            glEnd();

            if(++frameCounter % 60 * 3 == 0) {
               {
                  ByteBuffer bbPurple = BufferUtils.createByteBuffer(64 * 16 * 3);
                  while (bbPurple.hasRemaining())
                     bbPurple.put((byte) (0xff - rndm.nextInt(m))).put((byte) (0x00 + rndm.nextInt(m))).put((byte) (0xff - rndm.nextInt(m)));
                  bbPurple.flip();

                  texArraySet.putTexture("purple", 64, 16, bbPurple);
               }

               if(true) {
                  ByteBuffer bb = BufferUtils.createByteBuffer(4 * 4 * 3);
                  while (bb.hasRemaining())
                     bb.put((byte) (0xff - rndm.nextInt(m))).put((byte) (0xff - rndm.nextInt(m))).put((byte) (0xff - rndm.nextInt(m)));
                  bb.flip();
                  if(texArraySet.hasTexture("empty1"))
                     texArraySet.removeTexture("empty1");
                  texArraySet.putTexture("empty3", 4, 4, bb);
               }
            }

            if(System.currentTimeMillis() - lastSec > 0) {
               Display.setTitle(title + " - " + (frameCounter - lastFrame) + "fps");
               lastFrame = frameCounter;
               lastSec += 1000L;
            }
         }

         // swap
         //Display.sync(60);
         Display.update();
      }

      Display.destroy();
   }

   private static int createAttachLinkProgram(int vertShader, int fragShader) {
      int program = glCreateProgramObjectARB();
      glAttachObjectARB(program, vertShader);
      glAttachObjectARB(program, fragShader);

      glLinkProgramARB(program);
      if(glGetObjectParameteriARB(program, GL_OBJECT_LINK_STATUS_ARB) == GL_FALSE) {
         throw new IllegalStateException(getLogInfo(program));
      }
      return program;
   }

   private static int validateProgram(int program) {
      glValidateProgramARB(program);
      if(glGetObjectParameteriARB(program, GL_OBJECT_VALIDATE_STATUS_ARB) == GL_FALSE) {
         throw new IllegalStateException(getLogInfo(program));
      }
      return program;
   }

   private static int createShader(String filename, int shaderType) throws Exception {
      int shader = 0;
      try {
         shader = glCreateShaderObjectARB(shaderType);

         if(shader == 0)
            return 0;

         glShaderSourceARB(shader, readFileAsString(filename));
         glCompileShaderARB(shader);

         if(glGetObjectParameteriARB(shader, GL_OBJECT_COMPILE_STATUS_ARB) == GL_FALSE)
            throw new RuntimeException("Error creating shader: " + getLogInfo(shader));

         return shader;
      }
      catch (Exception exc) {
         glDeleteObjectARB(shader);
         throw exc;
      }
   }

   private static String getLogInfo(int obj) {
      return glGetInfoLogARB(obj, glGetObjectParameteriARB(obj, GL_OBJECT_INFO_LOG_LENGTH_ARB));
   }

   private static String readFileAsString(String filename) throws Exception {
      StringBuilder source = new StringBuilder();

      FileInputStream in = new FileInputStream(filename);

      Exception exception = null;

      BufferedReader reader;
      try {
         reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));

         Exception innerExc = null;
         try {
            String line;
            while ((line = reader.readLine()) != null)
               source.append(line).append('\n');
         }
         catch (Exception exc) {
            exception = exc;
         }
         finally {
            try {
               reader.close();
            }
            catch (Exception exc) {
               if(innerExc == null)
                  innerExc = exc;
               else
                  exc.printStackTrace();
            }
         }

         if(innerExc != null)
            throw innerExc;
      }
      catch (Exception exc) {
         exception = exc;
      }
      finally {
         try {
            in.close();
         }
         catch (Exception exc) {
            if(exception == null)
               exception = exc;
            else
               exc.printStackTrace();
         }

         if(exception != null)
            throw exception;
      }

      return source.toString();
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (26 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (34 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!