【发布时间】:2021-07-17 01:44:45
【问题描述】:
我有以下来自 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 的值,这导致渲染失败。问题可能出在哪里?
【问题讨论】:
-
opengl也可以渲染值大于1的顶点吗?
-
看起来一点也不像 C :P
-
3D 模型不限于任何特定尺寸。 OpenGL 对此也没有任何限制。它的唯一限制是投影后的顶点必须在所有轴上的 [-1, 1] 范围内才能可见。
-
我再次检查了 3D 模型,是的,有大于 1 的值,因此上面的代码按预期工作。使用 renderdoc 我已经能够看到 position 的缓冲区内容以像
1.1204E-44这样的小数字结束。所有缓冲区内容都以某种方式设置为如此小的指数数字。