- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我将 C++ 和 OpenGL 4.6 与 SDL2 和 glew 一起使用。我有一个类纹理:
#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));
}
如果我创建一个纹理,纹理会显示:
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 个小时,但我似乎无法找到这行不通的原因。你们能帮帮我吗?
最佳答案
在下面几行代码的末尾
Texture texture("ship.png");
texture.Bind(0);
Texture texture2("ship2.png");
texture2.Bind(1);
第二个纹理对象(“ship2.png”)绑定(bind)到纹理单元 1,但绑定(bind)到纹理单元 0 的是默认纹理对象 (0)。
请注意,当 Texture texture("ship.png");
被调用时,纹理对象被创建并绑定(bind)到当前纹理单元,之后默认纹理对象 (0) 被绑定(bind)到当前纹理单元:
Texture::Texture(const std::string &path) {
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
// [...]
GLCall(glBindTexture(GL_TEXTURE_2D, 0));
}
请注意,您所说的“解除绑定(bind)”,并没有解除任何绑定(bind)。 glBindTexture(GL_TEXTURE_2D, 0)
将默认纹理对象 (0) 绑定(bind)到当前事件的纹理单元。没有必要这样做。
只需更改说明的顺序即可解决您的问题。首先创建并加载纹理对象。然后将纹理对象绑定(bind)到纹理单元:
Texture texture("ship.png");
Texture texture2("ship2.png");
texture.Bind(0);
texture2.Bind(1);
shader.Bind();
va.Bind();
ib.Bind();
shader.SetUniform1i("u_Texture", 0);
glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr);
作为替代方案,您可以避免使用纹理单元 0。使用纹理单元 0 仅用于创建纹理对象:
Texture::Texture(const std::string &path, unsigned int slot) {
GLCall(glActiveTexture(GL_TEXTURE0));
// [...]
}
Texture texture("ship.png");
texture.Bind(1); // <--- 1
Texture texture2("ship2.png");
texture2.Bind(2); // <--- 2
shader.Bind();
va.Bind();
ib.Bind();
shader.SetUniform1i("u_Texture", 1); // <--- 1
glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, nullptr);
或者在类Texture
的构造函数中添加一个纹理单元的参数:
Texture::Texture(const std::string &path, unsigned int slot) {
GLCall(glGenTextures(1, &m_RendererID));
GLCall(glActiveTexture(GL_TEXTURE0 + slot));
GLCall(glBindTexture(GL_TEXTURE_2D, m_RendererID));
// [...]
// GLCall(glBindTexture(GL_TEXTURE_2D, 0)); <--- delete
}
Texture texture("ship.png", 0);
Texture texture2("ship2.png", 1);
关于c++ - 如果现代 opengl 中有多个纹理,则没有纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54963309/
我有一个库(围绕nlohmann / json封装),可以从JSON反序列化: struct MyStruct { int propertyA; std::string propert
如果 的第 1、3、5、7、9、11、13 或 15 位之一,我希望 var 不等于 FALSE输入已设置。 一个似乎相当普遍的解决方案是: int var = 1 & (input >> 1) |
当我说目标类型时,我的意思是使用接收者变量或参数的类型作为信息来推断我分配给它的部分代码。例如,在 C# 中,您会编写类似这样的内容来传递可为 null 的值或 null (空)如有必要: void
我需要从 native 内存读取/写入一堆结构。我想弄清楚我是否应该为结构对齐而烦恼。这是我编写的用于测试的简单代码。它将压缩结构写入未对齐的指针,然后读回该结构: public static uns
采用以下代码: char chars[4] = {0x5B, 0x5B, 0x5B, 0x5B}; int* b = (int*) &chars[0]; (int*) &chars[0] 值将在循环(
因此,当我发现将整个解决问题的方法颠倒过来时,我正在网上搜索最佳实践,以实现使用多个数据存储的存储库模式。这就是我所拥有的... 我的应用程序是一个BI工具,它从四个数据库中提取数据。由于内部限制,我
我想仅使用现代 OpenGL 技术(即没有即时模式的东西)来设置正交投影。我在网络上看到有关如何处理此问题的相互矛盾的信息。 有些人说调用 glMatrixMode(GL_PROJECTION) 然后
我想知道当前的 cpus 是否避免在其中至少一个为零时将两个数字相乘。谢谢 最佳答案 这取决于 CPU 和(在某些情况下)操作数的类型。 较旧/较简单的 CPU 通常使用如下乘法算法: integer
在精美的 OpenGL 新版本(3.0 和 4.0 以上)中,不推荐使用 gl_Vertex 等内置顶点属性 .实际渲染任何东西的“新方法”是为位置、颜色等指定您自己的顶点属性,然后将这些自定义属性绑
在我的 OpenGL 研究(我认为是 OpenGL 红皮书)中,我遇到了一个关节机器人 ARM 模型的示例,该模型由“上臂”、“下臂”、“手”和五个或更多“手指”。每个部分都应该能够独立移动,但受“关
像 Kaby Lake 这样的现代 CPU 如何处理小分支? (在下面的代码中,它是跳转到标签 LBB1_67)。据我所知,分支不会有害,因为跳转低于 16 字节块大小,即解码窗口的大小。 或者是否有
编辑:此问题假设您启用了发生检查。不是关于 setting Prolog flags . 30 年前有很多关于在安全的情况下自动优化发生检查的论文(大约 90% 的谓词,在典型的代码库中)。提出了不同
现在是 2020 年,在 iOS 终于添加了对 Widget 的支持之后,Widget 再次风靡一时。但是,自 2012 年以来,Android 小部件似乎没有更新。 来自 Android docs
我正在看一些关于算法的讲座,教授用乘法作为如何改进朴素算法的例子...... 它让我意识到乘法并不是那么明显,虽然当我编码时我只是认为它是一个简单的原子操作,乘法需要一个算法来运行,它不像求和数字那样
我们将 PIXI.js 用于内部使用 WebGL 进行渲染的游戏。时不时地,我会偶然发现避免 NPOT 纹理(https://developer.mozilla.org/en-US/docs/Web/
我是一名计算机科学专业的学生,即将毕业。我们现在必须用我们选择的语言编写完整的应用程序。我们选择 Objective-C 因为我们都是 Mac 人。 为了让我们的教授高兴,必须做一些事情:-)一项
我正在编写一个带有 x86 后端的 JIT 编译器,并且正在学习 x86 汇编器和机器代码。大约 20 年前,我使用 ARM 汇编程序,并对这些架构之间的成本模型差异感到惊讶。 具体来说,内存访问和分
如果负载与两个较早的存储重叠(并且负载未完全包含在最旧的存储中),现代 Intel 或 AMD x86 实现能否从两个存储转发以满足负载? 例如,考虑以下序列: mov [rdx + 0], eax
http://www.lighthouse3d.com/opengl/glsl/index.php?ogldir2 报告 OpenGL 上下文中的半向量是“眼睛位置 - 灯光位置”,但接着又说“幸运的
在现代 (GL3.3+) GPU 上使用 GLSL 时,在统一上进行分支的可能成本是多少? 在我的引擎中,我已经达到了拥有大量着色器的程度。我为其中的很多预设了几种不同的质量预设。就目前情况而言,我在
我是一名优秀的程序员,十分优秀!