c++ - 绘制 GL_TEXTURE_RECTANGLE 与 GL_TEXTURE_2D

标签 c++ opengl opengl-3

我正在尝试创建一个函数来绘制 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/

相关文章:

c++ - 编译器是否调整 int 大小?

c++ - 如果不是直接从代码中使用,为什么要包含 <string>?

qt - 在 Qt 中启动 2D 游戏

opengl - 我可以有一个没有 Alpha 和深度的默认帧缓冲区吗?

c++ - 为什么我的字符串仅限于一个词? (c++)

c++ - 大型 C++ 项目的推荐 Eclipse CDT 配置是什么(索引器需要永远)

python - pyglet 的形状或场景管理?

delphi - 有 Delphi 的游戏引擎吗?

opengl - 为什么 glGetUniformLocation 让我失望?

c++ - 如何将 opengl 3.3 渲染成 SDL2 纹理?