开发者

Drawing adjacent texture mapped rectangles in OpenGL

开发者 https://www.devze.com 2023-03-03 23:24 出处:网络
I\'m drawing a number of texture-mapped rectangles which are sat on top of each other using the following code:

I'm drawing a number of texture-mapped rectangles which are sat on top of each other using the following code:

glBegin(GL_QUADS);
glTexCoord2f(0.0, 1.0); glVertex3f(x, y, 0);
glTexCoord2f(0.0, 0.0); glVertex3f(x, y + dy, 0);
glTexCoord2f(1.0, 0.0); glVertex3f(x + dx, y + dy, 0);
glTexCoord2f(1.0, 1.0); glVertex3f(x + dx, y, 0);
glEnd();

This renders correctly at full size, but when I resize it with glScale, single-pixel gaps appear in between some of the rectangles.

However, if I remove the texture map, the rectangles always abut each other correctly.

开发者_如何学编程

What's going wrong, and how can I fix this?


How does the texture look like? Maybe you've to use a different clamping mode, depending on the texture's contents.


First of all, make sure they are really gaps, and not texture artifacts, by not using any alpha channel (assuming you were) and picking a background color that stands out like a sore thumb.

Even though you pinpointed texturing as the root cause, a much more likely culprit is rounding errors in the vertex coordinates. Rasterization of adjacent triangles is only guaranteed to be gap-free if the vertex coordinates used match 100%. In stead of calculating them on the fly (x + dx, y + dy), why not store them in an array and make sure you re-use the same coordinates?


I'm an idiot. The images I was using actually had a transparent row of pixels along the top and bottom.

The original "full-size" rendering was already resizing the texture, and it was a pure fluke that resizing artifacts caused the transparent rows not to be displayed in this case.

Then when I resized the whole lot again with glScale some of the artifacts must have disappeared, and the transparent pixels appeared as gaps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号