PDA

View Full Version : Trying to do 2D tiling



tinrocket
05-20-2003, 01:14 PM
Hello, I've created a sprite engine with OpenGL ( http://www.tinrocket.com/content/superspritesurface/index.html ) but I'm having problems tiling images. I'm using quads with texture maps, and the problem I'm having is getting the images to draw 1:1 with the pixels on the screen.

This is how I've set up my camera:

gluOrtho2D(0.0,SurfaceWidth-1.0,SurfaceHeight-1.0,0.0)

below is an image tile:

http://www.tinrocket.com/personal/opengl/grey.gif

I'm incrementing the X & Y translation in a double loop, stepping by the height and width of the image. This is how I think I should be drawing the image tiles:

glBegin(GL_QUADS)
glTexCoord2d(0.0,0.0)
glVertex2d(0.0,0.0)
glTexCoord2d(0.0,1.0)
glVertex2d(0.0,Image.Height-1.0)
glTexCoord2d(1.0,1.0)
glVertex2d(Image.Width-1.0,Image.Height-1.0)
glTexCoord2d(1.0,0.0)
glVertex2d(Image.Width-1.0,0.0)
glEnd

but this is the output I'm getting:

http://www.tinrocket.com/personal/opengl/tiled.gif

it seems that each tile is drawing 1 pixel short on the top and bottom. I think now I might be messing up where I'm assigning the texture coordinates. I've tried a bunch of things in various permutations:

offsetting the UV coordinates by half a pixel
offsetting the translated coordinate by half a pixel
offsetting the quad coordinates

but nothing is allowing me to get a 1:1 mapping to the screen. Can anyone help?

Thank you,
John Balestrieri

05-20-2003, 07:36 PM
John,

Not sure if this will help, but perhaps this project would point you in the right direction.
http://developer.apple.com/samplecode/Sample_Code/Graphics_3D/Texture_Clamp_Test.htm

Thomas C.

tinrocket
05-22-2003, 09:40 PM
Thanks, Thomas. That was not it, but I eventually found the problems (two!):

1) I mistakenly set up the surface's height & width with the exact number of pixel, rather than the number of pixels+1

2) I assumed that the GL_TEXTURE_MAG_FILTER was GL_NEAREST by default, rather than GL_LINEAR (which contributed to the artifact problem).

John

fringe
05-27-2003, 11:56 PM
Did you know that you can also use one hugh quad and get the texture to repeat by have texture cco-ordinates bigger than 1.

fringe

roffe
05-28-2003, 02:18 AM
Originally posted by tinrocket:

1) I mistakenly set up the surface's height & width with the exact number of pixel, rather than the number of pixels+1


If your textures are n*m you should specify this dimension not n+1/m+1 to glTexImage2D. Who told you to do +1?