提示コードですが参考サイトを参考にBloom効果を実装でぼかしをかける効果を実装するコードです。
camera->colorBuffer_B->texture
はライトによる輝度を抽出したピクセルの画像データです。
frameBuffer_A
でシェーダーによるぼかしをかけてそれをAというフレームバッファテクスチャに保存したいのですがその下のコードcamera->
で何も描画されないのでframeBuffer_A
コード部でうまくフレームバッファーに描画されていないためと思うのですがどうやってシェーダーを実行した後のフレームバッファをフレームバッファにコピーするのでしょうか?
一度描画したものを再描画するのは計算量がもったいないので行いたくないです。
参考サイト:https://stackoverflow.com/questions/15306899/is-it-possible-to-copy-data-from-one-framebuffer-to-another-in-opengl
参考サイト: https://learnopengl.com/Advanced-Lighting/Bloom
Main.cpp
//省略
shaderBloomBlur->setEnable();
shaderBloomBlur->setVertexAttribute(frameBuffer_A->quadVAO,frameBuffer_A->quadVBO,"vertexPosition",4,sizeof(FrameWork::VertexAttribute_Sprite),0);
shaderBloomBlur->setVertexAttribute(frameBuffer_A->quadVAO,frameBuffer_A->quadVBO,"vertexUV",4,sizeof(FrameWork::VertexAttribute_Sprite),3 * sizeof(float));
// frameBuffer_A->ScreenRender_Enable();
frameBuffer_A->ColorRender_Enable();
shaderBloomBlur->setUniform1i("horizontal", 0 );
shaderBloomBlur->setUniformSampler2D("image",0,camera->colorBuffer_B->texture);
// frameBuffer_A->ScreenRender();
// frameBuffer_A->ScreenRender_Disable();
frameBuffer_A->Render_Disable();
shaderBloomBlur->setDisable();
camera->shader->setEnable();
camera->frameBuffer->ScreenRender_Enable();
camera->shader->setUniformSampler2D("uImage",0,colorBuffer_A->texture);
// camera->shader->setUniformSampler2D("uImage",0,colorBuffer_B->texture);
//camera->shader->setUniformSampler2D("uImage",0,colorBuffer_A->texture);
camera->frameBuffer->ScreenRender();
camera->frameBuffer->ScreenRender_Disable();
camera->shader->setDisable();
FrameBuffer
/*############################################################################################
# コンストラクタ
############################################################################################*/
FrameWork::FrameBuffer::FrameBuffer(std::shared_ptr<FrameWork::Window> win)
{
windowContext = win;
glGenVertexArrays(1, &quadVAO);
glGenBuffers(1, &quadVBO);
glBindVertexArray(quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, Sprite::vertex.size() * sizeof(VertexAttribute_Sprite), Sprite::vertex.data(), GL_DYNAMIC_DRAW);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//MSAAバッファ
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
glGenTextures(1, &frameBufferTexture);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, frameBufferTexture);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 8, GL_RGBA, windowContext->getSize().x, windowContext->getSize().y, GL_TRUE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, frameBufferTexture, 0);
glBindFramebuffer(GL_FRAMEBUFFER,0);
glGenFramebuffers(1,&colorBuffer); //カラーバッファー
glGenFramebuffers(1,&depthBuffer); //デプスバッファー
}
/*############################################################################################
# カラーバッファー設定
############################################################################################*/
void FrameWork::FrameBuffer::setColorBufferNumber(const int start,const int num)
{
std::vector<unsigned int> buffer;
buffer.resize(num);
for(int i = start; i < buffer.size(); i++)
{
buffer.at(i) = GL_COLOR_ATTACHMENT0 + start + i;
}
glBindFramebuffer(GL_FRAMEBUFFER, colorBuffer);
glDrawBuffers(num, buffer.data());
glBindFramebuffer(GL_FRAMEBUFFER,0);
}
/*############################################################################################
# バッファ クリア
############################################################################################*/
void FrameWork::FrameBuffer::Clear()
{
glBindFramebuffer(GL_FRAMEBUFFER, colorBuffer);
glClearColor(0.0f,0.0f,0.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, depthBuffer);
glClear(GL_DEPTH_BUFFER_BIT);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
/*############################################################################################
# カラーバッファ レンダリング 有効
############################################################################################*/
void FrameWork::FrameBuffer::ColorRender_Enable()
{
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, colorBuffer);
}
/*############################################################################################
# デプスバッファ レンダリング 有効
############################################################################################*/
void FrameWork::FrameBuffer::DepthRender_Enable()
{
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, depthBuffer);
}
/*############################################################################################
# バッファ レンダリング 無効
############################################################################################*/
void FrameWork::FrameBuffer::Render_Disable()
{
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0);
}
/*############################################################################################
# スクリーン レンダリング 有効
############################################################################################*/
void FrameWork::FrameBuffer::ScreenRender_Enable()
{
glBindVertexArray(quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBindFramebuffer(GL_READ_FRAMEBUFFER, frameBuffer);
glBlitFramebuffer(0, 0, windowContext->getSize().x, windowContext->getSize().y, 0, 0, windowContext->getSize().x, windowContext->getSize().y, GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
/*############################################################################################
# スクリーン レンダリング
############################################################################################*/
void FrameWork::FrameBuffer::ScreenRender()
{
setVertex();
glDisable(GL_DEPTH_TEST);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(VertexAttribute_Sprite) * Sprite::vertex.size(), Sprite::vertex.data());
glDrawArrays(GL_TRIANGLE_STRIP,0,Sprite::vertex.size());
}
/*############################################################################################
# スクリーン レンダリング 無効
############################################################################################*/
void FrameWork::FrameBuffer::ScreenRender_Disable()
{
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
/*############################################################################################
# UV座標 設定
############################################################################################*/
void FrameWork::FrameBuffer::setVertex()
{
Sprite::vertex[0].uv[0] = 0;
Sprite::vertex[0].uv[1] = 0;
Sprite::vertex[1].uv[0] = 0;
Sprite::vertex[1].uv[1] = 1;
Sprite::vertex[2].uv[0] = 1;
Sprite::vertex[2].uv[1] = 0;
Sprite::vertex[3].uv[0] = 1;
Sprite::vertex[3].uv[1] = 1;
Sprite::vertex[0].position[0] = -1.0f;
Sprite::vertex[0].position[1] = 1.0f;
Sprite::vertex[0].position[2] = 0;
Sprite::vertex[1].position[0] = -1.0f;
Sprite::vertex[1].position[1] = -1.0f;
Sprite::vertex[1].position[2] = 0;
Sprite::vertex[2].position[0] = 1.0f;
Sprite::vertex[2].position[1] = 1.0f;
Sprite::vertex[2].position[2] = 0;
Sprite::vertex[3].position[0] = 1.0f;
Sprite::vertex[3].position[1] = -1.0f;
Sprite::vertex[3].position[2] = 0;
}
/*############################################################################################
# デストラクタ
############################################################################################*/
FrameWork::FrameBuffer::~FrameBuffer()
{
}
フラグメントシェーダー
#version 420
/*
out vec4 FragColor;
layout (location = 2) in vec2 TexCoords;
uniform sampler2D image;
void main()
{
FragColor = vec4(texture(image,TexCoords).rgb,1.0);
// FragColor = vec4(1.0,0.0,0.0,1.0);
}
*/
out vec4 FragColor;
//layout (location = 0) out vec4 FragColor;
layout (location = 2) in vec2 TexCoords;
uniform sampler2D image;
uniform int horizontal;
float weight[5] = float[] (0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162);
void main()
{
vec2 tex_offset = 1.0 / textureSize(image, 0); // gets size of single texel
vec3 result = texture(image, TexCoords).rgb * weight[0];
if(horizontal == 1)
{
for(int i = 1; i < 5; ++i)
{
result += texture(image, TexCoords + vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
result += texture(image, TexCoords - vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
}
}
else
{
for(int i = 1; i < 5; ++i)
{
result += texture(image, TexCoords + vec2(0.0, tex_offset.y * i)).rgb * weight[i];
result += texture(image, TexCoords - vec2(0.0, tex_offset.y * i)).rgb * weight[i];
}
}
FragColor = vec4(result, 1.0);
//FragColor = vec4(0.0,1.0,0.0, 1.0);
}