我在使用Open-GL进行纹理映射时遇到问题。
(1)是质地。我想使用这个三角形之外的一个三角形
(2),仅用于描述将纹理分割成三角形
(3)将纹理映射到三角形16x16(高度x宽度)
(4)变形三角形的错误映射(如果三角形尺寸不是16x16)
对于(4),仅设置变量H1,H2,H3,H4(请参见第二个代码框)
设置
//- 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;
画三角形
//-> 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);
如果H1 = H2 = H3 = H4 = 0; 然后一切看起来都很好(图片(3))。如果不是这样,我会遇到渲染问题(图像(4)),例如“四舍五入”问题,但我不知道在哪里/为什么。或问题在于,纹理向右移动了一个像素,但是为什么呢?还是我需要设置另一个Open-Gl参数。谢谢你的提示。
您无法避免,这是一个栅格化规则,后来是一个过滤规则(从片段坐标对UV进行取整),在这种情况下,人们通常所做的是仅对蓝色像素在纹理上应用膨胀,这意味着复制每一个足够接近彩色区域的蓝色像素的最近彩色像素。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句