Tile rendering

I’ve just spent the last few hours java-fying this tile rendering library, and I thought I’d share it with everyone here.

I’m using it in conjunction with this fast screenshot code to generate high-res images for print.

Like so:


try
{
	// allocate the buffer
	RandomAccessFile out = new RandomAccessFile( imageFile, "rw" );
	FileChannel ch = out.getChannel();
	int fileLength = TARGA_HEADER_SIZE + imageSize.width * imageSize.height * 3;
	out.setLength( fileLength );
	MappedByteBuffer image = ch.map( FileChannel.MapMode.READ_WRITE, 0, fileLength );

	// write the TARGA header
	image.put( 0, ( byte ) 0 ).put( 1, ( byte ) 0 );
	image.put( 2, ( byte ) 2 ); // uncompressed type
	image.put( 12, ( byte ) ( imageSize.width & 0xFF ) ); // width
	image.put( 13, ( byte ) ( imageSize.width >> 8 ) ); // width
	image.put( 14, ( byte ) ( imageSize.height & 0xFF ) ); // height
	image.put( 15, ( byte ) ( imageSize.height >> 8 ) ); // height
	image.put( 16, ( byte ) 24 ); // pixel size

	// go to image data position
	image.position( TARGA_HEADER_SIZE );
		
	// jogl needs a sliced buffer
	ByteBuffer bgr = image.slice();

	// setup the tile rendering
	TileRender tr = new TileRender();

	tr.setTileSize( tileSize.width, tileSize.height, 0 );
	tr.setImageSize( imageSize.width, imageSize.height );
	tr.setImageBuffer( GL.GL_BGR, GL.GL_UNSIGNED_BYTE, bgr );
	tr.trPerspective( fieldOfView, aspectRatio, nearPlane, farPlane );

	// do the rendering
	do
	{
		tr.beginTile( gl );

                // put your rendering here, but don't touch the projection
                // matrix stack

	}
	while( tr.endTile( gl );

	// close the file channel
	ch.close();
}	
catch( Exception e )
{
	e.printStackTrace();
}

Have fun!

So I guess it would be more helpful if I actually attached the code, huh? ::slight_smile:

edit: how peculiar, a Java forum where the software doesn’t allow attaching .java files :-\

Cool, thanks a bunch - that’s exactly what I need.

So the next problem - I didn’t write the positioning code. The tiling is working, since if I leave the gluPerspective() call in the drawing code, I get the same image repeated in all of the left column squares (other squares are blank).

If I remove the gluPerspective() call from the drawing code and place a trPerspective() before the tile loop, it retreives a completely blank image (the object probably isn’t in view). I’m guessing the problem is a call to gluLookAt() in the drawing code.

Can anyone offer guidance on this? I’ll try to understand what these operations are doing in the meantime.