gpt4 book ai didi

c++ - 绘制 GL_TEXTURE_RECTANGLE 与 GL_TEXTURE_2D

转载 作者:太空狗 更新时间:2023-10-29 21:45:37 26 4
gpt4 key购买 nike

我正在尝试创建一个函数来绘制 2D 纹理和矩形纹理。

它完美地绘制了 2D 的。然而,当绘制任何矩形纹理时,它会将它们绘制成完全白色。

GLuint LoadImage(std::string ImageTitle, GLenum Target)
{
Image Img(("Images/" + ImageTitle).c_str());
std::vector<std::uint8_t> Buffer;
ImageToBuffer(Buffer, Img);

GLuint ID;
glGenTextures(1, &ID);
glBindTexture(Target, ID);
glTexParameteri(Target, GL_TEXTURE_WRAP_S, Target == GL_TEXTURE_2D ? GL_REPEAT : GL_CLAMP_TO_EDGE);
glTexParameteri(Target, GL_TEXTURE_WRAP_T, Target == GL_TEXTURE_2D ? GL_REPEAT : GL_CLAMP_TO_EDGE);
glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(Target, 0, GL_RGB, Img.Width(), Img.Height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, Buffer.data());
return ID;
}

void DrawTexture(GLenum Target, GLuint ID, float X1, float Y1, float X2, float Y2)
{
GLfloat ViewPort[4];
glGetFloatv(GL_VIEWPORT, ViewPort);

glEnable(Target);
glBindTexture(Target, ID);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(X1, Y1);

glTexCoord2f(0, Target == GL_TEXTURE_2D ? 1 : ViewPort[3]);
glVertex2f(X1, Y2);

glTexCoord2f(Target == GL_TEXTURE_2D ? 1 : ViewPort[2], Target == GL_TEXTURE_2D ? 1 : ViewPort[3]);
glVertex2f(X2, Y2);

glTexCoord2f(Target == GL_TEXTURE_2D ? 1 : ViewPort[2], 0);
glVertex2f(X2, Y1);
glEnd();
glDisable(Target);
}

我还尝试使用 glTextCoordf(X, Y),其中 X 和 Y 为 1 而不是 ViewPort 宽度/高度。尝试使用宽度/高度,仍然没有。尝试纹理宽度和高度。我也尝试将 Rect 加载为 2D。运气不好。

没有任何效果 :S 但它以某种方式对 Texture2D 有效。

我这样使用它:

GLuint ID = LoadImage("RECT_116.png", GL_TEXTURE_RECTANGLE);

//Do other stuff here..

DrawTexture(GL_TEXTURE_RECTANGLE, ID, X1, Y1, X2, Y2);

它失败了,但是当我在 2D 纹理上这样做时:

GLuint ID = LoadImage("2D_116.png", GL_TEXTURE_2D);

//Do other stuff here..

DrawTexture(GL_TEXTURE_2D, ID, X1, Y1, X2, Y2);

它有效.. 知道我做错了什么吗?

最佳答案

来自 http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml

If target is GL_PROXY_TEXTURE_2D, GL_PROXY_TEXTURE_1D_ARRAY, GL_PROXY_TEXTURE_CUBE_MAP, or GL_PROXY_TEXTURE_RECTANGLE, no data is read from data, but all of the texture image state is recalculated, checked for consistency, and checked against the implementation's capabilities. If the implementation cannot handle a texture of the requested texture size, it sets all of the image state to 0, but does not generate an error (see glGetError). To query for an entire mipmap array, use an image array level greater than or equal to 1.

If target is GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE or one of the GL_TEXTURE_CUBE_MAP targets, data is read from data as a sequence of signed or unsigned bytes, shorts, or longs, or single-precision floating-point values, depending on type. These values are grouped into sets of one, two, three, or four values, depending on format, to form elements. Each data byte is treated as eight 1-bit elements, with bit ordering determined by GL_UNPACK_LSB_FIRST (see glPixelStore).

因此,一致性检查似乎由于某种原因可能会失败。

您似乎根据 GL_TEXTURE_2D 或 GL_TEXTURE_RECTANGLE 在

glTexParameteri(Target, GL_TEXTURE_WRAP_S, 
Target == GL_TEXTURE_2D ? GL_REPEAT : GL_CLAMP_TO_EDGE);

您是否尝试过删除该条件以查看是否会改变情况?

关于c++ - 绘制 GL_TEXTURE_RECTANGLE 与 GL_TEXTURE_2D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17166808/

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