2017-11-01 46 views
0

ディスプレイにレンダリングできる3Dシーンがあります。しかし、それを表示するためにレンダリングするだけでなく、レンダリングされたシーンを100フレームごとに1つのイメージ(JPGまたはPNGイメージなど)としてエクスポートすることもできます。機械。OpenGL:フレームバッファへのレンダリングと表示

私は次のようなものを試してみました:

do{ 
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffername); 
    drawScene(); 
    glBindFramebuffer(GL_FRAMEBUFFER, 0); 
    drawScene(); 
}while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS 

私はフレームバッファを使用している場合、私はわからないが正しくオブジェクトが、私の目標は与えられ続行する方法についての提案を探していました。上記のコードから、私が定義したFBOにフレームバッファをバインドしてから、シーンを描画しています(正しい場合はFBOに描画する必要があります)。それから私は私の普通のディスプレイに再び引きます。しかし、このコードは、ディスプレイが常に私のシーンと空ではない(黒い)シーンとの間で切り替わるようにします。私は、絶えずすべての黒を行くことなく、素敵なクリーンな3Dシーンとして、私のシーンを表示デバイスに表示したい。

// Swap buffers 
glfwSwapBuffers(window); 
glfwPollEvents(); 

あなたがいないときあなたがこれを呼び出すとどうなるどう思いますか:

void drawScene() { 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
//glBindTexture(GL_TEXTURE_2D, 0); 
//glBindFramebuffer(GL_FRAMEBUFFER, framebuffername); 

// Use our shader 
glUseProgram(programID); 

// Compute the MVP matrix from keyboard and mouse input 
computeMatricesFromInputs(); 
glm::mat4 ProjectionMatrix = getProjectionMatrix(); 
glm::mat4 ViewMatrix = getViewMatrix(); 
glm::mat4 ModelMatrix = glm::mat4(1.0); //DEFINE MODEL TO WORLD TRANSFORMATION 
             //glm::mat4 ModelMatrix = glm::scale(2.0,2.0,2.0); 
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix; 

// Send our transformation to the currently bound shader, 
// in the "MVP" uniform 
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); 
glUniform1f(FARPLANE, farplane_control); 
glUniform1f(NEARPLANE, nearplane_control); 
glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &ModelMatrix[0][0]); 
glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, &ViewMatrix[0][0]); 
glm::vec3 lightPos = glm::vec3(0, 0, 4); 
glUniform3f(LightID, lightPos.x, lightPos.y, lightPos.z); 

// 1st attribute buffer : vertices 
glEnableVertexAttribArray(0); 
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 
glVertexAttribPointer(
    0,     // attribute 
    3,     // size 
    GL_FLOAT,   // type 
    GL_FALSE,   // normalized? 
    0,     // stride 
    (void*)0   // array buffer offset 
); 

// 2nd attribute buffer : normals 
glEnableVertexAttribArray(1); 
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer); 
glVertexAttribPointer(
    1,        // attribute 
    3,        // size 
    GL_FLOAT,       // type 
    GL_FALSE,       // normalized? 
    0,        // stride 
    (void*)0       // array buffer offset 
); 

// Draw the triangle ! 
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.size()); 

glDisableVertexAttribArray(0); 
glDisableVertexAttribArray(1); 

glReadBuffer(GL_FRONT); 

// Swap buffers 
glfwSwapBuffers(window); 
glfwPollEvents(); 
} 
+0

[mcve]で編集します。 – genpfault

答えて

2

あなたdrawScene()コードは最後にこれらの行が含まれています

私の描画機能は、次のようになります実際に画面にレンダリング?

明白な解決策は、バッファ・スワッピングを実行するかどうかを指示機能にフラグを含めることです。

void drawScene(bool swap = true) { 
    /*...*/ 
    if(swap) { 
     glfwSwapBuffers(window); 
     glfwPollEvents(); 
    } 
} 

do{ 
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffername); 
    drawScene(false); 
    glBindFramebuffer(GL_FRAMEBUFFER, 0); 
    drawScene(true); 
}while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS 

それとも、それはdoesnのため、あなたはおそらく優れている機能、外にこれらの呼び出しを移動することができます最初の場所でそれらを描画関数のローカルにするのは本当に意味があります。

do{ 
    glfwPollEvents(); //Prefer having this be the first call, in case you need the window 
    //responding to user input immediately 
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffername); 
    drawScene(); 
    glBindFramebuffer(GL_FRAMEBUFFER, 0); 
    drawScene(); 
    glfwSwapBuffers(window); //No longer in drawScene() function 
}while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS 
+0

ソリューションが動作します!ありがとう! – user308485

+0

@ user308485あなたがうまくいけば、アップアップして答えを受け入れてください。 – Xirema

関連する問題