- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两种不同的方法可以将元素添加到 vector 中。
GUI_Vertices.emplace_back();
GUI_Vertices.back().pos.x = ((float)x / 400) - 1.f;
GUI_Vertices.back().pos.y = ((float)y / 300) - 1.f;
GUI_Vertices.back().texCoord.x = u;
GUI_Vertices.back().texCoord.y = v;
GUI_Vertices.back().color.r = m_Color.r / 128;
GUI_Vertices.back().color.g = m_Color.g / 128;
GUI_Vertices.back().color.b = m_Color.b / 128;
GUI_Vertices.back().color.a = m_Color.a / 128;
GUI_Vertices
vector 添加一个新元素。
Vertex NewVertex;
NewVertex.pos.x = ((float)x / 400) - 1.f;
NewVertex.pos.y = ((float)y / 300) - 1.f;
NewVertex.texCoord.x = u;
NewVertex.texCoord.y = v;
NewVertex.color.r = m_Color.r / 128;
NewVertex.color.g = m_Color.g / 128;
NewVertex.color.b = m_Color.b / 128;
NewVertex.color.a = m_Color.a / 128;
GUI_Vertices.emplace_back(NewVertex);
NewVertex
添加到
GUI_Vertices
vector 中。
Vertex
的定义:
struct Vertex {
glm::vec3 pos;
glm::vec4 color;
glm::vec2 texCoord;
static VkVertexInputBindingDescription getBindingDescription() {
VkVertexInputBindingDescription bindingDescription = {};
bindingDescription.binding = 0;
bindingDescription.stride = sizeof(Vertex);
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
return bindingDescription;
}
static std::array<VkVertexInputAttributeDescription, 3> getAttributeDescriptions() {
std::array<VkVertexInputAttributeDescription, 3> attributeDescriptions = {};
attributeDescriptions[0].binding = 0;
attributeDescriptions[0].location = 0;
attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
attributeDescriptions[0].offset = offsetof(Vertex, pos);
attributeDescriptions[1].binding = 0;
attributeDescriptions[1].location = 1;
attributeDescriptions[1].format = VK_FORMAT_R32G32B32A32_SFLOAT;
attributeDescriptions[1].offset = offsetof(Vertex, color);
attributeDescriptions[2].binding = 0;
attributeDescriptions[2].location = 2;
attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;
attributeDescriptions[2].offset = offsetof(Vertex, texCoord);
return attributeDescriptions;
}
bool operator==(const Vertex& other) const {
return pos == other.pos && color == other.color && texCoord == other.texCoord;
}
};
namespace std {
template<> struct hash<Vertex> {
size_t operator()(Vertex const& vertex) const {
return ((hash<glm::vec3>()(vertex.pos) ^
(hash<glm::vec4>()(vertex.color) << 1)) >> 1) ^
(hash<glm::vec2>()(vertex.texCoord) << 1);
}
};
}
Vertex
元素添加到
GUI_Vertex
vector 后,我对
GUI_Vertex
执行以下操作:
memcpy(GUI_VertexAllocation->GetMappedData(), GUI_Vertices.data(), sizeof(Vertex) * GUI_Vertices.size());
GUI_Vertices
复制到预分配的缓冲区中,Vulkan将使用该缓冲区来渲染我们的顶点。
Vertex
对象添加到
GUI_Vertices
中的第一种方法始终有效,而第二种方法有时仅可行。
GUI_Vertices
的有效性,直到执行
memcpy
为止,并且数据似乎是有效的,所以我不确定发生了什么。
最佳答案
NewVertex.pos.x = ((float)x / 400) - 1.f;
NewVertex.pos.y = ((float)y / 300) - 1.f;
...
glm::vec3 pos;
emplace_back
将始终对其创建的对象执行值初始化,该对象将初始化所有数据成员。相比之下,
Vertex NewVertex;
将默认初始化该对象,从而使其成员保持未初始化状态(因为GLM类型具有琐碎的默认构造函数)。
pos.z
未初始化。而且您的代码不会自行初始化。因此,您要将未初始化的垃圾发送到GPU。
Vertex NewVertex{};
创建对象,则将像
emplace_back
一样对它进行值初始化。
关于c++ - vector Push_Back VS Emplace_Back,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59009614/
这个问题在这里已经有了答案: Passing by value vs const & and && overloads (3 个答案) 关闭 8 年前。 为什么push_back的函数签名如下? v
代码如下: std::vector s; s.push_back(~Dword(0)); 什么是~Dword?它是如何工作的? 最佳答案 Dword 这里是一个数字类型(可能是 DWORD 的类型别名
我正在测试 C++ 中推回对象与推回对象指针到 Vector 之间的性能差异。 我在 Stackoverflow 和其他文章中读到,您应该避免向后推指针,除非您必须这样做... 但是,我意识到推回指针
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我有一个很大的 .txt 文件,需要加载并存储在 vector 中。该文件大小约为 5MB,500 000 行,每行约 10-20 个字符,以 '\n' 分隔。我正在使用以下示例代码对读取整个文件所需
这个问题在这里已经有了答案: What is object slicing? (18 个答案) 关闭 9 年前。 我有这两个类: class A { public: A(); vir
在我当前的代码中,我想将新的 DrawObjects 插入到我创建的 vector 中, std::vector 对象; 有什么区别: objects.push_back(DrawObject(nam
我将 Cygwin 与 GCC 一起使用,最终我想将字 rune 件读入字符 vector ,并使用此代码 #include #include #include using namespace
以下代码创建一个临时对象 A 并将其推送到一个 vector 中。 在 push_back 期间删除复制构造函数并调用移动构造函数。我不确定这段代码的设计是否正确,肯定存在内存泄漏。 #include
我知道push_back可以抛出bad_alloc异常,并且如果没有try catch block ,则调用析构函数是不正确的。如果有任何push_back抛出并且它不在try catch block
正如 Scott Meyers 所指出的(http://channel9.msdn.com/Events/GoingNative/2013/An-Effective-Cpp11-14-Sampler
我正在写一个 push_back将临时容器添加到另一个容器的函数。 并且它应该在使用 push_back 之前调整或保留容器(如果两者都可用,它应该更喜欢保留而不是调整大小) 当前代码是: names
这个问题在这里已经有了答案: Create an array when the size is a variable not a constant (2 个答案) 关闭 3 年前。 我认为我在代码中
代码: // test2.cpp #include #include struct test_class { test_class() = default; test_class(
我试图在 forloop 的 vector 中推回一个 const char*。 char_temp 是一个名为 segment 的结构,而 chars_temp 是一个结构 vector 。请参阅下
在 C++ 入门书第 (3) 章中,有以下 for 循环将 vector 中的元素重置为零。 vector ivec; //UPDATE: vector declaration for (vector
class A { public: A():a(0) {} A(int x):a(x) { coutve
我正在尝试使用 Visual Leak Detector 查找内存泄漏。它告诉我 m_neighbors.push_back(ent);导致泄漏。 (简短调用堆栈 = NeighborCalculat
我们正在制作一个包含棋盘游戏信息(名称、年份、分数)的列表。我们从 .csv 文件中扫描信息,根据该信息创建一个结构,然后将该结构添加到列表中。我们一直这样做,直到文档阅读完毕。问题是列表的 push
以下代码是将“非重叠”的 TablePath 从 vector v 移动到 vector u。我在“u.push_back(*it1);”行遇到段错误。我没有复制对象(而是只复制对象的指针)所以我相信
我是一名优秀的程序员,十分优秀!