PDA

View Full Version : openGL 2d drawing problems, objects appear to overlap


matthew
2003.10.05, 03:55 AM
I have noticed that when I draw objects in openGL, that the alpha seems to move a little to the right.

In the screenshot below, when I draw a screen full of 32x32 squares, a small line is produced as a result of this.

Any assistance would be greatly appreciated. (I read over Carlos' post, I am hoping I posted correctly.)

screenshot (http://www.foxchange.com/~matt/temp/glproblem.jpg)

screenshot (a little more clear) (http://www.foxchange.com/~matt/temp/problem.gif)

The problem area I am talking about, is the circled area.

Bummer, no avatar for me yet.

OneSadCookie
2003.10.05, 05:46 AM
What blending function are you using, and how are you drawing it?

matthew
2003.10.05, 06:04 AM
I am using _glBlend for blending.

All sprites are drawn to a rect. The below function converts the rest into information needed to draw to the screen.


xSize = (Sprite.toRect.right-Sprite.toRect.left)/2
ySize = (Sprite.toRect.bottom-Sprite.toRect.top)/2

x = xSize+Sprite.toRect.left
y = ySize+Sprite.toRect.top

t1x = Sprite.fromRect.left/gSpriteMap.mapWidth (Sprite.mapId)
t1y = Sprite.fromRect.top/gSpriteMap.mapWidth (Sprite.mapId)
t2x = (Sprite.fromRect.left+(Sprite.fromRect.right-Sprite.fromRect.left))/gSpriteMap.mapWidth (Sprite.mapId)
t2y = (Sprite.fromRect.top+(Sprite.fromRect.bottom-Sprite.fromRect.top))/gSpriteMap.mapWidth (Sprite.mapId)

x = Sprite.toRect.left+xSize
y = Sprite.toRect.top+ySize

DoG
2003.10.05, 06:18 AM
I am uncertain on this, but it could either be (a) that stupid OpenGL bug which does not really allow pixel-perfect alignement horizontally, there was a lengthy discussion here and on the Apple OpenGL mailing list, or it could simply be (b) numerical drift.

In case of (b), I have to ask if you are using shared vertices, or does each quad has its own. You should be using shared vertices to avoid any numerical inaccuracy. Though the way that looks is really strange.

OneSadCookie
2003.10.05, 06:19 AM
So you haven't changed the blend function with glBlendFunc?

In that case it defaults to GL_ONE, GL_ZERO which may not be what you want...

Are you drawing with quads? triangles? points? lines? what's your point/line size if applicable? are you using antialiased points/lines?

Give me some information here, or I can't hope to guess your problem...

matthew
2003.10.05, 06:25 AM
Originally posted by OneSadCookie
So you haven't changed the blend function with glBlendFunc?

In that case it defaults to GL_ONE, GL_ZERO which may not be what you want...

Are you drawing with quads? triangles? points? lines? what's your point/line size if applicable? are you using antialiased points/lines?

Give me some information here, or I can't hope to guess your problem...

Actually I am using this: glBlendFunc (_glSrcAlpha, _glOneMinusSrcAlpha)
(Sorry, not totally familiar with everything openGL)

I am using GLQuads, after the code posted above, I use the code below:


glTranslated (x,y,0)
glBegin(_glQuads)

glTexCoord2d(t1x,t1y)
glVertex2d(-xSize, -ySize)
glTexCoord2d(t2x,t1y)
glVertex2d(xSize, -ySize)
glTexCoord2d(t2x,t2y)
glVertex2d(xSize, ySize)
glTexCoord2d(t1x,t2y)
glVertex2d(-xSize, ySize)

glEnd
glTranslated(-x,-y,0)

matthew
2003.10.05, 06:28 AM
Originally posted by DoooG
In case of (b), I have to ask if you are using shared vertices, or does each quad has its own. You should be using shared vertices to avoid any numerical inaccuracy. Though the way that looks is really strange.

I do not know what shared vertices are..

matthew
2003.10.05, 06:58 AM
I had a friend of mine give me a helping hand, and instead of using _glLinear, I use _glNearest and it fixed the problem.

Another work around was to use t1x = t1x + 0.001

arekkusu
2003.10.05, 05:40 PM
Perhaps it was your texture border setting. If you texture with linear filtering you need to be careful about the border. Try clamping.

kelvin
2003.10.05, 11:28 PM
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

matthew
2003.10.05, 11:33 PM
Originally posted by kelvin
--snip--

--snip-- Got it.

OneSadCookie
2003.10.05, 11:45 PM
#define GL_CLAMP_TO_EDGE 0x812F

note it's also known as GL_CLAMP_TO_EDGE_SGIS... you should really check for either OpenGL 1.2 or the GL_SGIS_texture_edge_clamp extension before using it.

Of course, it's supported on all renderers on Mac OS X, so it's not really an issue other than of style...

matthew
2003.10.05, 11:53 PM
I tried _glClampToEdge and it didn't fix anything. I am going to stick with _glLinear for now, using the fix that I mentioned.