gpt4 book ai didi

C# OpenGL 绘制拉伸(stretch)的二维纹理(不调整大小!)

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

我正在尝试从这样的纹理中绘制一个按钮: enter image description here并通过这种方式做到这一点:

    //sWidth, sHeight - stretched size.
Size mySize = TheSprite.MyTexture.TextureSize;
OpenGL.glBindTexture(OpenGL.TextureTarget.Texture2D, TheSprite.MyTexture.MyTextureID);
OpenGL.glBegin(OpenGL.BeginMode.Quads);
double x = (double)TheDestination.Left;
double y = (double)TheDestination.Top;
double s = (double)(TheSprite.MyLocation.Left + TheSource.Left) / (double)mySize.Width + sWidth;
double t = (double)(TheSprite.MyLocation.Top + TheSource.Top) / (double)mySize.Height + sHeight;
double x2 = (double)TheDestination.Right + sWidth;
double s2 = (double)(TheSprite.MyLocation.Left + TheSource.Right) / (double)mySize.Width + sWidth;
double y2 = (double)TheDestination.Bottom + sHeight;
double t2 = (double)(TheSprite.MyLocation.Top + TheSource.Bottom) / (double)mySize.Height + sHeight;

OpenGL.glColor4(TheModulation);

OpenGL.glTexCoord2d(s, t2);
OpenGL.glVertex2d(x, y2);
OpenGL.glTexCoord2d(s, t);
OpenGL.glVertex2d(x, y);
OpenGL.glTexCoord2d(s2, t);
OpenGL.glVertex2d(x2, y);
OpenGL.glTexCoord2d(s2, t2);
OpenGL.glVertex2d(x2, y2);
OpenGL.glEnd();

但就我而言,它看起来像这样: enter image description here

而不是像这样的东西(相同的纹理): enter image description here

在我的情况下,我应该如何绘制它以使其拉伸(stretch)而不是调整大小?

最佳答案

在这段代码中

OpenGL.glTexCoord2d(s, t2);
OpenGL.glVertex2d(x, y2);
OpenGL.glTexCoord2d(s, t);
OpenGL.glVertex2d(x, y);
OpenGL.glTexCoord2d(s2, t);
OpenGL.glVertex2d(x2, y);
OpenGL.glTexCoord2d(s2, t2);
OpenGL.glVertex2d(x2, y2);

您正在将纹理的角映射到按钮的角,如下所示:

enter image description here

enter image description here

由于您的纹理是正方形而按钮是矩形,因此纹理会水平拉伸(stretch)以适应按钮的宽度。如果你不希望黄色框在按钮的两侧被水平拉伸(stretch),你需要在映射纹理时使用额外的点,以便只水平拉伸(stretch)它的中间部分(点 2-3-6-7 ).像这样:

enter image description here

enter image description here

关于C# OpenGL 绘制拉伸(stretch)的二维纹理(不调整大小!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47371729/

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