3
\$\begingroup\$

In Blender, UVs are stored "per face vertex", meaning that if you have a plane made of 2 triangles, you will have 2 faces, 4 vertices but 6 UV coordinates.

In Blender, the plane appears with the appropriate texture labeling quadrants 1 - 9. In my OpenGL engine, it is distorted, with UVs not aligning along the triangles.

I made a basic export script, and exported UVs with help from the Blender Stack Exchange, since I do not know much about Blender UV format:

for p in mesh.polygons:
    for vert, loop in zip(p.vertices, p.loop_indices):
        for z in (mesh.uv_layers.active.data[loop].uv 
            if mesh.uv_layers.active is not None else (0.0, 0.0)):  # uv
            f.write(struct.pack('f', z))

The UVs are stored as a std::vector of vec2s, in the engine, and uploaded to OpenGL like so:

glBufferData(GL_ARRAY_BUFFER, mdl.uvs.size() 
    * sizeof(mdl.uv[0]), &mdl.uvs[0], GL_STATIC_DRAW);

How can I use the correct Blender UV format, without wasting memory, or turn the per face vertex UVs into per vertex UVs?

\$\endgroup\$
1
  • \$\begingroup\$ I assume you're using glDrawElements? If you get 6 UVs I'd say that they aren't indexed yet. \$\endgroup\$ Commented Apr 20, 2017 at 23:27

2 Answers 2

3
\$\begingroup\$

Roughly speaking, you can't. That's just not how GL or the like work. For every unique set of vertex attributes you want, you must duplicate the entire vertex.

That said, that's not necessarily what you need here. You don't want to duplicate vertices, I think, you want to throw away the seemingly extraneous UVs that Blender uses.

The file formats and structures supported by 3D content tools like Blender typically have to be converted into formats suitable for runtime. This is one of the reasons that your typical game's asset folder isn't just full of .3ds files and the like; those formats are optimized for editing, not for rendering.

I recommend that you just use a library like Open Asset Importer Library unless you are doing something very unique and special. It can import .blend files (as well as many others) which can be used to write a renderer-friendly format. You can use it to generate your own files or you can use it to just convert to an existing well-documented runtime-friendly format like .md5.

\$\endgroup\$
2
  • \$\begingroup\$ Do you have any idea how I can get rid of the extra UVs without messing anything up? \$\endgroup\$ Commented Apr 20, 2017 at 23:55
  • \$\begingroup\$ @Accumulator You usually just analyse and preprocess the file. \$\endgroup\$
    – Bálint
    Commented Apr 21, 2017 at 6:46
0
\$\begingroup\$

You can use the blender UV in openGL if you separate all faces:

(yes this will duplicate vertices used by two or more faces)


goto edit mode

mark all edges as sharp


goto object mode

assign and apply the "edge split" modifier


now all faces use unique verts and edges,

thus each vertex has its own UV coord

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .