Trying to texture 6 images to each face of a cube using OpenGL

so I’ve tried several ways I was able to find from checking google but all to no avail. Here is what my program looks like

main.cpp

 GLfloat vertices[] =
    { // the coords for the cube
        // back
        -0.5f, -0.5f, -0.5f,    1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
        0.5f, -0.5f, -0.5f,     1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
        0.5f, 0.5f, -0.5f,      1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
        0.5f, 0.5f, -0.5f,      1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
        -0.5f, 0.5f, -0.5f,     1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,    1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
        // front
        -0.5f, -0.5f, 0.5f,     0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
        0.5f, -0.5f, 0.5f,      0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
        0.5f, 0.5f, 0.5f,       0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
        0.5f, 0.5f, 0.5f,       0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
        -0.5f, 0.5f, 0.5f,      0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, 0.5f,     0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
        // left
        -0.5f, 0.5f, 0.5f,      0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
        -0.5f, 0.5f, -0.5f,     0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,    0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, -0.5f,    0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
        -0.5f, -0.5f, 0.5f,     0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
        -0.5f, 0.5f, 0.5f,      0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
        // right
        0.5f, 0.5f, 0.5f,       0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
        0.5f, 0.5f, -0.5f,      0.0f, 1.0f, 1.0f, 1.0f, 1.0f,
        0.5f, -0.5f, -0.5f,     0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
        0.5f, -0.5f, -0.5f,     0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
        0.5f, -0.5f, 0.5f,      0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
        0.5f, 0.5f, 0.5f,       0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
        // bottom
        -0.5f, -0.5f, -0.5f,    1.0f, 0.0f, 1.0f, 0.0f, 1.0f,
        0.5f, -0.5f, -0.5f,     1.0f, 0.0f, 1.0f, 1.0f, 1.0f,
        0.5f, -0.5f, 0.5f,      1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
        0.5f, -0.5f, 0.5f,      1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
        -0.5f, -0.5f, 0.5f,     1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
        -0.5f, -0.5f, -0.5f,    1.0f, 0.0f, 1.0f, 0.0f, 1.0f,
        // up
        -0.5f, 0.5f, -0.5f,     1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
        0.5f, 0.5f, -0.5f,      1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
        0.5f, 0.5f, 0.5f,       1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
        0.5f, 0.5f, 0.5f,       1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
        -0.5f, 0.5f, 0.5f,      1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
        -0.5f, 0.5f, -0.5f,     1.0f, 1.0f, 0.0f, 0.0f, 1.0f,

    };
    GLuint VAO, VBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    // this is for texture coords
    
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
    glEnableVertexAttribArray(1);


    //Load Image
    //int width, height; // width1, height1;
    //unsigned char* image = SOIL_load_image("res/images/image1.jpg", &width, &height, 0, SOIL_LOAD_RGBA);

    GLuint texture1, texture2;
    // texture 1
    // ---------
    glGenTextures(1, &texture1);
    glBindTexture(GL_TEXTURE_2D, texture1);
    // set the texture wrapping parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    // set texture filtering parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // load image, create texture and generate mipmaps
    int width, height;
    stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
    unsigned char* image = SOIL_load_image("res/images/image1.jpg", &width, &height, 0, SOIL_LOAD_RGB);
    
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
        glGenerateMipmap(GL_TEXTURE_2D);
    
    SOIL_free_image_data(image);
    glBindTexture(GL_TEXTURE_2D, 0);
    // texture 2
    // ---------
    glGenTextures(1, &texture2);
    glBindTexture(GL_TEXTURE_2D, texture2);
    // set the texture wrapping parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    // set texture filtering parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // load image, create texture and generate mipmaps
    int width1, height1;
    unsigned char* image1 = SOIL_load_image("res/images/image2.jpg", &width1, &height1, 0, SOIL_LOAD_RGB);
    
        // note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width1, height1, 0, GL_RGB, GL_UNSIGNED_BYTE, image1);
        glGenerateMipmap(GL_TEXTURE_2D);
    
    SOIL_free_image_data(image1);
    glBindTexture(GL_TEXTURE_2D, 1);

    // tell opengl for each sampler to which texture unit it belongs to (only has to be done once)
    // -------------------------------------------------------------------------------------------
   // shader.Use();
   // shader.setInt("texture0", 0);
    //shader.setInt("texture1", 1);


    while (!glfwWindowShouldClose(window)) {
        glm::mat4 transformation = glm::mat4(1.0f);

        glm::mat4 view = glm::mat4(1.0f);
        glm::mat4 projection = glm::perspective(glm::radians(40.0f),
            static_cast<GLfloat>(screenWidth) / static_cast<GLfloat>(screenHeight),
            0.1f, 100.0f);

        unsigned int modelLoc = glGetUniformLocation(shader.Program, "transformation");
        unsigned int viewLoc = glGetUniformLocation(shader.Program, "view");

        glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(transformation));
        glUniformMatrix4fv(viewLoc, 1, GL_FALSE, &view[0][0]);
        
        shader.Use();
        shader.setMat4("projection", projection);

        glfwPollEvents();
        glViewport(0, 0, screenWidth, screenHeight);
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       
        

        
            
        
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, texture1);
        glUniform1i(glGetUniformLocation(shader.Program, "texture1"), 0);
        glBindVertexArray(VAO);
        //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        glDrawArrays(GL_TRIANGLES, 0, 36);
        glBindVertexArray(0);

        shader.Use();
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, texture2);
        glUniform1i(glGetUniformLocation(shader.Program, "texture1"), 0);
        glBindVertexArray(VAO);
        //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        glDrawArrays(GL_TRIANGLES, 0, 36);
        glBindVertexArray(0);


       
            


            

        transformation = glm::translate(transformation, glm::vec3(0.0f, 0.0f, -0.5f));
        transformation = glm::rotate(transformation, glm::radians(20.0f) * static_cast<GLfloat>(glfwGetTime()), glm::vec3(1.0f, 1.0f, 1.0f));


        transformation = glm::scale(transformation, glm::vec3(0.1f, 0.1f, 0.1f));

        glUniformMatrix4fv(glGetUniformLocation(shader.Program, "transform"), 1, GL_FALSE, glm::value_ptr(transformation));
        glUniformMatrix4fv(glGetUniformLocation(shader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view));
        glUniformMatrix4fv(glGetUniformLocation(shader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
        //glBindVertexArray(VAO);

       // glBindVertexArray(VAO);
       // glDrawArrays(GL_TRIANGLES, 0, 36);

        
        glfwSwapBuffers(window);

    }

    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glDeleteTextures(1, &texture1);
    
    glfwTerminate();
    return 0;
}

with this code all I get is a black cube with no image on it

my vertex shader.

#version 330 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
layout(location = 2) in vec2 textcoords; // this is texture coords

out vec2 textCoords;// send it to fragment shader
out vec3 ourColor;

uniform mat4 transform; // the key for transformation
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * transform * vec4(position.x, position.y, position.z, 1.0f);
    //ourColor = color;
    textCoords = vec2(textcoords.x, 1-textcoords.y);
}

and my fragment shader

#version 330 core
in vec2 textCoords;
in vec3 ourColor;

out vec4 color1;
//out vec4 color;

uniform sampler2D texture0;
uniform sampler2D texture1;
void main()
{
    //color = vec4(ourColor, 1.0f);
    color1 = texture(texture1,textCoords) ;   
    //color2 = texture(texture1,textCoords);

}

i would really appreciatehelp from anyone!

This may not solve the pending problem, but you should not have these variables defined within
the while() loop. Just define them once and keep

in the loop to catch changes to those variables.

this is another creature I don’t understand (it may not be wrong):

I suspect that it’s another one that only needs to be written once.

You may find that some of your other bindings or variables may not be effective if a program-object is not active. … I’m not professional, so don’t take it for more than suggestions on what to look at.

thanks for the suggestions, but can you please expatiate a little?

??

why do you define your variables 60 times a seconds for the duration of your execution?
One definition would do.
you need to define your matrixes external to the loop and modify them in loop-external glfw* input if you want to interact with the model- and view-posture through the mouse.

Once you’ve got the modelLoc and viewLoc 'allocated they should be well and good for use over and over again. You achtually allocates the same locations twice in the same loop … (120 times/sec)

Apart from that, your code looks sane

Set the uniform to the corresponding texture unit.

returns an identifier for a location. You only need to establish it once. Once you’ve got it, you use it for writing values to it like you do in:

You are in the infancy of your program where you don’t yet use the matrices. Later you’ll add new values to them to say move the model or view-point and they’ll make more sense. As for the texture-sampler it’s probably a one off.

If you consider that you can have multible program-objects that needs to draw … then all the bindings begins to make sense.

It’s debug-standard to use the glGetError() atleast once for each swap. If one is detected you can spread out call to glGetError() to detect exactly where the problem is rooted and then comment them out later.
I’ve made a wrapper around glGetError:
my_error(string){
if error, write string
}
Used like: my_error("after draw");
Then you know which error-call that is triggered.