OpenGL Adding Texture

I'm not sure how to add a texture to a cube in my project. I think the main problem is in the fragment shader but I'm not sure.

This should be the relevant cpp code:

void renderMainCube(glm::mat4 projection, glm::mat4 view) {
    glBindTexture(GL_TEXTURE_2D, cubeTexture);

    unsigned int objectColorLoc = glGetUniformLocation(cubeShader, "objectColor");
    glUniform3f(objectColorLoc, 1.0f, 0.5f, 0.31f);

    unsigned int lightColorLoc = glGetUniformLocation(cubeShader, "lightColor");
    glUniform3f(lightColorLoc, 1.0f, 1.0f, 1.0f);

    unsigned int lightPosLoc = glGetUniformLocation(cubeShader, "lightPos");
    glUniform3f(lightPosLoc, lightPos.x, lightPos.y, lightPos.z);

    unsigned int cameraPosLoc = glGetUniformLocation(cubeShader, "viewPos");
    glUniform3f(cameraPosLoc, camera.Position.x, camera.Position.y, camera.Position.z);

    unsigned int projectionLoc = glGetUniformLocation(cubeShader, "projection");
    glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));

    unsigned int viewLoc = glGetUniformLocation(cubeShader, "view");
    glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));

    glm::mat4 model = glm::mat4(1.0f);

    if (rotFlg1)
        angle1 = (float)glfwGetTime() / 2.0;
        model = glm::rotate(model, angle1, glm::vec3(0.0f, 1.0f, 0.0f));
    else {
        model = glm::rotate(model, angle1, glm::vec3(0.0f, 1.0f, 0.0f));

    model = glm::scale(model, glm::vec3(1.0f));
    unsigned int modelLoc = glGetUniformLocation(cubeShader, "model");
    glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));

    glDrawArrays(GL_TRIANGLES, 0, 36);

cpp code: Shaders:

Read more here:

Content Attribution

This content was originally published by Bushfries at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: