[英]Assimp opengl load model vertices some values greater than 1
我有以下來自 learnopengl.com 的代碼
void Model::load_model(string path)
{
//read file via ASSIMP
Assimp::Importer Importer;
const aiScene* scene = Importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
//check for errors
if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)// if not zero
{
cout << "error, assimp ," << Importer.GetErrorString() << endl;
return;
}
//retrieve the directory path of the filepath
directory = path.substr(0, path.find_first_of('/'));
process_node(scene->mRootNode, scene);
}
/*
* Process a node in a recursive fashion . Process each individual mesh located at the node and repeat this process on its children nodes (if any)
*/
void Model::process_node(aiNode* node, const aiScene* scene)
{
for( GLuint i = 0; i < node->mNumMeshes; i++ )
{
//the node object only contains indices to index the actual objects of the scene.
//The scene contains all the data , node is just to keep stuff organized( like relations between nodes )
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(process_mesh(mesh, scene));
}
//after we've processed all the meshes ( if any ) we then recusrsively process each of the children nodes
for(GLuint i = 0; i < node->mNumChildren; i++)
{
process_node(node->mChildren[i], scene);
}
}
Mesh Model::process_mesh(aiMesh* mesh, const aiScene* scene)
{
//data to fill
vector<Mesh::Vertex> vertices;
vector<GLuint> indices;
vector<Mesh::Texture> textures;
//walk through each of the meshes vertices
for(GLuint i = 0; i < mesh->mNumVertices; i++)
{
Mesh::Vertex vertex;
// we declare a placeholder vector since assimp uses its own vector class that doesn't directly convert to glm's vec3 class
//so we transfter the data to this placeholder glm::vec3 first
glm::vec3 vector;
//positions
vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
vertex.position = vector;
//normals
vector.x = mesh->mNormals[i].x;
vector.y = mesh->mNormals[i].y;
vector.z = mesh->mNormals[i].z; ...
當我打印出mesh->mVertices[i].x
y 和 z 的前 20 個值時,我得到一些值大於 1 的值,如下所示
x1: 1.58967 Y1: -0.618526 z1: -0.683333
x1: 1.58939 Y1: -0.626895 z1: -0.681676
我正在導入的 obj 文件沒有任何大於 1 的值,這導致渲染失敗。 問題可能出在哪里?
model 的位置與其 model 坐標系有關。 所以它們可以變得更大或小於 1、-1 或其他。 OpenGL 不限於任何值的 [0...1] 范圍。 因此,當您的渲染器需要從 0 到 1 的范圍時,您需要:
找到坐標的最小值和最大值
重新縮放所有坐標:
浮動比例 = 1.0/(最大值 - 最小值); foreach ( v: mesh.Vertices) v=v*scale;
或者重寫你的渲染器,他能夠渲染任意坐標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.