Creating a Custom Orthographic Projection With Pyglet

February 21st, 2012

Filed under: Game Development, OpenGL | Be the first to comment!

When you create a pyglet window, pyglet sets up an OpenGL orthographic projection for the window. The size of the projection matches the size of the window. If the window is 640 pixels wide and 480 pixels wide, the orthographic projection is 640 units wide and 480 units high. Pyglet’s default behavior works well in many cases, but there are situations where you may want to create an orthographic projection with different dimensions. If you’re writing a tile-based game, it is convenient to size the projection in tiles instead of pixels. How do you create a custom orthographic projection?

Override pyglet’s on_resize() method for the window and make a call to glOrtho(). The following code provides an example of creating a custom-sized orthographic projection:

@window.event

def on_resize(width, height):
	glViewport(0, 0, width, height)
	glMatrixMode(gl.GL_PROJECTION)
	glLoadIdentity()

	projectionWidth = 24
	projectionHeight = 16
	glOrtho(0, projectionWidth, 0, projectionHeight, -1, 1)
	glMatrixMode(gl.GL_MODELVIEW)
	glLoadIdentity()
	return pyglet.event.EVENT_HANDLED

My example copies what the default on_resize() method does, but I supply a custom width and height to glOrtho().

Facebooktwittergoogle_plusredditmail

Tags:


Leave a Reply

Your email address will not be published. Required fields are marked *