Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.geotoolkit.pending.demo;
- import com.jogamp.common.nio.Buffers;
- import com.jogamp.opengl.util.texture.Texture;
- import com.jogamp.opengl.util.texture.TextureData;
- import com.jogamp.opengl.util.texture.awt.AWTTextureData;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.image.BufferedImage;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.atomic.AtomicInteger;
- import javax.media.nativewindow.AbstractGraphicsDevice;
- import javax.media.opengl.GL;
- import javax.media.opengl.GL2;
- import javax.media.opengl.GLAutoDrawable;
- import javax.media.opengl.GLCapabilities;
- import javax.media.opengl.GLContext;
- import javax.media.opengl.GLDrawable;
- import javax.media.opengl.GLDrawableFactory;
- import javax.media.opengl.GLEventListener;
- import javax.media.opengl.GLException;
- import javax.media.opengl.GLProfile;
- import javax.media.opengl.awt.GLJPanel;
- import javax.swing.JFrame;
- /**
- *
- * @author Johann Sorel (Geomatys)
- */
- public class JOGLParallalLoading implements GLEventListener{
- private static final int NB_TILES = 30;
- private final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
- private final List<Tile> tiles = Collections.synchronizedList(new ArrayList<Tile>());
- private GL loaderGL;
- public JOGLParallalLoading() {
- final GLProfile glp = GLProfile.getDefault();
- final GLCapabilities caps = new GLCapabilities(glp);
- caps.setDoubleBuffered(true);
- final GLJPanel glCanvas = new GLJPanel();
- final JFrame frame = new JFrame();
- frame.setContentPane(glCanvas);
- glCanvas.addGLEventListener(this);
- frame.setSize(640, 480);
- frame.setVisible(true);
- }
- private void loadParallal(GL2 gl){
- loaderGL = createLoaderGL(gl);
- for(int i=0;i<NB_TILES;i++){
- executor.submit(new Runnable() {
- @Override
- public void run() {
- final BufferedImage image = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
- final Color c = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
- final Graphics2D g = image.createGraphics();
- g.setPaint(c);
- g.fillRect(0, 0, 256, 256);
- final Tile tile = new Tile();
- tile.vertices = Buffers.newDirectFloatBuffer(new float[]{
- -1,-1,0,
- -1,+1,0,
- +1,+1,0,
- +1,-1,0
- });
- tile.uv = Buffers.newDirectFloatBuffer(new float[]{
- -1,-1,
- -1,+1,
- +1,+1,
- +1,-1
- });
- tile.indices = Buffers.newDirectIntBuffer(new int[]{
- 0,1,2,
- 2,3,0
- });
- try{
- synchronized(loaderGL){
- final GLProfile profile = loaderGL.getGLProfile();
- final TextureData data = new AWTTextureData(profile, 0, 0, false, image);
- tile.texture = new Texture(loaderGL, data);
- }
- }catch(Exception ex){
- ex.printStackTrace();
- }
- }
- });
- }
- }
- @Override
- public void init(GLAutoDrawable gLDrawable) {
- final GL2 gl = gLDrawable.getGL().getGL2();
- gl.glEnable(GL.GL_DEPTH_TEST);
- gl.glEnable(GL.GL_CULL_FACE);
- gl.glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
- gl.glDepthFunc(GL.GL_LEQUAL);
- gl.glClearDepth(Float.MAX_VALUE);
- gl.glCullFace(GL.GL_BACK);
- //start loading datas in parallal
- loadParallal(gl);
- }
- @Override
- public void dispose(GLAutoDrawable gLDrawable) {
- }
- @Override
- public void display(GLAutoDrawable gLDrawable) {
- final GL2 gl = gLDrawable.getGL().getGL2();
- gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
- final Tile[] ts = tiles.toArray(new Tile[0]);
- for(int i=0;i<ts.length;i++){
- final Tile tile = ts[i];
- gl.glPolygonMode(GL2.GL_FRONT, GL2.GL_FILL);
- gl.glEnable(GL.GL_TEXTURE_2D);
- tile.texture.bind(gl);
- tile.texture.enable(gl);
- gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
- gl.glVertexPointer(3, GL.GL_FLOAT, 0, tile.vertices.position(0));
- gl.glEnableClientState(GL2.GL_TEXTURE_COORD_ARRAY);
- gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, tile.uv.position(0));
- gl.glDrawElements(GL2.GL_TRIANGLES, 6, GL2.GL_UNSIGNED_INT, tile.indices.position(0));
- gl.glDisable(GL.GL_TEXTURE_2D);
- }
- }
- @Override
- public void reshape(GLAutoDrawable gLDrawable, int i, int i1, int i2, int i3) {
- }
- private GL createLoaderGL(GL gl) {
- final GLContext baseContext = gl.getContext();
- final boolean isCurrent = baseContext.isCurrent();
- final GLProfile profile = gl.getGLProfile();
- final GLDrawableFactory factory = GLDrawableFactory.getFactory(profile);
- final AbstractGraphicsDevice device = baseContext.getGLDrawable().getNativeSurface().getGraphicsConfiguration().getScreen().getDevice();
- final GLDrawable loaderDrawable = factory.createDummyDrawable(device, true, profile);
- loaderDrawable.setRealized(true);
- final GLContext loaderContext = loaderDrawable.createContext(baseContext);
- makeCurrent(loaderContext);
- if(isCurrent) {
- makeCurrent(baseContext);
- }else{
- loaderContext.release();
- }
- return loaderContext.getGL();
- }
- private void makeCurrent(GLContext ctx) {
- if( GLContext.CONTEXT_NOT_CURRENT >= ctx.makeCurrent() ) {
- throw new GLException("Couldn't make ctx current: "+ctx);
- }
- }
- public static void main(String[] args) {
- new JOGLParallalLoading();
- }
- private static class Tile{
- Texture texture;
- FloatBuffer vertices;
- FloatBuffer uv;
- IntBuffer indices;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement