1
\$\begingroup\$

I've got a function which generates geometry data for a rectangle. So far I've got the vertex, indices and the normals nailed, but I have no idea how to generate the texture coordinates - so far I've only used textures with imported 3d models, but now I want to generate my own rectangle on the fly and so need to generate the texcoords aswell.

So how do I make the texcoords? I am not sure how the mapping works.

bool CreateRectangleData(const float sizeX, const float sizeY, const float sizeZ, std::vector<float>& vertexData, std::vector<float>& normalData, std::vector<float>& texcoordData, std::vector<uint32_t>& indiceData)
{
    if (sizeX <= 0 || sizeY <= 0 || sizeZ <= 0)
        return false;

    const float X = sizeX / 2.0f;
    const float Y = sizeY / 2.0f;
    const float Z = sizeZ / 2.0f;

    float cubeVertices[] = {-X, -Y, -Z, -X, -Y, Z, X, -Y, Z,
                            X, -Y, -Z, -X, Y, -Z, -X, Y, Z,
                            X, Y, Z, X, Y, -Z, -X, -Y, -Z,
                            -X, Y, -Z, X, Y, -Z, X, -Y, -Z,
                            -X, -Y, Z, -X, Y, Z, X, Y, Z,
                            X, -Y, Z, -X, -Y, -Z, -X, -Y, Z,
                            -X, Y, Z, -X, Y, -Z, X, -Y, -Z,
                            X, -Y, Z, X, Y, Z, X, Y, -Z};

    float cubeNormals[] = {0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
                           0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
                           0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f,
                           0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f,
                           0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
                           0.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
                           -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
                           1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f};

    float cubeTexCoords[] = {};  // ???

    uint32_t cubeIndices[] = {0, 2, 1, 0, 3, 2, 4, 5, 6,
                              4, 6, 7, 8, 9, 10, 8, 10, 11,
                              12, 15, 14, 12, 14, 13, 16, 17, 18,
                              16, 18, 19, 20, 23, 22, 20, 22, 21};

    vertexData.insert(vertexData.begin(), cubeVertices, cubeVertices + (sizeof(cubeVertices) / sizeof(float)));
    normalData.insert(normalData.begin(), cubeNormals, cubeNormals + (sizeof(cubeNormals) / sizeof(float)));
    texcoordData.insert(texcoordData.begin(), cubeTexCoords, cubeTexCoords + (sizeof(cubeTexCoords) / sizeof(float)));
    indiceData.insert(indiceData.begin(), cubeIndices, cubeIndices + (sizeof(cubeIndices) / sizeof(uint32_t)));

    return true;
}

Thanks

\$\endgroup\$
2
\$\begingroup\$

Texture coordinates always start at 0 and end at 1. Imagine stretching a cloth over the extremities of the object you are texture mapping.

For example (assuming the x-axis increments right and the y-axis increments down), for a square the top left UV coordinate would always be (0, 0) and the top right UV coordinate would always be (1, 0) regardless of the size of the square.

For a 3D model you can map textures however you like as long as there is a direct relationship between the texture image and the UV coordinates.

The simplest way to map a texture to a cuboid would be to map the texture to each face independently as opposed to an unwrapped model. So, each face would have the same texture coordinates.

In your supplied code all you would need to do is to create an array which matches your index list in a way that would ensure that the top-left of a face has UV coords (0, 0) and the bottom-right has UV coords (1, 1) when looking at the face straight on (assuming the coordinate system mentioned above).

The actual texture image can be a square or a rectangle.

\$\endgroup\$

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.