Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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: theagentd (posted 2012-11-11 19:45:04, viewed 676 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  
package dam.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.nio.FloatBuffer;
import java.util.Random;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL32.*;
import static org.lwjgl.opengl.GL40.GL_TRANSFORM_FEEDBACK;
import static org.lwjgl.opengl.GL40.glBindTransformFeedback;
import static org.lwjgl.opengl.GL40.glDrawTransformFeedback;
import static org.lwjgl.opengl.GL40.glGenTransformFeedbacks;

public class MiniTFTest {
   
   private static final int NUM_POINTS = 100000;
   
   private int shaderProgram;
   private int positionLocation;
   
   private FloatBuffer inputData;
   private int inputVBO;
   
   private int feedbackObject;
   private int outputVBO;
   private int queryObject;
   
   public MiniTFTest(){
      initLWJGL();
      initShader();
      initTransformFeedback();
      randomizeInputData();
   }

   private void initLWJGL() {
      try{
         Display.setDisplayMode(new DisplayMode(512, 512));
         Display.create();
         Mouse.create();
      }catch(LWJGLException ex){
         ex.printStackTrace();
      }
   }

   private void initShader() {
      
      shaderProgram = glCreateProgram();
      
      int vertexShader = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(vertexShader, loadFileSource("shaders/tf/test.vert"));
      glCompileShader(vertexShader);
      glAttachShader(shaderProgram, vertexShader);
      
      int geometryShader = glCreateShader(GL_GEOMETRY_SHADER);
      glShaderSource(geometryShader, loadFileSource("shaders/tf/test.geom"));
      glCompileShader(geometryShader);
      glAttachShader(shaderProgram, geometryShader);
      
      //This line tells the shader which output attributes from the geometry shader 
      //we want to save to the transform feedback output VBO. It's an array of varying
      //names followed by an enum controlling how we want the data to be stored.
      //GL_INTER_LEAVED_ATTRIBS tells OpenGL to put them in the same VBO ordered in
      //the way specified by the array.
      //It's very important to call this BEFORE linking the program.
      glTransformFeedbackVaryings(shaderProgram, new CharSequence[]{"outPosition"}, GL_INTERLEAVED_ATTRIBS);
      
      //Note that we don't even have a fragment shader for this shader program.
      
      glLinkProgram(shaderProgram);
        String log = glGetProgramInfoLog(shaderProgram, 65536);
        if(log.length() != 0){
            System.out.println("Program link log:\n" + log);
        }
        
        //Save the input variable location
        positionLocation = glGetAttribLocation(shaderProgram, "position");
        
   }

   private void initTransformFeedback() {

      //This is the buffer we fill with random points to process.
      inputData = BufferUtils.createFloatBuffer(NUM_POINTS * 2);
      //And the VBO which we upload the data to.
      inputVBO = glGenBuffers();
      
      //This is the data in which the processed points will end up.
      //We make it big enough to fit all input points, in case all
      //of them pass. If the buffer is filled, additional data will
      //simply be discarded.
      outputVBO = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, outputVBO);
      glBufferData(GL_ARRAY_BUFFER, NUM_POINTS * 2 * 4, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
      
      //We create our transform feedback object. We then bind it and
      //tell it to store its output into outputVBO.
      feedbackObject = glGenTransformFeedbacks();
        glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, feedbackObject);
        glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, outputVBO);
        glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
        
        //We also create a query object. This object will be used to
        //query how many points that were stored in the output VBO.
        queryObject = glGenQueries();
   }

   //Fills the input data buffer with random point data,
   //with coordinates ranging from -1 to 1 (the whole screen).
   private void randomizeInputData() {
      
      
      Random r = new Random();
      
      for(int i = 0; i < NUM_POINTS; i++){
         inputData.put(r.nextFloat() * 2 - 1).put(r.nextFloat() * 2 - 1);
      }
      
      inputData.flip();
   }

   //Loads shader source code from a file.
   public static String loadFileSource(String path){
      
      File file = new File(path);
        if(!file.exists()){
            System.out.println("Unable to open file " + file.getAbsolutePath() + "!!!");
            return null;
        }

        StringBuilder source = new StringBuilder();
        String line;
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            while ((line = reader.readLine()) != null) {
                source.append(line).append('\n');
            }
            reader.close();
        } catch (Exception e) {
            System.out.println("Failed to read shader source!");
            e.printStackTrace();
            return null;
        }
        return source.toString();
    }

   public void gameloop(){
      while(!Display.isCloseRequested()){
         glClear(GL_COLOR_BUFFER_BIT);
         
         //Randomize the input points if the left mouse button is pressed.
         if(Mouse.isButtonDown(0)){
            randomizeInputData();
         }
         
         processPoints();
         renderOutput();
         
         Display.update();
      }
   }

   private void processPoints() {
      
      //Disable pixel rendering, we're doing transform feedback baby!
      glEnable(GL_RASTERIZER_DISCARD);
      
      //Bind the shader...
      glUseProgram(shaderProgram);
      
      //and then the feedback object
      glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, feedbackObject);
      glBeginTransformFeedback(GL_POINTS);
      {
         //Between glBeginTransformFeedback(GL_POINTS) and glEndTransformFeedback()
         //we can of course only draw points.
         
         //Bind and update the input data VBO.
         glBindBuffer(GL_ARRAY_BUFFER, inputVBO);
         glBufferData(GL_ARRAY_BUFFER, inputData, GL_STREAM_DRAW);
         
         //Enable our only shader input attribute.
         glEnableVertexAttribArray(positionLocation);
         glVertexAttribPointer(positionLocation, 2, GL_FLOAT, false, 0, 0);

         //Draw the points with a standard glDrawArrays() call, but wrap it in
         //a query so we can determine exactly how many points that were stored
         //in outputVBO.
         //WARNING: Querying is VERY SLOW and is only done so we can write out
         //how many points that passed to the console! It's possible to draw 
         //all points that passed without a query! See renderOutput()!
         glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, queryObject);
         glDrawArrays(GL_POINTS, 0, NUM_POINTS);
         glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
         System.out.println("Points drawn: " + glGetQueryObjecti(queryObject, GL_QUERY_RESULT));

         //Clean up after us...
         glDisableVertexAttribArray(positionLocation);
         glBindBuffer(GL_ARRAY_BUFFER, 0);
         
      }
      glEndTransformFeedback();

      glUseProgram(0);

      glDisable(GL_RASTERIZER_DISCARD);
   }

   private void renderOutput() {
      
      //Bind the outputVBO just like any other VBO
      glBindBuffer(GL_ARRAY_BUFFER, outputVBO);
      
      //We're using the fixed functionality pipeline here,
      //so just set it up to read positions from the outputVBO.
      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(2, GL_FLOAT, 0, 0);

      //glDrawTransformFeedback is a special draw call that is very similar to 
      //glDrawArrays(). It allows us to draw all points that were output by our
      //shader without having to involve the CPU to determine how many they were.
      //This is the same as glDrawArrays(GL_POINTS, 0, num_points_that_passed);,
      //but is a LOT faster than if we had used a query object to get the count
      //and then manually calling glDrawArrays() with that count.
      glDrawTransformFeedback(GL_POINTS, feedbackObject);
      
      //Clean up...
      glDisableClientState(GL_VERTEX_ARRAY);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
      
   }

   public static void main(String[] args){
      new MiniTFTest().gameloop();
   }
}





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.

BurntPizza (19 views)
2014-09-21 02:42:18

BurntPizza (14 views)
2014-09-21 01:30:30

moogie (14 views)
2014-09-21 00:26:15

UprightPath (25 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (73 views)
2014-09-10 13:57:51

TehJavaDev (96 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

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

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

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!