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:
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);
}
}
}
Re: Slow drawing to bufferedImage
Moved from AWT/Swing.
Quote:
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
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)