Using OpenGL for tiles

Moderator
Posts: 508
Joined: 2002.09
Post: #1
Hey,

Quick question here. I'm working on a little experiment that consists of having an image that is then sliced in smaller images. I then use the smaller images to move them around, like in my game HeadBreaker.

I was wondering if doing this kind of drawing would be better (read: faster) if it was done in OpenGL, instead of Quartz. However, if I do this in OpenGL, I suppose I'll have trouble with the camera position, as I want the whole image to be shown in the entire window (I don't want black spaces).
The coordinates in OpenGL are not in pixels, so I don't know how to draw all the tiles.

I'm using Cocoa and so for the textures I would just use NSImages in an array, and then draw by using a double for loop. I have sample code for turning NSImages to OpenGL Textures.

Obviously, doing this stuff in OpenGL has a lot of benefits, like doing stuff in 3D and getting hardware acceleration.

"When you dream, there are no rules..."
Quote this message in a reply
pkraft
Unregistered
 
Post: #2
I am doing 2D stuff in OpenGL and there is no problem with positioning images. You define an orthographic projection matrix to make sure everything is nice and square. If you are using texture coordinates less than the full size of a texture I would presume you can just make them integers and let OpenGL work out the rest. Also apparently you may need to at 0.5 to each coordinate since a pixel is really at the center of a pixel. Drawing an image from 0,0 through to 9,9 and then another from 10,0 to 19,9 shouldn't have any gaps.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #3
OpenGL is not just for 3D (look at the OS X window manager.)
Read the gluOrtho2D manpage to learn how to set up a 2D projection.

For texture coordinates you always use [0.0..1.0] except for TEXTURE_RECTANGLE targets, which use [0..n]. You should use rectangle textures when the hardware supports them (the Rage128 does not.)

pkraft is incorrect about pixel coordinates. In GL, fragments are located by their lower left corners, NOT their centers. So you need to use a 0.5 offset when drawing points or lines. But not when drawing filled primitives (tris, quads, fans, strips, polys.) Just draw on integral pixel boundaries then. If you are using antialiasing you should read the GL spec carefully to understand how the rasterization handles sub pixel precision.
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #4
So basically I can just set gluOrtho2D to be the size of the view and the do my drawing like I always would?

For example, if my view is 100x100 (which is too small, but it's for calculating fast Smile) I can say: gluOrtho2D(0, 0, 100, 100).
And then to draw the tiles (10 columns and 10 rows). I would do:

Code:
glTexCoord2f (col*colWidth, row*colHeight);
glVertex2f (col*colWidth, row*colHeight);

where row and col would be variables in my double for loop ranging from 0 to 9 and colWidth and colHeight would both be 10.

"When you dream, there are no rules..."
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #5
That's the basic idea, yes.
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #6
Excellent! Thanks.

"When you dream, there are no rules..."
Quote this message in a reply
tigakub
Unregistered
 
Post: #7
OpenGL is much (like way, way) faster because rasterizing a textured polgyon basically maps display ram resident textures while Quartz blitting to the screen bottlenecks at the system bus.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Tiles/Sprites question Bonked 0 2,007 Dec 16, 2006 06:06 AM
Last Post: Bonked
  SDL and transparancy in tiles Taxxodium 1 2,380 Oct 7, 2003 11:44 PM
Last Post: sealfin
  Using OpenGL to make windows, tiles, etc. LongJumper 6 3,148 Oct 7, 2003 09:41 AM
Last Post: inio
  Textures & Tiles in OGL Iceman 10 3,835 Jul 30, 2003 06:39 PM
Last Post: Iceman
  Implementation of 2D Tiles using OpenGL aaronsullivan 18 11,854 Jun 14, 2003 05:41 PM
Last Post: OneSadCookie