3

私はこの視覚的なバグを数日間解決しようとしてきましたが、うまくいきませんでした。そのため、誰かが何が起こっているのかを理解するのを手伝ってくれるかどうかを確認するためにこの質問をしています.

最初にコードなしで問題を説明し、次にいくつかのコードを提示します。状況は次のとおりです。

  • 私の OpenGL アプリケーションは、この画像をマルチサンプル フレームバッファにレンダリングします。

    ここに画像の説明を入力

  • 次に、そのマルチサンプル フレームバッファを通常のフレームバッファ (マルチサンプル フレームバッファではありません) にブリットします。

  • 次に、その通常のフレーム バッファから RGB データを読み取り、glReadPixels.

  • 最後に、stbi_write_png符号なしバイトの配列を呼び出します。結果は次のとおりです。

    ここに画像の説明を入力

私には、バイトの最初の行が右にシフトされているように見えます。これにより、他のすべての行がシフトされ、斜めの形状になります。

これが私のコードです:

  • マルチサンプル フレームバッファを作成するには:
   int width        = 450;
   int height       = 450;
   int numOfSamples = 1;

   // Create the multisample framebuffer

   glGenFramebuffers(1, &mMultisampleFBO);

   glBindFramebuffer(GL_FRAMEBUFFER, mMultisampleFBO);

   // Create a multisample texture and use it as a color attachment
   glGenTextures(1, &mMultisampleTexture);

   glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, mMultisampleTexture);
   glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, numOfSamples, GL_RGB, width, height, GL_TRUE);
   glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);

   glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, mMultisampleTexture, 0);

   // Create a multisample renderbuffer object and use it as a depth attachment
   glGenRenderbuffers(1, &mMultisampleRBO);

   glBindRenderbuffer(GL_RENDERBUFFER, mMultisampleRBO);
   glRenderbufferStorageMultisample(GL_RENDERBUFFER, numOfSamples, GL_DEPTH_COMPONENT, width, height);
   glBindRenderbuffer(GL_RENDERBUFFER, 0);

   glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mMultisampleRBO);
  • 通常のフレームバッファを作成するには:
   // Create the regular framebuffer

   glGenFramebuffers(1, &mRegularFBO);

   glBindFramebuffer(GL_FRAMEBUFFER, mRegularFBO);

   // Create a texture and use it as a color attachment
   glGenTextures(1, &mRegularTexture);

   glBindTexture(GL_TEXTURE_2D, mRegularTexture);
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
   glBindTexture(GL_TEXTURE_2D, 0);

   glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mRegularTexture, 0);
  • 両方のフレームバッファが完了していると報告されることに注意してください。

  • マルチサンプル フレームバッファを通常のフレームバッファにブリットするには、通常のフレームバッファから読み取り、PNG イメージを書き込みます。

   int width  = 450;
   int height = 450;
   static GLubyte* data = new GLubyte[3 * 450 * 450];
   memset(data, 0, 3 * width * height);

   // Blit the multisample framebuffer into the regular framebuffer
   glBindFramebuffer(GL_READ_FRAMEBUFFER, mMultisampleFBO);
   glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mRegularFBO);
   glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);

   // Read from the regular framebuffer into the data array
   glBindFramebuffer(GL_FRAMEBUFFER, mRegularFBO);
   glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);

   // Write the PNG image
   int numOfComponents = 3; // RGB
   int strideInBytes   = width * 3;
   stbi_write_png(imgName.c_str(), width, height, 3, data, width * 3);
  • glGetError はエラーを報告しないことに注意してください。

私は何が間違っているのか理解できませんでした。助けてくれてありがとう!

4

1 に答える 1