Texture rendering with weird stuff

Started by DoubleDahlia, June 20, 2011, 23:53:14

Previous topic - Next topic

DoubleDahlia

I have used a couple of tutorials to write this program and make it work, so its very possible that I've messed something up. Every time i render an image, there is a line below it and on the right of it. I'm not sure how to get rid of it, so I'm attaching the code I've used to display this.

Also, is there a way to just pass what texture to draw (in case of different tiles) or do i have to bind and unbind each time i want to draw a different one?

It those are easy questions, I apologize. This is my first time with OpenGL :)

Here is a screenshot of what I'm talking about: http://imageshack.us/photo/my-images/269/problemrn.jpg/
Mind what people do, not only what they say, for deeds will betray a lie.

CodeBunny

What are the dimensions of the image you want to render? Textures usually are allocated in PoT (power of two) dimensions, so by supplying 1 for the width and height (in texture coordinates) you bind a vertex at the edge of the texture - not the image. The same problem occurs with your getTextureWidth(); you're accessing the width of the allocated texture, not the image you are dealing with.

This disconnect between the image you're using and the actual texture in memory is frustrating (you either are inefficient with your graphics, or you force your images into power-of-two dimensions), but you learn to live with it. NPoT (non-power of two) textures are possible, but if you want to use Slick for loading, I don't believe you can obtain them.

But, to solve your problem, this should work. Replace your drawTile code with this:

glTexCoord2f(0, 0);
		glVertex2f(x, y);

		glTexCoord2f(tex.getWidth(), 0);
		glVertex2f(x + tex.getImageWidth(), y);

		glTexCoord2f(tex.getWidth(), tex.getHeight());
		glVertex2f(x + tex.getImageWidth(), y + tex.getImageHeight());

		glTexCoord2f(0, heightRatio);
		glVertex2f(x, y + tex.getImageHeight());

DoubleDahlia

The "tiles" are 20x20 pixels. Your code worked perfectly with them. Since you said its inefficient, I've changed my tiles to 32x32 and my old code worked without artifacts. Thanks a lot for great help :)
Mind what people do, not only what they say, for deeds will betray a lie.

CodeBunny

No problem. :)

I'd recommend that you keep the code I supplied; it will work with images that are either PoT or not, so you're a little more flexible.

You probably don't have to worry too much about efficiency unless you're planning on having a lot of images active at any given time; most graphics card have quite a lot of video memory.