Advertisement
codeanticode

Shader test - animator

May 9th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.23 KB | None | 0 0
  1. package p5.awt.shadertest;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.EventQueue;
  5. import java.awt.Frame;
  6. import java.awt.event.WindowAdapter;
  7. import java.awt.event.WindowEvent;
  8. import java.lang.reflect.InvocationTargetException;
  9. import java.nio.FloatBuffer;
  10.  
  11. import javax.media.opengl.GL;
  12. import javax.media.opengl.GL2ES2;
  13. import javax.media.opengl.GLAnimatorControl;
  14. import javax.media.opengl.GLAutoDrawable;
  15. import javax.media.opengl.GLCapabilities;
  16. import javax.media.opengl.GLEventListener;
  17. import javax.media.opengl.GLProfile;
  18. import javax.media.opengl.GLUniformData;
  19. import javax.media.opengl.awt.GLCanvas;
  20.  
  21. import com.jogamp.opengl.util.Animator;
  22. import com.jogamp.opengl.util.GLArrayDataServer;
  23. import com.jogamp.opengl.util.glsl.ShaderCode;
  24. import com.jogamp.opengl.util.glsl.ShaderProgram;
  25. import com.jogamp.opengl.util.glsl.ShaderState;
  26.  
  27. public class ShaderTest {
  28.   private int width = 500;
  29.   private int height = 290;
  30.  
  31.   private ShaderCode vertShader;
  32.   private ShaderCode fragShader;
  33.   private ShaderProgram shaderProg;
  34.   private ShaderState shaderState;
  35.   private GLUniformData resolution;
  36.   private GLUniformData time;  
  37.   private GLArrayDataServer vertices;
  38.  
  39.   private long millisInit;
  40.  
  41.   private Frame frame;
  42.   private GLCanvas canvas;
  43.  
  44.   private int fcount, lastm;  
  45.   private int fint = 3;
  46.   private float frameRate;
  47.  
  48.   void setup(GL2ES2 gl) {
  49.     vertShader = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, this.getClass(), "shaders",
  50.         "shaders/bin", "landscape", true);
  51.     fragShader = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, this.getClass(), "shaders",
  52.         "shaders/bin", "landscape", true);
  53.     shaderProg = new ShaderProgram();
  54.     shaderProg.add(gl, vertShader, System.err);
  55.     shaderProg.add(gl, fragShader, System.err);
  56.    
  57.     shaderState = new ShaderState();
  58.     shaderState.attachShaderProgram(gl, shaderProg, true);
  59.    
  60.     resolution = new GLUniformData("iResolution", 3, FloatBuffer.wrap(new float[] {width, height, 0}));
  61.     shaderState.ownUniform(resolution);
  62.     shaderState.uniform(gl, resolution);    
  63.    
  64.     time = new GLUniformData("iGlobalTime", 0.0f);
  65.     shaderState.ownUniform(time);
  66.        
  67.     vertices = GLArrayDataServer.createGLSL("inVertex", 2, GL.GL_FLOAT, false, 4, GL.GL_STATIC_DRAW);
  68.     vertices.putf(-1.0f); vertices.putf(-1.0f);
  69.     vertices.putf(+1.0f); vertices.putf(-1.0f);
  70.     vertices.putf(-1.0f); vertices.putf(+1.0f);
  71.     vertices.putf(+1.0f); vertices.putf(+1.0f);
  72.     vertices.seal(gl, true);
  73.     shaderState.ownAttribute(vertices, true);
  74.    
  75.     millisInit = System.currentTimeMillis();
  76.   }
  77.  
  78.   void draw(GL2ES2 gl) {    
  79.     frame.setTitle("AWT Shader Test - fps: " + frameRate);
  80.    
  81.     gl.glClearColor(0, 0, 0, 1);
  82.     gl.glClear(GL2ES2.GL_COLOR_BUFFER_BIT);
  83.    
  84.     shaderState.useProgram(gl, true);    
  85.    
  86.     time.setData((System.currentTimeMillis() - millisInit) / 1000.0f);
  87.     shaderState.uniform(gl, time);
  88.     vertices.enableBuffer(gl, true);
  89.     gl.glDrawArrays(GL2ES2.GL_TRIANGLE_STRIP, 0, 4);
  90.     vertices.enableBuffer(gl, false);
  91.    
  92.     shaderState.useProgram(gl, false);
  93.    
  94.     fcount += 1;
  95.     int m = (int) (System.currentTimeMillis() - millisInit);
  96.     if (m - lastm > 1000 * fint) {
  97.       frameRate = (float)(fcount) / fint;
  98.       fcount = 0;
  99.       lastm = m;
  100.     }      
  101.   }
  102.  
  103.   class TestGLListener implements GLEventListener {
  104.     public void display(GLAutoDrawable drawable) {
  105.       draw(drawable.getGL().getGL2ES2());
  106.     }
  107.     public void dispose(GLAutoDrawable drawable) { }
  108.     public void init(GLAutoDrawable drawable) {
  109.       setup(drawable.getGL().getGL2ES2());
  110.     }
  111.     public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) { }    
  112.   }
  113.  
  114.   public void run() throws InterruptedException, InvocationTargetException {
  115.     frame = new Frame("AWT Shader Test");
  116.     frame.setLocation(100, 100);
  117.     frame.setSize(width, height);    
  118.    
  119.     GLProfile profile = GLProfile.getDefault();
  120.     GLCapabilities capabilities = new GLCapabilities(profile);
  121.    
  122.     canvas = new GLCanvas(capabilities);
  123.     canvas.setBounds(0, 0, width, height);
  124.     canvas.setFocusable(true);    
  125.    
  126.     frame.setLayout(new BorderLayout());
  127.     frame.add(canvas, BorderLayout.CENTER);
  128.    
  129.     TestGLListener glListener = new TestGLListener();
  130.     canvas.addGLEventListener(glListener);    
  131.     final GLAnimatorControl animator = new Animator(canvas);
  132.     animator.start();
  133.    
  134.     frame.addWindowListener(new WindowAdapter() {
  135.       public void windowClosing(WindowEvent e) {
  136.         animator.stop();
  137.         System.exit(0);
  138.       }
  139.     });
  140.    
  141.     EventQueue.invokeAndWait(new Runnable() {
  142.       public void run() {
  143.         frame.validate();                
  144.         frame.setVisible(true);
  145.     }});
  146.   }
  147.  
  148.   public static void main(String[] args) {
  149.     ShaderTest test;
  150.     try {
  151.       Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(ShaderTest.class.getName());
  152.       test = (ShaderTest) c.newInstance();
  153.     } catch (Exception e) {
  154.       throw new RuntimeException(e);
  155.     }    
  156.     if (test != null) {
  157.       try {
  158.         test.run();
  159.       } catch (Exception e) {
  160.         e.printStackTrace();
  161.       }
  162.     }
  163.   }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement