Som Pra
Som Pra

Reputation: 453

How do I draw a mesh loaded from .obj(wavefront) file in qt3d by reading file myself?

The example code(comes with the Qt creator tool) for loading a wireframe mesh from .obj files in Qt3D draws this elephant (which I found in a site that peddled digital assets). The question is I want to open the .obj files using my own code and form a geometry. I have code to open .obj file and form triangles. All I want to know is how shall I write the C++ part to populate the geometry.

Like I want to know how to structure the C++ code.

Here is the code that I have to read obj file.

elefante

Upvotes: 2

Views: 1137

Answers (1)

Som Pra
Som Pra

Reputation: 453

Well for those of you wondering about the same thing I found a github repo that creates a geometry in C++ by reading an OBJ model and rendering it in a Qt3D context.

It is a simple process if you think about it.

First you need to Create a Qt3DRender::QGeometry based object.

class ModelGeometry : public Qt3DRender::QGeometry
{
public:
ModelGeometry(){

}

Then you need to add attributes like points, triangles and normals to it using

auto attribute = new Qt3DRender::QAttribute(parent);

and then adding the attributes to the geometry.

The entire process is illustrated in the repo here: https://github.com/bmkamath2000/Qt3DExamples

After running this repo with the example OBJ file containing the elephant I have got it to draw as expected:

Elephant can be rotated by pressing Alt+Arrow Keys

Thanks A Lot!!!

Upvotes: 2

Related Questions