FBO draw to screen ok, now for modyfing the texture

This commit is contained in:
Hugo Willaume 2019-04-15 14:04:20 +09:00
parent c5b2dd897b
commit 640244c556
4 changed files with 21 additions and 32 deletions

View File

@ -95,7 +95,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
<AdditionalLibraryDirectories>C:\Graphics\Tools\SOIL\lib;C:\Graphics\Tools\Assimp\lib\x86;C:\Graphics\Tools\glfw\lib\x86;C:\Graphics\Tools\glew\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>C:\Graphics\Tools\SOIL\lib;C:\Graphics\Tools\Assimp\lib\x86;C:\Graphics\Tools\glfw\lib\x86;C:\Graphics\Tools\glew\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>assimp.lib;SOIL.lib;glfw3.lib;glew32.lib;opengl32.lib;FreeImage.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>assimp.lib;SOIL.lib;glfw3.lib;glew32.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">

View File

@ -2,26 +2,32 @@
Multipass::Multipass() : _quad_shader("textureViewer.vert", "textureViewer.frag") Multipass::Multipass() : _quad_shader("textureViewer.vert", "textureViewer.frag")
{ {
_draw_buffers_size = 0; _draw_buffers_size = 0;
glGenFramebuffers(1, &_fboId); glGenFramebuffers(1, &_fboId);
glBindFramebuffer(GL_FRAMEBUFFER, _fboId); glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
_createQuad(); _createQuad();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
void Multipass::setDrawBuffers() void Multipass::setDrawBuffers()
{ {
glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
glDrawBuffers(_draw_buffers_size, _draw_buffers); glDrawBuffers(_draw_buffers_size, _draw_buffers);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
throw std::runtime_error("Error during framebuffer initialisation"); throw std::runtime_error("Error during framebuffer initialisation");
glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
void Multipass::bindToFrameBuffer(GLenum type, GLenum texture, std::string texName) void Multipass::bindToFrameBuffer(GLenum type, GLenum texture, std::string texName)
{ {
glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
glFramebufferTexture2D(GL_FRAMEBUFFER, type, texture, _pass_textures[texName], 0); glFramebufferTexture2D(GL_FRAMEBUFFER, type, texture, _pass_textures[texName], 0);
if (type != GL_DEPTH_ATTACHMENT) { if (type != GL_DEPTH_ATTACHMENT) {
_draw_buffers[_draw_buffers_size] = type; _draw_buffers[_draw_buffers_size] = type;
_draw_buffers_size++; _draw_buffers_size++;
} }
glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
Multipass::~Multipass() Multipass::~Multipass()
@ -31,7 +37,8 @@ Multipass::~Multipass()
void Multipass::addTexture(const std::string & tex_name, GLuint filter, GLuint type, GLuint type_2, void Multipass::addTexture(const std::string & tex_name, GLuint filter, GLuint type, GLuint type_2,
bool depth, SceneContext &scnctx) bool depth, SceneContext &scnctx)
{ {
glBindFramebuffer(GL_FRAMEBUFFER, _fboId);
glGenTextures(1, &_pass_textures[tex_name]);; glGenTextures(1, &_pass_textures[tex_name]);;
glBindTexture(GL_TEXTURE_2D, _pass_textures[tex_name]); glBindTexture(GL_TEXTURE_2D, _pass_textures[tex_name]);
glTexImage2D(GL_TEXTURE_2D, 0, type, scnctx.width, scnctx.height, 0, type_2, GL_FLOAT, 0); glTexImage2D(GL_TEXTURE_2D, 0, type, scnctx.width, scnctx.height, 0, type_2, GL_FLOAT, 0);
@ -43,9 +50,9 @@ void Multipass::addTexture(const std::string & tex_name, GLuint filter, GLuint t
{ {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
} }
glBindTexture(GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_2D, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
void Multipass::enableFrameBufferTexture(const std::string tex_name) void Multipass::enableFrameBufferTexture(const std::string tex_name)
@ -63,7 +70,7 @@ void Multipass::drawResultToScreen(SceneContext & scnctx)
_quad_shader.enable(); _quad_shader.enable();
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _pass_textures[_current_tex]); glBindTexture(GL_TEXTURE_2D, _pass_textures[_current_tex]);
_quad_shader.addUniform("tex", (int)_pass_textures[_current_tex]); _quad_shader.addUniform("tex", 0);
glBindVertexArray(_quad_vao); glBindVertexArray(_quad_vao);
glBindBuffer(GL_ARRAY_BUFFER, _quad_vbo); glBindBuffer(GL_ARRAY_BUFFER, _quad_vbo);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

View File

@ -1,5 +1,4 @@
#include "MyGlWindow.h" #include "MyGlWindow.h"
#include "..\..\..\..\Downloads\FreeImage3180Win32Win64\FreeImage\Dist\x32\FreeImage.h"
//Getting the projection matrix //Getting the projection matrix
glm::mat4x4 perspective(float fovy, float aspect, float near, float far) glm::mat4x4 perspective(float fovy, float aspect, float near, float far)
{ {
@ -135,9 +134,9 @@ void MyGlWindow::multipassSetup()
_multipassManager.addTexture("render_tex", GL_NEAREST, GL_RGB, GL_RGB, false, _scnctx); _multipassManager.addTexture("render_tex", GL_NEAREST, GL_RGB, GL_RGB, false, _scnctx);
_multipassManager.bindToFrameBuffer(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, "render_tex"); _multipassManager.bindToFrameBuffer(GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, "render_tex");
//_multipassManager.addTexture("depth_tex", GL_LINEAR, GL_DEPTH_COMPONENT24, _multipassManager.addTexture("depth_tex", GL_LINEAR, GL_DEPTH_COMPONENT24,
// GL_DEPTH_COMPONENT, true, _scnctx); GL_DEPTH_COMPONENT, true, _scnctx);
//_multipassManager.bindToFrameBuffer(GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, "depth_tex"); _multipassManager.bindToFrameBuffer(GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, "depth_tex");
_multipassManager.setDrawBuffers(); _multipassManager.setDrawBuffers();
} }
@ -154,6 +153,7 @@ void MyGlWindow::setup()
skybox.scale = 10; skybox.scale = 10;
_scnctx.skybox_tex = skybox.getTexID(); _scnctx.skybox_tex = skybox.getTexID();
lightSetup(); lightSetup();
multipassSetup();
//meshes.emplace("Ogre", new Mesh("ogre/ogre.obj", shaders["TexNmapLight"])); //meshes.emplace("Ogre", new Mesh("ogre/ogre.obj", shaders["TexNmapLight"]));
//meshes["Ogre"]->addTranslation(glm::vec4(-0.5, 1, 0, 0)); //meshes["Ogre"]->addTranslation(glm::vec4(-0.5, 1, 0, 0));
@ -210,7 +210,6 @@ void MyGlWindow::setup()
//meshes.emplace("Teapot", new Mesh("teapot.obj", shaders["Skybox"])); //meshes.emplace("Teapot", new Mesh("teapot.obj", shaders["Skybox"]));
multipassSetup();
} }
void MyGlWindow::draw() void MyGlWindow::draw()
@ -237,22 +236,8 @@ void MyGlWindow::draw()
for (auto it = meshes.begin(); it != meshes.end(); it++) for (auto it = meshes.begin(); it != meshes.end(); it++)
(*it).second->draw(_scnctx); (*it).second->draw(_scnctx);
//_multipassManager.drawResultToScreen(_scnctx); _multipassManager.drawResultToScreen(_scnctx);
//skybox.draw(shaders["Skybox"], _scnctx); //skybox.draw(shaders["Skybox"], _scnctx);
BYTE* pixels = new BYTE[3 * _scnctx.width * _scnctx.height];
glReadPixels(0, 0, _scnctx.width, _scnctx.height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
// Convert to FreeImage format & save to file
FIBITMAP* image = FreeImage_ConvertFromRawBits(pixels, _scnctx.width, _scnctx.height, 3 * _scnctx.width, 24, 0x0000FF, 0xFF0000, 0x00FF00, false);
FreeImage_Save(FIF_BMP, image, "D:/Users/Hurlu/Desktop/bitmap.bmp", 0);
// Free resources
FreeImage_Unload(image);
delete[] pixels;
exit(0);
} }

View File

@ -17,8 +17,6 @@ float LinearizeDepth(in vec2 uv)
} }
void main() void main()
{ {
//rrra because depth textures are not usual textures, they have only one channel //rrra because depth textures are not usual textures, they have only one channel
@ -28,7 +26,6 @@ void main()
float d; float d;
d = LinearizeDepth(uv); d = LinearizeDepth(uv);
final_color = vec4(d,d,d,1.0); final_color = vec4(d,d,d,1.0);
} }
final_color = texture(tex, uv).rgba;
//final_color = vec4(1.0,0.0,0.0,1.0); //final_color = vec4(1.0,0.0,0.0,1.0);
} }