OpenGL多维数据集地图工件

时间:2014-08-16 03:46:09

标签: opengl graphics 3d glsl

我尝试为对象反射实现动态立方体贴图。我几乎成功了,除了我在镜子中注意到的一些文物。

Reflection Artifacts

这些是我用来设置和渲染立方体贴图的主要功能。

bool setup(GLsizei width = 256, GLsizei height = 256)
{
    // The framebuffer, which regroups 0, 1, or more textures, and 0 or 1 depth buffer.
    if (!fbo)
        glGenFramebuffers(1, &fbo);

    if (!cubeMap)
        glGenTextures(1, &cubeMap);

    if (!depthrenderbuffer)
        glGenRenderbuffers(1, &depthrenderbuffer); // The depth buffer

    for (int i = 0; i < 6; ++i)
    {
        if (facesFbo[i])
            break;

        glGenFramebuffers(1, &facesFbo[i]);
        //NSLog(@"Generated facesFbo[%u]", i);
    }

    GLint mainFrameBuffer; // save the previous framebuffer, most probably the main one
    glGetIntegerv(GL_FRAMEBUFFER_BINDING, &mainFrameBuffer);

    //setup the texture
    glActiveTexture(GL_TEXTURE5); // for mixing textures and cubemaps
    glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMap);
    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);
    //glEnable(GL_TEXTURE_CUBE_MAP);

    // allocate
    if (!allocatedTextures)
    {
        for (int i = 0; i < 6; ++i)
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

        allocatedTextures = true;
    }

    //bind it
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X, cubeMap, 0);

    // set the texture rendering targets
    //for (int i = GL_TEXTURE_CUBE_MAP_POSITIVE_X, len = i + 6; i < len; ++i)
    if (!attachTextures)
    {
        for (int i = 0; i < 6; ++i)
        {
            glBindFramebuffer(GL_FRAMEBUFFER, facesFbo[i]);

            //depth
            glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer);
            glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
            glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer);

            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, cubeMap, 0);
        }

        attachTextures = true;
    }


    //bring it back
    glActiveTexture(GL_TEXTURE0);

    // Set the list of draw buffers.
    glDrawBuffer(GL_COLOR_ATTACHMENT0); // "1" is the size of DrawBuffers
    // restore
    glBindFramebuffer(GL_FRAMEBUFFER, mainFrameBuffer);

    // Always check that our framebuffer is ok
    return (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE);
}

void beginDrawFace(GLenum textarget, GLsizei fxWidth = 256, GLsizei fxHeight = 256)
{
    // save the previous frame buffer
    glGetIntegerv(GL_FRAMEBUFFER_BINDING, &mainFrameBuffer);
    // and the viewport resolution
    glGetIntegerv(GL_VIEWPORT, &vp[0]);

    // just in case, turn off cube mapping
    glDisable(GL_TEXTURE_CUBE_MAP);

    // bind it cuz we doin stuff to it
    glViewport(0, 0, fxWidth, fxHeight);
    //glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    //glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + textarget, cubeMap, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, facesFbo[textarget]);

    // clear stuff out
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);
    glDepthFunc(GL_LEQUAL);
    glClearDepth(1.0f); //used to be 2
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}

void endDrawFace()
{
    // restore
    glViewport(vp[0], vp[1], vp[2], vp[3]);
    glBindFramebuffer(GL_FRAMEBUFFER, mainFrameBuffer);
}

有没有人见过这种问题?我怀疑它与深度缓冲区有关,因为我最近附加了一个用来打击更糟糕的文物。

1 个答案:

答案 0 :(得分:0)

我通过缩短远和近平面之间的距离来管理那些文物消失。