简体   繁体   English

在STB / Opengl中渲染白盒纹理

[英]Rendering white box textures in STB/Opengl

Program compiles fine. 程序编译正常。 Quads print. 四边形打印。 Trying to insert a 32 bit png, but it only prints a white box. 尝试插入32位png,但只打印白色框。 What am I doing wrong? 我究竟做错了什么? I've spent a few days trying to figure it out doing countless tutorials. 我花了几天的时间尝试做无数的教程。 I don't want to give up. 我不想放弃

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,

};
......

It's all set up with in classes and functions, but I will try to show the whole draw. 所有这些都在类和函数中进行了设置,但是我将尝试显示整个绘图。

.....
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();

The fragment shader. 片段着色器。

#version 330

in vec4 vCol;
in vec2 TexCoord;

out vec4 color;

uniform sampler2D theTexture;

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

The Vertex Shader. 顶点着色器。

#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;
}

Projection was out of wack. 投影不合时宜。 The reason I was getting all of those 1281 and 12 82 GLerrors was the fact that I was calling the wrong vector index, so I could see a draw. 我得到所有那些1281和12 82 GLerrors的原因是我调用了错误的向量索引,因此我可以看到平局。 I saw a draw and assumed I was heading in the right direction. 我看到了平局,并认为自己正朝着正确的方向前进。

If I called what I knew was right, all the drawing happened off screen. 如果我说我所知道的是对的,那么所有绘图都在屏幕外进行。

What I did was delete the projection matrix in my shader since all I wanted was a 2d rendering of a gui window with calculated pixel ratios in a calculated position. 我要做的是删除我的着色器中的投影矩阵,因为我想要的是对gui窗口进行2d渲染,并在计算位置上计算出像素比率。

I will keep model in the shader if I want to tweak things in the future. 如果将来要调整功能,我会将模型保留在着色器中。

All of your help made me a better programmer. 您的所有帮助使我成为了一个更好的程序员。 @Rabbid76 all of your suggestions were things I needed to do. @ Rabbid76您所有的建议都是我需要做的。 Ty all. 全部

Corrected Shader 校正着色器

#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;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM