Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parsing gltf file #97

Open
sancelot opened this issue Jan 14, 2020 · 5 comments
Open

parsing gltf file #97

sancelot opened this issue Jan 14, 2020 · 5 comments

Comments

@sancelot
Copy link

Hi,
I am trying to decode triangles from gltf file.
Unfortunately, I don't manage to reach vertex data , what may be wrong ?

for (int m=0;m < mesh->primitives_count;m++)
  {
    cgltf_primitive *primitive = &mesh->primitives[m];

      if (primitive->type == cgltf_primitive_type_triangles)
      {
      index_accessor = primitive->indices;
      for (int i = 0; i < primitive->attributes_count; i++) {
                cgltf_attribute attr = primitive->attributes[i];
                switch (attr.type) {
                case cgltf_attribute_type_position:
                        { 
                        position_accessor = attr.data;
                        const float* position_data = (float *)position_accessor->buffer_view->buffer->data;

                        if (index_accessor->component_type == cgltf_component_type_r_16u)
                        {
                          for (int indice=0;indice <index_accessor->count ;indice  ++)
                          {                         
                            unsigned short t_indice = cgltf_accessor_read_index(index_accessor,indice);
                             std::cout << "vertex1 " << *(position_data+t_indice) << " / " << *(position_data+t_indice+1) << " / " <<  *(position_data+t_indice+2) << std::endl;
                          }
                        }
                        break;
                case cgltf_attribute_type_normal:
                        normal_accessor = attr.data;
                        break;
                case cgltf_attribute_type_texcoord:
                        uv_accessor = attr.data;
                        break;
                }
        }
 
        if (index_accessor == nullptr || position_accessor == nullptr || normal_accessor == nullptr)
                throw std::runtime_error("Gltf model invalid or unsupported");
 
      }  
  }
    }
@LxLasso
Copy link

LxLasso commented Jan 15, 2020

I would open the glTF file in a text editor and step through the code in a debugger until I discover what is not happening as I had expected. Are there triangles in the glTF? 16-bit indexed? Has the buffer been loaded?

@zeux
Copy link
Contributor

zeux commented Jan 18, 2020

                    const float* position_data = (float *)position_accessor->buffer_view->buffer->data;

This ignores any type of offset that might apply to the accessor. I would recommend using cgltf_accessor_read_float or cgltf_accessor_unpack_floats helpers to read any attribute data.

                    if (index_accessor->component_type == cgltf_component_type_r_16u)

Note that you don't actually need this if you use cgltf_accessor_read_index since that function will handle different types transparently.

@jkuhlmann
Copy link
Owner

@sancelot Did the comments here help?

@sancelot
Copy link
Author

sancelot commented Jan 29, 2020

I was trying To decode the blender cube at first before a bigger file ..I have not had time to continue. I will retry it very soon. I want to load the file in ordre To display it and have thé scenegraph available.

@ghost
Copy link

ghost commented Mar 10, 2020

@jkuhlmann could the code above maybe get worked into an example? with incorporated use of cgltf_accessor_read_index as you mentioned

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants