Code Monkey home page Code Monkey logo

Comments (11)

CheezBoiger avatar CheezBoiger commented on May 29, 2024 7

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:
accessdata

Hopefully this will be hopeful for people.

from tinygltf.

ladiszlai avatar ladiszlai commented on May 29, 2024 3

@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.

ladiszlai avatar ladiszlai commented on May 29, 2024 2

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.

syoyo avatar syoyo commented on May 29, 2024

You can use ```(three backquotes) to wrap codes

from tinygltf.

ladiszlai avatar ladiszlai commented on May 29, 2024

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.

syoyo avatar syoyo commented on May 29, 2024

You are first better to study 3D graphics programming > http://graphicscodex.com/

from tinygltf.

ladiszlai avatar ladiszlai commented on May 29, 2024

If you can't do it, how did you made the full loader ? lol

from tinygltf.

riverreal avatar riverreal commented on May 29, 2024

@ladiszlai
I was looking for the same data.
This helps understand it better:
#15 (comment)

from tinygltf.

syoyo avatar syoyo commented on May 29, 2024

Initial question is not expressive and it looks @ladiszlai find a solution, so close the issue.

from tinygltf.

syoyo avatar syoyo commented on May 29, 2024

@CheezBoiger Super awesome work!

I'm considering to add your description of how to access to data at Wiki.

from tinygltf.

CheezBoiger avatar CheezBoiger commented on May 29, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.