【问题标题】:Rendering white box textures in STB/Opengl在 STB/Opengl 中渲染白盒纹理
【发布时间】:2018-06-07 05:10:26
【问题描述】:

程序编译良好。四边形打印。尝试插入 32 位 png,但它只打印一个白框。我究竟做错了什么?我花了几天时间试图弄清楚它做了无数的教程。我不想放弃。

GLfloat cancelButtonVert[]
{
    0.1367f, -0.928f, 0.0f, 0.0f, 0.0f,
    0.4833f, -0.928f, 0.0f, 1.0f, 0.0f,
    0.4833f, -0.744f, 0.0f, 1.0f, 1.0f,
    0.1367f, -0.744f, 0.0f,    0.0f, 1.0f     
};

const unsigned int buttonIndices[]
{
    0, 1, 2,
    2, 3, 0,

};
......

这一切都是在类和函数中设置的,但我会尝试展示整个平局。

.....
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(m_vertices[0]) * 5, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(m_vertices[0]) * 5, (void*)(sizeof(m_vertices[0]) * 3));
glEnableVertexAttribArray(1);

.....

void Texture::loadTexture()
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
unsigned char *texData = stbi_load(m_fileLocation.c_str(), &m_width, &m_height, &m_bitDepth, 0);
if (!texData)
{
    printf("Failed to find: %s\n", m_fileLocation.c_str());
    return;
}
glGenTextures(1, &m_textureId);
glBindTexture(GL_TEXTURE_2D, m_textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(texData);
}

void Texture::useTexture()
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_textureId);
}
shaderList[0].useShader();
    uniformModel = shaderList[0].getModelLocation();
    uniformTexture = shaderList[0].getUniformTexture();
    //Draw Boxes
    model = glm::mat4();
    model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f));
    model = glm::scale(model, glm::vec3(1.0f, 1.0f, 1.0f));
    glUniformMatrix4fv(uniformModel, 3, GL_FALSE, glm::value_ptr(model));
    cancelButton.useTexture();
    glUniform1i(uniformTexture, 0);
    meshlist[0]->renderMesh();

片段着色器。

#version 330

in vec4 vCol;
in vec2 TexCoord;

out vec4 color;

uniform sampler2D theTexture;

void main()
{
    color = texture(theTexture, TexCoord);
}

顶点着色器。

#version 330

layout (location = 0) in vec3 pos;
layout (location = 1) in vec2 tex;

out vec4 vCol;
out vec2 TexCoord;

uniform mat4 model;
uniform mat4 projection;

void main()
{
    gl_Position = projection * model * vec4(pos, 1.0);
    vCol = vec4(clamp(pos, 0.0f, 1.0f), 1.0f);

    TexCoord = tex;
}

【问题讨论】:

  • glGetError() 在绘图期间是否返回任何错误?另外,如果你将片段着色器更改为简单地返回红色和绿色通道中的纹理坐标,你会看到什么?
  • 太,太多了。我对编程很陌生。我只做了不到3个月。仍然没有解决它,但你把我带到了正确的方向。我什至不知道 glGetError()。我现在是一个更好的调试器。我仍然得到带有返回颜色作为纹理坐标的白框,并且控制台在我的 while 循环结束时打印错误 1281。我现在会调查一下。
  • @Rabbid76 顶点着色器已添加。
  • 我不是想发牢骚,但我已经坚持了 6 个小时。我仍然无法得到我想要的结果。我做了@Rabbid76 建议的所有更改。现在我正在对所有内容进行内联编程,以帮助我在 glGetError(); 上放置一个位置;设想。帽子提示用户^^^。现在在我的 OOP 程序中,我收到一个 1281 错误和一个 1282 错误。我不知道具体在哪里。我用一些错误提升来探测着色器类,但是 1281 错误在我的 vectorIndex.useShader 函数中不断弹出,但我仍然无法隔离 1282 错误。问题是如果我系统暂停 82 会不断重复。
  • 不,按索引的碎片仍在打印白色。

标签: c++ opengl textures glm-math stb-image


【解决方案1】:

投影不正常。我得到所有这些 1281 和 12 82 GLerrors 的原因是我调用了错误的向量索引,所以我可以看到平局。我看到了平局,并认为我正朝着正确的方向前进。

如果我调用我知道是正确的,所有的绘图都发生在屏幕之外。

我所做的是删除我的着色器中的投影矩阵,因为我想要的只是一个 gui 窗口的 2d 渲染,在计算的位置具有计算的像素比率。

如果我以后想调整一些东西,我会将模型保留在着色器中。

您的所有帮助使我成为了一个更好的程序员。 @Rabbid76 你所有的建议都是我需要做的事情。全部。

修正着色器

#version 330

layout (location = 0) in vec3 pos;
layout (location = 1) in vec2 tex;

out vec4 vCol;
out vec2 TexCoord;

uniform mat4 model;


void main()
{
gl_Position = model * vec4(pos, 1.0);
vCol = vec4(clamp(pos, 0.0f, 1.0f), 1.0f);

TexCoord = tex;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 2023-03-30
    相关资源
    最近更新 更多