Search code examples
copengltexturestexture-mappingtexture2d

Open-GL Texture-coordinates changing by changing triangle size


I have a problem with a texture-mapping with Open-GL.

example
(1) is the texture. I want to use a triangle out of this
(2) just for description the texture splitted in triangles
(3) mapped texture to triangles 16x16 (height x width)
(4) WRONG mapping for deformed triangles (in case of triangle size not 16x16)

For (4) only the variables H1,H2,H3,H4 are set (see second code-box)

Setup

//- setup cam / OpenGL
int width = 1024;
int height = 800;

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.f, width, 0.f, height, 1.f, -1.f);

glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

...

//- texture position in texture [Value range: 0..1]
float textX1 = 0.f;
float textY1 = 0.f;

//- size of texture triangle
const float textSizeWidth = 16.0f / getTextureWidth(); //- getTextureWidth()=2^N !
const float textSizeHeight = 16.0f / getTextureHeight();//- getTextureHeight()=2^M !
const float textSizeWidthHalf = textSizeWidth * 0.5f;

//- output screen coordinates [Value range: 0.. N !!]
int outX = 0;
int outY = 0;


//- the heigh of one "line"
static const int YSTEP = 9;

draw the triangles

//-> cal: outX, outY, textX1, textY1, textX2, textY2 

//-  deformation values
unsigned int H1 = l->AraeHeight1;
unsigned int H2 = l->AraeHeight2;
unsigned int H3 = l->AraeHeight3;
unsigned int H4 = l->AraeHeight4;

//- disable the Height/deformation
if (!m_useHeight) H1 = H2 = H3 = H4 = 0;


//--      1 --- 4  ^
//--     / \ B /   |
//--    / A \ /    |YSTEP
//--   2 --- 3     v

////// A /////
//-- ->1
glTexCoord2f(textX1 + textSizeWidthHalf, textY1 + textSizeHeight);
glVertex3i(outX + 8, outY + YSTEP + H1, 0);

//-- ->2
glTexCoord2f(textX1, textY1);
glVertex3i(outX, outY + H2, 0);


//-- ->3
glTexCoord2f(textX1 + textSizeWidth, textY1);
glVertex3i(outX + 16, outY + H3, 0);


////// B /////
//-- ->1
glTexCoord2f(textX2, textY2 + textSizeHeight);
glVertex3i(outX + 8, outY + YSTEP + H1, 0);

//-- ->3
glTexCoord2f(textX2 + textSizeWidthHalf, textY2);
glVertex3i(outX + 16, outY + H3, 0);

//-- ->4
glTexCoord2f(textX2 + textSizeWidth, textY2 + textSizeHeight);
glVertex3i(outX + 8 + 16, outY + YSTEP + H4, 0);

If H1 = H2 = H3 = H4 = 0; then everything looks fine ( image (3) ). If not then I got the rendering problems ( image (4) ) like a "rounding" problem, but I don't know where/why. Or the problem is, that the texture is shifted by one pixle to the right, but why? Or do I need to set another Open-Gl parameter. Thank you for a hint.


Solution

  • You can't avoid this, this is a rasterization rule and later a filtering rule (rounding of UVs from fragment coordinates), what people usually do in these cases is apply a dilatation on the texture only for the blue pixels, which means copying the nearest colored pixel for every blue pixel that stands near enough a colored zone.