Advertisement
codeanticode

Shader test - manual

May 9th, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.55 KB | None | 0 0
  1. package p5.awt.papplet;
  2.  
  3. import java.applet.Applet;
  4. import java.awt.BorderLayout;
  5. import java.awt.Frame;
  6. import java.awt.GraphicsDevice;
  7. import java.awt.GraphicsEnvironment;
  8. import java.awt.Insets;
  9. import java.awt.event.WindowAdapter;
  10. import java.awt.event.WindowEvent;
  11. import java.nio.FloatBuffer;
  12.  
  13. import javax.media.opengl.*;
  14. import javax.media.opengl.awt.GLCanvas;
  15.  
  16. import com.jogamp.opengl.util.GLArrayDataServer;
  17. import com.jogamp.opengl.util.glsl.ShaderCode;
  18. import com.jogamp.opengl.util.glsl.ShaderProgram;
  19. import com.jogamp.opengl.util.glsl.ShaderState;
  20.  
  21. // Sample program that relies on JOGL's mechanism to handle the OpenGL context  
  22. // and rendering loop when using an AWT canvas attached to an Applet.
  23. public class MiniPApplet extends Applet {
  24.   private static final long serialVersionUID = 1L;
  25.  
  26.   /////////////////////////////////////////////////////////////
  27.   //
  28.   // Test parameters  
  29.  
  30.   public int width               = 500;
  31.   public int height              = 290;    
  32.   public int targetFrameRate     = 60;
  33.   public int numSamples          = 2;
  34.  
  35.   /////////////////////////////////////////////////////////////
  36.   //
  37.   // Internal variables
  38.  
  39.   private Frame frame;
  40.   private GLProfile profile;
  41.   private GLCapabilities capabilities;
  42.   private GLCanvas canvas;
  43.  
  44.   private SimpleListener listener;
  45.  
  46.   private long beforeTime;
  47.   private long overSleepTime;
  48.   private long frameRatePeriod = 1000000000L / targetFrameRate;
  49.  
  50.   private ShaderCode vertShader;
  51.   private ShaderCode fragShader;
  52.   private ShaderProgram shaderProg;
  53.   private ShaderState shaderState;
  54.   private GLUniformData resolution;
  55.   private GLUniformData time;  
  56.   private GLArrayDataServer vertices;  
  57.  
  58.   private float currentFrameRate;
  59.  
  60.   private boolean initialized = false;    
  61.   private long millisOffset;
  62.  
  63.   private boolean runSetup = false;
  64.  
  65.   private int fcount, lastm;  
  66.   private int fint = 3;  
  67.    
  68.   void run() {
  69.     Thread loop = new Thread("Animation Thread") {
  70.       public void run() {        
  71.         while (true) {
  72.           if (!initialized) {
  73.             initialize();
  74.           }
  75.           canvas.display();                      
  76.           clock();
  77.         }
  78.       }
  79.     };
  80.     loop.start();        
  81.   }
  82.  
  83.   void initialize() {
  84.     millisOffset = System.currentTimeMillis();    
  85.  
  86.     // Frame setup ----------------------------------------------------------    
  87.     GraphicsEnvironment environment =
  88.       GraphicsEnvironment.getLocalGraphicsEnvironment();
  89.     GraphicsDevice displayDevice = environment.getDefaultScreenDevice();
  90.     frame = new Frame(displayDevice.getDefaultConfiguration());
  91.    
  92.     frame.setTitle("MiniPApplet");
  93.     frame.setLayout(null);
  94.     frame.add(this);
  95.     frame.pack();
  96.    
  97.     Insets insets = frame.getInsets();
  98.  
  99.     int windowW = width + insets.left + insets.right;
  100.     int windowH = height + insets.top + insets.bottom;
  101.     int locationX = 100;
  102.     int locationY = 100;
  103.    
  104.     frame.setSize(windowW, windowH);
  105.     frame.setLocation(locationX, locationY);
  106.    
  107.     int usableWindowH = windowH - insets.top - insets.bottom;
  108.     setBounds((windowW - width)/2, insets.top + (usableWindowH - height)/2, width, height);      
  109.    
  110.     frame.add(this);
  111.     frame.addWindowListener(new WindowAdapter() {
  112.       public void windowClosing(WindowEvent e) {
  113.           System.exit(0);
  114.       }
  115.     });    
  116.    
  117.     frame.setVisible(true);
  118.  
  119.     // Canvas setup ----------------------------------------------------------
  120.    
  121.     profile = GLProfile.getDefault();
  122.     capabilities = new GLCapabilities(profile);
  123.     capabilities.setSampleBuffers(true);
  124.     capabilities.setNumSamples(numSamples);
  125.     capabilities.setDepthBits(24);
  126.     capabilities.setStencilBits(8);
  127.     capabilities.setAlphaBits(8);
  128.    
  129.     canvas = new GLCanvas(capabilities);
  130.     canvas.setBounds(0, 0, width, height);
  131.        
  132.     this.setLayout(new BorderLayout());
  133.     this.add(canvas, BorderLayout.CENTER);
  134.    
  135.     // Setting up animation
  136.     listener = new SimpleListener();
  137.     canvas.addGLEventListener(listener);
  138.      
  139.     initialized = true;    
  140.   }
  141.  
  142.   void setup(GL2ES2 gl) {
  143.     vertShader = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shaders",
  144.         "shaders/bin", "landscape", true);
  145.     fragShader = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shaders",
  146.         "shaders/bin", "landscape", true);
  147.     shaderProg = new ShaderProgram();
  148.     shaderProg.add(gl, vertShader, System.err);
  149.     shaderProg.add(gl, fragShader, System.err);
  150.    
  151.     shaderState = new ShaderState();
  152.     shaderState.attachShaderProgram(gl, shaderProg, true);
  153.    
  154.     resolution = new GLUniformData("iResolution", 3, FloatBuffer.wrap(new float[] {width, height, 0}));
  155.     shaderState.ownUniform(resolution);
  156.     shaderState.uniform(gl, resolution);    
  157.    
  158.     time = new GLUniformData("iGlobalTime", 0.0f);
  159.     shaderState.ownUniform(time);
  160.        
  161.     vertices = GLArrayDataServer.createGLSL("inVertex", 2, GL.GL_FLOAT, false, 4, GL.GL_STATIC_DRAW);
  162.     vertices.putf(-1.0f); vertices.putf(-1.0f);
  163.     vertices.putf(+1.0f); vertices.putf(-1.0f);
  164.     vertices.putf(-1.0f); vertices.putf(+1.0f);
  165.     vertices.putf(+1.0f); vertices.putf(+1.0f);
  166.     vertices.seal(gl, true);
  167.     shaderState.ownAttribute(vertices, true);
  168.    
  169.     runSetup = true;
  170.   }
  171.  
  172.   void draw(GL2ES2 gl) {
  173.     frame.setTitle("MiniPApplet - fps: " + currentFrameRate);
  174.    
  175.     gl.glClearColor(0, 0, 0, 1);
  176.     gl.glClear(GL2ES2.GL_COLOR_BUFFER_BIT);
  177.    
  178.     shaderState.useProgram(gl, true);    
  179.    
  180.     time.setData((System.currentTimeMillis() - millisOffset) / 1000.0f);
  181.     shaderState.uniform(gl, time);
  182.     vertices.enableBuffer(gl, true);
  183.     gl.glDrawArrays(GL2ES2.GL_TRIANGLE_STRIP, 0, 4);
  184.     vertices.enableBuffer(gl, false);
  185.    
  186.     shaderState.useProgram(gl, false);
  187.    
  188.     fcount += 1;
  189.     int m = (int) (System.currentTimeMillis() - millisOffset);
  190.     if (m - lastm > 1000 * fint) {
  191.       currentFrameRate = (float)(fcount) / fint;
  192.       fcount = 0;
  193.       lastm = m;
  194.     }    
  195.   }
  196.  
  197.   void clock() {
  198.     long afterTime = System.nanoTime();
  199.     long timeDiff = afterTime - beforeTime;
  200.     long sleepTime = (frameRatePeriod - timeDiff) - overSleepTime;
  201.  
  202.     if (sleepTime > 0) {  // some time left in this cycle
  203.       try {
  204.         Thread.sleep(sleepTime / 1000000L, (int) (sleepTime % 1000000L));
  205.       } catch (InterruptedException ex) { }
  206.  
  207.       overSleepTime = (System.nanoTime() - afterTime) - sleepTime;
  208.  
  209.     } else {    // sleepTime <= 0; the frame took longer than the period
  210.       overSleepTime = 0L;
  211.     }
  212.  
  213.     beforeTime = System.nanoTime();    
  214.   }  
  215.  
  216.   class SimpleListener implements GLEventListener {
  217.     @Override
  218.     public void display(GLAutoDrawable drawable) {
  219.       if (runSetup) {
  220.         draw(drawable.getGL().getGL2ES2());              
  221.       } else {
  222.         setup(drawable.getGL().getGL2ES2());
  223.       }
  224.     }
  225.  
  226.     @Override
  227.     public void dispose(GLAutoDrawable drawable) { }
  228.  
  229.     @Override
  230.     public void init(GLAutoDrawable drawable) { }
  231.  
  232.     @Override
  233.     public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) { }    
  234.   }
  235.  
  236.   public static void main(String[] args) {
  237.     MiniPApplet mini;
  238.     try {
  239.       Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(MiniPApplet.class.getName());
  240.       mini = (MiniPApplet) c.newInstance();
  241.     } catch (Exception e) {
  242.       throw new RuntimeException(e);
  243.     }    
  244.     if (mini != null) {
  245.       mini.run();
  246.     }
  247.   }        
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement