FBO depth_texture working

This commit is contained in:
Hugo Willaume 2019-04-15 14:15:16 +09:00
parent 640244c556
commit f404b6ae9b
4 changed files with 18 additions and 18 deletions

View File

@ -1,6 +1,6 @@
#include "Multipass.h" #include "Multipass.h"
Multipass::Multipass() : _quad_shader("textureViewer.vert", "textureViewer.frag") Multipass::Multipass() : shader("textureViewer.vert", "textureViewer.frag")
{ {
_draw_buffers_size = 0; _draw_buffers_size = 0;
@ -67,17 +67,17 @@ void Multipass::drawResultToScreen(SceneContext & scnctx)
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, scnctx.width, scnctx.height); glViewport(0, 0, scnctx.width, scnctx.height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_quad_shader.enable(); 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", 0); 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);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
_quad_shader.disable(); shader.disable();
} }
void Multipass::_createQuad() void Multipass::_createQuad()

View File

@ -20,6 +20,8 @@ public:
void enableFrameBufferTexture(const std::string tex_name); void enableFrameBufferTexture(const std::string tex_name);
void drawResultToScreen(SceneContext &scnctx); void drawResultToScreen(SceneContext &scnctx);
Shader shader;
private: private:
void _createQuad(); void _createQuad();
@ -28,7 +30,6 @@ private:
GLuint _quad_vao; GLuint _quad_vao;
GLuint _quad_vbo; GLuint _quad_vbo;
Shader _quad_shader;
std::string _current_tex; std::string _current_tex;
std::map<std::string, GLuint> _pass_textures; std::map<std::string, GLuint> _pass_textures;

View File

@ -180,10 +180,10 @@ void MyGlWindow::setup()
//meshes["Cube"]->assignTexture(_scnctx.textures["MossTex"]); //meshes["Cube"]->assignTexture(_scnctx.textures["MossTex"]);
//meshes["Cube"]->addTranslation(glm::vec4(4, 3, -4, 0)); //meshes["Cube"]->addTranslation(glm::vec4(4, 3, -4, 0));
meshes.emplace("Mountain", new Mesh("mountain/mount.blend1.obj", shaders["TexBaseLight"])); //meshes.emplace("Mountain", new Mesh("mountain/mount.blend1.obj", shaders["TexBaseLight"]));
//meshes.emplace("Sponza", new Mesh("sponza/sponza.obj", shaders["TexBaseLight"])); meshes.emplace("Sponza", new Mesh("sponza/sponza.obj", shaders["TexBaseLight"]));
//meshes["Sponza"]->addTranslation(glm::vec4(0, -200, 0, 1)); meshes["Sponza"]->addTranslation(glm::vec4(0, -200, 0, 1));
@ -226,7 +226,7 @@ void MyGlWindow::draw()
_scnctx.viewMatrix = view; _scnctx.viewMatrix = view;
_scnctx.projectionMatrix = projection; _scnctx.projectionMatrix = projection;
_multipassManager.enableFrameBufferTexture("render_tex"); _multipassManager.enableFrameBufferTexture("depth_tex");
glClearColor(_scnctx.bg.r, _scnctx.bg.g, _scnctx.bg.b, _scnctx.bg.a); glClearColor(_scnctx.bg.r, _scnctx.bg.g, _scnctx.bg.b, _scnctx.bg.a);
glViewport(0, 0, _scnctx.width, _scnctx.height); glViewport(0, 0, _scnctx.width, _scnctx.height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -236,6 +236,7 @@ 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.shader.addUniform("depth", true);
_multipassManager.drawResultToScreen(_scnctx); _multipassManager.drawResultToScreen(_scnctx);
//skybox.draw(shaders["Skybox"], _scnctx); //skybox.draw(shaders["Skybox"], _scnctx);
} }

View File

@ -10,22 +10,20 @@ uniform bool depth;
float LinearizeDepth(in vec2 uv) float LinearizeDepth(in vec2 uv)
{ {
float zNear = .1; // zNear of your perspective projection float zNear = 0.1f; // zNear of your perspective projection
float zFar = 2000.0; // zFar of your perspective projection float zFar = 2000.0f; // zFar of your perspective projection
float depth = texture(tex, uv).x; float depth = texture(tex, uv).x;
return (2.0 * zNear) / (zFar + zNear - depth * (zFar - zNear)); return (2.0 * zNear) / (zFar + zNear - depth * (zFar - zNear));
} }
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
final_color = (depth) ? texture(tex, uv).rrra : texture(tex, uv).rgba; final_color = (depth) ? texture(tex, uv).rrra : texture(tex, uv).rgba;
if (depth) { if (depth) {
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 = vec4(1.0,0.0,0.0,1.0);
} }