🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Problem with textures and OpenGL

Started by
5 comments, last by CrimsonSun 17 years, 5 months ago
...........
Advertisement
This is called "swimming." You are mapping a 32x32 texture to a 31x25 polygon. As a result, openGL interpolates the portion of the texture that should be shown based on the pixel of your texture closest to the actual pixel on your screen. The solution is to create a another texture (32x32) and fill in a 31x25 portion of it that contains the horizontal roading (leave the rest of the texture blank, or use it for something else). Then to display a textured polygon (starting from the upper left corner and clockwise) you use:

glTexCoord2f(0, 25.0 / 32.0);glVertex2i(0, 25);glTexCoord2f(31.0 / 32.0, 25.0 / 32.0)glVertex2i(31,25);glTexCoord2f(31.0 / 32.0, 0);glVertex2i(31,0);glTexCoord2f(0,0);glVertex2i(0, 0);
I don't see why I map a 32*32 texture on a 31*25 polygon. In fact I have 192 for the Bottom GUI and then 768 for the tiles. That is 768/24: 32 and Same thing for the right bar and therefore horizontal.
Here is a portion of your image with width and height of your polygons. I've miscounted, they are infact 31x26:



In fact, your image is 1280x800, not 1280x960. Thus your openGL implementation thinks your window is 1280x960 instead of the actual 1280x800 size that it is and it appropriately shrinks the vertical distances by approximately 17%. Since your vertical distance has been shrunk, your polygons are no longer 32x32 in terms of screen coordinates (they are now ~31x26, some will be smaller and some bigger based upon how they are positioned and interpolated). This is what is causing the swimming effect you are observing.

So to conclude, I have one question for you: Are your polygons being drawn 32x32? Because if they are, then the solution is very simple: First, disregard my first post in this thread and then make sure the window you're generating is 1280x960 instead of 1280x800.

[Edited by - CrimsonSun on January 15, 2007 3:10:32 PM]
Mmmm thats because my screen resolution is 1280*800...
So does that mean that my textures will be dependant upon the resolution of the screen?
No, it means you should base your quads positioning and size on the current resolution. That's much easier to do than to create textures for every possible resolution. :)
Create-ivity - a game development blog Mouseover for more information.
Quote: Original post by crackingod
Mmmm thats because my screen resolution is 1280*800...
So does that mean that my textures will be dependant upon the resolution of the screen?


It's not so much the fact that they are dependent on the screen, it's that you have a window that is 1280x800 and you told your opengl implementation to draw it as if it were 1280x960 with your glOrtho() function.

If you want your polygons to appear "as is", you need to have your orthographic projection's right and top clipping planes equal to the width and height of the screen. So since you declared
glOrtho (0, 1280, 0, 960, 1.0f, -1.0f);

the only two solutions are to either change your window's resolution or your glOrtho command to
glOrtho (0, 1280, 0, 800, 1.0f, -1.0f);

(Though not both, because then your vertical distance will be streched instead of shrunk)
Ok, but then not all screens support 1280*800 resolutions...
I would need a solution which is independant on the resolution of the screen (and which doesn't force the player into one resolution)
First, it's the ratio of the resolution that matters (i e, 1280/800 is 16/10), so any other resolution that's 16/10 will work well enough.
Second, turn on texture filtering, so that textures will be scaled up/down appropriately when run on different resolution screens.
Third, if the screen is 4/3 then you can add black borders at the top/bottom of the screen to make the viewport 16/10 in ratio. Similarly, if the screen is 5/4. If the screen is 16/9, you can add black borders on left/right.
enum Bool { True, False, FileNotFound };
If you want cross-resolution support, then add glViewport(0,0,width,height) to your draw routine and change your orthographic function to glOrtho(0, width, 0, height, 1.0f, -1.0f). The important thing to remember in order to avoid swimming is to have your screen's resolution equal to the world coordinates being displayed.

This topic is closed to new replies.

Advertisement