我在OpenGL中制作游戏,我在世界空间中有一些对象.我想创建一个函数,我可以在其中获取对象的位置(3D)并将其转换为屏幕的位置(2D)并返回它.
我知道以下变量中对象的3D位置,投影矩阵和视图矩阵:
Matrix projectionMatrix;
Matrix viewMatrix;
Vector3 point3D;
Run Code Online (Sandbox Code Playgroud) 我最近一直在学习托管指针并遇到以下情况.
我正在为游戏视图实现模型/控制器类.我的观点,将在模型中呈现内容.挺直的.在我的main函数中,我将这三个实例化为:
RenderModel m;
m.AddItem(rect); // rect gets added just fine, it's an "entity" derivee
RenderView v;
v.SetModel(m);
Run Code Online (Sandbox Code Playgroud)
我的渲染视图类非常简单:
class RenderView
{
public:
explicit RenderView();
~RenderView();
void Update();
void SetModel(RenderModel& model);
private:
// disable
RenderView(const RenderView& other);
RenderView& operator=(const RenderView& other);
// private members
boost::scoped_ptr<RenderModel> _model;
};
Run Code Online (Sandbox Code Playgroud)
setView的实现非常标准:
void RenderView::SetModel(RenderModel& model)
{
_model.reset(&model);
}
Run Code Online (Sandbox Code Playgroud)
关键是,视图将模型存储在智能指针中.但是在main中,模型是在堆栈上分配的.程序退出时,内存将被删除两次.这是有道理的.我目前的理解告诉我,任何存储在smart_ptr(任何类型)中的东西都不应该在堆栈上分配.
完成上述所有设置之后,我的问题很简单:如何指示参数未在堆栈上分配?接受智能指针作为参数唯一的解决方案?即使这样,我也无法确保使用我的视图类的人不能做错误的事情,例如:
// If I implemented SetModel this way:
void RenderView::SetModel(const std::shared_ptr<RenderModel>& model)
{
_model.reset(&*model);
}
RenderModel m;
RenderView v;
std::shared_ptr<RenderModel> ptr(&m); // create a shared_ptr …Run Code Online (Sandbox Code Playgroud) 我很想进入模板元编程,慢慢地,我不知道如何实现以下内容:
// hpp file
enum MyEnum { Alive = 0, Dead };
class A {
public:
template<typename T, typename O, MyEnum ls>
static int Register();
};
// elsewhere in the code...
A::Register<IType1, Type1, Dead>();
Run Code Online (Sandbox Code Playgroud)
在编译时,我将知道第三个模板类型的枚举值(编译时不变量),死亡或活动.是否可以为Register函数定义两个实体,如:
// desired hpp file
template<typename T, typename O, Alive>
int Register();
template<typename T, typename O, Dead>
int Register();
// corresponding desired .inc file
template<typename T, typename O, Alive>
int Register() { // Alive specific implementation ... }
template<typename T, typename O, Dead>
int Register() { …Run Code Online (Sandbox Code Playgroud) 如果我有一个整数constexpr数组,N我该如何将其转换为合适的constexpr std::tuple<...>?
我正在尝试从教程中学习生锈.我认为康威的生活游戏将是一个很好的起点.
我无法理解如何编写这个Grid :: new()fn.
这是我到目前为止所拥有的:
enum Cell {
alive, dead
}
impl Cell {
fn new() -> Cell {
alive
}
struct Grid {
priv inner: [ [Cell, .. GRID_SIZE], .. GRID_SIZE],
}
impl Grid {
fn new() {
Grid { inner: ???? }
}
}
Run Code Online (Sandbox Code Playgroud)
...
fn main () {
let grid = Grid::new(); // Stack allocated grid (internal stack allocad array)
}
Run Code Online (Sandbox Code Playgroud)
我想要的是用网格初始化网格所有值'alive'.
我花了整个周末试图解决这个问题,我正在迈出最后一步.我的目标:让visual studio 2010和Qt 4.7.3一起工作.
我从源代码安装了Qt,指定使用以下配置进行构建:
configure.exe -debug-and-release -opensource -platform win32-msvc2010 -no-webkit -no-phonon -no-phonon-backend -no-script -no-scripttools -no-qt3support -no-multimedia -no-ltcg
配置完成后,我运行nmake,没问题.
在我的visual studio 2010解决方案中,我有两个项目.
它抱怨它无法链接Qt库.在Common属性中
AssetIO最初是使用Qt IDE构建的,我使用visual studio中的Qt插件导入项目.编译项目AssetIO工作得非常好.但是,编译Short项目会导致以下链接器错误:
右键单击Short项目,选择属性.我添加了AssetIO作为参考.单击配置属性VC++目录,我添加了以下Include目录:
以下是我为项目提供的库文件:
而不是发布更多的屏幕截图,我的AssetIO项目的包含目录是:C:\ qt_source\4.7.3\include
我的AssetIO项目的库目录是:C:\ qt_source\4.7.3\bin
这是我想要工作的项目的简单源代码(我的简单测试项目)
main.cpp
int main(int argc, char* argv[])
{
AssetIO::LevelLoader a;
a.dostuff();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
LevelLoader.h
#ifndef LEVELLOADER_HPP
#define LEVELLOADER_HPP
namespace AssetIO
{
class LevelLoader {
public:
explicit LevelLoader();
~LevelLoader();
void dostuff();
};
}
#endif // LEVELLOADER_HPP
Run Code Online (Sandbox Code Playgroud)
LevelLoader.cpp
#include "LevelLoader.hpp"
#include <QDomDocument>
#include <QFile>
#include <QDebug>
#include <QString>
using …Run Code Online (Sandbox Code Playgroud) 我正在探索从这个答案中获取constexpr char const*连接的程度: constexpr连接两个或多个char字符串
我有以下用户代码,准确显示我正在尝试做什么.似乎编译器无法看到函数参数(a和b)作为constexpr传入.
任何人都可以看到一种方法,使我指示的两个不在下面工作,实际工作?能够通过这样的函数组合字符数组是非常方便的.
template<typename A, typename B>
constexpr auto
test1(A a, B b)
{
return concat(a, b);
}
constexpr auto
test2(char const* a, char const* b)
{
return concat(a, b);
}
int main()
{
{
// works
auto constexpr text = concat("hi", " ", "there!");
std::cout << text.data();
}
{
// doesn't work
auto constexpr text = test1("uh", " oh");
std::cout << text.data();
}
{
// doesn't work
auto constexpr text = test2("uh", " oh");
std::cout << …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个函数,它根据枚举的运行时值将值的枚举映射到一组类型.我意识到你不能根据枚举的运行时值返回不同的类型,因为编译器不知道要分配多少堆栈空间.但是我正在尝试将其写为constexpr函数,使用新的if-constexpr功能来实现它.
我从clang抱怨我正在使用非法指定的模板参数时收到错误.有谁看到如何实现这个?
编辑:这是一个更容易理解的版本,更简洁地演示我的问题:http: //coliru.stacked-crooked.com/a/2b9fef340bd167a8
旧代码:
#include <cassert>
#include <tuple>
#include <type_traits>
namespace
{
enum class shape_type : std::size_t
{
TRIANGLE = 0u,
RECTANGLE,
POLYGON,
CUBE,
INVALID_SHAPE_TYPE
};
template<std::size_t T>
struct shape {
};
using triangle = shape<static_cast<std::size_t>(shape_type::TRIANGLE)>;
using rectangle = shape<static_cast<std::size_t>(shape_type::RECTANGLE)>;
using polygon = shape<static_cast<std::size_t>(shape_type::POLYGON)>;
using cube = shape<static_cast<std::size_t>(shape_type::CUBE)>;
template<std::size_t A, std::size_t B>
static bool constexpr same() noexcept { return A == B; }
template<std::size_t ST>
static auto constexpr make_impl(draw_mode const dm)
{
if constexpr (same<ST, shape_type::TRIANGLE>()) { …Run Code Online (Sandbox Code Playgroud) 我已经被困在这两天了,我不确定还能在哪里看。我正在使用 OpenGL 渲染两个 3d 立方体,并尝试对这些场景中的每个立方体应用局部旋转以响应我按下按钮。
我已经到了我的立方体在 3d 空间中旋转的地步,但它们都围绕世界空间原点旋转,而不是它们自己的本地原点。
(第二个视频) https://www.youtube.com/watch?v=3mrK4_cCvUw
网上搜了下,合适的MVP计算公式如下:
auto const model = TranslationMatrix * RotationMatrix * ScaleMatrix;
auto const modelview = projection * view * model;
Run Code Online (Sandbox Code Playgroud)
我的每个立方体都有自己的“模型”,其定义如下:
struct model
{
glm::vec3 translation;
glm::quat rotation;
glm::vec3 scale = glm::vec3{1.0f};
};
Run Code Online (Sandbox Code Playgroud)
当我按下键盘上的一个按钮时,我创建了一个代表新角度的四元数,并将它与之前的旋转四元数相乘,并就地更新它。
该函数如下所示:
template<typename TData>
void rotate_entity(TData &data, ecst::entity_id const eid, float const angle,
glm::vec3 const& axis) const
{
auto &m = data.get(ct::model, eid);
auto const q = glm::angleAxis(glm::degrees(angle), axis);
m.rotation = q * m.rotation;
// I'm a bit …Run Code Online (Sandbox Code Playgroud) 如果我有以下假设课程:
namespace System
{
template <class T>
class Container
{
public:
Container() { }
~Container() { }
}
}
Run Code Online (Sandbox Code Playgroud)
如果我实例化两个具有不同T的容器,请说:
Container<int> a;
Container<string> b;
Run Code Online (Sandbox Code Playgroud)
我想用指向a和b的指针创建向量.由于a和b是不同的类型,通常这是不可能的.但是,如果我做了类似的事情:
std::stack<void*> _collection;
void *p = reinterpret_cast<void*>(&a);
void *q = reinterpret_cast<void*>(&b);
_collection.push(a);
_collection.push(b);
Run Code Online (Sandbox Code Playgroud)
然后,我可以从_collection返回a和b,如下所示:
Container<string> b = *reinterpret_cast<Container<string>*>(_collection.pop());
Container<int> a = *reinterpret_cast<Container<int>*>(_collection.pop());
Run Code Online (Sandbox Code Playgroud)
我的问题是,这是存储一组不相关类型的最佳方式吗?这也是存储和检索向量指针(重新解释转换)的首选方法吗?我环顾四周,看到提升有一个更好的解决方法,Boost :: Any,但由于这是一个学习项目,我想自己做(我也很好奇找到一个很好的理由正确使用reinterpret_cast).
我一整天都在这工作,所以我希望我不会忘记任何重要的细节,但是这里有.我最初的目标是拥有一个封装了如何创建播放器的逻辑的播放器工厂.
这是看起来像:
Player PlayerFactory::CreatePlayer() {
Player constructee(id_generator++);
// c++1x move semantics says this isn't a copy!
return constructee;
}
Run Code Online (Sandbox Code Playgroud)
在播放器中,有一个复合的PlayerInputComponent成员变量.这个PlayerInputComponent对象封装了处理玩家输入的逻辑,为此,它需要一个指向实际玩家本身的引用/指针.很容易,它引用了播放器,因为它是它的构造函数的唯一参数.
构造播放器对象时,它的初始化列表将对自身的引用传递给PlayerInputComponent对象.以下是它的外观:
Player::Player(const unsigned int id)
: id(id),
input_component(*this)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我知道在初始化列表中取消引用它通常是一个坏主意,但我只是用它在PlayerInputComponent对象中设置引用.这是构造函数:
PlayerInputComponent::PlayerInputComponent(Player &player)
: player_entity(player) { // set the reference, it is never used within the constructor body
}
Run Code Online (Sandbox Code Playgroud)
无论出于何种原因,当玩家工厂返回其创建的玩家的本地副本时,引用会出现乱码.我的意图是,在堆栈上创建的玩家的实例被移动并分配给被调用者.这看起来如下:
auto player = player_factory.CreatePlayer();
Run Code Online (Sandbox Code Playgroud)
代码执行后,播放器的复合PlayerInputComponent对象对它的引用被破坏了.我知道当工厂将本地播放器对象返回给被调用者时,会调用播放器的移动构造函数,但是PlayerInputComponent中播放器的引用会发生什么?有没有更好的方法来解决这个问题?我喜欢在这种情况下使用引用vs指针的语义,虽然我确实用指针尝试它并得到相同的东西.
任何人都可以向我解释当玩家对象移出CreatePlayer()成员函数并分配给"自动播放器"时,PlayerInputComponent对玩家的引用会发生什么?
为了完整性,这里是对象的声明:
class PlayerInputComponent {
private:
Player &player_entity;
void HandleKeyboardInput();
public:
//PlayerInputComponent(PlayerInputComponent &&other);
//PlayerInputComponent(const PlayerInputComponent &other);
PlayerInputComponent(Player &player);
void Update();
};
Run Code Online (Sandbox Code Playgroud)
这是玩家:
class Player …Run Code Online (Sandbox Code Playgroud) 我有以下层次结构:
我有以下层次结构:
GameStateBaseClass -> IGameStateInterface -> IntroState
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是当我使用GameEngine引用实例化一个IntroState时(我在GameStateBaseClass中定义)我得到以下错误:
错误1错误C2664:'IntroState :: IntroState(const IntroState&)':无法将参数1从'GameEngine'转换为'const Short :: IntroState&'
在GameStateBaseClass中,我定义了一个带有const GameState引用的构造函数,在main.cpp中我传入了一个游戏引擎的实例.为什么它试图将我的GameEngine参数转换为IntroState引用呢?
这是相应的代码:
GameStateBaseClass.hpp
class GameStateBaseClass
{
public:
GameStateBaseClass(const GameEngine &instance);
private:
GameStateBaseClass(void); // = delete; // c++1x
GameStateBaseClass(const GameStateBaseClass &instance); // = delete; // c++1x
GameStateBaseClass operator=(const GameStateBaseClass &instance); // = delete; // c++1x
// private members
const GameEngine &game_engine_instance;
}
Run Code Online (Sandbox Code Playgroud)
GameStateBaseClass.cpp
GameStateBaseClass::GameStateBaseClass(const GameEngine &instance)
: game_engine_instance(instance) {
}
// IGameStateInterface.hpp
class IGameStateInterface : GameStateBaseClass
{
public:
virtual void Init() = 0;
virtual void …Run Code Online (Sandbox Code Playgroud)