3
\$\begingroup\$

I attempting to texture 3D models (a cube for example) using ibo’s, with OpenGL in Java. Currently, my textures are distorted. I believe this is because only a single texture coordinate is being assigned to each vertex, as the meshes are loaded from wave front (.obj) files.

As each vertex should correspond to multiple texture coordinates, should I be assigning texture coordinates to vertices as the data is loaded from file or would it be better to assign texture coordinates to vertices in the shader?

enter image description here

I would appreciate example code.

\$\endgroup\$
  • 2
    \$\begingroup\$ "Distorted" how? Is it "almost right"? What do you mean by a vertex having multiple coordinates? Would a picture help show the issue? \$\endgroup\$ – david van brink Jun 13 '15 at 14:41
  • \$\begingroup\$ Each vertex should have more than one texture coordinate? \$\endgroup\$ – Jack Jun 13 '15 at 15:11
1
\$\begingroup\$

Your picture is of a cube, so I assume that's what you're talking about.

You can only share vertices between continuous surfaces. That is, on a sphere, you share all/most of the vertices. On a cube, you must you separate vertices for each face.

Aside from texturing, think of the normals you need for lighting. The normal for each side of a cube should point directly out of it. That means that the vertices' that form that side must all have normals pointing in the same direction. That's a problem for the vertices in the corners, as those vertices must have normals that somehow point in three different directions; clearly impossible. The solution is to just make separate vertices for the corners.

On a sphere you can share the normals/vertices because you want the normal to smoothly interpolate along the surface in order to get a rounded look.

For more complex models, whether or not you can share vertices between two surfaces comes down to whether the "edge" between them is something you distinctly want (it's a hard, pointy edge) or whether the edge is just a necessity of polygonal rendering (it's an edge that's simulating a smooth, rolling surface).

Short version: a cube needs a minimum of 24 vertices, 4 for each face. More if you tessellate.

\$\endgroup\$
  • \$\begingroup\$ Ok, so can I store the vertices once in RAM (8 in total) and let the shader make copies when it gets sent to VRAM (24 in total)? If so, is this common practice or unnecessary optimisation? \$\endgroup\$ – Jack Jun 13 '15 at 16:34
  • \$\begingroup\$ @Jack: I honestly don't know how you'd correctly duplicate them in the shader in any way that is actually an "optimization." Even if you use fewer vertices, you need more data per vertex so the shader knows what to actually generate, so you're not saving as much space as you think. And then you'd have a more complex shader that requires a geometry stage and takes longer to execute, which is certainly not an optimization. And you possibly end up with a shader that only even works for cube-like objects, which is rather unhelpful. \$\endgroup\$ – Sean Middleditch Jun 13 '15 at 17:02
  • \$\begingroup\$ I am currently sending an array of vertices and an array of indices to the GPU for the shader to connect into faces. I was thinking I could also send an array of texture coordinates and an array of indices - detailing which vertices match which texture coordinates. If not, would there be no point in using IBOs since I will have repeating vertices? \$\endgroup\$ – Jack Jun 13 '15 at 17:31
  • \$\begingroup\$ @Jack: no, IBOs aren't meant to solve problems of rendering a simple cube. IBOs help when you have complex meshes with many shared vertices, which is what most "real" objects in a 3D scene will be comprised of. Your array solution is also missing some key details, such as how you will index those arrays correctly. You're also adding a lot of complexity to your shaders just to avoid having a handful of extra vertices. You're trying to solve a problem that isn't actually a problem with a "solution" that creates problems you don't need. :) \$\endgroup\$ – Sean Middleditch Jun 13 '15 at 17:52
  • \$\begingroup\$ Very insightful. :D \$\endgroup\$ – Jack Jun 13 '15 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.