Advertisement
Guest User

Untitled

a guest
Oct 24th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.56 KB | None | 0 0
  1.  
  2.  
  3. package org.geotoolkit.pending.demo;
  4.  
  5. import com.jogamp.common.nio.Buffers;
  6. import com.jogamp.opengl.util.texture.Texture;
  7. import com.jogamp.opengl.util.texture.TextureData;
  8. import com.jogamp.opengl.util.texture.awt.AWTTextureData;
  9. import java.awt.Color;
  10. import java.awt.Graphics2D;
  11. import java.awt.image.BufferedImage;
  12. import java.nio.FloatBuffer;
  13. import java.nio.IntBuffer;
  14. import java.util.ArrayList;
  15. import java.util.Collections;
  16. import java.util.List;
  17. import java.util.concurrent.ExecutorService;
  18. import java.util.concurrent.Executors;
  19. import java.util.concurrent.atomic.AtomicInteger;
  20. import javax.media.nativewindow.AbstractGraphicsDevice;
  21. import javax.media.opengl.GL;
  22. import javax.media.opengl.GL2;
  23. import javax.media.opengl.GLAutoDrawable;
  24. import javax.media.opengl.GLCapabilities;
  25. import javax.media.opengl.GLContext;
  26. import javax.media.opengl.GLDrawable;
  27. import javax.media.opengl.GLDrawableFactory;
  28. import javax.media.opengl.GLEventListener;
  29. import javax.media.opengl.GLException;
  30. import javax.media.opengl.GLProfile;
  31. import javax.media.opengl.awt.GLJPanel;
  32. import javax.swing.JFrame;
  33.  
  34. /**
  35.  *
  36.  * @author Johann Sorel (Geomatys)
  37.  */
  38. public class JOGLParallalLoading implements GLEventListener{
  39.  
  40.     private static final int NB_TILES = 30;
  41.  
  42.     private final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
  43.     private final List<Tile> tiles = Collections.synchronizedList(new ArrayList<Tile>());
  44.     private GL loaderGL;
  45.  
  46.     public JOGLParallalLoading() {
  47.  
  48.         final GLProfile glp = GLProfile.getDefault();
  49.         final GLCapabilities caps = new GLCapabilities(glp);
  50.     caps.setDoubleBuffered(true);
  51.  
  52.         final GLJPanel glCanvas = new GLJPanel();
  53.         final JFrame frame = new JFrame();
  54.         frame.setContentPane(glCanvas);
  55.  
  56.         glCanvas.addGLEventListener(this);
  57.  
  58.         frame.setSize(640, 480);
  59.         frame.setVisible(true);
  60.     }
  61.  
  62.     private void loadParallal(GL2 gl){
  63.         loaderGL = createLoaderGL(gl);
  64.  
  65.         for(int i=0;i<NB_TILES;i++){
  66.             executor.submit(new Runnable() {
  67.                 @Override
  68.                 public void run() {
  69.                     final BufferedImage image = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
  70.                     final Color c = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
  71.                     final Graphics2D g = image.createGraphics();
  72.                     g.setPaint(c);
  73.                     g.fillRect(0, 0, 256, 256);
  74.  
  75.                     final Tile tile = new Tile();
  76.                     tile.vertices = Buffers.newDirectFloatBuffer(new float[]{
  77.                         -1,-1,0,
  78.                         -1,+1,0,
  79.                         +1,+1,0,
  80.                         +1,-1,0
  81.                     });
  82.                     tile.uv = Buffers.newDirectFloatBuffer(new float[]{
  83.                         -1,-1,
  84.                         -1,+1,
  85.                         +1,+1,
  86.                         +1,-1
  87.                     });
  88.                     tile.indices = Buffers.newDirectIntBuffer(new int[]{
  89.                         0,1,2,
  90.                         2,3,0
  91.                     });
  92.  
  93.                     try{
  94.                         synchronized(loaderGL){
  95.                             final GLProfile profile = loaderGL.getGLProfile();
  96.                             final TextureData data = new AWTTextureData(profile, 0, 0, false, image);
  97.                             tile.texture = new Texture(loaderGL, data);
  98.                         }
  99.                     }catch(Exception ex){
  100.                         ex.printStackTrace();
  101.                     }
  102.  
  103.                 }
  104.             });
  105.         }
  106.  
  107.     }
  108.  
  109.  
  110.     @Override
  111.     public void init(GLAutoDrawable gLDrawable) {
  112.  
  113.         final GL2 gl = gLDrawable.getGL().getGL2();
  114.         gl.glEnable(GL.GL_DEPTH_TEST);
  115.         gl.glEnable(GL.GL_CULL_FACE);
  116.         gl.glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
  117.         gl.glDepthFunc(GL.GL_LEQUAL);
  118.         gl.glClearDepth(Float.MAX_VALUE);
  119.         gl.glCullFace(GL.GL_BACK);
  120.  
  121.         //start loading datas in parallal
  122.         loadParallal(gl);
  123.     }
  124.  
  125.     @Override
  126.     public void dispose(GLAutoDrawable gLDrawable) {
  127.     }
  128.  
  129.     @Override
  130.     public void display(GLAutoDrawable gLDrawable) {
  131.         final GL2 gl = gLDrawable.getGL().getGL2();
  132.         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  133.  
  134.  
  135.         final Tile[] ts = tiles.toArray(new Tile[0]);
  136.  
  137.         for(int i=0;i<ts.length;i++){
  138.             final Tile tile = ts[i];
  139.             gl.glPolygonMode(GL2.GL_FRONT, GL2.GL_FILL);
  140.             gl.glEnable(GL.GL_TEXTURE_2D);
  141.             tile.texture.bind(gl);
  142.             tile.texture.enable(gl);
  143.             gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
  144.             gl.glVertexPointer(3, GL.GL_FLOAT, 0, tile.vertices.position(0));
  145.             gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
  146.             gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, tile.uv.position(0));
  147.             gl.glDrawElements(GL2.GL_TRIANGLES, 6, GL2.GL_UNSIGNED_INT, tile.indices.position(0));
  148.             gl.glDisable(GL.GL_TEXTURE_2D);
  149.         }
  150.  
  151.     }
  152.  
  153.     @Override
  154.     public void reshape(GLAutoDrawable gLDrawable, int i, int i1, int i2, int i3) {
  155.     }
  156.  
  157.  
  158.     private GL createLoaderGL(GL gl) {
  159.         final GLContext baseContext = gl.getContext();
  160.         final boolean isCurrent = baseContext.isCurrent();
  161.         final GLProfile profile = gl.getGLProfile();
  162.         final GLDrawableFactory factory = GLDrawableFactory.getFactory(profile);
  163.         final AbstractGraphicsDevice device = baseContext.getGLDrawable().getNativeSurface().getGraphicsConfiguration().getScreen().getDevice();
  164.         final GLDrawable loaderDrawable = factory.createDummyDrawable(device, true, profile);
  165.         loaderDrawable.setRealized(true);
  166.         final GLContext loaderContext = loaderDrawable.createContext(baseContext);
  167.  
  168.         makeCurrent(loaderContext);
  169.         if(isCurrent) {
  170.             makeCurrent(baseContext);
  171.         }else{
  172.             loaderContext.release();
  173.         }
  174.         return loaderContext.getGL();
  175.     }
  176.  
  177.     private void makeCurrent(GLContext ctx) {
  178.         if( GLContext.CONTEXT_NOT_CURRENT >= ctx.makeCurrent() ) {
  179.             throw new GLException("Couldn't make ctx current: "+ctx);
  180.         }
  181.     }
  182.  
  183.     public static void main(String[] args) {
  184.         new JOGLParallalLoading();
  185.     }
  186.  
  187.     private static class Tile{
  188.         Texture texture;
  189.         FloatBuffer vertices;
  190.         FloatBuffer uv;
  191.         IntBuffer indices;
  192.     }
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement