构造函数在退出函数时不能初始化一个参数
Constructor cannot Initialize one parameter when it exits the function
我的 C++ 代码存在一个问题。我正在使用 Assimp 库,并且我有一个 class,它有一个 const aiScene*
类型的属性。在 class 的构造函数中,我分配了这个值并且一切正常,但是当我退出构造函数代码时,参数 const aiScene*
没有分配任何数据
我把代码贴在这里。
Model.h
class Model
{
public:
Model(std::string objectLoc, std::string vertexLoc, std::string fragmentLoc,
glm::vec3 position, glm::vec3 scale, glm::vec3 rotation,
Camera* camera);
void render();
~Model();
private:
std::string objectLoc, vertexLoc, fragmentLoc;
Shader* shader;
glm::mat4 model;
glm::vec3 position, scale, rotation;
Camera* camera;
std::vector<Mesh*> meshes;
std::vector<Texture*> textures;
std::vector<unsigned int> meshToTex;
std::unordered_map<std::string, GLuint> boneMapping;
std::vector<glm::mat4> bonesTransformations;
const aiScene* scene;
glm::mat4 globalInverseTransform;
GLuint boneCount;
};
Model.cpp
Model::Model(std::string objectLoc, std::string vertexLoc, std::string fragmentLoc, glm::vec3
position, glm::vec3 scale, glm::vec3 rotation, Camera* camera)
: objectLoc(objectLoc), vertexLoc(vertexLoc), fragmentLoc(fragmentLoc),
position(position), scale(scale), rotation(rotation),
camera(camera),
boneCount(0)
{
shader = new Shader(vertexLoc, fragmentLoc,
DirectionalLight(glm::vec3(1.0f, 1.0f, 1.0f), 0.4f, 0.1f, glm::vec3(0.0f, 0.0f, -1.0f)),
SpecularLight(1.0f, 250.0f),
SpotLight(glm::vec3(1.0f, 1.0f, 1.0f), 0.0f, 1.0f, glm::vec3(0.0f, 0.0f, 0.0f), 1.0f, 0.0f, 0.0f,
glm::vec3(0.0f, 0.0f, -1.0f), glm::radians(10.0f)));
shader->addPointLight(PointLight(glm::vec3(1.0f, 0.0f, 0.0f), 0.2f, 0.2f, glm::vec3(0.0f, 0.0f,
-15.0f), 0.3f, 0.2f, 0.1f));
shader->addPointLight(PointLight(glm::vec3(0.0f, 1.0f, 0.0f), 0.2f, 0.2f, glm::vec3(10.0f, 0.0f,
-15.0f), 0.3f, 0.2f, 0.1f));
shader->addPointLight(PointLight(glm::vec3(0.0f, 0.0f, 1.0f), 0.2f, 1.2f, glm::vec3(-10.0f, 0.0f,
-15.0f), 0.8f, 0.2f, 0.1f));
model = glm::translate(glm::mat4(1.0f), position)
* glm::scale(glm::mat4(1.0f), scale)
* glm::rotate(glm::mat4(1.0f), glm::radians(rotation.x), glm::vec3(1, 0, 0))
* glm::rotate(glm::mat4(1.0f), glm::radians(rotation.y), glm::vec3(0, 1, 0))
* glm::rotate(glm::mat4(1.0f), glm::radians(rotation.z), glm::vec3(0, 0, 1));
Assimp::Importer importer;
scene = importer.ReadFile(objectLoc,
aiProcess_Triangulate |
aiProcess_FlipUVs |
aiProcess_GenSmoothNormals |
aiProcess_JoinIdenticalVertices);
if (!scene)
{
printf("Cannot load model %s: %s\n", objectLoc.c_str(), importer.GetErrorString());
return;
}
aiNode* rootNode = scene->mRootNode;
globalInverseTransform = aiMatrix4x4ToGlm(rootNode->mTransformation.Inverse());
loadNode(rootNode);
loadTextures();
}
我已经尝试了所有方法,从一个一个地复制属性到对象场景,到在所有涉及的class中制作一个克隆方法,但没有。甚至,我认为问题可能是场景属性中的 const
标识符,但如果我使对象不是常量,它也会错误地工作。
我没有在模型的任何方法中修改该属性 class。
我给你贴了一些问题的照片。
Good result in the constructor
Bad result out of the constructor
如果你能帮助我,那就太好了。
文档说
The returned data is intended to be read-only, the importer object keeps ownership of the data and will destroy it upon destruction.
当构造函数离开 Assimp::Importer importer;
并且 scene
指向的对象被销毁时。
你必须在离开构造函数之前复制它或者
Use GetOrphanedScene()
to take ownership of it.
aiScene* Assimp::Importer::GetOrphanedScene ( )
Returns the scene loaded by the last successful call to ReadFile() and releases the scene from the ownership of the Importer instance.
The application is now responsible for deleting the scene. Any further calls to GetScene() or GetOrphanedScene() will return NULL - until a new scene has been loaded via ReadFile().
Returns:
Current scene or NULL if there is currently no scene loaded
Note:
Use this method with maximal caution, and only if you have to. By design, aiScene's are exclusively maintained, allocated and deallocated by Assimp and no one else. The reasoning behind this is the golden rule that deallocations should always be done by the module that did the original allocation because heaps are not necessarily shared. GetOrphanedScene() enforces you to delete the returned scene by yourself, but this will only be fine if and only if you're using the same heap as assimp. On Windows, it's typically fine provided everything is linked against the multithreaded-dll version of the runtime library. It will work as well for static linkage with Assimp.
我的 C++ 代码存在一个问题。我正在使用 Assimp 库,并且我有一个 class,它有一个 const aiScene*
类型的属性。在 class 的构造函数中,我分配了这个值并且一切正常,但是当我退出构造函数代码时,参数 const aiScene*
没有分配任何数据
我把代码贴在这里。
Model.h
class Model
{
public:
Model(std::string objectLoc, std::string vertexLoc, std::string fragmentLoc,
glm::vec3 position, glm::vec3 scale, glm::vec3 rotation,
Camera* camera);
void render();
~Model();
private:
std::string objectLoc, vertexLoc, fragmentLoc;
Shader* shader;
glm::mat4 model;
glm::vec3 position, scale, rotation;
Camera* camera;
std::vector<Mesh*> meshes;
std::vector<Texture*> textures;
std::vector<unsigned int> meshToTex;
std::unordered_map<std::string, GLuint> boneMapping;
std::vector<glm::mat4> bonesTransformations;
const aiScene* scene;
glm::mat4 globalInverseTransform;
GLuint boneCount;
};
Model.cpp
Model::Model(std::string objectLoc, std::string vertexLoc, std::string fragmentLoc, glm::vec3
position, glm::vec3 scale, glm::vec3 rotation, Camera* camera)
: objectLoc(objectLoc), vertexLoc(vertexLoc), fragmentLoc(fragmentLoc),
position(position), scale(scale), rotation(rotation),
camera(camera),
boneCount(0)
{
shader = new Shader(vertexLoc, fragmentLoc,
DirectionalLight(glm::vec3(1.0f, 1.0f, 1.0f), 0.4f, 0.1f, glm::vec3(0.0f, 0.0f, -1.0f)),
SpecularLight(1.0f, 250.0f),
SpotLight(glm::vec3(1.0f, 1.0f, 1.0f), 0.0f, 1.0f, glm::vec3(0.0f, 0.0f, 0.0f), 1.0f, 0.0f, 0.0f,
glm::vec3(0.0f, 0.0f, -1.0f), glm::radians(10.0f)));
shader->addPointLight(PointLight(glm::vec3(1.0f, 0.0f, 0.0f), 0.2f, 0.2f, glm::vec3(0.0f, 0.0f,
-15.0f), 0.3f, 0.2f, 0.1f));
shader->addPointLight(PointLight(glm::vec3(0.0f, 1.0f, 0.0f), 0.2f, 0.2f, glm::vec3(10.0f, 0.0f,
-15.0f), 0.3f, 0.2f, 0.1f));
shader->addPointLight(PointLight(glm::vec3(0.0f, 0.0f, 1.0f), 0.2f, 1.2f, glm::vec3(-10.0f, 0.0f,
-15.0f), 0.8f, 0.2f, 0.1f));
model = glm::translate(glm::mat4(1.0f), position)
* glm::scale(glm::mat4(1.0f), scale)
* glm::rotate(glm::mat4(1.0f), glm::radians(rotation.x), glm::vec3(1, 0, 0))
* glm::rotate(glm::mat4(1.0f), glm::radians(rotation.y), glm::vec3(0, 1, 0))
* glm::rotate(glm::mat4(1.0f), glm::radians(rotation.z), glm::vec3(0, 0, 1));
Assimp::Importer importer;
scene = importer.ReadFile(objectLoc,
aiProcess_Triangulate |
aiProcess_FlipUVs |
aiProcess_GenSmoothNormals |
aiProcess_JoinIdenticalVertices);
if (!scene)
{
printf("Cannot load model %s: %s\n", objectLoc.c_str(), importer.GetErrorString());
return;
}
aiNode* rootNode = scene->mRootNode;
globalInverseTransform = aiMatrix4x4ToGlm(rootNode->mTransformation.Inverse());
loadNode(rootNode);
loadTextures();
}
我已经尝试了所有方法,从一个一个地复制属性到对象场景,到在所有涉及的class中制作一个克隆方法,但没有。甚至,我认为问题可能是场景属性中的 const
标识符,但如果我使对象不是常量,它也会错误地工作。
我没有在模型的任何方法中修改该属性 class。
我给你贴了一些问题的照片。
Good result in the constructor
Bad result out of the constructor
如果你能帮助我,那就太好了。
文档说
The returned data is intended to be read-only, the importer object keeps ownership of the data and will destroy it upon destruction.
当构造函数离开 Assimp::Importer importer;
并且 scene
指向的对象被销毁时。
你必须在离开构造函数之前复制它或者
Use
GetOrphanedScene()
to take ownership of it.
aiScene* Assimp::Importer::GetOrphanedScene ( )
Returns the scene loaded by the last successful call to ReadFile() and releases the scene from the ownership of the Importer instance.The application is now responsible for deleting the scene. Any further calls to GetScene() or GetOrphanedScene() will return NULL - until a new scene has been loaded via ReadFile().
Returns: Current scene or NULL if there is currently no scene loaded
Note: Use this method with maximal caution, and only if you have to. By design, aiScene's are exclusively maintained, allocated and deallocated by Assimp and no one else. The reasoning behind this is the golden rule that deallocations should always be done by the module that did the original allocation because heaps are not necessarily shared. GetOrphanedScene() enforces you to delete the returned scene by yourself, but this will only be fine if and only if you're using the same heap as assimp. On Windows, it's typically fine provided everything is linked against the multithreaded-dll version of the runtime library. It will work as well for static linkage with Assimp.