diff --git a/data/shaders/fs_depthbuffer.glsl b/data/shaders/fs_depthbuffer.glsl new file mode 100644 index 0000000..01e2638 --- /dev/null +++ b/data/shaders/fs_depthbuffer.glsl @@ -0,0 +1,17 @@ +#version 330 core + +in vec2 uv; + +out vec3 color; + +uniform sampler2D rendered_texture; +uniform float near; +uniform float far; + +void main() { + float z = texture(rendered_texture, uv).r; + float c = (2.0 * near) / (far + near - z * (far - near)); + c = 2.0 * near * far / (far + near - z * (far - near)); + + color = vec3(z); +} diff --git a/data/shaders/fs_simpletexture.glsl b/data/shaders/fs_simpletexture.glsl index 857e0cd..83c035b 100644 --- a/data/shaders/fs_simpletexture.glsl +++ b/data/shaders/fs_simpletexture.glsl @@ -14,6 +14,4 @@ void main() { cos(time + 768.0 * uv.y) ) ).xyz; - color = vec3((texture(rendered_texture, uv).x + 1) * 0.5 ); -// * 1 * (cos(time * 0.3) * 0.5) + 0.5); } diff --git a/src/FileModificationObserver.h b/src/FileModificationObserver.h new file mode 100644 index 0000000..4e3ad53 --- /dev/null +++ b/src/FileModificationObserver.h @@ -0,0 +1,85 @@ +#pragma once + +#include +#include + +struct AFileModificationListener { + virtual bool OnTrigger() = 0; +}; + +struct FileModificationObserver { + struct FileObserverInfo { + ino_t mFileId = 0; + int mFileMTime = 0; + int mFileMTimeNSec = 0; + off_t mFileSize = 0; + std::vector mListeners; + }; + + typedef std::unordered_map FilenameListenerMap; + FilenameListenerMap mObserverInfos; + + void AddListener(const std::string &filename, AFileModificationListener* listener) { + FilenameListenerMap::iterator iter; + iter = mObserverInfos.find(filename); + + if (iter == mObserverInfos.end()) { + FileObserverInfo observer_info; + observer_info.mListeners.push_back(listener); + mObserverInfos[filename] = observer_info; + } else { + iter->second.mListeners.push_back(listener); + } + } + + void Trigger(const std::string &filename) { + FilenameListenerMap::iterator iter; + iter = mObserverInfos.find(filename); + + assert (iter != mObserverInfos.end()); + + std::vector::iterator listener_iter = iter->second.mListeners.begin(); + while (listener_iter != iter->second.mListeners.end()) { + if ((*listener_iter)->OnTrigger()) + break; + + listener_iter++; + } + } + + void CheckFileModification(const std::string& filename, FileObserverInfo& observer_info) { + struct stat attr; + bool stat_result = stat(filename.c_str(), &attr); + + if (stat_result != 0) { + gLog ("Error: could not stat watched file %s", filename.c_str()); + abort(); + } + + if ( observer_info.mFileId != attr.st_ino + || observer_info.mFileMTime != attr.st_mtime + || observer_info.mFileMTimeNSec != attr.st_mtim.tv_nsec + || observer_info.mFileSize != attr.st_size + || observer_info.mFileSize == 0 + ) { + observer_info.mFileId = attr.st_ino; + observer_info.mFileMTime = attr.st_mtime; + observer_info.mFileMTimeNSec = attr.st_mtim.tv_nsec; + observer_info.mFileSize = attr.st_size; + + gLog ("Detected file change of %s: new size %d", + filename.c_str(), attr.st_size); + + Trigger(filename); + } + } + + void Update() { + FilenameListenerMap::iterator iter = mObserverInfos.begin(); + + while (iter != mObserverInfos.end()) { + CheckFileModification(iter->first, iter->second); + iter++; + } + } +}; diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index aeed2df..79a9a38 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -144,12 +144,20 @@ glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data glGenBuffers(1, &mRenderQuadVertexBufferId); glBindBuffer(GL_ARRAY_BUFFER, mRenderQuadVertexBufferId); glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW); + + // Program for color texture rendering mRenderQuadProgramColor = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_simpletexture.glsl"); load_result = mRenderQuadProgramColor.Load(); assert(load_result); - muRenderQuadTexture = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "rendered_texture"); muRenderQuadTime = glGetUniformLocation(mRenderQuadProgramColor.mProgramId, "time"); + + // Program for depth texture rendering + mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl"); + load_result = mRenderQuadProgramDepth.Load(); + assert(load_result); + muRenderQuadDepthNear = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "near"); + muRenderQuadDepthFar = glGetUniformLocation(mRenderQuadProgramDepth.mProgramId, "far"); } void Renderer::Shutdown() { @@ -197,14 +205,24 @@ void Renderer::RenderGui() { glBindFramebuffer(GL_FRAMEBUFFER, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // Render the full screen quad - glUseProgram(mRenderQuadProgramColor.mProgramId); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, mRenderTarget.mColorTexture); -// glBindTexture(GL_TEXTURE_2D, mRenderTarget.mDepthTexture); - glUniform1i(muRenderQuadTexture, 0); - glUniform1f(muRenderQuadTime, (float)(glfwGetTime() * 10.0f)); + + bool render_color = false; + + if (render_color) { + // Render the full screen quad + glUseProgram(mRenderQuadProgramColor.mProgramId); + glBindTexture(GL_TEXTURE_2D, mRenderTarget.mColorTexture); + glUniform1i(muRenderQuadTexture, 0); + glUniform1f(muRenderQuadTime, (float)(glfwGetTime() * 10.0f)); + } else { + // render depth texture + glUseProgram(mRenderQuadProgramDepth.mProgramId); + glBindTexture(GL_TEXTURE_2D, mRenderTarget.mDepthTexture); + glUniform1i(muRenderQuadTexture, 0); + glUniform1f(muRenderQuadDepthNear, 1.0); + glUniform1f(muRenderQuadDepthFar, -1.0); + } glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, mRenderQuadVertexBufferId); diff --git a/src/modules/RenderModule.h b/src/modules/RenderModule.h index 55dbc74..ac29df1 100644 --- a/src/modules/RenderModule.h +++ b/src/modules/RenderModule.h @@ -118,11 +118,15 @@ struct Renderer { GLuint mRenderQuadVertexArrayId; GLuint mRenderQuadVertexBufferId; + RenderProgram mRenderQuadProgramColor; - RenderProgram mRenderQuadProgramDepth; GLuint muRenderQuadTexture; GLuint muRenderQuadTime; + RenderProgram mRenderQuadProgramDepth; + GLuint muRenderQuadDepthNear; + GLuint muRenderQuadDepthFar; + Renderer() : mInitialized(false), mWidth (0),