OpenGL - nanogui面板覆盖3d场景

时间:2017-04-14 07:13:47

标签: c++ opengl

我想在nanogui的当前项目中使用c++ OpenGL。所以我做了所有的构建和链接,并使其成功运行。但是,它覆盖了我的3D场景。

我的3D场景只是整个屏幕的四边形,并使用片段着色器进行各种SDF渲染。现在我想将nanogui叠加在3d场景之上。

以下是我的代码:

int main ()
{
    GLFWwindow* window = NULL;
    glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1);
    glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    glfwMakeContextCurrent (window);

    ///nanogui
#if defined(NANOGUI_GLAD)
    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
        throw std::runtime_error("Could not initialize GLAD!");
    glGetError(); 
#endif

    glClearColor(0.2f, 0.25f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    // Create a nanogui screen and pass the glfw pointer to initialize
    screen = new Screen();
    screen->initialize(window, true);

    glewExperimental = GL_TRUE;
    glewInit ();
    glViewport(0, 0, screenWidth*2, screenHeight*2);

    // Create nanogui gui
    bool enabled = true;
    FormHelper *gui = new FormHelper(screen);
    ref<Window> nanoguiWindow = gui->addWindow(Eigen::Vector2i(10, 10), "Form helper example");
    gui->addGroup("Basic types");
    gui->addVariable("bool", bvar)->setTooltip("Test tooltip.");
    gui->addVariable("string", strval);
      ... //all the nanogui setup code
    gui->addButton("A button", []() { std::cout << "Button pressed." << std::endl; })->setTooltip("Testing a much longer tooltip, that will wrap around to new lines multiple times.");;

    screen->setVisible(true);
    screen->performLayout();
    nanoguiWindow->center();

    glfwSetKeyCallback(window, key_callback);
    glfwSetCursorPosCallback(window, mouse_callback);
    glfwSetScrollCallback(window, scroll_callback);

    glEnable (GL_DEPTH_TEST);
    glDepthFunc (GL_LESS);

    Shader ourShader("vertex_shader.glsl", "fragment_shader.glsl");

    GLint positionLocation = glGetAttribLocation(ourShader.Program, "a_position");
    GLint resolutionloc = glGetUniformLocation(ourShader.Program, "resolution");

    ourShader.Use();

    glUniform2f(resolutionloc, (GLfloat)screenWidth*2, (GLfloat)screenHeight*2);

    GLint mouseLocation = glGetUniformLocation(ourShader.Program, "mouse");
//    glUniform2f(mouseLocation,firstMouse.);

    GLfloat vertices[] = {
        -1.0f, -1.0f,
        1.0f, -1.0f,
        -1.0f,  1.0f,
        -1.0f,  1.0f,
        1.0f, -1.0f,
        1.0f,  1.0f   // Top Left
    };

    GLuint VBO, VAO;
    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, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    // Game loop
    while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();

        // Clear the colorbuffer
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 6);
        glBindVertexArray(0);

        screen->drawContents();
        screen->drawWidgets();

        glfwSwapBuffers(window);
    }
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glfwTerminate();
    return 0;
}

有了这个,我只看到nanogui叠加背景。我不知道如何将它们放在一起。

我该如何解决这个问题?我对OpenGL很新,所以我不确定这里发生了什么。但我怀疑是与帧缓冲器或类似的东西有关。

0 个答案:

没有答案