gpt4 book ai didi

c - Opengl Texture-coordinates 通过改变三角形大小改变

转载 作者:太空宇宙 更新时间:2023-11-03 23:25:15 24 4
gpt4 key购买 nike

我在使用 Open-GL 进行纹理映射时遇到问题。

example
(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 进行舍入),人们通常在这些情况下所做的只是对蓝色像素的纹理应用扩张,这意味着为距离足够近的彩色区域的每个蓝色像素复制最近的彩色像素。

关于c - Opengl Texture-coordinates 通过改变三角形大小改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28709091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com