PDA

View Full Version : Using texture mapping for small rotatable 2d ornaments



Achim Schoen
08-22-2013, 01:53 AM
I'm working on a 2d layout application which harbors rotatable and translatable elements (or example a square box). The application includes the abilityto magnify and translate the current view. These elements should contain ornaments (for example a 2d coordinate system) which should be translated and rotated but not never been scaled.

For this purpose I have defined small mipmaps (size 16x16) via gluBuild2Dmipmaps and bound them to textures (glBindTexture(GL_TEXTURE_2D)
Using this texture via GL_QUADS works very well, but shows ugly results in case it is rotated. I wonder if texture mapping is the right approach for this purpose or if it is better to use other techniques?


This is the code snippet Im using to create the texture:


glPixelStorei(GL_UNPACK_ALIGNMENT,4);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_LINEAR);
// create the image
BYTE* pMemory = new BYTE[nSize];
....
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, bitmapColor.bmWidth, bitmapColor.bmHeight, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pMemory);
delete[] pMemory

This is the code snippet Im using to draw the texture:


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(m_dX, m_dY, 0);
glRotated(m_dAngle, 0, 0, 1);

glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );

glAlphaFunc(GL_GEQUAL, (GLclampf)1);
glEnable(GL_ALPHA_TEST);
glClearColor (0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth (1.0f);g
glDepthFunc (GL_LEQUAL);
glDisable(GL_DEPTH_TEST);
glShadeModel (GL_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);

double dWidth = 16;
double dHeight = 16;
int nNumber = 1;
glBegin(GL_QUADS);
// top left
glTexCoord2d(0, nNumber);
glVertex3d(-dWidth/2.0, dHeight/2.0, 0);
// top right
glTexCoord2d(nNumber, nNumber);
glVertex3d(dWidth/2.0, dHeight/2.0, 0);
// bottom right
glTexCoord2d(nNumber, 0);
glVertex3d(dWidth/2.0, -dHeight/2.0, 0);
// bottom left
glTexCoord2d(0, 0);
glVertex3d(-dWidth/2.0, -dHeight/2.0, 0);
glEnd();

glDisable(GL_ALPHA_TEST);
glDisable(GL_TEXTURE_2D);
SwapBuffers(dc.m_hDC);

GClements
08-22-2013, 03:52 AM
For this purpose I have defined small mipmaps (size 16x16) via gluBuild2Dmipmaps and bound them to textures (glBindTexture(GL_TEXTURE_2D)
Using this texture via GL_QUADS works very well, but shows ugly results in case it is rotated. I wonder if texture mapping is the right approach for this purpose or if it is better to use other techniques?
There isn't any technique which will make rotating small "icon"-type bitmaps work well. If you apply low-pass filtering (e.g. linear interpolation), you get blurring. If you don't apply low-pass filtering, you get aliasing. If the textures are never scaled, there isn't much point in using mipmapping, as it should only use the base mipmap level.

Achim Schoen
08-22-2013, 07:13 AM
There isn't any technique which will make rotating small "icon"-type bitmaps work well. If you apply low-pass filtering (e.g. linear interpolation), you get blurring. If you don't apply low-pass filtering, you get aliasing. If the textures are never scaled, there isn't much point in using mipmapping, as it should only use the base mipmap level.

So in other words this means that it is not a good idea to use textures for small symbols in case they should be rotated?

GClements
08-23-2013, 01:47 AM
So in other words this means that it is not a good idea to use textures for small symbols in case they should be rotated?
No, it means it's not a good idea to have symbols with features the size of a screen pixel.

If you have such symbols, the only way to get them to look good is to use bitmaps, which are created by hand, and to ensure that the bitmap doesn't undergo any transformation other than translation by multiples of a screen pixel, reflection (i.e. scaling by -1) or rotation by multiples of 90 degrees (IOW, operations which map the pixel grid to itself).

Outline fonts have complex hinting mechanisms in order to deal with having features at the scale of a pixel, and that's without considering rotation. And decent TTF files still include hand-tuned bitmaps for small sizes.

This all follows from the Nyquist theorem (http://en.wikipedia.org/wiki/Nyquist_theorem), which essentially says that if you sample a signal which has frequencies above or just below the sampling frequency (or, in the time domain, features smaller or just larger than the sampling interval), you're bound to lose information and the reconstructed signal will (in general) differ significantly from the original.