我正在尝试在我的 OpenGL 程序中实现透明度。目前我有这样的功能(最小的可重现示例):
#include <map>
#include <unordered_map>
#include <iostream>
#include <vector>
class Sprite
{
public:
std::vector<int> m_meshes;
std::pair<float, float> pos;
};
int main()
{
std::unordered_map<int, Sprite> m_sprites = {
{ 0, { { 43 } } },
{ 1, { { 234 } } }
};
std::multimap<int, const Sprite*, std::greater<int>> translucentSprites{};
for (const std::pair<int, Sprite>& spritePair : m_sprites)
{
translucentSprites.insert({ spritePair.second.pos.first + spritePair.second.pos.second, &spritePair.second});
std::cout << translucentSprites.begin()->second->m_meshes.size() << ' ';
}
std::cout << translucentSprites.begin()->second->m_meshes.size() << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行此代码后,我得到以下结果: …
我创建了一个程序(C++/OpenGL),它使用 OpenSimplex 噪声生成景观并将其绘制为一个网格(因此我必须每帧仅调用一次绘制函数)。我正在使用glDrawElements函数。网格包含 1023 * 1023 个矩形 = 1023 * 1023 * 2 个三角形,景观看起来像 PS1 游戏中的一样,并使用超过 500 MB 的 RAM。如何在不使用这么多三角形的情况下制作平滑的景观(就像在现代游戏中一样)?
编辑:拥有少量不同详细的 VBO,并从最详细的 VBO 绘制最近的三角形,从不太详细的 VBO 绘制较远的三角形,是否会有效?
我最近开始学习 C++ 中的智能指针和移动语义。但我不明白为什么这段代码有效。我有这样的代码:
#include <iostream>
#include <memory>
using namespace std;
class Test
{
public:
Test()
{
cout << "Object created" << endl;
}
void testMethod()
{
cout << "Object existing" << endl;
}
~Test()
{
cout << "Object destroyed" << endl;
}
};
int main(int argc, char *argv[])
{
Test* testPtr = new Test{};
{
unique_ptr<Test> testSmartPtr(testPtr);
}
testPtr->testMethod();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的输出是:
Object created
Object destroyed
Object existing
Run Code Online (Sandbox Code Playgroud)
为什么行testPtr->testMethod()有效?如果指针是左值,unique_ptr 不会在销毁时删除分配给它的指针吗?
编辑:我从评论中了解到,此方法不会检查指针是否存在。如果是这样,有没有办法检查指针是否有效?
编辑:我了解到我不应该对无效指针做任何事情。感谢您的所有回答和评论。
编辑:即使这个代码也有效:
#include <iostream>
#include <memory> …Run Code Online (Sandbox Code Playgroud) 我有std::vector<std::map<int, std::unique_ptr<int>>>容器(如果为了简化)。最初,我必须插入std::vector一定数量的std::map,每个都有一个键值对。我尝试过这样的代码:
#include <iostream>
#include <map>
#include <vector>
#include <memory>
using namespace std;
int main()
{
vector<map<int, std::unique_ptr<int>>> data{};
for (int x = 0; x < 10; x++)
{
data.emplace_back(std::make_pair(x, std::make_unique<int>(x)));
}
}
Run Code Online (Sandbox Code Playgroud)
但这没有用。我怎样才能改变它以使其按预期工作?
编辑:我明白我的错误,我必须使用这段代码来添加元素:
std::map<int, std::unique_ptr<int>> m;
m.emplace(x, std::make_unique<int>(y));
data.emplace_back(std::move(m));
Run Code Online (Sandbox Code Playgroud)
然后,该代码可以在在线编译器中运行,但在 Visual Studio 中仍然无法运行。