Results 1 to 3 of 3
  1. #1
    3xa
    3xa is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Slow drawing to bufferedImage

    Hello,

    I am making a game in java, but I ran into a speed problem. First I thought it was slow
    because of graphics2d limitations, now I am using jogl, but the problem still persists.
    One thing I noticed that drawImage is taking too long when drawing to bufferedImage (this
    takes about ~200ms, only background, ~90 gif images).
    Video card is intel hd, but it shouldn't be a problem because I get solid 100+ fps in q3?

    Main code where drawing takes place:
    Java Code:
        private Graphics2D g2 = null;
        private GLCanvas glcanvas = null;
        
        private WritableRaster raster = null;
        private ComponentColorModel colorModel = null;
        private BufferedImage bi = null;
    			
    			
    	public Game() 
        {
            raster = Raster.createInterleavedRaster (DataBuffer.TYPE_BYTE,
                        C.SCREEN_WIDTH,
                        C.SCREEN_HEIGHT,
                        4,
                        null);
            colorModel= new ComponentColorModel (ColorSpace.getInstance(ColorSpace.CS_sRGB),
                        new int[] {8,8,8,8},
                        true,
                        false,
                        ComponentColorModel.TRANSLUCENT,
                        DataBuffer.TYPE_BYTE);
            bi = new BufferedImage (colorModel,
                        raster,
                        false,
                        null);
    
            g2 = (Graphics2D) bi.createGraphics(); 
    	}
    	
    	public void render()
        {
            glcanvas.repaint();
    	}
        
        @Override
        public void display(GLAutoDrawable gLDrawable) 
        {
            g2.setColor(Color.BLACK);
            g2.clearRect(0, 0, C.SCREEN_WIDTH, C.SCREEN_HEIGHT);
            
    		drawBackground(g2);
            
            
            GL2 gl = gLDrawable.getGL().getGL2();
            gl.glMatrixMode(GL2.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrtho(0, C.SCREEN_WIDTH, C.SCREEN_HEIGHT, 0, 0, 1);
            gl.glMatrixMode(GL2.GL_MODELVIEW);
            gl.glDisable(GL2.GL_DEPTH_TEST);
            gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  
            gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
            gl.glBlendFunc (GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA); 
            gl.glEnable (GL2.GL_BLEND);
      
            g2.drawImage(bi, null, null);
            
            DataBufferByte dukeBuf = (DataBufferByte) raster.getDataBuffer();
            byte[] dukeRGBA = dukeBuf.getData();
            ByteBuffer bb = ByteBuffer.wrap(dukeRGBA);
            bb.position(0);
            bb.mark();
            gl.glBindTexture(GL2.GL_TEXTURE_2D, 13);
            gl.glPixelStorei(GL2.GL_UNPACK_ALIGNMENT, 1);
            gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_CLAMP);
            gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_CLAMP);
            gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
            gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
            gl.glTexEnvf(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_REPLACE);
            gl.glTexImage2D (GL2.GL_TEXTURE_2D, 0, GL2.GL_RGBA, bi.getWidth(), bi.getHeight(), 0, GL2.GL_RGBA, GL2.GL_UNSIGNED_BYTE, bb);
    
            gl.glEnable(GL2.GL_TEXTURE_2D);
            gl.glBindTexture (GL2.GL_TEXTURE_2D, 13);
    
            gl.glBegin (GL2.GL_POLYGON);
            gl.glTexCoord2d (0, 0);
            gl.glVertex2d (0, 0);
            gl.glTexCoord2d(1,0);
            gl.glVertex2d (bi.getWidth(), 0);
            gl.glTexCoord2d(1,1);
            gl.glVertex2d (bi.getWidth(), bi.getHeight());
            gl.glTexCoord2d(0,1);
            gl.glVertex2d (0, bi.getHeight());
            gl.glEnd ();
            gl.glFlush();
        }
    
     
        @Override
        public void init(GLAutoDrawable gLDrawable) 
        {
        	System.out.println("init() called");
            GL2 gl = gLDrawable.getGL().getGL2();
            gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            gl.glShadeModel(GL2.GL_FLAT);
        }
     
        @Override
        public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) 
        {
        }
     
     
        @Override
    	public void dispose(GLAutoDrawable arg0) 
    	{
    	}
    	
    	public void drawBackground(Graphics2D g2)
        {
            
    		BufferedImage texture = Images.getImage(1);
    		int tWidth = texture.getWidth();
    		int tHeight = texture.getHeight();
    
    		for (int i = -100; i < C.SCREEN_HEIGHT+tWidth; i+= tHeight/2) {
    			for (int j =  -240; j < C.SCREEN_WIDTH + tWidth; j+= tWidth) {
    				g2.drawImage(texture, null, j + i%tHeight*2 - Data.getPlayer().getX()%tWidth, i - Data.getPlayer().getY()%tHeight);
    			}
            }
    
    	}

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Slow drawing to bufferedImage

    Moved from AWT/Swing.

    Java Code:
    ComponentColorModel.TRANSLUCENT,
    Painting with partial transparency is known to be slow. No idea how much JOGL can speed that up. How big is this image anyways?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    3xa
    3xa is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Slow drawing to bufferedImage

    Right. The image is 256x136, we did this to minimize calls to drawImage. Anyway, when using 64x40 pics and drawing ~100
    of them, the result is 40ms, which is still a lot. (add +~150 when drawing this bufferedImage onto image with raster and it gets even worse)

Similar Threads

  1. Replies: 0
    Last Post: 03-23-2012, 11:13 PM
  2. Problem Drawing Grid with BufferedImage.
    By Shikatsu in forum Java 2D
    Replies: 10
    Last Post: 11-26-2011, 01:01 AM
  3. Drawing BufferedImage or Graphics2D to window
    By danfoster3141 in forum Java 2D
    Replies: 6
    Last Post: 06-16-2011, 03:33 PM
  4. Slow applet
    By robs in forum Java Applets
    Replies: 20
    Last Post: 02-28-2011, 04:15 PM
  5. creating a BufferedImage from a drawing
    By chappa in forum Java 2D
    Replies: 2
    Last Post: 01-10-2010, 07:04 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •