# Texturing in OpenGL, Should texture coordinates be assigned to vertices in the shader?

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?

I would appreciate example code.

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

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.

• 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? – Jack Jun 13 '15 at 16:34
• @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. – Sean Middleditch Jun 13 '15 at 17:02
• 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? – Jack Jun 13 '15 at 17:31
• @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. :) – Sean Middleditch Jun 13 '15 at 17:52
• Very insightful. :D – Jack Jun 13 '15 at 17:56