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) {
    glUseProgram(cubeShader);
    glActiveTexture(GL_TEXTURE0);
    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));

    glBindVertexArray(cubeVAO);
    glDrawArrays(GL_TRIANGLES, 0, 36);
}

cpp code: https://github.com/coolcodehere/Final/blob/main/src/source/Final.cpp Shaders: https://github.com/coolcodehere/Final/blob/main/src/shader/cube.vert https://github.com/coolcodehere/Final/blob/main/src/shader/cube.frag



Read more here: https://stackoverflow.com/questions/67376359/opengl-adding-texture

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: