Comments (11)
For future reference:
A little late on this one, but here's a way to access data from buffers when querying.
The specs literally tell you how to access contents within a buffer, with the exception of it not telling you that 'count' is the number of values, that define some data type, of which the buffer view refers to within the buffer. Accessing vertex data from a gltf file you do as so:
const tinygltf::Model& model;
// Assuming you loaded the gltf model.
...
for each primitive in a mesh...
const tinygltf::Accessor& accessor = model.accessors[primitive.attributes["POSITION"]];
const tinygltf::BufferView& bufferView = model.bufferViews[accessor.bufferView];
finally, to obtain and read out each vertex position:
// cast to float type read only. Use accessor and bufview byte offsets to determine where position data
// is located in the buffer.
const tinygltf::Buffer& buffer = model.buffers[bufferView.buffer];
// bufferView byteoffset + accessor byteoffset tells you where the actual position data is within the buffer. From there
// you should already know how the data needs to be interpreted.
const float* positions = reinterpret_cast<const float*>(&buffer.data[bufferView.byteOffset + accessor.byteOffset]);
// From here, you choose what you wish to do with this position data. In this case, we will display it out.
for (size_t i = 0; i < accessor.count; ++i) {
// Positions are Vec3 components, so for each vec3 stride, offset for x, y, and z.
std::cout << "(" << positions[i * 3 + 0] << ", "// x
<< positions[i * 3 + 1] << ", " // y
<< positions[i * 3 + 2] << ")" // z
<< "\n";
}
proceed to next primitive...
You can also incorporate this for indices, normals, texcoords, joints, weights, etc (because the component and data types are also in the specs) of a given primitive as well. Keep in mind that even though position should always be available for primitives, other attributes may not be available.
Also, you don't need to slander syoyo for making a tool as helpful as this, read the specs first, and then ask where you are specifically stuck (you don't specify how and what hints at where to start reading within the buffer, for instance). And while it's okay that you have found the solution, it would be appreciative if you share how you solved this problem, so that others whom may get stuck here as well, won't have to ask the same questions in the future, not to show off and become a ****.
here's a nice reference from the gltf 2.0 docs from which I hoped the asker has read:
Hopefully this will be hopeful for people.
from tinygltf.
@CheezBoiger Yeah you are right and I'm sry, so here is my solution it's almost like yours but I don't store the full vertices memory, I keep pushing the datas to a struct:
for (int i = 0; i < vertexaccess.count; ++i)
{
char* vertexarray = new char[12];
int arraytemp = 0;
for (int j = (vertexaccess.byteOffset + vertexbview.byteOffset) + (i * 12); j < (vertexaccess.byteOffset + vertexbview.byteOffset) + (i * 12) + 12; ++j)
{
vertexarray[arraytemp] = vertexbuffer.data[j];
++arraytemp;
}
FloatVec3& coordinates = *reinterpret_cast<FloatVec3*>(vertexarray);
cout << coordinates.x << " " << coordinates.y << " " << coordinates.z << endl;
delete[] vertexarray;
}
from tinygltf.
Nope, that code is wrong because it just gets the buffer not the vertex positions.. buffer can contain more than just vertex coordinates. I already figured out the script and working for me, but I will not share until Syoyo learnt that stuff what he linked here xD
from tinygltf.
You can use ```(three backquotes) to wrap codes
from tinygltf.
Thanks, and can you help me ? I also asked a question here : https://stackoverflow.com/questions/50074638/c-get-vertex-arrays-from-buffer-gltf-model-loader
No one wants to help me, but I think it is basic programming skill.
from tinygltf.
You are first better to study 3D graphics programming > http://graphicscodex.com/
from tinygltf.
If you can't do it, how did you made the full loader ? lol
from tinygltf.
@ladiszlai
I was looking for the same data.
This helps understand it better:
#15 (comment)
from tinygltf.
Initial question is not expressive and it looks @ladiszlai find a solution, so close the issue.
from tinygltf.
@CheezBoiger Super awesome work!
I'm considering to add your description of how to access to data at Wiki.
from tinygltf.
@syoyo much appreciated! Please, you are free to find anything useful from it.
Also, I again thank you for this tool you have developed, along with the many others you have created before and after this one. I be in your gratitude.
from tinygltf.
Related Issues (20)
- No default initialization of `light` and `emitter` in `tinygltf::Node` HOT 1
- Inclusion of third-party libraries causes conflicts HOT 2
- MinGW don't have an implementation of the function "_wfopen_s" HOT 4
- enable draco
- compile failed after enable draco
- Deprecate Travis build
- LoadBinaryFromMemory fails for empty but not omitted binary chunk HOT 2
- Version numbers in header file is outdate HOT 1
- hows to get model coordinates
- How can I retreive referenced file names in GLB/GLTF file?
- Convert .B3DM to .glb, .glb file has lost its geospatial coordinates
- Unused string in LoadBinaryFromFile in tiny_gltf.h HOT 2
- Empty nodes are wrongly serialized as null HOT 5
- Light reference in the node HOT 3
- Default constructed Material has wrong emissiveFactor
- how to enable EXT_Meshopt_compression
- Empty scenes are wrongly serialized as null HOT 2
- Enabling the file "tiny_gltf.h" to another header file HOT 1
- stbi_load and stbi_free_image are disabled
- model.nodes[idx].name returned wrong string when name is unicode string? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tinygltf.