samedi 5 décembre 2020

Dynamic reflection of rendering to cubemap doesn't work

enter image description here

I have 2 sphere objects and it isn't reflecting the other one. The picture is when it simply reflects the cube map but when I try to render to the cube map the spheres turn out black.

In camera class:

    void switchToFace(int faceIndex) // For Cube Map camera
    {
        switch (faceIndex)
        {
        case 0:
            m_pitch = 0;
            m_yaw = 90;
            break;
        case 1:
            m_pitch = 0;
            m_yaw = -90;
            break;
        case 2:
            m_pitch = -90;
            m_yaw = 180;
            break;
        case 3:
            m_pitch = 90;
            m_yaw = 180;
            break;
        case 4:
            m_pitch = 0;
            m_yaw = 180;
            break;
        case 5:
            m_pitch = 0;
            m_yaw = 0;
            break;
        default:
            break;
        }
    }

In main

static GLuint createEmptyCubeMap(int width, int height)
{
    GLuint textureID;
    glGenTextures(1, &textureID);

    glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
    for (GLuint i{ 0 }; i < 6; ++i)
    {
        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
    }
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

    return textureID;
}
main()
{
// stuff
    glm::vec3 spherePositions[] = {
    glm::vec3(0.0f, 0.0f, 0.0f),
    glm::vec3(-1.5f, -2.2f, -2.5f),
    };
    // new cubemap
    Camera reflectionCamera(glm::vec3(0.0f, 0.0f, 0.0f));  // Camera position. Should be on ball

    GLint imageWidth, imageHeight;
    SOIL_load_image(faces[1], &imageWidth, &imageHeight, 0, SOIL_LOAD_RGB);
    GLuint newEnvironment = createEmptyCubeMap(imageWidth, imageHeight);

    GLuint FBO{}, DBO{};

    glGenFramebuffers(1, &FBO);
    glBindFramebuffer(GL_FRAMEBUFFER, FBO);
    glDrawBuffer(GL_COLOR_ATTACHMENT0);

    glGenRenderbuffers(1, &DBO);
    glBindRenderbuffer(GL_RENDERBUFFER, DBO);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, imageWidth, imageHeight);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, DBO);

    glViewport(0, 0, imageWidth, imageHeight);

    for (GLuint i{ 0 }; i < 6; ++i)
    {
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, newEnvironment, 0);

        reflectionCamera.switchToFace(i);
    }

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    glViewport(0, 0, WIDTH, HEIGHT);

    while(window.isOpen() // SFML)
    {
        glBindVertexArray(sphereVAO);
        for (GLuint i{ 0 }; i < 2; ++i)
        {
            model = glm::mat4();
            model = glm::translate(model, spherePositions[i]);
            glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); // modelLoc is location of uniform model
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_CUBE_MAP, newEnvironment);
            glDrawElements(GL_TRIANGLES, numberOfIndexes, GL_UNSIGNED_INT, 0);
        }
    }
}

The sphereVAO is simply the Vertex Array Object for the sphere and here we bind the texture to the newEnvironment which is supposed to reflect the other object in spherePositions[i]

The picture above is the sphere Position at (0, 0, 0).

But the code results in this: enter image description here





Aucun commentaire:

Enregistrer un commentaire