Advertisement
Guest User

Java SB

a guest
Jul 14th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.75 KB | None | 0 0
  1. package EGL;
  2.  
  3. import static org.lwjgl.opengl.GL11.glDrawArrays;
  4. import static org.lwjgl.opengl.GL15.glBindBuffer;
  5. import static org.lwjgl.opengl.GL15.glBufferData;
  6. import static org.lwjgl.opengl.GL15.glBufferSubData;
  7. import static org.lwjgl.opengl.GL15.glDeleteBuffers;
  8. import static org.lwjgl.opengl.GL15.glGenBuffers;
  9. import static org.lwjgl.opengl.GL20.glAttachShader;
  10. import static org.lwjgl.opengl.GL20.glBindAttribLocation;
  11. import static org.lwjgl.opengl.GL20.glCompileShader;
  12. import static org.lwjgl.opengl.GL20.glCreateProgram;
  13. import static org.lwjgl.opengl.GL20.glCreateShader;
  14. import static org.lwjgl.opengl.GL20.glDeleteProgram;
  15. import static org.lwjgl.opengl.GL20.glDeleteShader;
  16. import static org.lwjgl.opengl.GL20.glDetachShader;
  17. import static org.lwjgl.opengl.GL20.glEnableVertexAttribArray;
  18. import static org.lwjgl.opengl.GL20.glGetProgrami;
  19. import static org.lwjgl.opengl.GL20.glGetShaderi;
  20. import static org.lwjgl.opengl.GL20.glGetUniformLocation;
  21. import static org.lwjgl.opengl.GL20.glLinkProgram;
  22. import static org.lwjgl.opengl.GL20.glShaderSource;
  23. import static org.lwjgl.opengl.GL20.glUniformMatrix4;
  24. import static org.lwjgl.opengl.GL20.glUseProgram;
  25. import static org.lwjgl.opengl.GL20.glValidateProgram;
  26. import static org.lwjgl.opengl.GL20.glVertexAttribPointer;
  27. import static org.lwjgl.opengl.GL30.glBindVertexArray;
  28. import static org.lwjgl.opengl.GL30.glDeleteVertexArrays;
  29. import static org.lwjgl.opengl.GL30.glGenVertexArrays;
  30.  
  31. import java.nio.ByteBuffer;
  32. import java.nio.FloatBuffer;
  33. import java.util.ArrayDeque;
  34. import java.util.ArrayList;
  35. import java.util.Collections;
  36. import java.util.Comparator;
  37. import java.util.Queue;
  38.  
  39. import EGL.GL.BufferTarget;
  40. import EGL.GL.BufferUsageHint;
  41. import EGL.GL.GetProgramParameterName;
  42. import EGL.GL.PrimitiveType;
  43. import EGL.GL.ShaderParameter;
  44. import EGL.GL.ShaderType;
  45. import EGL.GL.VertexAttribPointerType;
  46. import EGL.Math.Color;
  47. import EGL.Math.Matrix4;
  48. import EGL.Math.Vector2;
  49. import EGL.Math.Vector4;
  50.  
  51. public class SpriteBatch {
  52.     public static final int INITIAL_GLYPH_CAP = 32;
  53.  
  54.     public static int SSMTexture(SpriteGlyph g1, SpriteGlyph g2) {
  55.         return Integer.compare(g1.Texture.getID(), g2.Texture.getID());
  56.     }
  57.     public static int SSMFrontToBack(SpriteGlyph g1, SpriteGlyph g2) {
  58.         return Float.compare(g1.Depth, g2.Depth);
  59.     }
  60.     public static int SSMBackToFront(SpriteGlyph g1, SpriteGlyph g2) {
  61.         return Float.compare(g2.Depth, g1.Depth);
  62.     }
  63.  
  64.     static class SpriteBatchCall {
  65.         public GLTexture Texture;
  66.         public int Indices;
  67.         public int IndexOffset;
  68.  
  69.         public SpriteBatchCall(int iOff, GLTexture t, ArrayList<SpriteBatchCall> calls) {
  70.             Texture = t;
  71.             IndexOffset = iOff;
  72.             Indices = 6;
  73.             calls.add(this);
  74.         }
  75.  
  76.         public SpriteBatchCall Append(SpriteGlyph g, ArrayList<SpriteBatchCall> calls) {
  77.             if(g.Texture != Texture) return new SpriteBatchCall(IndexOffset + Indices, g.Texture, calls);
  78.             else Indices += 6;
  79.             return this;
  80.         }
  81.     }
  82.  
  83.     private static final String VS_SRC =
  84.             "#version 130\r\n" +
  85.                     "uniform mat4 World;\r\n" +
  86.                     "uniform mat4 VP;\r\n" +
  87.                     "in vec4 vPosition;  // Sem  (Position    0)\r\n" +
  88.                     "in vec2 vUV;        // Sem  (Texcoord    0)\r\n" +
  89.                     "in vec4 vUVRect;    // Sem  (Texcoord    1)\r\n" +
  90.                     "in vec4 vTint;      // Sem  (Color       0)\r\n" +
  91.                     "out vec2 fUV;\r\n" +
  92.                     "out vec4 fUVRect;\r\n" +
  93.                     "out vec4 fTint;\r\n" +
  94.                     "void main(void) {\r\n" +
  95.                     "    fTint = vTint;\r\n" +
  96.                     "    fUV = vUV;\r\n" +
  97.                     "    fUVRect = vUVRect;\r\n" +
  98.                     //          "    vec4 worldPos = vPosition * World;\r\n" +
  99.                     //          "    gl_Position = worldPos * VP;\r\n" +
  100.                     "    gl_Position = vec4(vPosition.xy, 0, 1);\r\n" +
  101.                     "}";
  102.     private static final String FS_SRC =
  103.             "#version 130\r\n" +
  104.                     "uniform sampler2D SBTex;\r\n" +
  105.                     "in vec2 fUV;\r\n" +
  106.                     "in vec4 fUVRect;\r\n" +
  107.                     "in vec4 fTint;\r\n" +
  108.                     "out vec4 out_Color;\r\n" +
  109.                     "void main(void) {\r\n" +
  110.                     //          "    out_Color = texture(SBTex, (vec2(fract(fUV.x), fract(fUV.y)) * fUVRect.zw) + fUVRect.xy) * fTint;\r\n" +
  111.                     "    out_Color = vec4(1, 0, 0, 1);\r\n" +
  112.                     "}";
  113.  
  114.     private static final String VS_SRC_TEST =
  115.         "#version 130 core\n" +
  116.         "in vec4 vPosition;\n" +
  117.         "in vec4 vTint;\n" +
  118.         "out vec4 fColor;\n" +
  119.         "void main(void) {\n" +
  120.         "   fColor = vTint;\n" +
  121.         "   gl_Position = vPosition;\n" +
  122.         "}";
  123.     private static final String FS_SRC_TEST =
  124.         "#version 130 core\n" +
  125.         "in vec4 fColor;\n" +
  126.         "out vec4 out_Color;\n" +
  127.         "void main(void) {\n" +
  128.         "   out_Color = fColor;\n" +
  129.         "}";
  130.  
  131.     public static final Vector4 FULL_UV_RECT = new Vector4(0, 0, 1, 1);
  132.     public static final Vector2 UV_NO_TILE = new Vector2(1, 1);
  133.  
  134.     public static Matrix4 CreateCameraFromWindow(float w, float h) {
  135.         w *= 0.5f;
  136.         h *= 0.5f;
  137.         // TODO: Check This Out
  138.         //        Matrix4 mm = new Matrix4(
  139.         //            w, 0, 0, 0,
  140.         //            0, -h, 0, 0,
  141.         //            0, 0, -1, 0,
  142.         //            w, h, 0, 1
  143.         //            ).invert();
  144.         Matrix4 mo =
  145.                 Matrix4.createScale(1 / w, -1 / h, 1).mul(
  146.                         Matrix4.createTranslation(-1, 1, -1))
  147.                         ;
  148.         return mo;
  149.     }
  150.  
  151.     // Glyph Information
  152.     private ArrayList<SpriteGlyph> glyphs;
  153.     private Queue<SpriteGlyph> emptyGlyphs;
  154.  
  155.     // Render Batches
  156.     private int bufUsage;
  157.     private int vao, vbo, glyphCapacity;
  158.     private ArrayList<SpriteBatchCall> batches;
  159.  
  160.     // Custom Shader
  161.     private int idProg, idVS, idFS;
  162.     private int unWorld, unVP, unTexture;
  163.     private FloatBuffer fbUniforms;
  164.  
  165.     public SpriteBatch(boolean isDynamic) {
  166.         bufUsage = isDynamic ? BufferUsageHint.DynamicDraw : BufferUsageHint.StaticDraw;
  167.  
  168.         InitGL();
  169.  
  170.         emptyGlyphs = new ArrayDeque<SpriteGlyph>();
  171.         fbUniforms = ByteBuffer.allocateDirect(16 * 4).asFloatBuffer();
  172.     }
  173.     public void InitGL() {
  174.         CreateProgram();
  175.         SearchUniforms();
  176.         CreateVertexArray();
  177.  
  178.  
  179.     }
  180.     public void Dispose() {
  181.         glDeleteBuffers(vbo);
  182.         vbo = 0;
  183.         glDeleteVertexArrays(vao);
  184.         vao = 0;
  185.  
  186.         //      program.Dispose();
  187.         glDetachShader(idProg, idVS);
  188.         glDeleteShader(idVS);
  189.         glDetachShader(idProg, idFS);
  190.         glDeleteShader(idFS);
  191.         glDeleteProgram(idProg);
  192.     }
  193.  
  194.     private void CreateProgram() {
  195.         // Create The Program
  196.         idProg = glCreateProgram();
  197.  
  198.         // Make Vertex Shader
  199.         idVS = glCreateShader(ShaderType.VertexShader);
  200.         glShaderSource(idVS, VS_SRC_TEST);
  201.         glCompileShader(idVS);
  202.         if(glGetShaderi(idVS, ShaderParameter.CompileStatus) != 1)
  203.             throw new RuntimeException("Vert Shader Had Compilation Errors");
  204.         glAttachShader(idProg, idVS);
  205.  
  206.         // Make Fragment Shader
  207.         idFS = glCreateShader(ShaderType.FragmentShader);
  208.         glShaderSource(idFS, FS_SRC_TEST);
  209.         glCompileShader(idFS);
  210.         if(glGetShaderi(idFS, ShaderParameter.CompileStatus) != 1)
  211.             throw new RuntimeException("Frag Shader Had Compilation Errors");
  212.         glAttachShader(idProg, idFS);
  213.  
  214.         // Setup Vertex Attribute Locations
  215.         glBindAttribLocation(idProg, 0, "vPosition");
  216.         glBindAttribLocation(idProg, 1, "vTint");
  217.         //        glBindAttribLocation(idProg, 2, "vUV");
  218.         //        glBindAttribLocation(idProg, 3, "vUVRect");
  219.  
  220.         glLinkProgram(idProg);
  221.         glValidateProgram(idProg);
  222.         if(glGetProgrami(idProg, GetProgramParameterName.LinkStatus) != 1)
  223.             throw new RuntimeException("Program Had Compilation Errors");
  224.     }
  225.     private void SearchUniforms() {
  226.         unWorld = glGetUniformLocation(idProg, "World");
  227.         unVP = glGetUniformLocation(idProg, "VP");
  228.         unTexture = glGetUniformLocation(idProg, "SBTex");
  229.     }
  230.     private void CreateVertexArray() {
  231.         vao = glGenVertexArrays();
  232.         glBindVertexArray(vao);
  233.  
  234.         vbo = glGenBuffers();
  235.         glyphCapacity = INITIAL_GLYPH_CAP;
  236.         glBindBuffer(BufferTarget.ArrayBuffer, vbo);
  237.         glBufferData(BufferTarget.ArrayBuffer, (glyphCapacity * 6) * VertexSpriteBatch.Size, bufUsage);
  238.  
  239.         glEnableVertexAttribArray(0);
  240.         glEnableVertexAttribArray(1);
  241.         glEnableVertexAttribArray(2);
  242.         glEnableVertexAttribArray(3);
  243.         glVertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, VertexSpriteBatch.Size, 0);
  244.         glVertexAttribPointer(1, 4, VertexAttribPointerType.UnsignedByte, true, VertexSpriteBatch.Size, 36);
  245.         glVertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, VertexSpriteBatch.Size, 12);
  246.         glVertexAttribPointer(3, 4, VertexAttribPointerType.Float, false, VertexSpriteBatch.Size, 20);
  247.  
  248.         glBindVertexArray(0);
  249.  
  250.         GLBuffer.Unbind(BufferTarget.ArrayBuffer);     
  251.     }
  252.  
  253.     public void Begin() {
  254.         // Only Clear The Glyphs
  255.         glyphs = new ArrayList<SpriteGlyph>();
  256.         batches = new ArrayList<SpriteBatchCall>();
  257.     }
  258.  
  259.     private SpriteGlyph CreateGlyph(GLTexture t, float d) {
  260.         if(emptyGlyphs.size() > 0) {
  261.             SpriteGlyph g = emptyGlyphs.remove();
  262.             g.Texture = t;
  263.             g.Depth = d;
  264.             return g;
  265.         }
  266.         else {
  267.             return new SpriteGlyph(t, d);
  268.         }
  269.     }
  270.     public void Draw(GLTexture t, Vector4 uvRect, Vector2 uvTiling, Matrix4 mTransform, Color tint, float depth) {
  271.         Vector4 uvr = uvRect != null ? uvRect : FULL_UV_RECT;
  272.         Vector2 uvt = uvTiling != null ? uvTiling : UV_NO_TILE;
  273.         SpriteGlyph g = CreateGlyph(t, depth);
  274.  
  275.         g.VTL.Position.x = 0;
  276.         g.VTL.Position.y = 0;
  277.         g.VTL.Position.z = depth;
  278.         mTransform.mul(g.VTL.Position);
  279.         g.VTL.UV.x = 0;
  280.         g.VTL.UV.y = 0;
  281.         g.VTL.UVRect.set(uvr);
  282.         g.VTL.Color.set(tint);
  283.  
  284.         g.VTR.Position.x = 1;
  285.         g.VTR.Position.y = 0;
  286.         g.VTR.Position.z = depth;
  287.         mTransform.mul(g.VTR.Position);
  288.         g.VTR.UV.x = uvt.x;
  289.         g.VTR.UV.y = 0;
  290.         g.VTR.UVRect.set(uvr);
  291.         g.VTR.Color.set(tint);
  292.  
  293.         g.VBL.Position.x = 0;
  294.         g.VBL.Position.y = 1;
  295.         g.VBL.Position.z = depth;
  296.         mTransform.mul(g.VBL.Position);
  297.         g.VBL.UV.x = 0;
  298.         g.VBL.UV.y = uvt.y;
  299.         g.VBL.UVRect.set(uvr);
  300.         g.VBL.Color.set(tint);
  301.  
  302.         g.VBR.Position.x = 1;
  303.         g.VBR.Position.y = 1;
  304.         g.VBR.Position.z = depth;
  305.         mTransform.mul(g.VBR.Position);
  306.         g.VBR.UV.x = uvt.x;
  307.         g.VBR.UV.y = uvt.y;
  308.         g.VBR.UVRect.set(uvr);
  309.         g.VBR.Color.set(tint);
  310.  
  311.         glyphs.add(g);
  312.     }
  313.     public void Draw(GLTexture t, Vector4 uvRect, Vector2 uvTiling, Vector2 position, Vector2 offset, Vector2 size, float rotation, Color tint, float depth) {
  314.         Vector4 uvr = uvRect != null ? uvRect : FULL_UV_RECT;
  315.         Vector2 uvt = uvTiling != null ? uvTiling : UV_NO_TILE;
  316.         SpriteGlyph g = CreateGlyph(t, depth);
  317.  
  318.         float rxx = (float)Math.cos(-rotation);
  319.         float rxy = (float)Math.sin(-rotation);
  320.         float cl = size.x * (-offset.x);
  321.         float cr = size.x * (1 - offset.x);
  322.         float ct = size.y * (-offset.y);
  323.         float cb = size.y * (1 - offset.y);
  324.  
  325.         g.VTL.Position.x = (cl * rxx) + (ct * rxy) + position.x;
  326.         g.VTL.Position.y = (cl * -rxy) + (ct * rxx) + position.y;
  327.         g.VTL.Position.z = depth;
  328.         g.VTL.UV.x = 0;
  329.         g.VTL.UV.y = 0;
  330.         g.VTL.UVRect.set(uvr);
  331.         g.VTL.Color.set(tint);
  332.  
  333.         g.VTR.Position.x = (cr * rxx) + (ct * rxy) + position.x;
  334.         g.VTR.Position.y = (cr * -rxy) + (ct * rxx) + position.y;
  335.         g.VTR.Position.z = depth;
  336.         g.VTR.UV.x = uvt.x;
  337.         g.VTR.UV.y = 0;
  338.         g.VTR.UVRect.set(uvr);
  339.         g.VTR.Color.set(tint);
  340.  
  341.         g.VBL.Position.x = (cl * rxx) + (cb * rxy) + position.x;
  342.         g.VBL.Position.y = (cl * -rxy) + (cb * rxx) + position.y;
  343.         g.VBL.Position.z = depth;
  344.         g.VBL.UV.x = 0;
  345.         g.VBL.UV.y = uvt.y;
  346.         g.VBL.UVRect.set(uvr);
  347.         g.VBL.Color.set(tint);
  348.  
  349.         g.VBR.Position.x = (cr * rxx) + (cb * rxy) + position.x;
  350.         g.VBR.Position.y = (cr * -rxy) + (cb * rxx) + position.y;
  351.         g.VBR.Position.z = depth;
  352.         g.VBR.UV.x = uvt.x;
  353.         g.VBR.UV.y = uvt.y;
  354.         g.VBR.UVRect.set(uvr);
  355.         g.VBR.Color.set(tint);
  356.  
  357.         glyphs.add(g);
  358.     }
  359.     public void Draw(GLTexture t, Vector4 uvRect, Vector2 uvTiling, Vector2 position, Vector2 offset, Vector2 size, Color tint, float depth) {
  360.         Vector4 uvr = uvRect != null ? uvRect : FULL_UV_RECT;
  361.         Vector2 uvt = uvTiling != null ? uvTiling : UV_NO_TILE;
  362.         SpriteGlyph g = CreateGlyph(t, depth);
  363.  
  364.         float cl = size.x * (-offset.x);
  365.         float cr = size.x * (1 - offset.x);
  366.         float ct = size.y * (-offset.y);
  367.         float cb = size.y * (1 - offset.y);
  368.  
  369.         g.VTL.Position.x = cl + position.x;
  370.         g.VTL.Position.y = ct + position.y;
  371.         g.VTL.Position.z = depth;
  372.         g.VTL.UV.x = 0;
  373.         g.VTL.UV.y = 0;
  374.         g.VTL.UVRect.set(uvr);
  375.         g.VTL.Color.set(tint);
  376.  
  377.         g.VTR.Position.x = cr + position.x;
  378.         g.VTR.Position.y = ct + position.y;
  379.         g.VTR.Position.z = depth;
  380.         g.VTR.UV.x = uvt.x;
  381.         g.VTR.UV.y = 0;
  382.         g.VTR.UVRect.set(uvr);
  383.         g.VTR.Color.set(tint);
  384.  
  385.         g.VBL.Position.x = cl + position.x;
  386.         g.VBL.Position.y = cb + position.y;
  387.         g.VBL.Position.z = depth;
  388.         g.VBL.UV.x = 0;
  389.         g.VBL.UV.y = uvt.y;
  390.         g.VBL.UVRect.set(uvr);
  391.         g.VBL.Color.set(tint);
  392.  
  393.         g.VBR.Position.x = cr + position.x;
  394.         g.VBR.Position.y = cb + position.y;
  395.         g.VBR.Position.z = depth;
  396.         g.VBR.UV.x = uvt.x;
  397.         g.VBR.UV.y = uvt.y;
  398.         g.VBR.UVRect.set(uvr);
  399.         g.VBR.Color.set(tint);
  400.  
  401.         glyphs.add(g);
  402.     }
  403.     public void Draw(GLTexture t, Vector4 uvRect, Vector2 uvTiling, Vector2 position, Vector2 size, Color tint, float depth) {
  404.         Vector4 uvr = uvRect != null ? uvRect : FULL_UV_RECT;
  405.         Vector2 uvt = uvTiling != null ? uvTiling : UV_NO_TILE;
  406.         SpriteGlyph g = CreateGlyph(t, depth);
  407.  
  408.         g.VTL.Position.x = position.x;
  409.         g.VTL.Position.y = position.y;
  410.         g.VTL.Position.z = depth;
  411.         g.VTL.UV.x = 0;
  412.         g.VTL.UV.y = 0;
  413.         g.VTL.UVRect.set(uvr);
  414.         g.VTL.Color.set(tint);
  415.  
  416.         g.VTR.Position.x = size.x + position.x;
  417.         g.VTR.Position.y = position.y;
  418.         g.VTR.Position.z = depth;
  419.         g.VTR.UV.x = uvt.x;
  420.         g.VTR.UV.y = 0;
  421.         g.VTR.UVRect.set(uvr);
  422.         g.VTR.Color.set(tint);
  423.  
  424.         g.VBL.Position.x = position.x;
  425.         g.VBL.Position.y = size.y + position.y;
  426.         g.VBL.Position.z = depth;
  427.         g.VBL.UV.x = 0;
  428.         g.VBL.UV.y = uvt.y;
  429.         g.VBL.UVRect.set(uvr);
  430.         g.VBL.Color.set(tint);
  431.  
  432.         g.VBR.Position.x = size.x + position.x;
  433.         g.VBR.Position.y = size.y + position.y;
  434.         g.VBR.Position.z = depth;
  435.         g.VBR.UV.x = uvt.x;
  436.         g.VBR.UV.y = uvt.y;
  437.         g.VBR.UVRect.set(uvr);
  438.         g.VBR.Color.set(tint);
  439.  
  440.         glyphs.add(g);
  441.     }
  442.     public void Draw(GLTexture t, Vector4 uvRect, Vector2 position, Vector2 size, Color tint, float depth) {
  443.         Vector4 uvr = uvRect != null ? uvRect : FULL_UV_RECT;
  444.         SpriteGlyph g = CreateGlyph(t, depth);
  445.  
  446.         g.VTL.Position.x = position.x;
  447.         g.VTL.Position.y = position.y;
  448.         g.VTL.Position.z = depth;
  449.         g.VTL.UV.x = 0;
  450.         g.VTL.UV.y = 0;
  451.         g.VTL.UVRect.set(uvr);
  452.         g.VTL.Color.set(tint);
  453.  
  454.         g.VTR.Position.x = size.x + position.x;
  455.         g.VTR.Position.y = position.y;
  456.         g.VTR.Position.z = depth;
  457.         g.VTR.UV.x = 1;
  458.         g.VTR.UV.y = 0;
  459.         g.VTR.UVRect.set(uvr);
  460.         g.VTR.Color.set(tint);
  461.  
  462.         g.VBL.Position.x = position.x;
  463.         g.VBL.Position.y = size.y + position.y;
  464.         g.VBL.Position.z = depth;
  465.         g.VBL.UV.x = 0;
  466.         g.VBL.UV.y = 1;
  467.         g.VBL.UVRect.set(uvr);
  468.         g.VBL.Color.set(tint);
  469.  
  470.         g.VBR.Position.x = size.x + position.x;
  471.         g.VBR.Position.y = size.y + position.y;
  472.         g.VBR.Position.z = depth;
  473.         g.VBR.UV.x = 1;
  474.         g.VBR.UV.y = 1;
  475.         g.VBR.UVRect.set(uvr);
  476.         g.VBR.Color.set(tint);
  477.  
  478.         glyphs.add(g);
  479.     }
  480.     public void Draw(GLTexture t, Vector2 position, Vector2 size, Color tint, float depth) {
  481.         SpriteGlyph g = CreateGlyph(t, depth);
  482.  
  483.         g.VTL.Position.x = position.x;
  484.         g.VTL.Position.y = position.y;
  485.         g.VTL.Position.z = depth;
  486.         g.VTL.UV.x = 0;
  487.         g.VTL.UV.y = 0;
  488.         g.VTL.UVRect.set(FULL_UV_RECT);
  489.         g.VTL.Color.set(tint);
  490.  
  491.         g.VTR.Position.x = size.x + position.x;
  492.         g.VTR.Position.y = position.y;
  493.         g.VTR.Position.z = depth;
  494.         g.VTR.UV.x = 1;
  495.         g.VTR.UV.y = 0;
  496.         g.VTR.UVRect.set(FULL_UV_RECT);
  497.         g.VTR.Color.set(tint);
  498.  
  499.         g.VBL.Position.x = position.x;
  500.         g.VBL.Position.y = size.y + position.y;
  501.         g.VBL.Position.z = depth;
  502.         g.VBL.UV.x = 0;
  503.         g.VBL.UV.y = 1;
  504.         g.VBL.UVRect.set(FULL_UV_RECT);
  505.         g.VBL.Color.set(tint);
  506.  
  507.         g.VBR.Position.x = size.x + position.x;
  508.         g.VBR.Position.y = size.y + position.y;
  509.         g.VBR.Position.z = depth;
  510.         g.VBR.UV.x = 1;
  511.         g.VBR.UV.y = 1;
  512.         g.VBR.UVRect.set(FULL_UV_RECT);
  513.         g.VBR.Color.set(tint);
  514.  
  515.         glyphs.add(g);
  516.     }
  517.  
  518.     // TODO: Finish The Fight
  519.     //    public void DrawString(SpriteFont font, String s, Vector2 position, Vector2 scaling, Color tint, float depth) {
  520.         //        if(s == null) s = "";
  521.         //        font.Draw(this, s, position, scaling, tint, depth);
  522.         //    }
  523.     //    public void DrawString(SpriteFont font, String s, Vector2 position, float desiredHeight, float scaleX, Color tint, float depth) {
  524.         //        if(s == null) s = "";
  525.         //        Vector2 scaling = new Vector2(desiredHeight / font.getFontHeight());
  526.         //        scaling.x *= scaleX;
  527.         //        font.Draw(this, s, position, scaling, tint, depth);
  528.         //    }
  529.  
  530.     private void SortGlyphs(int spriteSortMode) {
  531.         if(glyphs.size() < 1) return;
  532.         switch(spriteSortMode) {
  533.         case SpriteSortMode.Texture:
  534.             Collections.sort(glyphs, new Comparator<SpriteGlyph>() {
  535.                 @Override
  536.                 public int compare(SpriteGlyph o1, SpriteGlyph o2) {
  537.                     return SSMTexture(o1, o2);
  538.                 }
  539.             });
  540.             break;
  541.         case SpriteSortMode.FrontToBack:
  542.             Collections.sort(glyphs, new Comparator<SpriteGlyph>() {
  543.                 @Override
  544.                 public int compare(SpriteGlyph o1, SpriteGlyph o2) {
  545.                     return SSMFrontToBack(o1, o2);
  546.                 }
  547.             });
  548.             break;
  549.         case SpriteSortMode.BackToFront:
  550.             Collections.sort(glyphs, new Comparator<SpriteGlyph>() {
  551.                 @Override
  552.                 public int compare(SpriteGlyph o1, SpriteGlyph o2) {
  553.                     return SSMBackToFront(o1, o2);
  554.                 }
  555.             });
  556.             break;
  557.         default:
  558.             break;
  559.         }
  560.     }
  561.     private void GenerateBatches() {
  562.         if(glyphs.size() < 1) return;
  563.  
  564.         // Create Arrays
  565.         ByteBuffer bb = ByteBuffer.allocateDirect(6 * glyphs.size() * VertexSpriteBatch.Size);
  566.         SpriteBatchCall call = new SpriteBatchCall(0, glyphs.get(0).Texture, batches);
  567.         glyphs.get(0).VTL.AppendToBuffer(bb);
  568.         glyphs.get(0).VTR.AppendToBuffer(bb);
  569.         glyphs.get(0).VBL.AppendToBuffer(bb);
  570.         glyphs.get(0).VBL.AppendToBuffer(bb);
  571.         glyphs.get(0).VTR.AppendToBuffer(bb);
  572.         glyphs.get(0).VBR.AppendToBuffer(bb);
  573.  
  574.         int gc = glyphs.size();
  575.         for(int i = 1; i < gc; i++) {
  576.             SpriteGlyph glyph = glyphs.get(i);
  577.             call = call.Append(glyph, batches);
  578.             glyph.VTL.AppendToBuffer(bb);
  579.             glyph.VTR.AppendToBuffer(bb);
  580.             glyph.VBL.AppendToBuffer(bb);
  581.             glyph.VBL.AppendToBuffer(bb);
  582.             glyph.VTR.AppendToBuffer(bb);
  583.             glyph.VBR.AppendToBuffer(bb);
  584.             emptyGlyphs.add(glyphs.get(i));
  585.         }
  586.         bb.flip();
  587.         glyphs = null;
  588.  
  589.         // Set The Buffer Data
  590.         glBindBuffer(BufferTarget.ArrayBuffer, vbo);
  591.         if(gc > glyphCapacity) {
  592.             glyphCapacity = gc * 2;
  593.             glBufferData(
  594.                     BufferTarget.ArrayBuffer,
  595.                     (glyphCapacity * 6) * VertexSpriteBatch.Size,
  596.                     bufUsage
  597.                     );
  598.         }
  599.         glBufferSubData(BufferTarget.ArrayBuffer, 0, bb);
  600.         GLBuffer.Unbind(BufferTarget.ArrayBuffer);
  601.     }
  602.     public void End(int spriteSortMode) {
  603.         SortGlyphs(spriteSortMode);
  604.         GenerateBatches();
  605.     }
  606.  
  607.     public void RenderBatch(Matrix4 mWorld, Matrix4 mCamera, BlendState bs, SamplerState ss, DepthState ds, RasterizerState rs) {
  608.         // Set Up Render State
  609.         if(bs == null) bs = BlendState.PremultipliedAlphaBlend;
  610.         if(ds == null) ds = DepthState.None;
  611.         if(rs == null) rs = RasterizerState.CullNone;
  612.         if(ss == null) ss = SamplerState.LinearWrap;
  613.         bs.Set();
  614.         ds.Set();
  615.         rs.Set();
  616.  
  617.         // Setup The Program
  618.         glUseProgram(idProg);
  619.  
  620.         // Set Up The Matrices
  621.         fbUniforms.position(0);
  622.         fbUniforms.put(mWorld.m);
  623.         fbUniforms.flip();
  624.         glUniformMatrix4(unWorld, true, fbUniforms);
  625.         fbUniforms.position(0);
  626.         fbUniforms.put(mCamera.m);
  627.         fbUniforms.flip();
  628.         glUniformMatrix4(unVP, true, fbUniforms);
  629.  
  630.         glBindVertexArray(vao);
  631.         glEnableVertexAttribArray(0);
  632.         glEnableVertexAttribArray(1);
  633.         glEnableVertexAttribArray(2);
  634.         glEnableVertexAttribArray(3);
  635.  
  636.         // Draw All The Batches
  637.         int bc = batches.size();
  638.         for(int i = 0; i < bc; i++) {
  639.             SpriteBatchCall batch = batches.get(i);
  640.             //            batch.Texture.Use(TextureUnit.Texture0, unTexture);
  641.             //            ss.Set(batch.Texture.getTarget());
  642.             glDrawArrays(PrimitiveType.Triangles, batch.IndexOffset, batch.Indices);
  643.             //            batch.Texture.Unuse();
  644.         }
  645.  
  646.         //        GLProgram.Unuse();
  647.         glUseProgram(0);
  648.         glBindVertexArray(0);
  649.     }
  650. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement