现代OpenGL,在对象上获得白色纹理



im 尝试将纹理加载到Modern NVIDIA openGL中的对象

我有一个正方形,我想将纹理加载到,我使用 stb_image 作为我的加载器

这是主要代码:

float position1[16]{
0.0f , 0.0f, 0.0f , 0.0f,
0.8f , 0.0f, 1.0f , 0.0f,
0.0f , 0.8f, 0.0f , 1.0f,
0.8f , 0.8f, 1.0f , 1.0f ,
};
unsigned int Indexs[6]{
0 , 1 , 2 ,
2 , 1 , 3
};
VertexBuffer buffer(position1, sizeof(position1));
IndexBuffer Index(Indexs, 6);
Texture Tex("Smile.png");
Tex.Bind();
buffer.Bind();
Index.Bind();
Shader BasicShader("basic.shader");
Renderer renderer; 
glfwSwapInterval(0);

//! MAIN LOOP !!!

BasicShader.Uniform1i("u_Texture ", 0);
BasicShader.Uniform4f("u_Color", 0.0f, 1.0f, 1.0f, 1.0f );
while (!glfwWindowShouldClose(window) )
{
//! RENDER
renderer.Clear();
glEnable(GL_TEXTURE_2D);
BasicShader.Uniform1i("u_Texture ", 0);
BasicShader.Uniform4f("u_Color", 0.f, 1.0f, 1.0f, 1.0f);
Tex.Bind();
BasicShader.Bind();
renderer.Draw(buffer, Index, Layout::D4Vertex, BasicShader);

//! FRAME
glfwSwapBuffers(window);
//! EVENTS
glfwPollEvents();
}
glfwTerminate();
return 0;
};

这是我的着色器,它当然按照预期以正确的方式编译,没有错误:

#shader vertex
#version 410 core
layout(location = 0 ) in vec4 position;
layout(location = 1) in vec2 texCoord;
out vec2  v_TexCoord;
void main()
{
gl_Position = position;
v_TexCoord = texCoord;
};

#shader fragment
#version 410 core
layout(location = 0 ) out vec4 color;
uniform vec4 u_Color;
uniform sampler2D u_Texture;
in vec2 v_TexCoord;
void main()
{
vec4 texColor = texture2D(u_Texture, v_TexCoord);
color = texColor;
};

这是我的抽奖电话:

void Renderer::Draw(VertexBuffer& vb,  IndexBuffer& iv, 
Layout layout,Shader& shader) 
{
BufferLayout bufferLayout;
shader.Bind();
vb.Bind();
iv.Bind();
bufferLayout.SetLayout(layout);
glDrawElements(GL_TRIANGLES, iv.GetCount(), GL_UNSIGNED_INT, nullptr);
}

这是我设置缓冲区布局的方式:

void BufferLayout::SetLayout(Layout l)
{
switch (l) {
case Layout::NONE :
glEnableVertexAttribArray(0);
case Layout::D2Vertex :
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 
2,0);
case Layout::D4Vertex:
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 
4,0);
}
}

这是我的纹理类(亨德尔斯加载参数和绑定):

#include "Texture.h"
#include "stb_image.h";
#include "Renderer.h"
Texture::Texture(string  path)
: m_RendererID(0) , m_BPP(0) , m_Height (0) , m_Width (0), 
m_LocalBuffer(nullptr) , m_FilePath(path)
{
stbi_set_flip_vertically_on_load(1);
m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4);
//seems like the image is there
glEnable(GL_TEXTURE_2D);
glGenTextures(1, & m_RendererID);
glBindTexture(GL_TEXTURE_2D, m_RendererID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
GLenum temp = glGetError();
if (temp != GL_NO_ERROR)  throw std::invalid_argument("OPENGL ERROR");
// I Get  no erros
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, 
GL_UNSIGNED_BYTE, m_LocalBuffer);
}
Texture::~Texture()
{
glDeleteTextures(1, &m_RendererID);
}
void Texture::Bind(unsigned int slot)
{
glActiveTexture(GL_TEXTURE0 + slot);
glBindTexture(GL_TEXTURE_2D, m_RendererID); 
}
void Texture::Unbind()
{
glBindTexture(GL_TEXTURE_2D, 0);
}

和我的纹理.h文件:

#pragma once
#include "Renderer.h"
class Texture {
private :
unsigned int  m_RendererID;
string m_FilePath;
unsigned char * m_LocalBuffer;
int m_Width, m_Height, m_BPP;
public:
Texture(string  path);
~Texture();
void Bind(unsigned int slot = 0);
void Unbind();
inline int GetWidth()const { return m_Width; }
inline int GetHeight()const { return m_Height; }
};

基本上我很确定问题一定在我发布的代码中的某个地方,并且所有其他代码都按预期工作.

任何人都可以发现问题吗? 结果应该是一个带有纹理"Smile.png"的正方形,我得到一个白色纹理.我的项目目录中确实有文件"Smile.png",在我看来stb_image可以找到并加载它。

*没有错误来自glGetError();

在代码示例中,缺少属性 1 (texCoord) 的设置代码。对于着色器使用的每个属性,必须绑定缓冲区,必须启用该属性,并且必须指定属性和着色器之间的关系。

如前所述,这是针对属性 0 完成的,而不是针对属性 1 完成的。SetLayout方法中的代码可能如下所示:

//Attribute 0: uses first two floats
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, 0);
//Attribute 1: uses 3rd and 4th float
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void*)(2 * sizeof(float)));

相关内容

  • 没有找到相关文章

最新更新