Tex Coordinates issues when drawing a torus with an ofMesh

Okay so ive been experimenting with openFrameworks for a while, and i have written code that draws a torus using the ofMesh, when i add my texture coordinates i have an issue when the torus connects, because as far as im aware vertices cant have more than one texture coordinate, meaning i cant connect the last vertex back to the first, as the first is already connected to the second. i dont know if im explaining myself well so ill add an screenshot that shows whats going on. I know that adding an extra set of vertices in the same place as the first would probably work, but i think might cause problems with other parts of the code, was wondering if anyone had any other way to fix it.

Hi @Olliy_Clayden-Smith , a quick and simple fix might be to try mapping the texCoords in a “radial” way instead of a “rectangular way”. A “radial” way would be like cutting a circle from a rectangular texture using an angle and radius, and then projecting it onto the shape. The “rectangular way” wraps (and warps) the rectangular texture around a circle so that the beginning meets the end, which I think is how it is in the pict above.

To “close” the mesh without adding duplicate vertices, you could try adding indices (the draw order) to draw some final triangles between the first and last vertices. Indices allow each vertex to be shared amongst multiple triangles in a specific way (like when you want to connect the last to the first). Also be aware that an ofMesh has different modes that describe how it draws; OF_PRIMITIVE_TRIANGLES works well for lots of shapes. The mode can affect how the mesh should be set up with its vertices and/or indices.

And also check out the relevant sections in ofBook if you haven’t already. They have lots of great in info and detail about how to use ofMesh and 3d drawing in general.

hey, thanks for the response,

would you mind explaining the radial way?


Hey sure! The idea is to use a loop to increment an angle and calculate some texture coordinates at that angle. There are lots ways to do this, but a quick study for a 2D ring (not a torus) with normalized coordinates is below. And I didn’t add indices to the mesh, but this would be necessary.

If you like, you’re welcome to post some code for how you’re generating the values for the texCoords in the mesh. Sometimes it helps to see ideas expressed in code. The </> button will format code in a post. And simple examples are often much easier to follow.

    ofMesh mesh;
    int num = 11;
    float angleStep = TWO_PI / static_cast<float>(num);
    float innerRadius = 0.2f;
    float outerRadius = 1.f;
    for(size_t i{0}; i < num; ++i){
        float x = cos(i * angleStep);
        float y = sin(i * angleStep);

        // vertex values range from -1.0 - 1.0
        glm::vec3 vertex = glm::vec3(x, y, 0.f);
        // texCoord values range from 0.0 - 1.0
        glm::vec2 texCoord = glm::vec2(vertex.x * 0.5 + 0.5, vertex.y * 0.5 + 0.5);
        // inner
        mesh.addVertex(vertex * innerRadius);
        mesh.addTexCoord(texCoord * innerRadius);
        // outer
        mesh.addVertex(vertex * outerRadius);
        mesh.addTexCoord(texCoord * outerRadius);