1

I am trying to apply texture mapping to my cubes but are unsure on how to proceed. Current I am using indices to avoid having to repeat vec3s to make a cube and a vertex array of the points and their normals like so:

// Cube data as our basic building block
       unsigned int indices[] = {
        10, 8, 0, 2, 10, 0, 12, 10, 2, 4, 12, 2,
        14, 12, 4, 6, 14, 4, 8, 14, 6, 0, 8, 6,
        12, 14, 8, 10, 12, 8, 2, 0, 6, 4, 2, 6
       };


       vec3 vertexArray[] = {
        vec3(-0.5f, -0.5f, -0.5f),  vec3(-0.408248, -0.816497, -0.408248),
        vec3(0.5f, -0.5f, -0.5f),    vec3(0.666667, -0.333333, -0.666667),
        vec3(0.5f, 0.5f, -0.5f),    vec3(0.408248, 0.816497, -0.408248),
        vec3(-0.5f, 0.5f, -0.5f),   vec3(-0.666667, 0.333333, -0.666667),
        vec3(-0.5f, -0.5f, 0.5f),    vec3(-0.666667, -0.333333, 0.666667),
        vec3(0.5f, -0.5f, 0.5f),     vec3(0.666667, -0.666667, 0.333333),
        vec3(0.5f, 0.5f, 0.5f),     vec3(0.408248, 0.408248, 0.816497),
        vec3(-0.5f, 0.5f, 0.5f),    vec3(-0.408248, 0.816497, 0.408248),
       };

        // convert arrays to vectors
        std::vector<vec3> vertexArrayVector;
        vertexArrayVector.insert(vertexArrayVector.begin(), std::begin(vertexArray), std::end(vertexArray));

        std::vector<unsigned int> indicesVector;
        indicesVector.insert(indicesVector.begin(), std::begin(indices), std::end(indices));

I want to now apply textures to the cube but I am not sure how to add the use of a vec2 for UV when using indices. My creating of VBOs and VAOs like this if it helps:

GLuint vertexBufferObject;
    GLuint indexBufferObject;
    GLuint vertexArrayObject;
    glGenVertexArrays(1, &vertexArrayObject);
    glGenBuffers(1, &indexBufferObject);
    glGenBuffers(1, &vertexBufferObject);

    glBindVertexArray(vertexArrayObject);


    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObject);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertexIndicesArray[0]) * vertexIndicesArray.size(), &vertexIndicesArray[0], GL_STATIC_DRAW);

    // Upload Vertex Buffer to the GPU, keep a reference to it (vertexBufferObject)

    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPointsArray[0]) * vertexPointsArray.size(), &vertexPointsArray[0], GL_STATIC_DRAW);

    // Teach GPU how to read position data from vertexBufferObject
    glVertexAttribPointer(0,                   // attribute 0 matches aPos in Vertex Shader
        3,                   // size
        GL_FLOAT,            // type
        GL_FALSE,            // normalized?
        0,                   // 0 stride
        (void*)0              // array buffer offset
    );
    glEnableVertexAttribArray(0);

    // Teach GPU how to read normals data from vertexBufferObject
    glVertexAttribPointer(1,                            // attribute 1 matches normals in Vertex Shader
        3,
        GL_FLOAT,
        GL_FALSE,
        0,
        (void*)sizeof(glm::vec3)      // normal is offseted a vec3 (comes after position)
    );
    glEnableVertexAttribArray(1);
Fabrizo
  • 29
  • 1
  • 2

1 Answers1

1

The vertex coordinate an the texture coordinates for a tuple with 5 components (x, y, z, u, v). If you have a vertex coordinate that is shared by the face but is associated to different texture coordinates, you need to duplicate a vertex coordinate. You must specify 1 attribute tuple for each vertex coordinate and texture coordinate combination required in your mesh.
It is not possible to specify different indices for the vertex coordinates and texture coordinates. See Rendering meshes with multiple indices and Why does OpenGL not support multiple index buffering?.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174