Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (796)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
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: StumpyStrust (posted 2012-10-28 01:28:17, viewed 322 times)

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  
package com.atlas.engine.graphics;

import static org.lwjgl.opengl.GL11.glBindTexture;
import static org.lwjgl.opengl.GL11.glDrawArrays;

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
import org.lwjgl.util.vector.Vector4f;

import com.stumpycode.atlas.utils.FastMath;

public class GeomBatcher {
   
   private static float[] empty = new float[8];
   private static Vector4f empty1 = new Vector4f(0,0,0,0);
   
   private float[] vertArray;
   private byte[] colorArray;
   private float[] sizeArray;
   private int draws;
   private int maxDraws = 1000;
   private int vertIndex;
   private int colIndex;
   private int sizeIndex;
   private int currentTex;
   private FloatBuffer vertBuff, texBuff;
   private ByteBuffer colBuff;
   private int gPositionLocation;
   private int gSizeLocation;
   private int gColorLocation;
   private ShaderProgram geometryShader;
   
   static{
      empty[0] = 0;
      empty[1] = 0;
      empty[2] = 1;
      empty[3] = 0;
      empty[4] = 1;
      empty[5] = 1;
      empty[6] = 0;
      empty[7] = 1;
   }
   
   public GeomBatcher()
   {
      this(1000);
   }
   
   public GeomBatcher(int size)
   {
      vertArray = new float[size*2];
      vertBuff = BufferUtils.createFloatBuffer(vertArray.length);
      colorArray = new byte[size*4];
      colBuff = BufferUtils.createByteBuffer(colorArray.length);
      sizeArray = new float[size*2];
      texBuff = BufferUtils.createFloatBuffer(sizeArray.length);
      vertIndex = 0;
      colIndex = 0;
      sizeIndex = 0;
      maxDraws = size;
      draws = 0; 
      
      geometryShader = new ShaderProgram();
      geometryShader.attachShader(new Shader(35633, new File("geometry shader.vert")));
      geometryShader.attachShader(new Shader(36313, new File("geometry shader.geom")));
      geometryShader.attachShader(new Shader(35632, new File("geometry shader.frag")));
      geometryShader.link();
      
      gPositionLocation = geometryShader.getAttribLocation("position");
      gSizeLocation = geometryShader.getAttribLocation("size");
       gColorLocation = geometryShader.getAttribLocation("color");
       geometryShader.bind();
       glUniform2f(this.geometryShader.getUniformLocation("screenSize"), Display.getWidth(), Display.getHeight());
       
   }
   
   public void begin()
   {
      geometryShader.bind();
      glBindTexture(GL_TEXTURE_2D, currentTex);
   }
   
   public void end()
   {
      render();
      ShaderProgram.useFixed();
   }
   
   private int getColor(float f)
   {
      return (int) (f*255);
   }
   
   
   private void render()
   {
      vertBuff.put(vertArray);
      vertBuff.flip();
      colBuff.put(colorArray);
      colBuff.flip();
      texBuff.put(sizeArray);
      texBuff.flip();
      
        //glVertexPointer(2, 0, vertBuff);
        glVertexAttribPointer(gPositionLocation, 2, false, 0, vertBuff);
      //glColorPointer(4,true, 0, colBuff);
        glVertexAttribPointer(gSizeLocation, 2, false, 0, texBuff);
        //glTexCoordPointer(2, 0, texBuff);
        glVertexAttribPointer(gColorLocation, 4, true,false, 0, colBuff);
        
        glEnableVertexAttribArray(gPositionLocation);
        glEnableVertexAttribArray(gSizeLocation);
        glEnableVertexAttribArray(gColorLocation);
        
        glDrawArrays(GL_POINTS, 0, draws);
        vertBuff.clear();
        colBuff.clear();
        texBuff.clear();
        vertIndex = 0;
      colIndex = 0;
      sizeIndex = 0;
      draws = 0; 
   }
   
   public void setTexture(int tex)
   {
      if(tex != currentTex)
      {
         render();
         currentTex = tex; 
      }
   }
   
   public void unbind()
   {
      ShaderProgram.useFixed();
   }
   
   public void draw(int texID, float x, float y, float sizex, float sizey )
   {
      draw(texID, empty, x, y, sizex, sizey, 0, empty1);
   }

   public void draw(int texID, float x, float y, float sizex, float sizey,float rotation, Vector4f col )
   {
      draw(texID, empty, x, y, sizex, sizey, rotation, col);
   }
   
   public void draw(int texID, float[] region, float x, float y, float sizex, float sizey, float rotation, Vector4f col )
   {
      if(texID != currentTex)
      {
         render();
         glBindTexture(GL_TEXTURE_2D, currentTex);
         currentTex = texID; 
      }
      if(draws == maxDraws)
      {
         render();
      }

      final float p1x = -sizex/2;
      final float p1y = -sizey/2;
      final float p2x = sizex/2;
      final float p2y = -sizey/2;
      final float p3x = sizex/2;
      final float p3y = sizey/2;
      final float p4x = -sizex/2;
      final float p4y = sizey/2;

      float x1;
      float y1;
      float x2;
      float y2;
      float x3;
      float y3;
      float x4;
      float y4;

      // rotate
      if (rotation != 0) {
      final float cos = (float) FastMath.cosDeg(rotation);
      final float sin = (float) FastMath.sinDeg(rotation);

      x1 = cos * p1x - sin * p1y;
      y1 = sin * p1x + cos * p1y;

      x2 = cos * p2x - sin * p2y;
      y2 = sin * p2x + cos * p2y;

      x3 = cos * p3x - sin * p3y;
      y3 = sin * p3x + cos * p3y;

      x4 = cos * p4x - sin * p4y;
      y4 = sin * p4x + cos * p4y;
      } else {
      x1 = p1x;
      y1 = p1y;

      x2 = p2x;
      y2 = p2y;

      x3 = p3x;
      y3 = p3y;

      x4 = p4x;
      y4 = p4y;
      }
      x1+=x;
      x2+=x;
      x3+=x;
      x4+=x;
      y1+=y;
      y2+=y;
      y3+=y;
      y4+=y;
      
      vertArray[vertIndex] = x1+(sizex/2)/2;
      sizeArray[sizeIndex] = sizex;
      vertArray[vertIndex+1] = y1+(sizey/2)/2;
      sizeArray[sizeIndex+1] = sizey;
      
      //vertBuff.put(x1+(sizex/2)/2);
      //vertBuff.put(y1+(sizey/2)/2);
      //texBuff.put(sizex);
      //texBuff.put(sizey);
      
      colorArray[colIndex] = (byte) getColor(col.x);
      colorArray[colIndex+1] = (byte)getColor(col.y);
      colorArray[colIndex+2] = (byte) getColor(col.z);
      colorArray[colIndex+3] = (byte) getColor(col.w);
      
      //colBuff.put((byte) getColor(col.x));
      //colBuff.put((byte) getColor(col.y));
      //colBuff.put((byte) getColor(col.z));
      //colBuff.put((byte) getColor(col.w));
      
      
       vertIndex+=2;
       sizeIndex+=2;
       colIndex += 4;
      draws++; 
   }
   

}





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
 
hadezbladez (3627 views)
2018-11-16 13:46:03

hadezbladez (1292 views)
2018-11-16 13:41:33

hadezbladez (3633 views)
2018-11-16 13:35:35

hadezbladez (695 views)
2018-11-16 13:32:03

EgonOlsen (3998 views)
2018-06-10 19:43:48

EgonOlsen (4525 views)
2018-06-10 19:43:44

EgonOlsen (2686 views)
2018-06-10 19:43:20

DesertCoockie (3546 views)
2018-05-13 18:23:11

nelsongames (3699 views)
2018-04-24 18:15:36

nelsongames (4723 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
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!