Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果现代opengl中有多个纹理,则无纹理 我用C++和OpenGL 4.6用SDL2和GLWW。 我有一个类纹理: #include "stb_image/stb_image.h" class Texture { private: unsigned int m_RendererID; std::string m_FilePath; unsigned char *m_LocalBuffer; int m_Width, m_Height, m_BPP; public: Texture(const std::string &path); ~Texture(); void Bind(unsigned int slot = 0) const; void Unbind() const; inline int GetWidth() const { return m_Width; } inline int GetHeight() const { return m_Height; } inline int GetID() const { return m_RendererID; } }; Texture::Texture(const std::string &path) : m_RendererID(0), m_FilePath(path), m_LocalBuffer(nullptr), m_Width(0), m_Height(0), m_BPP(0) { stbi_set_flip_vertically_on_load(1); //Flip vertically because OpenGL renders texture in the opposite way m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4); GLCall(glGenTextures(1, &m_RendererID)); GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID)); GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)); GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer)); GLCall(glBindTexture(GL_TEXTURE_2D, 0)); if (m_LocalBuffer) { stbi_image_free(m_LocalBuffer); } } Texture::~Texture() { GLCall(glDeleteTextures(1, &m_RendererID)); } void Texture::Bind(unsigned int slot) const { GLCall(glActiveTexture(GL_TEXTURE0 + slot)); GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID)); } void Texture::Unbind() const { GLCall(glBindTexture(GL_TEXTURE_2D, 0)); }_C++_Opengl_Stb Image - Fatal编程技术网

如果现代opengl中有多个纹理,则无纹理 我用C++和OpenGL 4.6用SDL2和GLWW。 我有一个类纹理: #include "stb_image/stb_image.h" class Texture { private: unsigned int m_RendererID; std::string m_FilePath; unsigned char *m_LocalBuffer; int m_Width, m_Height, m_BPP; public: Texture(const std::string &path); ~Texture(); void Bind(unsigned int slot = 0) const; void Unbind() const; inline int GetWidth() const { return m_Width; } inline int GetHeight() const { return m_Height; } inline int GetID() const { return m_RendererID; } }; Texture::Texture(const std::string &path) : m_RendererID(0), m_FilePath(path), m_LocalBuffer(nullptr), m_Width(0), m_Height(0), m_BPP(0) { stbi_set_flip_vertically_on_load(1); //Flip vertically because OpenGL renders texture in the opposite way m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4); GLCall(glGenTextures(1, &m_RendererID)); GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID)); GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)); GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer)); GLCall(glBindTexture(GL_TEXTURE_2D, 0)); if (m_LocalBuffer) { stbi_image_free(m_LocalBuffer); } } Texture::~Texture() { GLCall(glDeleteTextures(1, &m_RendererID)); } void Texture::Bind(unsigned int slot) const { GLCall(glActiveTexture(GL_TEXTURE0 + slot)); GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID)); } void Texture::Unbind() const { GLCall(glBindTexture(GL_TEXTURE_2D, 0)); }

如果现代opengl中有多个纹理,则无纹理 我用C++和OpenGL 4.6用SDL2和GLWW。 我有一个类纹理: #include "stb_image/stb_image.h" class Texture { private: unsigned int m_RendererID; std::string m_FilePath; unsigned char *m_LocalBuffer; int m_Width, m_Height, m_BPP; public: Texture(const std::string &path); ~Texture(); void Bind(unsigned int slot = 0) const; void Unbind() const; inline int GetWidth() const { return m_Width; } inline int GetHeight() const { return m_Height; } inline int GetID() const { return m_RendererID; } }; Texture::Texture(const std::string &path) : m_RendererID(0), m_FilePath(path), m_LocalBuffer(nullptr), m_Width(0), m_Height(0), m_BPP(0) { stbi_set_flip_vertically_on_load(1); //Flip vertically because OpenGL renders texture in the opposite way m_LocalBuffer = stbi_load(path.c_str(), &m_Width, &m_Height, &m_BPP, 4); GLCall(glGenTextures(1, &m_RendererID)); GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID)); GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)); GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_Width, m_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_LocalBuffer)); GLCall(glBindTexture(GL_TEXTURE_2D, 0)); if (m_LocalBuffer) { stbi_image_free(m_LocalBuffer); } } Texture::~Texture() { GLCall(glDeleteTextures(1, &m_RendererID)); } void Texture::Bind(unsigned int slot) const { GLCall(glActiveTexture(GL_TEXTURE0 + slot)); GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID)); } void Texture::Unbind() const { GLCall(glBindTexture(GL_TEXTURE_2D, 0)); },c++,opengl,stb-image,C++,Opengl,Stb Image,如果创建一个纹理,该纹理将显示: Texture texture("ship.png"); texture.Bind(0); shader.Bind(); va.Bind(); ib.Bind(); shader.SetUniform1i("u_Texture", 0); glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr); 但如果我创建两种纹理,我只会得到黑屏: Texture texture("ship

如果创建一个纹理,该纹理将显示:

Texture texture("ship.png");
texture.Bind(0);
shader.Bind();
va.Bind();
ib.Bind();
shader.SetUniform1i("u_Texture", 0);
glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr);
但如果我创建两种纹理,我只会得到黑屏:

Texture texture("ship.png");
texture.Bind(0);
Texture texture2("ship2.png");
texture2.Bind(1);
shader.Bind();
va.Bind();
ib.Bind();
shader.SetUniform1i("u_Texture", 0);
glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr);

这应该行得通。我在互联网上搜索了2个小时,但我似乎找不到为什么这不起作用的原因。你们能帮帮我吗?

在下面几行代码的末尾

第二个纹理对象(“ship2.png”)绑定到纹理单元1,但默认纹理对象(0)绑定到纹理单元0

注意
纹理时的纹理(“ship.png”)”,然后创建纹理对象并将其绑定到当前纹理单元,然后将默认纹理对象(0)绑定到当前纹理单元:

请注意,您所谓的“解除绑定”并不解除任何绑定
glBindTexture(GL\u TEXTURE\u 2D,0)
将默认纹理对象(0)绑定到当前处于活动状态的纹理单元。没有必要那样做


只需更改说明的顺序即可解决问题。首先创建并加载纹理对象。然后将纹理对象绑定到纹理单元:

纹理(“ship.png”);
纹理纹理2(“ship2.png”);
纹理绑定(0);
2.绑定(1);
shader.Bind();
va.Bind();
ib.Bind();
SetUniform1i(“u_纹理”,0);
glpaurements(GL_三角形,ib.GetCount(),GL_UNSIGNED_INT,nullptr);

作为替代方法,可以避免使用纹理单元0。仅使用纹理单元0创建纹理对象:

纹理::纹理(常量std::字符串和路径,无符号int槽){
GLCall(glActiveTexture(GL_TEXTURE0));
// [...]
}
纹理(“ship.png”);

纹理绑定(1);// 在以下代码行的末尾

第二个纹理对象(“ship2.png”)绑定到纹理单元1,但默认纹理对象(0)绑定到纹理单元0

注意
纹理时的纹理(“ship.png”)”,然后创建纹理对象并将其绑定到当前纹理单元,然后将默认纹理对象(0)绑定到当前纹理单元:

请注意,您所谓的“解除绑定”并不解除任何绑定
glBindTexture(GL\u TEXTURE\u 2D,0)
将默认纹理对象(0)绑定到当前处于活动状态的纹理单元。没有必要那样做


只需更改说明的顺序即可解决问题。首先创建并加载纹理对象。然后将纹理对象绑定到纹理单元:

纹理(“ship.png”);
纹理纹理2(“ship2.png”);
纹理绑定(0);
2.绑定(1);
shader.Bind();
va.Bind();
ib.Bind();
SetUniform1i(“u_纹理”,0);
glpaurements(GL_三角形,ib.GetCount(),GL_UNSIGNED_INT,nullptr);

作为替代方法,可以避免使用纹理单元0。仅使用纹理单元0创建纹理对象:

纹理::纹理(常量std::字符串和路径,无符号int槽){
GLCall(glActiveTexture(GL_TEXTURE0));
// [...]
}
纹理(“ship.png”);

纹理绑定(1);//创建两个纹理后,尝试绑定这两个纹理。@tohomas:代码中有一个不相关的错误。有关详细信息,请参阅。另外,
m_FilePath
m_LocalBuffer
仅由构造函数使用。为什么要将它们保留为成员?在创建两种纹理后尝试绑定它们。@tohomas:代码中有一个不相关的错误。有关详细信息,请参阅。另外,
m_FilePath
m_LocalBuffer
仅由构造函数使用。为什么要让他们成为会员?前两种方法不起作用,但最后一种有效。@Tohomas不,如果你做对了,这三种方法都有效!好的,我再试了一次,第二个也可以(我没有看到更新的版本)。我似乎无法使第一种方法起作用。前两种方法不起作用,但最后一种方法起作用。@Tohomas不,如果你做对了,这三种方法都起作用!好的,我再试了一次,第二个也可以(我没有看到更新的版本)。我似乎无法找到第一种方法。
 Texture texture("ship.png");
 texture.Bind(0);
 Texture texture2("ship2.png");
 texture2.Bind(1);
 Texture::Texture(const std::string &path) {

     GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));

     // [...]

     GLCall(glBindTexture(GL_TEXTURE_2D, 0));
 }